分析目錄下所有Eudemon防火牆配置的Python腳本:
- # coding=gb2312
-
- '''''
- Created on 2011-8-26
-
- @author: piky
- '''
-
- from os.path import walk, join, normpath
- import sys
- import os.path
- from os import getcwd
- import re
- import MySQLdb
- import time
-
- #判斷str是否符合正則表達式regex,符合則返回符合的部分
- def match_sepcial_str(regex, str):
- p = re.compile(regex)
- t = p.match(str)
- if t != None:
- return t.group()
- return None
-
- #查找str中符合正則表達式regex,符合則返回符合的部分
- def search_sepcial_str(regex, str):
- p = re.compile(regex)
- t = p.search(str)
- if t != None:
- return t.group()
- return None
-
- def time_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
- return tm
-
- class Database():
- def __init__(self):
- self.conn = MySQLdb.connect('localhost', 'root', '123456', 'smart_analyzer')
- self.cur = self.conn.cursor()
- return
-
- def commit(self):
- self.conn.commit()
- return
-
- database = Database()
-
- class AnylyzerToolTemplate(object):
- def __init__(self):
- return
-
- def anylyze(self, line):
- key = ""
- value = ""
- return key,value
-
- #獲取時間
- class AnylyzerToolGetTime(object):
- def __init__(self):
- return
-
- def anylyze(self, line):
- r = "[0-9]+:[0-9]+:[0-9]+ [0-9]+/[0-9]+/[0-9]+"
- value = match_sepcial_str(r, line)
- if value != None:
- #print "time:",value
- return "time",value
- return None,None
-
- class AnylyzerToolStat(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
-
- def anylyze(self, line):
- #print line
- if len(self.key) == 0:
- self.get_key_list()
- #print self.key
-
- if line.find('System statistic information') != -1:
- self.start = self.time
-
- if self.start != "" and self.start != self.time:
- #for key in self.key:
- # print key
- self.add_data(self.time)
- self.start = ""
-
-
- for key in self.key:
- if line.find(key[1]) != -1:
- value = search_sepcial_str("[0-9]+",line)
- if value != None:
- key[2] = value
- return
- return
-
- def get_key_list(self):
- sql = 'select name, name_desc from system_stat_type'
- self.database.cur.execute(sql)
- results = self.database.cur.fetchall()
- for key in results:
- item = [key[0], key[1], '0']
- self.key.append(item)
- return
-
- def add_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()
- if results != None and len(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)
- print sql
- self.database.cur.execute(sql)
- self.database.commit()
- return
-
- def add_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()
- if results != None and len(results) != 0:
- return
- else:
- items = ""
- for key in self.key:
- items = items + key[0] + ","
- #items = items[0:len(items)-1]
- values = ""
- for key in self.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)
- print sql
- self.database.cur.execute(sql)
- self.database.commit()
- return
-
-
- class AnylyzerToolVersion(object):
- def __init__(self):
- self.database = None
- self.project_id = 0
- self.time = ""
- return
-
- def anylyze(self, line):
- if line.find('Software Version') == -1:
- return
- tmp1 = line.split(':')
- tmp2 = tmp1[1].split('(')
- version = tmp2[0].strip()
- self.add_data(self.time, version)
- return
-
- def add_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()
- if results != None and len(results) != 0:
- return
- sql = 'insert into version (version, time, project_id) values ("%s", "%s", %d)'%(version, time_str, self.project_id)
- print sql
- self.database.cur.execute(sql)
- self.database.commit()
- return
-
-
- class AnylyzerToolInterface(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
-
- def set_data(self, key, data):
- print key,data
- for tmp in self.data:
- if tmp[0] == key:
- tmp[3] = data
- return
-
- def anylyze(self, line):
- #Ethernet1/0/0 current state
- #GigabitEthernet0/0/0 current state
- #Ethernet1/0 current state
-
- if self.start != "":
- for tmp in self.data:
- if line.find(tmp[1]) != -1:
- tmp1 = search_sepcial_str(tmp[2], line)
- tmp2 = match_sepcial_str('[0-9]*', tmp1)
- if tmp2 != None:
- tmp[3] = tmp2
- if line.find('Input:') != -1:
- print self.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)
- if name != None:
- name = name.split(' ')[0]
- self.name = name
- self.start = self.name
- tmp = line.split(':')
- self.status = tmp[1].strip()
- print self.name, self.status
- return
-
- def add_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()
- if results != None and len(results) != 0:
- return
- items = ""
- for key in self.data:
- items = items + key[0] + ","
- #items = items[0:len(items)-1]
- values = ""
- for key in self.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)
- print sql
- self.database.cur.execute(sql)
- self.database.commit()
-
- class AnylyzerToolProject(object):
- def __init__(self):
- self.database = None
- return
-
- def add_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()
- if results != None and len(results) != 0:
- id = results[0][0]
- return id
- else:
- sql = 'insert into project (name, path, file_name) values ("%s", "%s", "%s")'%(project_name, project_dir, anylyze_file_name)
- sql.encode("utf-8")
- print sql
- 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()
- if results != None:
- id = results[0][0]
- return id
- return None
-
- class Anylyzer(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
-
- def register_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
-
- def anylyzer_file(self):
- file = self.anylyze_file_name
- #print file
-
- #過濾空文件
- if 0 == 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)
- while True:
- line = fp.readline()
- if line == None or len(line) == 0:
- break
- anylyzer_tool_get_time = AnylyzerToolGetTime()
- key,value = anylyzer_tool_get_time.anylyze(line)
- if key != None:
- tm = time_change_format(value)
- for anylyzer_tool in self.analyze_class:
- anylyzer_tool.time = tm
- anylyzer_tool.database = self.database
- anylyzer_tool.project_id = self.project_id
- anylyzer_tool.anylyze(line)
- fp.close()
- except BaseException, e:
- print "can't open",file
- print str(e)
- return
- print "end anylyze file:",file
- return
-
- def init_analyze_class(self):
- self.register_analyze_tool(AnylyzerToolStat)
- self.register_analyze_tool(AnylyzerToolVersion)
- self.register_analyze_tool(AnylyzerToolInterface)
- return
-
- class Collector(object):
- '''''
- classdocs
- '''
- def __init__(self):
- self.project_root_dir_depth = 0
- self.collector_project = []
- self.analyze_file_type = []
- return
-
- def set_project_root_dir_depth(self, depth):
- self.project_root_dir_depth = depth
- return
-
- def get_dir_depth(self, dir1, dir2):
- dir1_len = len(dir1)
- dir2_len = len(dir2)
- depth = 0
- if dir1_len > dir2_len:
- return -1
- if dir2[0:dir1_len] != dir1:
- return -1
- for c in dir2[dir1_len:dir2_len]:
- if "\\" == c:
- depth = depth + 1
- return depth
-
- def register_analyze_file_type(self, type):
- self.analyze_file_type.append(type)
- return
-
- def get_dir_name(self, dir):
- names = os.path.split(dir)
- return names[1]
-
- def read_file(self, file, project_name, project_dir_name):
- #過濾文件擴展名,如果沒有設置過濾器,則不作判斷
-
- file = file.replace("\\", "/")
- project_name = project_name.replace("\\", "/")
- project_dir_name = project_dir_name.replace("\\", "/")
- print file, project_name, project_dir_name
-
- can_read = True
- tmp1 = os.path.splitext(file)
- if len(self.analyze_file_type) > 0:
- can_read = False
- for tmp2 in self.analyze_file_type:
- if tmp1[1] == tmp2:
- can_read = True
- break
- if False == can_read:
- return
-
- a = Anylyzer(project_name, project_dir_name, file)
- a.anylyzer_file()
- return
-
- #遍歷指定目錄中的所有子目錄和文件
- def walk_dir(self, path=None):
- if path == None:
- path =getcwd() #current path
- alldir = os.walk(path)
- project_name = ""
- project_dir_name = ""
- for sub_path in alldir:
- #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
- for file in sub_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()