通过psutilrrdtool模块相结合绘制网卡网络流量

3、python自动化运维——定制业务质量报表_第1张图片

 


                                           

 

实例代码:

./cretea.py

#!/usr/bin/python
 
import time
 
import rrdtool
 
  cur_time=str(int(time.time()))
  rrd=rrdtool.create(
'Flow.rrd','--step','300','--start',cur_time,
       
'DS:eth0_in:COUNTER:600:0:U',
       
'DS:eth0_out:COUNTER:600:0:U',
       
'RRA:AVERAGE:0.5:1:600',
       
'RRA:AVERAGE:0.5:6:700',
       
'RRA:AVERAGE:0.5:24:775',
       
'RRA:AVERAGE:0.5:288:797',
       
'RRA:MAX:0.5:1:600',
       
'RRA:MAX:0.5:6:700',
                 
'RRA:MAX:0.5:24:775',
       
'RRA:MAX:0.5:444:797',
       
'RRA:MIN:0.5:1:600',
                 
'RRA:MIN:0.5:6:700',
                 
'RRA:MIN:0.5:24:775',
                 
'RRA:MIN:0.5:444:797')
 
if rrd:
    
print(rrdtool.error())

 

                                                                

./update.py

import time,psutil
 
import rrdtool
total_input_traffic=psutil.net_io_counters()[
1]
total_output_traffic=psutil.net_io_counters()[
0]
starttime=int(time.time())
update=rrdtool.updatev(
'/root/Scripts/rrdtools/Flow.rrd','%s:%s:%s' % (str(starttime),str(total_input_traffic),str(total_output_traffic)))
 
print(update)
 

 

 

./graph.py

import time,psutil
import rrdtool
 

 

title="Server network,traffic flow ("+time.strftime('%Y-%m-%d',time.localtime(time.time()))+")"
 
rrdtool.graph("Flow.png","--start","-1d","--vertical-label=Bytes/s",
 
"--x-grid","MINUTE:12:HOUR:1:HOUR:1:0:%H","--width","650","--height","230",
 
"--title",title,
 
"DEF:inoctets=Flow.rrd:eth0_in:AVERAGE",
 
"DEF:outoctets=Flow.rrd:eth0_out:AVERAGE","CDEF:total=inoctets,outoctets,+",
 
"LINE1:total#FF8833:Total traffic","AREA:inoctets#00FF00:IN traffic",
 
"LINE1:outoctets#0000FF:Out traffic","HRULE:6144#FF0000:Alarm value\\r",
 
"CDEF:inbits=inoctets,8,*","CDEF:outbits=outoctets,8,*",
 
"COMMENT:\\r",
 
"COMMENT:\\r",
 
"GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps",
 
"COMMENT: ","GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps",
 
"COMMENT: ","GPRINT:inbits:MIN:Min In traffic\: %6.2lf %Sbps\\r","COMMENT: ","GPRINT:outbits:AVERAGE:Avg OuT traffic\: %6.2lf %Sbps","COMMENT: ","GPRINT:outbits:MAX:Max OuT traffic\: %6.2lf %Sbps",
 
"COMMENT: ","GPRINT:outbits:MIN:Min OuT traffic\: %6.2lf %Sbps\\r")
 

 

 

实现TCP探测目标路由轨迹

所需安装包:ImageMagick(转换图片格式工具),graphviz(绘制图形工具)

通过scapytraceroute方法查询到路由轨迹,然后调用graph()方法进行路由轨迹的绘制,中间调用ASN映射查询IP地理信息并生成svg流程文档,最后使用ImageMagic工具转换svg格式为png格式。

 

import logging,warnings
 
import sys,os,subprocess
 
import time
 
  warnings.filterwarnings(
"ignore",category=DeprecationWarning)  

#屏蔽scapy 无用信息
  logging.getLogger(
"scapy.runtime").setLevel(logging.ERROR)     
  #
屏蔽模块IPV6多余告警
 
from scapy.all   import traceroute
  domains=raw_input(
"please input you   domain name:\n")

#接受输入的域名或IP
  target=domains.split(
" ")
  dport = [
80]
 
 
if len(target) >=1 and target[0]!='':
     res,unans =traceroute(target,dport=dport,retry=-
2)#启动路由跟踪
     res.graph(target=
"> test.svg")    #生成svg图片
     time.sleep(
1)
     subprocess.Popen(
"/usr/bin/convert test.svg test.png",shell=True)
 
else:
    
print("IP/domain name Error")