| | |
| | | return '\n'.join(lines) + '\n\n' |
| | | |
| | | |
| | | def search(sock, password, deadline, present=b'replicationServer-database', |
| | | attributes=(b'replicationServer-database', b'domain-name', b'last-change')): |
| | | bind = tlv(0x02, b'\x03') + tlv(0x04, b'cn=Directory Manager') + tlv(0x80, password) |
| | | transmit(sock, tlv(0x30, tlv(0x02, b'\x01') + tlv(0x60, bind)), deadline) |
| | | def bind(sock, password, deadline): |
| | | request = tlv(0x02, b'\x03') + tlv(0x04, b'cn=Directory Manager') + tlv(0x80, password) |
| | | transmit(sock, tlv(0x30, tlv(0x02, b'\x01') + tlv(0x60, request)), deadline) |
| | | tag, result = message(sock, 1, deadline) |
| | | if tag != 0x61: |
| | | raise LdapError('expected LDAP bind response') |
| | | require_success(result) |
| | | |
| | | |
| | | def search(sock, password, deadline, present=b'replicationServer-database', |
| | | attributes=(b'replicationServer-database', b'domain-name', b'last-change')): |
| | | bind(sock, password, deadline) |
| | | query = (tlv(0x04, b'cn=monitor') + tlv(0x0a, b'\x02') + tlv(0x0a, b'\x00') + |
| | | tlv(0x02, b'\x00') + tlv(0x02, b'\x00') + tlv(0x01, b'\x00') + |
| | | tlv(0x87, present) + tlv(0x30, b''.join(tlv(0x04, attr) for attr in attributes))) |
| | |
| | | return attributes |
| | | |
| | | |
| | | def membership_search(sock, deadline, message_id, base, scope, ldap_filter, attribute): |
| | | def attribute_search(sock, deadline, message_id, base, scope, ldap_filter, attribute): |
| | | query = (tlv(0x04, base) + tlv(0x0a, bytes([scope])) + tlv(0x0a, b'\x00') + |
| | | tlv(0x02, b'\x00') + tlv(0x02, b'\x00') + tlv(0x01, b'\x00') + |
| | | ldap_filter + tlv(0x30, tlv(0x04, attribute))) |
| | |
| | | raise LdapError('unexpected LDAP search response') |
| | | |
| | | |
| | | def connected_to(sock, password, deadline, domain): |
| | | bind(sock, password, deadline) |
| | | attribute = b'connected-to' |
| | | entries = attribute_search(sock, deadline, 2, domain.encode('utf-8'), 1, tlv(0x87, attribute), attribute) |
| | | values = entries[0].get('connected-to', []) if len(entries) == 1 else [] |
| | | if len(values) != 1 or not values[0]: |
| | | raise LdapError('expected one connected-to endpoint for ' + domain) |
| | | return values[0] |
| | | |
| | | |
| | | def membership_matches(registry, config, peer, wanted): |
| | | if wanted not in ('present', 'absent') or not registry or not config: |
| | | return False |
| | |
| | | |
| | | |
| | | def membership(sock, password, deadline, peer, wanted): |
| | | bind = tlv(0x02, b'\x03') + tlv(0x04, b'cn=Directory Manager') + tlv(0x80, password) |
| | | transmit(sock, tlv(0x30, tlv(0x02, b'\x01') + tlv(0x60, bind)), deadline) |
| | | tag, result = message(sock, 1, deadline) |
| | | if tag != 0x61: |
| | | raise LdapError('expected LDAP bind response') |
| | | require_success(result) |
| | | registry = membership_search(sock, deadline, 2, b'cn=Servers,cn=admin data', 1, |
| | | tlv(0x87, b'objectClass'), b'hostname') |
| | | bind(sock, password, deadline) |
| | | registry = attribute_search(sock, deadline, 2, b'cn=Servers,cn=admin data', 1, |
| | | tlv(0x87, b'objectClass'), b'hostname') |
| | | equal = lambda value: tlv(0xa3, tlv(0x04, b'objectClass') + tlv(0x04, value)) |
| | | ldap_filter = tlv(0xa1, equal(b'ds-cfg-replication-server') + equal(b'ds-cfg-replication-domain')) |
| | | config = membership_search(sock, deadline, 3, b'cn=config', 2, ldap_filter, |
| | | b'ds-cfg-replication-server') |
| | | config = attribute_search(sock, deadline, 3, b'cn=config', 2, ldap_filter, |
| | | b'ds-cfg-replication-server') |
| | | return membership_matches(registry, config, peer, wanted) |
| | | |
| | | |
| | | def main(argv): |
| | | if len(argv) not in (3, 6) or (len(argv) == 6 and (argv[3] != 'membership' or argv[5] not in ('present', 'absent'))): |
| | | print('usage: scale-monitor.py <loopback port> <load deadline in milliseconds> [membership <peer> <present|absent>]', file=sys.stderr) |
| | | args = argv[3:] |
| | | route = len(args) == 2 and args[0] == 'connected-to' |
| | | member = len(args) == 3 and args[0] == 'membership' and args[2] in ('present', 'absent') |
| | | if len(argv) < 3 or (args and not (route or member)): |
| | | print('usage: scale-monitor.py <loopback port> <load deadline in milliseconds> ' |
| | | '[connected-to <monitor DN> | membership <peer> <present|absent>]', file=sys.stderr) |
| | | return 2 |
| | | try: |
| | | port, load_deadline = int(argv[1]), int(argv[2]) / 1000 |
| | |
| | | context.verify_mode = ssl.CERT_NONE # Same trust policy as the existing --trustAll search. |
| | | with socket.create_connection(('127.0.0.1', port), timeout=remaining) as raw: |
| | | with context.wrap_socket(raw, server_hostname='localhost') as connection: |
| | | output = search(connection, password, deadline) if len(argv) == 3 else None |
| | | matched = membership(connection, password, deadline, argv[4], argv[5]) if len(argv) == 6 else True |
| | | output, matched = None, True |
| | | if route: |
| | | output = connected_to(connection, password, deadline, args[1]) + '\n' |
| | | elif member: |
| | | matched = membership(connection, password, deadline, args[1], args[2]) |
| | | else: |
| | | output = search(connection, password, deadline) |
| | | if time.monotonic() >= deadline or time.time() >= load_deadline: |
| | | raise TimeoutError('LDAP response completed after the deadline') |
| | | if output is not None: |