# coding=gb2312
'''''
Created on 2011-8-26
@author: piky
'''
fromos.pathimportwalk, join, normpath
importsys
importos.path
fromosimportgetcwd
importre
importMySQLdb
importtime
#判断str是否符合正则表达式regex,符合则返回符合的部分
defmatch_sepcial_str(regex, str):
p = re.compile(regex)
t = p.match(str)
ift !=None:
returnt.group()
returnNone
#查找str中符合正则表达式regex,符合则返回符合的部分
defsearch_sepcial_str(regex, str):
p = re.compile(regex)
t = p.search(str)
ift !=None:
returnt.group()
returnNone
deftime_change_format(time_str):
tm1 = search_sepcial_str("[0-9]+:[0-9]+:[0-9]+", time_str)
tm2 = search_sepcial_str("[0-9]+/[0-9]+/[0-9]+", time_str)
tm2 = tm2.replace("/","-")
tm = tm2 +" "+ tm1
returntm
classDatabase():
def__init__(self):
self.conn = MySQLdb.connect('localhost','root','123456','smart_analyzer')
self.cur =self.conn.cursor()
return
defcommit(self):
self.conn.commit()
return
database = Database()
classAnylyzerToolTemplate(object):
def__init__(self):
return
defanylyze(self, line):
key = ""
value = ""
returnkey,value
#获取时间
classAnylyzerToolGetTime(object):
def__init__(self):
return
defanylyze(self, line):
r ="[0-9]+:[0-9]+:[0-9]+ [0-9]+/[0-9]+/[0-9]+"
value = match_sepcial_str(r, line)
ifvalue !=None:
#print "time:",value
return"time",value
returnNone,None
classAnylyzerToolStat(object):
def__init__(self):
self.database =None
self.project_id =0
self.key = []
self.file = ""
self.project_name = ""
self.project_dir_name = ""
self.time = ""
self.start = ""
return
defanylyze(self, line):
#print line
iflen(self.key) ==0:
self.get_key_list()
#print self.key
ifline.find('System statistic information') != -1:
self.start =self.time
ifself.start != ""andself.start !=self.time:
#for key in self.key:
# print key
self.add_data(self.time)
self.start = ""
forkeyinself.key:
ifline.find(key[1]) != -1:
value = search_sepcial_str("[0-9]+",line)
ifvalue !=None:
key[2] = value
return
return
defget_key_list(self):
sql ='select name, name_desc from system_stat_type'
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
forkeyinresults:
item = [key[0], key[1],'0']
self.key.append(item)
return
defadd_data1(self, key, data, time_str):
sql ='select * from system_stat_data where type="%s" and data=%s and time="%s" and project_id =%d'% (key, data, time_str,self.project_id)
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
ifresults !=Noneandlen(results) !=0:
return
else:
sql ='insert into system_stat_data (type,data,time,project_id) values ("%s", %s, "%s", %d)'%(key, data, time_str,self.project_id)
printsql
self.database.cur.execute(sql)
self.database.commit()
return
defadd_data(self, time_str):
sql ='select * from system_stat where time="%s" and project_id =%d'% (time_str,self.project_id)
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
ifresults !=Noneandlen(results) !=0:
return
else:
items = ""
forkeyinself.key:
items = items + key[0] +","
#items = items[0:len(items)-1]
values = ""
forkeyinself.key:
values = values + key[2] +','
#values = values[0:len(values)-1]
sql ='insert into system_stat (%s time, project_id) values (%s "%s", %d)'%(items, values, time_str,self.project_id)
printsql
self.database.cur.execute(sql)
self.database.commit()
return
classAnylyzerToolVersion(object):
def__init__(self):
self.database =None
self.project_id =0
self.time = ""
return
defanylyze(self, line):
ifline.find('Software Version') == -1:
return
tmp1 = line.split(':')
tmp2 = tmp1[1].split('(')
version = tmp2[0].strip()
self.add_data(self.time, version)
return
defadd_data(self, time_str, version):
sql ='select * from version where version="%s" and project_id =%d'% (version,self.project_id)
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
ifresults !=Noneandlen(results) !=0:
return
sql ='insert into version (version, time, project_id) values ("%s", "%s", %d)'%(version, time_str,self.project_id)
printsql
self.database.cur.execute(sql)
self.database.commit()
return
classAnylyzerToolInterface(object):
def__init__(self):
self.database =None
self.project_id =0
self.time = ""
self.start = ""
self.name = ""
self.data = [
['outpps','output rate','[0-9]*[ ]*packets/sec','0'],
['outBps','output rate','[0-9]*[ ]*bytes/sec','0'],
['inpps','input rate','[0-9]*[ ]*packets/sec','0'],
['inBps','input rate','[0-9]*[ ]*bytes/sec','0']
]
'''''
{'outpps':'0',
'outBps':'0',
'inpps':'0',
'inBps':'0',
}'''
self.status = ""
return
defset_data(self, key, data):
printkey,data
fortmpinself.data:
iftmp[0] == key:
tmp[3] = data
return
defanylyze(self, line):
#Ethernet1/0/0 current state
#GigabitEthernet0/0/0 current state
#Ethernet1/0 current state
ifself.start != "":
fortmpinself.data:
ifline.find(tmp[1]) != -1:
tmp1 = search_sepcial_str(tmp[2], line)
tmp2 = match_sepcial_str('[0-9]*', tmp1)
iftmp2 !=None:
tmp[3] = tmp2
ifline.find('Input:') != -1:
printself.data
self.add_data(self.time,self.name)
self.start = ""
'''''
if self.start != "":
if line.find('input') != -1:
print line
inpps = search_sepcial_str('[0-9]*[ ]*packets/sec', line)
if inpps != None:
inpps_str = match_sepcial_str('[0-9]*', inpps)
if inpps_str != None:
self.set_data('inpps', inpps_str)
inBps = search_sepcial_str('[0-9]*[ ]*bytes/sec', line)
if inBps != None:
inBps_str = match_sepcial_str('[0-9]*', inBps)
if inBps_str != None:
self.set_data('inBps', inBps_str)
if line.find('output rate') != -1:
outpps = search_sepcial_str('[0-9]*[ ]*packets/sec', line)
if outpps != None:
outpps_str = match_sepcial_str('[0-9]*', outpps)
if outpps_str != None:
self.set_data('outpps', outpps_str)
outBps = search_sepcial_str('[0-9]*[ ]*bytes/sec', line)
if outBps != None:
outBps_str = match_sepcial_str('[0-9]*', outBps)
if outBps_str != None:
self.set_data('outBps', outBps_str)
if line.find('Output') != -1:
print self.data
self.add_data(self.time, self.name)
self.start = ""
'''
name = match_sepcial_str("(Ethernet|GigabitEthernet)[0-9]/[0-9]/[0-9][ ]*current state", line)
ifname !=None:
name = name.split(' ')[0]
self.name = name
self.start =self.name
tmp = line.split(':')
self.status = tmp[1].strip()
printself.name,self.status
return
defadd_data(self, time_str, name):
sql ='select id from interface where name="%s" and time="%s" and project_id=%d'%(name, time_str,self.project_id)
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
ifresults !=Noneandlen(results) !=0:
return
items = ""
forkeyinself.data:
items = items + key[0] +","
#items = items[0:len(items)-1]
values = ""
forkeyinself.data:
values = values + key[3] +','
#values = values[0:len(values)-1]
sql ='insert into interface (%s name, status, time, project_id) values (%s "%s", "%s", "%s", %d)'%(items, values,self.name,self.status, time_str,self.project_id)
printsql
self.database.cur.execute(sql)
self.database.commit()
classAnylyzerToolProject(object):
def__init__(self):
self.database =None
return
defadd_data(self, project_name, project_dir, anylyze_file_name):
sql ='select id from project where path="%s" and file_name="%s"'%(project_dir, anylyze_file_name)
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
ifresults !=Noneandlen(results) !=0:
id = results[0][0]
returnid
else:
sql ='insert into project (name, path, file_name) values ("%s", "%s", "%s")'%(project_name, project_dir, anylyze_file_name)
sql.encode("utf-8")
printsql
self.database.cur.execute(sql)
self.database.commit()
sql ='select id from project where path="%s" and file_name="%s"'%(project_dir, anylyze_file_name)
self.database.cur.execute(sql)
results =self.database.cur.fetchall()
ifresults !=None:
id = results[0][0]
returnid
returnNone
classAnylyzer(object):
def__init__(self, project_name, project_dir_name, anylyze_file_name):
self.anylyze_file_name = anylyze_file_name
self.analyze_file_type = []
self.project_name = project_name
self.project_dir = project_dir_name
self.analyze_class = []
self.init_analyze_class()
self.database = Database()
self.project_id =0
return
defregister_analyze_tool(self, class_name):
tool = class_name()
tool.file =self.anylyze_file_name
tool.project_name =self.project_name
tool.project_dir_name =self.project_dir
self.analyze_class.append(tool)
return
defanylyzer_file(self):
file =self.anylyze_file_name
#print file
#过滤空文件
if0== os.path.getsize(file):
return
#将本文件记录的数据库中
anylyzer_tool_project = AnylyzerToolProject()
anylyzer_tool_project.database =self.database
self.project_id = anylyzer_tool_project.add_data(self.project_name,self.project_dir,self.anylyze_file_name)
print"begin anylyze file=%s, id=%d"%(file,self.project_id)
#开始读取文件
try:
tm = ""
fp = open(file)
whileTrue:
line = fp.readline()
ifline ==Noneorlen(line) ==0:
break
anylyzer_tool_get_time = AnylyzerToolGetTime()
key,value = anylyzer_tool_get_time.anylyze(line)
ifkey !=None:
tm = time_change_format(value)
foranylyzer_toolinself.analyze_class:
anylyzer_tool.time = tm
anylyzer_tool.database =self.database
anylyzer_tool.project_id =self.project_id
anylyzer_tool.anylyze(line)
fp.close()
exceptBaseException, e:
print"can't open",file
printstr(e)
return
print"end anylyze file:",file
return
definit_analyze_class(self):
self.register_analyze_tool(AnylyzerToolStat)
self.register_analyze_tool(AnylyzerToolVersion)
self.register_analyze_tool(AnylyzerToolInterface)
return
classCollector(object):
'''''
classdocs
'''
def__init__(self):
self.project_root_dir_depth =0
self.collector_project = []
self.analyze_file_type = []
return
defset_project_root_dir_depth(self, depth):
self.project_root_dir_depth = depth
return
defget_dir_depth(self, dir1, dir2):
dir1_len = len(dir1)
dir2_len = len(dir2)
depth =0
ifdir1_len > dir2_len:
return-1
ifdir2[0:dir1_len] != dir1:
return-1
forcindir2[dir1_len:dir2_len]:
if"\\"== c:
depth = depth +1
returndepth
defregister_analyze_file_type(self, type):
self.analyze_file_type.append(type)
return
defget_dir_name(self, dir):
names = os.path.split(dir)
returnnames[1]
defread_file(self, file, project_name, project_dir_name):
#过滤文件扩展名,如果没有设置过滤器,则不作判断
file = file.replace("\\", "/")
project_name = project_name.replace("\\", "/")
project_dir_name = project_dir_name.replace("\\", "/")
printfile, project_name, project_dir_name
can_read =True
tmp1 = os.path.splitext(file)
iflen(self.analyze_file_type) >0:
can_read =False
fortmp2inself.analyze_file_type:
iftmp1[1] == tmp2:
can_read =True
break
ifFalse== can_read:
return
a = Anylyzer(project_name, project_dir_name, file)
a.anylyzer_file()
return
#遍历指定目录中的所有子目录和文件
defwalk_dir(self, path=None):
ifpath ==None:
path =getcwd()#current path
alldir = os.walk(path)
project_name = ""
project_dir_name = ""
forsub_pathinalldir:
#print self.get_dir_depth(path, sub_path[0]), sub_path[0]
#print sub_path
if(self.project_root_dir_depth ==self.get_dir_depth(path, sub_path[0]) -1):
project_name =self.get_dir_name(sub_path[0])
project_dir_name = sub_path[0]
#print "project_dir_name:",project_dir_name
#print "project_name:",project_name
forfileinsub_path[2]:
self.read_file(sub_path[0]+"\\"+file, project_name, project_dir_name)
return
c = Collector()
c.set_project_root_dir_depth(0)
c.register_analyze_file_type(".txt")
c.walk_dir()