Created
September 17, 2014 18:21
-
-
Save munk/5ebc69a3c0721eac953b to your computer and use it in GitHub Desktop.
Mongo Log Parser to find open connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import logging | |
import datetime | |
def parse_connection(line): | |
tokens = line.split() | |
kind = 'OPEN' if "connection accepted from" in line else 'CLOSE' | |
connection_id = tokens[9] if kind is 'OPEN' \ | |
else tokens[4][1:-1].replace('conn', '#') | |
timestamp = datetime.datetime.strptime(' '.join(tokens[0:4]), '%a %b %d %H:%M:%S.%f') | |
ip_address = tokens[8] if kind is 'OPEN' else tokens[7] | |
return connection_id, timestamp, ip_address, kind | |
def calc_open_time(start, end): | |
return end[1] - start[1] | |
def test(): | |
OPEN_CONNECTION = 'Wed Sep 17 11:17:19.163 [initandlisten] connection accepted from 192.0.0.1:123 #8841 (705 connections now open)' | |
END_CONNECTION = 'Wed Sep 17 11:17:05.842 [conn8828] end connection 192.0.0.2:123 (704 connections now open)' | |
assert parse_connection(END_CONNECTION) == ('#8828', | |
datetime.datetime(1900, 9, 17, 11, 17, 5, 842000), | |
'192.0.0.2:123', | |
'CLOSE') | |
assert parse_connection(OPEN_CONNECTION) == ('#8841', | |
datetime.datetime(1900, 9, 17, 11, 17, 19, 163000), | |
'192.0.0.1:123', | |
'OPEN') | |
print "All tests completed successfully" | |
def main(show_time_open): | |
open_connections = {} | |
time_open = {} | |
for num, line in enumerate(sys.stdin): | |
try: | |
key, timestamp, ip_address, kind = parse_connection(line) | |
except: | |
continue #Skip bad lines no matter what | |
if kind is 'OPEN': | |
if key in open_connections: | |
logging.warning(key + ' in open connections already! ' + str(open_connections[key])) | |
open_connections[key] = (timestamp, ip_address) | |
else: | |
if key not in open_connections: | |
logging.info(key + ' not in open connections yet, skipping it') | |
continue | |
time_open[(key, timestamp, ip_address)] = timestamp - open_connections[key][0] | |
del open_connections[key] | |
sys.stderr.write('\r' + str(num)) | |
print "=====================" | |
for key, value in open_connections.items(): | |
print key, '|', str(value[0].time()), '|', value[1] | |
print "=====================" | |
if __name__ == '__main__': | |
if len(sys.argv) > 1 and sys.argv[1] == 'test': | |
test() | |
else: | |
main(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment