|
| 1 | +# Run as python xld-config-hash-utility.py log1.log log2.log |
| 2 | +# Configure xld-config-hash-utility.properties file in this way: |
| 3 | +# [CommonSection] |
| 4 | +# datetime_pattern=([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}) |
| 5 | +# hash_pattern=Using hash ([0-9a-f]{64}) for (\S+) |
| 6 | +# |
| 7 | +# [Log1Section] |
| 8 | +# start_datetime=2020-02-05 00:00:00.000 |
| 9 | +# end_datetime=2020-02-05 23:59:59.999 |
| 10 | +# |
| 11 | +# [Log2Section] |
| 12 | +# start_datetime=2020-02-05 00:00:00.000 |
| 13 | +# end_datetime=2020-02-05 23:59:59.999 |
| 14 | +# |
| 15 | + |
| 16 | +import re |
| 17 | +import sys |
| 18 | +import ConfigParser |
| 19 | + |
| 20 | +def create_map(pattern, log_file_path, key_hash_map, start_datetime, end_datetime): |
| 21 | + line_count = 0 |
| 22 | + matched_line_count = 0 |
| 23 | + log_file = open(log_file_path) |
| 24 | + for line in log_file: |
| 25 | + line_count += 1 |
| 26 | + m = pattern.match(line) |
| 27 | + if m: |
| 28 | + matched_line_count += 1 |
| 29 | + line_datetime = m.group(1) |
| 30 | + line_hash = m.group(2) |
| 31 | + line_itemname = m.group(3) |
| 32 | + if m.group(1) >= start_datetime and m.group(1) <= end_datetime: |
| 33 | + key_hash_map[m.group(3)] = m.group(2) |
| 34 | + log_file.close() |
| 35 | + print "Read %s lines from %s" % (line_count, log_file_path) |
| 36 | + print "Processing %s hashed items from %s" % (matched_line_count, log_file_path) |
| 37 | + |
| 38 | +def compare_maps(key_hash_map_1, key_hash_map_2): |
| 39 | + print "Match %d items in left log to right log\n" % len(key_hash_map_1) |
| 40 | + for key in key_hash_map_1.keys(): |
| 41 | + if key in key_hash_map_2.keys(): |
| 42 | + if key_hash_map_1[key] == key_hash_map_2[key]: |
| 43 | + continue |
| 44 | + else: |
| 45 | + print "Mismatch on item %s:\nleft file hash is %s\nright file hash is %s\n" % (key, key_hash_map_1[key], key_hash_map_2[key]) |
| 46 | + else: |
| 47 | + print "Mismatch on item %s in left file but not in right file\n" % key |
| 48 | + |
| 49 | +config = ConfigParser.RawConfigParser() |
| 50 | +config.read('xld-config-hash-utility.properties') |
| 51 | + |
| 52 | +datetime_pattern = config.get('CommonSection', 'datetime_pattern') |
| 53 | +hash_pattern = config.get('CommonSection', 'hash_pattern') |
| 54 | +pattern = re.compile(datetime_pattern + ".*" + hash_pattern + ".*") |
| 55 | + |
| 56 | +log1_map = {} |
| 57 | +log2_map = {} |
| 58 | +divider = config.get('CommonSection', 'divider_char') * int(config.get('CommonSection', 'divider_length')) + "\n" |
| 59 | +print "" |
| 60 | +create_map(pattern, sys.argv[1], log1_map, config.get('Log1Section', 'start_datetime'), config.get('Log1Section', 'end_datetime')) |
| 61 | +print "" |
| 62 | +create_map(pattern, sys.argv[2], log2_map, config.get('Log1Section', 'start_datetime'), config.get('Log1Section', 'end_datetime')) |
| 63 | +print "\n" + divider |
| 64 | +print "Compare %s (left) to %s (right)" % (sys.argv[1], sys.argv[2]) |
| 65 | +compare_maps(log1_map, log2_map) |
| 66 | +print divider |
| 67 | +print "Compare %s (left) to %s (right)" % (sys.argv[2], sys.argv[1]) |
| 68 | +compare_maps(log2_map, log1_map) |
| 69 | +print divider |
| 70 | +print "Execution completed" |
0 commit comments