diff --git a/bin/deltalogparse.py b/bin/deltalogparse.py index 281856a2868baeac60702c1c5791cab79a911b90..d4bdab88114ef6950f783ed8dc9ecd6f727ac53a 100755 --- a/bin/deltalogparse.py +++ b/bin/deltalogparse.py @@ -9,13 +9,16 @@ import lib_path import lib import ldap3 +debug = 2 update_every = 1 +log_path = '/var/log/ldap/delta.log' search_base = 'cn=deltalog' search_filter = '(|(objectClass=auditModify)(objectClass=auditAdd)(objectClass=auditDelete))' search_scope = ldap3.SUBTREE attributes = [ldap3.ALL_ATTRIBUTES, ldap3.ALL_OPERATIONAL_ATTRIBUTES] -rgx_filter = re.compile( + +rgx_skip = re.compile( r'^(' r'heartbeatTimestamp|' r'modifyTimestamp|' @@ -25,29 +28,50 @@ rgx_filter = re.compile( r'):' ) +rgx_filter = re.compile( + r'^(' + r'heartbeatTimestamp|' + r'lastUse.*?|' + r'(context|entry)CSN' + r'):' +) + -def is_filtered(attributes): +def is_skipped(attributes): for attribute in attributes: - if not rgx_filter.search(attribute): - #print('hmm... interesting attribute: %s' % attribute) + if not rgx_skip.search(attribute): + if debug > 0: + print('hmm... interesting attribute: %s' % attribute) return False - #else: - # print('filter match: %s' % attribute) return True -def backup(req_type, req_dn, entry=None): - if entry: - attributes = dict(entry['attributes']) - entry['attributes'] = attributes - del entry['raw_attributes'] +def log(entry): + req_type = entry['attributes']['reqType'][0] + req_dn = entry['attributes']['reqDN'][0] + + attributes = dict(entry['attributes']) + entry['attributes'] = attributes + del entry['raw_attributes'] + + if debug > 1: pprint.pprint(entry, indent=4) + + if debug > 0: + print('log: %s %s' % (req_type, req_dn)) + - if req_type == 'skip': - print('skip: %s' % req_dn) - else: - print('backup: %s %s' % (req_type, req_dn)) +def sleep(start_time): + current_time = time.perf_counter() + elapsed_time = current_time - start_time + sleep_time = update_every - elapsed_time + + if debug > 0: + print('\nruntime %0.3fs, sleeping %0.3fs' % (elapsed_time, sleep_time)) + + if sleep_time > 0: + time.sleep(sleep_time) def main(): @@ -56,7 +80,8 @@ def main(): 'ldapi:///var/run/slapd/ldapi', get_info=[ ldap3.ALL, - ldap3.OFFLINE_SLAPD_2_4] + ldap3.OFFLINE_SLAPD_2_4 + ] ) connection = ldap3.Connection( @@ -71,7 +96,6 @@ def main(): connection.bind() - reqs_last_run = list() req_start = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S.%fZ') while True: @@ -93,26 +117,19 @@ def main(): req_type = entry['attributes']['reqType'][0] req_dn = entry['attributes']['reqDN'][0] - print(' '.join(['\nprocessing:', req_start, entry['dn']])) + if debug > 0: + print(' '.join(['\nprocessing:', req_start, entry['dn']])) if req_type == 'modify': req_mods = entry['attributes']['reqMod'] - if is_filtered(req_mods): - #backup('skip', req_dn, entry) - backup('skip', req_dn) + if is_skipped(req_mods): + if debug > 0: + print('skip: %s %s' % (req_type, req_dn)) continue - #backup(req_type, req_dn, entry=entry) - backup(req_type, req_dn) - - current_time = time.perf_counter() - elapsed_time = current_time - start_time - sleep_time = update_every - elapsed_time - - print('\nruntime %0.3fs, sleeping %0.3fs' % (elapsed_time, sleep_time)) + log(entry) - if sleep_time > 0: - time.sleep(sleep_time) + sleep(start_time) if __name__ == '__main__':