mongo_overtime_log.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import re
  2. import sys
  3. def getmongo_overtime_log(len_time, logfile):
  4. d = {}
  5. l = []
  6. open_list = []
  7. with open(logfile, 'r') as f:
  8. for line in f:
  9. line_sql = re.search('^.*\[conn(\d+)\].*\s+(\d+)ms$', line)
  10. line_conn_open = re.search('^.*T(.*)\+.*#(\d+).*$', line)
  11. line_conn_close = re.search('^.*T(.*)\+.*\[conn(\d+)\].*end\sconnection.*$', line)
  12. if line_conn_open:
  13. conn_num = line_conn_open.group(2)
  14. if not d.get(conn_num):
  15. d[conn_num] = []
  16. d[conn_num].append(line)
  17. open_list.append(conn_num)
  18. else:
  19. d[conn_num].append(line)
  20. elif line_conn_close:
  21. conn_num = line_conn_close.group(2)
  22. if not d.get(conn_num):
  23. d[conn_num] = []
  24. d[conn_num].append(line)
  25. else:
  26. d[conn_num].append(line)
  27. if line in d[conn_num] and len(d[conn_num]) >= 3 and conn_num in open_list:
  28. for x in d[conn_num]:
  29. print x
  30. print '=' * 100
  31. elif line_sql:
  32. use_time = line_sql.group(2)
  33. conn_num = line_sql.group(1)
  34. if int(use_time) > int(len_time):
  35. if not d.get(conn_num):
  36. d[conn_num] = []
  37. d[conn_num].append(line)
  38. else:
  39. d[conn_num].append(line)
  40. if __name__ == '__main__':
  41. logfile = sys.argv[1]
  42. len_time = sys.argv[2]
  43. #logfile = r'C:\Users\Administrator\Desktop\mongodb_27037.log.2016-01-07T16-00-09'
  44. getmongo_overtime_log(int(len_time), logfile)