由于上证所,深交所level1,level2金融数据服务器在上午9:00开始到11:30和下午13:00开始到15:30一共大约5个小时的时间内流量比较大所以被监控服务器的网络流速算是一个被监控的重要指标。可以通过累加一段时间内各个网卡的上行,下行流量除以这个时间间隔计算出这段时间内的平均网速,我现在的采集频率是1分钟采集一次,在实际开盘期间运行过程中得到的网速监控信息用还是比较准确的,都保持在5M/S左右的速度,有时候在平时非服务期看见某台服务器的内网网卡网速达到5M/S ,果然就是有人在大手笔传输。
独立的监控脚本是返回一个列表嵌套元组的数据结构,最后再汇总成一个完整的XML数据岛,为了调试方便脚本的每一个中间结果都导出到一个临时文本中。
运行以下脚本要确定你的linux装了ethtool工具,在ubuntu2.6.27-7-server,ubuntu22.6.27.19-5-default,suse 2.6.27.19-5-default 测试通过。
代码:
-
-
- import re
- import os
- import time
-
- import utils
- def sortedDictValues3(adict):
- keys = adict.keys()
- keys.sort()
- return map(adict.get, keys)
-
- def run():
- if utils.isLinux() == False:
- return [('ifconfig_collect os type error','this is windows')]
-
- if os.path.isfile('./oldifconfig'):
- fileold = open('./oldifconfig', 'r')
- fileold.seek(0)
-
- (oldtime, fileoldcontent) = fileold.read().split('#')
- fileold.close;
- netcard = {}
- tempstr = ''
- key = ''
- for strline in fileoldcontent.split('\n'):
- reobj = re.compile('^lo*.')
- if reobj.search(strline):
- break;
- reobj = re.compile('^eth*.')
- if reobj.search(strline):
- key = strline.split()[0]
- tempstr = tempstr + strline + '\n'
- netcard[key] = tempstr
- RXold = {}
- TXold = {}
- for key,value in netcard.items():
- tempsplit = value.split('\n')
- netcard[key] = ''
- for item in tempsplit:
- item = item + '<br>'
- netcard[key] = netcard[key] + item
- tempcount = 1
- for match in re.finditer("(bytes:)(.*?)( \()", item):
- if tempcount == 1:
- RXold[key] = match.group(2)
- tempcount = tempcount + 1
- elif tempcount == 2:
- TXold[key] = match.group(2)
- netcard[key] = netcard[key] + 'net io percent(bytes/s): 0 <br>'
-
-
- os.system('ifconfig > ifconfigtemp')
- file = open('./ifconfigtemp','r');
- fileold = open('./oldifconfig', 'w')
- temptimestr = str(int(time.time()));
- fileold.write(temptimestr)
- fileold.write('#')
- file.seek(0)
- fileold.write(file.read())
- fileold.close()
- returnkeys = []
- returnvalues = []
- netcard = {}
- tempcountcard = 0
- file.seek(0)
- key = ''
- for strline in file.readlines():
- reobj = re.compile('^lo*.')
- if reobj.search(strline):
- break;
- reobj = re.compile('^eth*.')
- if reobj.search(strline):
- key = strline.split()[0]
- netcard[key] = ''
- netcard[key] = netcard[key] + strline
- newnetcard = {}
- file.seek(0)
- key = ''
- for strline in file.readlines():
- reobj = re.compile('^lo*.')
- if reobj.search(strline):
- break;
- if re.search("^eth", strline):
- templist = strline.split()
- key = templist[0]
- newnetcard[key] = ''
- newnetcard[key] = templist[4] + newnetcard[key] + ' '
- if re.search("^ *inet ", strline):
- templist = strline.split()
- newnetcard[key] = templist[1][5:] + ' ' + newnetcard[key] + ' '
- for key,value in newnetcard.items():
-
- os.system('ethtool %s > ethtooltemp'%(key))
- file = open('./ethtooltemp','r');
- tempethtooltemplist = file.read().split('\n\t')
- file.close
- if re.search("yes", tempethtooltemplist[-1]):
- templist = newnetcard[key].split()
- newnetcard[key] = templist[0] + ' runing! ' + templist[1]
- else:
- templist = newnetcard[key].split()
- if len(templist) > 1:
- newnetcard[key] = templist[0] + ' stop! ' + templist[1]
- else:
- newnetcard[key] = 'stop! ' + templist[0]
- file.close()
- RX = {}
- TX = {}
- for key,value in netcard.items():
- tempsplit = value.split('\n')
- netcard[key] = ''
- for item in tempsplit:
- item = item + '<br>'
- netcard[key] = netcard[key] + item
- tempcount = 1
- for match in re.finditer("(bytes:)(.*?)( \()", item):
- if tempcount == 1:
- RX[key] = str(int(match.group(2)) - int(RXold[key]))
- tempcount = tempcount + 1
- elif tempcount == 2:
- TX[key] = str(int(match.group(2)) - int(TXold[key]))
- divtime = float(int(time.time()) - int(oldtime))
- if divtime == 0:
- rate = (float(TX[key]) + float(RX[key]))
- else:
- rate = (float(TX[key]) + float(RX[key]))/(divtime)
- if rate == 0:
- newnetcard[key] = '0' + ' ' + newnetcard[key]
- else:
- newnetcard[key] = '%.2f'%rate + ' ' + newnetcard[key]
- return zip(['order'], ['48']) + newnetcard.items();
- else:
- os.system('ifconfig > ifconfigtemp')
- file = open('./ifconfigtemp','r');
- fileold = open('./oldifconfig', 'w')
- temptimestr = str(int(time.time()));
- fileold.write(temptimestr)
- fileold.write('#')
- file.seek(0)
- fileold.write(file.read())
- fileold.close()
-
- netcard = {}
- file.seek(0)
- key = ''
- for strline in file.readlines():
- reobj = re.compile('^lo*.')
- if reobj.search(strline):
- break;
- reobj = re.compile('^eth*.')
- if reobj.search(strline):
- key = strline.split()[0]
- netcard[key] = ''
- netcard[key] = netcard[key] + strline
- RX = {}
- TX = {}
-
- key = ''
- newnetcard = {}
- file.seek(0)
- for strline in file.readlines():
- reobj = re.compile('^lo*.')
- if reobj.search(strline):
- break;
- if re.search("^eth", strline):
- templist = strline.split()
- key = templist[0]
- newnetcard[key] = templist[4] + ' '
- if re.search("^ *inet ", strline):
- templist = strline.split()
- newnetcard[key] = newnetcard[key] + templist[1][5:] + ' '
- for key,value in newnetcard.items():
- os.system('ethtool %s > ethtooltemp'%(key))
- file = open('./ethtooltemp','r');
- tempethtooltemplist = file.read().split('\n')
- file.close
- if re.search("yes", tempethtooltemplist[-1]):
- newnetcard[key] = newnetcard[key] + 'runing!'
- else:
- newnetcard[key] = newnetcard[key] + 'stop!'
- file.close()
- for key,value in netcard.items():
- tempsplit = value.split('\n')
- netcard[key] = ''
- for item in tempsplit:
- item = item + '<br>'
-
- netcard[key] = netcard[key] + item
- tempcount = 1
- for match in re.finditer("(bytes:)(.*?)( \()", item):
- if tempcount == 1:
- RX[key] = match.group(2)
- tempcount = tempcount + 1
- elif tempcount == 2:
- TX[key] = match.group(2)
- netcard[key] = netcard[key] + 'net io percent(bytes/s): 0 <br>'
- newnetcard[key] = newnetcard[key] + ' ' + '0 <br>'
- return zip(['order'], ['48']) + newnetcard.items();
- if __name__ == '__main__':
- print run()
使用例子:
每一个列表元素元组里面第二个元素第一个字段为网速 Bytes/S,例如eth1网卡的网速就是3.3KB/s,eth0网速是2.9KB/s,今天是周六这个流量很正常