日志根据时间有一定的显示,可以根据这个做日志分析消息流程
import re
import fnmatch
import os
import time
import sys
logfiles = []
class SmtransStats:
def __init__(self, interval = 1):
self.stats = {
'recvmca': ['recved SCP MCA msg from',0, 0]
# 'sendchg' : (0),
# 'chgtimeout' : 0,
# 'chgexpire' : 0,
# 'mergedb' : 0,
# 'chgresp' : 0,
# 'delayedmerge' : 0,
# 'maxsendmerge' : 0,
# 'memmerge' : 0,
# 'canreplace' : 0,
# 'replace' : 0,
# 'submit' : 0,
# 'submitresp' : 0,
# 'report' : 0
}
self.curtime, self.lasttime = 0,0
self.printInterval = interval
def getCurTime(self, line):
timestr = line[:len("2013-01-14 14:55:13")]
epocsec = time.mktime(time.strptime(timestr, "%Y-%m-%d %H:%M:%S"))
return epocsec
def setCurStat(self, k, v, line):
self.curtime = self.getCurTime(line);
self.stats[k][1] = v
def setLastStat(self, k, v, line):
self.curtime = self.getCurTime(line);
self.stats[k][2] = v
def getStatLog(self, k):
return self.stats[k][0]
def getCurStat(self, k):
return self.stats[k][1]
def getLastStat(self, k):
return self.stats[k][2]
def incCurStat(self, k, line):
self.curtime = self.getCurTime(line);
if self.curtime - self.lasttime > self.printInterval:
self.printStats()
time.sleep(1)
self.stats[k][1] += 1
def getStatKeys(self):
return self.stats.keys()
def printStats(self):
for k in self.stats.keys():
print "%s : %d %d" % (k, self.stats[k][1], self.stats[k][1] - self.stats[k][2])
self.updateStats();
def updateStats(self):
self.lasttime = self.curtime
for k in self.stats.keys():
tmp = self.stats[k][1]
self.stats[k][2] = tmp
def main(ss):
while 1:
for file in os.listdir('.'):
if fnmatch.fnmatch(file, 'smtrans_*.log'):
if not file in logfiles:
print "run file: ", file
logfiles.append(file)
for line in open(file, 'r'):
for k in ss.getStatKeys():
if ss.getStatLog(k) in line:
ss.incCurStat(k, line)
break
break
else:
time.sleep(1)
print __name__, sys.argv
if __name__ == "__main__":
if len(sys.argv) > 1:
ss = SmtransStats(sys.argv[1])
else:
ss = SmtransStats(10)
main(ss)