用python

具体:http://www.verydemo.com/demo_c122_i34285.html

1
sudo apt-get install python-rrdtool

二、创建rrd文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
import rrdtool
// 创建net.rrd,数据获取间隔是300
rrdtool.create(“/opt/rrdtool/net.rrd”, “–step”, “300″, “–start”, ’0′,
// 变量input,类型COUNTER,超时时间600秒,U最小值无,U最大值无。
“DS:input:COUNTER:600:U:U”,
“DS:output:COUNTER:600:U:U”,
// 0.5: 一个CDP里所有PDP值超过一半为UNKNOWN,则该CDP的值也为UNKNOWN。
“RRA:AVERAGE:0.5:1:600″,
// 6表示对6个PDP合成一个CDP,取6个PDP的平均值(6*300=半个小时的平均值)
“RRA:AVERAGE:0.5:6:700″,
// 755便是RRA记录的条数
“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″)

三、更新rrd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
import rrdtool
input = 0
output = 0
// 读取proc里网卡数据
F = open(‘/proc/net/dev’)
for line in F.readlines():
//过去eth0的数据
if ‘eth0′ in line:
input =  line.split()[1]
output = line.split()[9]
//eth0的进出流量更新到net.rrd里。
rrdtool.update(‘/opt/rrdtool/net.rrd’,'N:’ + input + ‘:’ + output)

四、画图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
import rrdtool
// 生成net.png图片,-8h起始时间8小时以前。
rrdtool.graph( “/tmp/net.png”, “–start”, “-8h”,
// Y轴说明
“–vertical-label=Bytes/s”,
// 将net.rrd里input的平均数据赋值给inoctets
“DEF:inoctets=/opt/rrdtool/net.rrd:input:AVERAGE”,
“DEF:outoctets=/opt/rrdtool/net.rrd:output:AVERAGE”,
// 用面积表示inoctets数据,颜色为绿色
“AREA:inoctets#00FF00:In traffic”,
// 1像素线表示outoctets数据,颜色为蓝色。
“LINE1:outoctets#0000FF:Out traffic\\r”,
// inbits=inoctets*8 将小b换成大B
“CDEF:inbits=inoctets,8,*”,
“CDEF:outbits=outoctets,8,*”,
// 换行
“COMMENT:\\n”,
“GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps”,
“COMMENT:  ”,
“GPRINT:inbits:MAX:MAX In traffic\: %6.2lf %Sbps\\r”,
“GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps”,
“COMMENT: “,
“GPRINT:outbits:MAX:MAX Out traffic\: %6.2lf %Sbps\\r”)

相关推荐