#encoding:utf8
import os
import string
import re
import MySQLdb
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
class sendXmlData():
fileDir = '/opt/boyi/iftop/tmp'
def __getPassengerIp(self):
conn = MySQLdb.connect(host='localhost', user='root', passwd='')
cursor = conn.cursor()
conn.select_db('idc_database')
cursor.execute('set names utf8')
cursor.execute('select ip from passenger_table')
ipList = cursor.fetchall()
print '#' * 30 + 'ipList' + '#' * 30
print ipList
cursor.close()
conn.close()
return ipList
#主函数
def main(self):
while True:
listxml = self.__getXmlPath()
if len(listxml) > 0:
xmllist = []
for _xmlpath in listxml:
xmlfileName = os.path.join(self.fileDir, _xmlpath)
print xmlfileName
xmlData = self.__returnxmlList(xmlfileName)
for xmlInfo in xmlData:
xmllist.append(xmlInfo.values())
print xmllist
self.writexmlInfo(xmllist)
#将列表中的子列表用逗号分隔 返回新列表
def joinList(self, list):
returnList = []
for i in list:
returnList.append(string.join(i, ','))
return returnList
#向文件中写入信息,修改之前的文件信息
def writexmlInfo(self, xmllist):
if os.path.exists('/tmp/trafficAnalysis.txt'):
print 'open file'
fp = open('/tmp/trafficAnalysis.txt', 'r+')
fileList = fp.readlines()
if len(fileList) == 0:
fp.write(string.join(self.joinList(xmllist), '\n'))
fp.close()
else:
newfileListinfo = []
#获取到的xml 信息与文件中比对 比对上了就累加
newfileListinfo = self.__upateFileMsg(fileList, newfileListinfo, xmllist)
#新列表添加已经记录在/tmp/trafficAnalysis 中的元素
newfileListinfo = self.__addOldFileMsg(fileList, newfileListinfo)
#获取机器ip信息
ipList = self.__getPassengerIp()
print '#' * 40 + 'new fileinfo' + '#' * 40
print newfileListinfo
print '#' * 40 + 'filterNewList' + '#' * 40
print self.filterNewList(newfileListinfo, ipList)
fp.close()
fp = open('/tmp/trafficAnalysis.txt', 'w')
newlistinfo = self.filterNewList(newfileListinfo, ipList)
fp.write(string.join(newlistinfo).replace(' ', ''))
fp.close()
os.system('unlink /opt/html/framework/trafficAnalysis.txt')
os.system('cp /tmp/trafficAnalysis.txt /opt/html/framework/trafficAnalysis.txt')
else:
print 'touch file'
os.system('touch /tmp/trafficAnalysis.txt')
#使用ip 过滤列表
def filterNewList(self, newfileListinfo, ipList):
returnList = []
for newfileListinfoMsg in newfileListinfo:
for ipListMsg in ipList:
if newfileListinfoMsg.find(',' + ipListMsg[0] + ',') != -1:
returnList.append(newfileListinfoMsg)
return returnList
#获取到的xml 信息与文件中比对 比对上了就累加
def __upateFileMsg(self, fileList, newfileListinfo, xmllist):
for xmlListInfo in xmllist:
i = -1
for fileListInfo in fileList:
if fileListInfo.find(',' + xmlListInfo[1] + ',') != -1:
i = 1
filelistMsg = fileListInfo.split(',')
if len(filelistMsg) > 4:
filelistMsg[3] = str(int(filelistMsg[3]) + int(xmlListInfo[0]))
filelistMsg[4] = str(int(filelistMsg[4]) + int(xmlListInfo[2]))
else:
filelistMsg.append(str(int(xmlListInfo[0]) + int(filelistMsg[0])))
filelistMsg.append(str(int(xmlListInfo[2]) + int(filelistMsg[2].replace('\n', ''))))
filelistMsg[0] = xmlListInfo[0]
filelistMsg[1] = xmlListInfo[1]
filelistMsg[2] = xmlListInfo[2]
newrow = string.join(filelistMsg, ',') + '\n'
newfileListinfo.append(newrow)
if i == -1:
xmlListInfo[2] = xmlListInfo[2] + '\n'
newfileListinfo.append(string.join(xmlListInfo, ','))
return newfileListinfo
#删除列表中相同的元素
def __deleteDuplicatedElement(self, list):
resultList = []
for item in list:
if not item in resultList:
resultList.append(item)
return resultList
#新列表添加已经记录在/tmp/trafficAnalysis 中的元素
def __addOldFileMsg(self, fileList, newfileListinfo):
for fileListInfo in fileList:
j = -1
fileListMsg = fileListInfo.split(',')
for newfileList in newfileListinfo:
if newfileList.find(',' + fileListMsg[1] + ',') != -1:
j = 1
if j == -1:
newfileListinfo.append(fileListInfo)
return newfileListinfo
#获取xml 信息
def __getXmlPath(self):
filelist = os.listdir(self.fileDir)
for fileName in filelist:
filepath = os.path.join(self.fileDir, fileName)
if os.path.isfile(filepath):
os.remove(filepath)
print filepath + ' removed!'
os.system('/opt/boyi/iftop/sbin/iftop')
return os.listdir(self.fileDir)
#返回xmlList 信息
def __returnxmlList(self, fileName):
list = []
tree = ET.ElementTree(file=fileName);
root = tree.getroot()
for child_of_root in root:
list.append(child_of_root.attrib)
return list
if __name__ == '__main__':
sendXmlData = sendXmlData()
sendXmlData.main()
转载于:https://my.oschina.net/u/2005212/blog/308020