码迷,mamicode.com
首页 > 编程语言 > 详细

3、python自动化运维——定制业务质量报表

时间:2017-12-02 19:17:10      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:python自动化运维 psutil

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

技术分享图片

 


                                           

 

实例代码:

./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")

 

 


3、python自动化运维——定制业务质量报表

标签:python自动化运维 psutil

原文地址:http://blog.51cto.com/mbb97/2046782

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!