Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 4.90 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Python query: iterating through log file
  2. jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217
  3.     jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537
  4.     jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 recv: 1 timestamp: 00:00:08,018
  5.     jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 nack: 14 timestamp: 00:00:10,338
  6.        
  7. jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217
  8.     jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537
  9.        
  10. jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 00:00:02,217 ack: 00:00:04,537
  11.        
  12. #!/opt/SP/bin/python
  13.  
  14.     log = file(/opt/SP/logs/generic.log, "r")
  15.     filecontent = log.xreadlines()
  16.     storage = {}
  17.     for line in filecontent:
  18.         line = line.strip()
  19.         jarid, JARID, status, STATUS, timestamp, TIME = line.split(" ")
  20.         if JARID not in storage:
  21.             storage[JARID] = {}
  22.         if STATUS not in storage[JARID]:
  23.             storage[JARID][STATUS] = {}
  24.         if TIME not in storage[JARID][STATUS]:
  25.             storage[JARID][STATUS][TIME] = {}
  26.  
  27.     jarids = storage.keys()
  28.     jarids.sort()
  29.     for JARID in jarids:
  30.         stats = storage[JARID].keys()
  31.         stats.sort()
  32.         for STATUS in stats:
  33.             times = storage[JARID][STATUS].keys()
  34.             times.sort()
  35.             for TIME in times:
  36.                 all = storage[JARID][STATUS][TIME].keys()
  37.                 all.sort()
  38.  
  39.     for JARID in jarids:
  40.         if "1" in storage[JARID].keys() and "13" in storage[JARID].keys():
  41.             print "MSG: %s, RECV: %s, ACK: %s" % (JARID, storage[JARID]["1"], storage[JARID]["13"])
  42.         else:
  43.             if "1" in storage[JARID].keys() and "14" in storage[JARID].keys():
  44.                 print "MSG: %s, RECV: %s, NACK: %s" % (JARID, storage[JARID]["1"], storage[JARID]["14"])
  45.        
  46. MSG: 7e5ae720-9151-11e0-eff2-00238bce4216, RECV: {'00:00:02,217': {}}, ACK: {'00:00:04,537': {}}
  47.        
  48. log = ['jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217',
  49.        'jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537',
  50.        'jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 recv: 1 timestamp: 00:00:08,018',
  51.        'jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 nack: 14 timestamp: 00:00:10,338']
  52.        
  53. d = {}
  54. for i in (line.split() for line in log):
  55.     d.setdefault(i[1], {}).update({i[2]:i[-1]})
  56.  
  57. #as pointed by @gnibbler, you can also use "defaultdict"
  58. #instead of dict with "setdefault"
  59.        
  60. for i,j in d.items():
  61.     print 'jarid:', i,
  62.     for k,m in j.items():
  63.         print k, m,
  64.     print
  65.        
  66. from collections import defaultdict
  67. log = ['jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217',
  68.        'jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537',
  69.        'jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 recv: 1 timestamp: 00:00:08,018',
  70.        'jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 nack: 14 timestamp: 00:00:10,338']
  71.  
  72. d = defaultdict(dict)
  73. for i in (line.split() for line in log):
  74.     d[i[1]][i[2]] = i[-1]
  75.        
  76. for label1, jarid, jartype, x, label2, timestamp in (line.split() for line in log):
  77.     d[jarid][jartype] = timestamp
  78.        
  79. def search_jarids(jarid):
  80.     stored_jarid = storage[jarid]
  81.     entry = "jarid: %s" % jarid
  82.     for status in stored_jarid:
  83.         entry += " %s: %s" % (status, stored_jarid[status])
  84.     return entry
  85.  
  86. with open("yourlog.log", 'r') as log:
  87.     lines = log.readlines()
  88.  
  89. storage = {}
  90.  
  91. for line in lines:
  92.     line = line.strip()
  93.     jarid_tag, jarid, status_tag, status, timestamp_tag, timestamp = line.split(" ")
  94.  
  95.     if jarid not in storage:
  96.         storage[jarid] = {}
  97.  
  98.     status_tag = status_tag[:-1]
  99.     storage[jarid][status_tag] = timestamp
  100.  
  101. print search_jarids("462c6d11-9151-11e0-a72c-00238bbdc9e7")
  102.        
  103. import re
  104. pattern = re.compile(r"""jarid:s(S+)       # save jarid to group 1
  105.                          s(recv:)sd+      # save 'recv:' to group 2
  106.                          stimestamp:s(S+) # save recv timestamp to group 3
  107.                          .*?jarid:s1       # make sure next line has same jarid
  108.                          s(n?ack:)sd+     # save 'ack:' or 'nack:' to group 4
  109.                          stimestamp:s(S+) # save ack timestamp to group 5
  110.                      """, re.VERBOSE | re.DOTALL | re.MULTILINE)
  111.  
  112. for content in pattern.finditer(log):
  113.     print "    jarid: " + " ".join(content.groups())
  114.        
  115. import re
  116.  
  117. line_pattern = re.compile(
  118.     r"jarid: (?P<jarid>[a-z0-9-]+) (?P<action>[a-z]+): (?P<status>[0-9]+) timestamp: (?P<ts>[0-9:,]+)"
  119. )
  120.  
  121. infile = open('/path/to/file.log')
  122. entries = (line_pattern.match(line).groupdict() for line in infile)
  123. events = {}
  124.  
  125. for entry in entries:
  126.     event = events.setdefault(entry['jarid'], {})
  127.     event[entry['action']] = entry['ts']
  128.  
  129. for jarid, event in events.iteritems():
  130.     ack_event = 'ack' if 'ack' in event else 'nack' if 'nack' in event else None
  131.     print 'jarid: %s recv: %s %s: %s' % (jarid, event.get('recv'), ack_event, event.get(ack_event))