Using Python to achieve network card flow chart drawing!!!

Source: Internet
Author: User
Tags rrd rrdtool

Project background:

The use of Python to achieve an automated network card flow chart drawing, which for our implementation of the automated operation and maintenance platform has a deeper understanding,

will also allow us to some of the existing monitoring software implementation of some of the great help.


Implementing the Environment:

Virtual Machines VMware Workstation player

Server: centos6.5 system ip:192.168.0.25

python2.6.6

RRDtool module, Time module, Psutil module.

SECURECRT SSH remote connection software



Experimental process:

The idea is very clear: Create an RRD database----> data written to the RRD database-----> based on data drawing ok!!! Done, son.

First, create our working directory, then create three files (create.py, update.py, graph.py).

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/05/wKioL1bejZyCHAxyAADA4WLYnYU976.png "title=" Create the three files. png "alt=" Wkiol1bejzychaxyaada4wlynyu976.png "/>

Second, create the RRD database.

[email protected] www rrdtool]# cat create.py

#!/usr/bin/env python

Import RRDtool

Import time


CUR_TIME=STR (int (time.time ()))

Rrd=rrdtool.create (' flow.rrd ', '--step ', ' a ', '--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 ()


Give execution permission to execute.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/05/wKioL1bejxCSeBrrAAC3ROyXeO4423.png "title=" Create an RRD database. png "alt=" wkiol1bejxcsebrraac3royxeo4423.png "/> can see that we have created a database file named FLOW.RRD.

Third, data written to the RRD database

[email protected] www rrdtool]# cat update.py

#!/usr/bin/env python

Import RRDtool

Import Time,psutil


Total_input_traffic = Psutil.net_io_counters () [1]


Total_output_traffic = psutil.net_io_counters () [0]

Starttime=int (Time.time ())

Update=rrdtool.updatev ('/home/test/rrdtool/flow.rrd ', '%s:%s:%s '%

(Str (starttime), str (total_input_traffic), str (total_output_traffic)))

Print update


Execute, Data write ~ ~ ~

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/05/wKioL1bekLCjPsObAAAiuVRuPi4291.png "title=" Data is written to. png "alt=" Wkiol1beklcjpsobaaaiuvrupi4291.png "/> Four, last step, drawing!!!!!

[email protected] www rrdtool]# cat graph.py

#!/usr/bin/python

Import RRDtool

Import time


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", "$", "--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")

Execute it, Draw!!!

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7D/06/wKiom1bekS6yNT6vAADh6iDAfDQ974.png "title=" drawing. png "alt=" Wkiom1beks6ynt6vaadh6idafdq974.png "/> can see we have generated a picture, you must want to see a beautiful picture, not this!!!!

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/07/wKiom1bekYWTnB_7AACeE9vkqBU254.png "title=" Flow.png "alt=" Wkiom1bekywtnb_7aacee9vkqbu254.png "/> can see that there is no magic!!!!!!!!!!!! We wrote a monitoring software, and it looked beautiful!!!!!


Summary: Through this project we can have a clearer understanding of the monitoring, in fact, in general, the three steps:

Create an RRD database----> Data write to the RRD database-----> Draw ok!!! based on data Done, son.

Hope you can get learning, thank you, knowledge is shared!!!!

This article is from the "Make a few" blog, be sure to keep this source http://9399369.blog.51cto.com/9389369/1748831

Using Python to achieve network card flow chart drawing!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.