Use python to create a network card Traffic chart !!!

Source: Internet
Author: User
Tags rrd rrdtool

Use python to create a network card Traffic chart !!!

Project Background:

Using python to implement an automated network card Traffic chart, which gives us a deeper understanding of how to implement an automated O & M platform,

It will also help us to implement some of the existing monitoring software.

Implementation environment:

Virtual Machine VMwareWorkstation12 player

Server: centos6.5 system ip Address: 192.168.0.25

Python2.6.6

Rrdtool module, time module, and psutil module.

SecureCRT ssh remote connection Software

Experiment process:

The idea is actually very clear: Create an rrd database ----> write data to the rrd database -----> draw a picture based on the data. OK !!! Complete

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

2. Create an rrd database.

[Root @ 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', '123', '-- 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 ()

 

Grant the execution permission.

We can see that we have created a database file named Flow. rrd.

Iii. Writing data to rrd Database

[Root @ 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' %

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

Print update

 

Run the following command to write data ~~~

4. Drawing the last step !!!!!

[Root @ 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", "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 ",

"Uplint: inbits: AVERAGE: Avg In traffic \: % 6.2lf % Sbps ",

"COMMENT :",

"Uplint: inbits: MAX: Max In traffic \: % 6.2lf % Sbps ",

"COMMENT :",

"Uplint: inbits: MIN In traffic \: % 6.2lf % Sbps \ r ",

"COMMENT :",

"Uplint: outbits: AVERAGE: Avg Out traffic \: % 6.2lf % Sbps ",

"COMMENT :",

"Uplint: outbits: MAX: Max Out traffic \: % 6.2lf % Sbps ",

"COMMENT :",

"Uplint: outbits: MIN Out traffic \: % 6.2lf % Sbps \ r ")

Execute it, drawing !!!

We can see that we have generated an image. You must want to see a beautiful image, not this one !!!!

You can see that it's amazing !!!!!!!!!!!! It is equivalent to writing a monitoring software, and it looks beautiful !!!!!

Conclusion: Through this project, we can have a clearer understanding of monitoring. In fact, this is generally the three steps:

Create an rrd database ----> write data to the rrd database -----> draw a picture based on the data. OK !!! Complete

Hope you can learn it. Thank you. Knowledge is shared !!!!

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.