3 import sys, os, time, bz2
5 if len(sys.argv) != 2 :
6 print 'usage: bz2comonlogfile'
10 start_time = time.time()
11 if not ('dump_comon_' in filename and filename.endswith('bz2')) :
12 print 'not a comon log file:'
15 def str_to_ts(date_str, format="%Y-%m-%d"):
16 ts = time.mktime(time.strptime(date_str, format))
20 # .../dump_cotop_20080101 -> 2008-01-01
21 indx = filename.rfind('dump_comon_') + len('dump_comon') + 1
22 date = filename[indx:indx+8]
23 date = date[0:4] + '-' + date[4:6] + '-' + date[6:8]
26 # read in bz2 log file
27 bzobj = bz2.BZ2File(filename, mode = 'r')
28 lines = bzobj.readlines()
32 processed_tags = set()
34 # keep track of malformed entries
39 important_tags = ['Start:', 'Name:', 'RespTime:']
41 def get_field(table, *args):
44 for i,v in enumerate(args[:-1]):
49 if args[-1] not in pos:
53 def set_field(table, *args):
56 #get_field(table, *args[0:-1])
57 for i,v in enumerate(args[:-2]):
59 pos[args[-2]] = args[-1]
62 # check important_tags
63 for t in important_tags:
65 #print "bad entry", entry
69 if 'Uptime:' in entry:
70 float(entry['Uptime:'])
77 hs = {} # HOST SUMMARY
85 this_time = int(entry['Start:'][0])
86 fmtime = time.strftime('%D %T', time.localtime(this_time))
87 ambigTS = this_time < last_time
92 #outcsv.write('%d,%s' % (this_time, ambigTS))
98 processed_tags = set()
100 #Process other fields
106 if not isValid(entry):
108 processed_tags = set()
114 get_field(hs,h,'offline')
115 get_field(hs,h,'online')
116 get_field(hs,h,'uptime')
119 if len(entry['RespTime:'].split()) > 1:
120 set_field(hs,h,'offline', get_field(hs,h,'offline') + 1)
122 set_field(hs,h,'online', get_field(hs,h,'online') + 1)
123 set_field(hs,h,'uptime', max(get_field(hs,h,'uptime'),entry['Uptime:']) )
125 #print "except resptime"
130 ## print "key error! on hostname: %s" % h
134 processed_tags = set()
138 if tag in processed_tags : # start over, since a tag is repeating
140 processed_tags = set()
141 entry[tag] = " ".join(words[1:len(words)])
142 processed_tags.add(tag)
144 # finished processing log file
146 # clean up memory objs
154 #print "number of hosts:%s" % len(hs)
156 if hs[h]['uptime'] > 0: uptimes.append(float(hs[h]['uptime']))
157 if hs[h]['online'] > hs[h]['offline']:
164 end_time = time.time()
165 print date, ts, online+offline, online, offline, uptimes[0], uptimes[l/4], uptimes[l/2], uptimes[l/2+l/4], uptimes[-1], end_time-start_time, filename