ljwsy 发表于 2013-01-04 11:28

py-rrdtool备忘

本帖最后由 ljwsy 于 2013-01-04 12:14 编辑

近来折腾rrdtool,为了给以后提供参考或备忘,特发本帖。

有点人反问“为什么不用cacti”,我也同意这观点。但生命在于折腾,况且cacti没有自己做的灵活性大,且不用去“适应”cacti。另一方面,只要是能找到的数据都可以用rrd保存下来并绘成图,想怎么存数据就怎么存,想怎样画图就怎样画。

环境


stu# uname -a
FreeBSD stu.lcn 9.0-STABLE FreeBSD 9.0-STABLE #0: Fri May 18 22:02:36 CST 2012   w@stu.cn:/usr/obj/usr/src/sys/STUi386
stu# pkg_info | grep py
...
python26-2.6.8      An interpreted object-oriented programming language
py26-rrdtool_lgpl-1.0b1_2 Python interface to RRDTool, the graphing and logging utili
py26-snmp4-4.2.1    SNMP framework for Python
...


rrd文件建立

共收集N个数据:
rrdtool create netinfc.rrd --start 0 --step 300 \
DS:nocw_i:COUNTER:600:0:U \
DS:nocw_o:COUNTER:600:0:U \
DS:nocl_i:COUNTER:600:0:U \
DS:nocl_o:COUNTER:600:0:U \
DS:ros10_i:COUNTER:600:0:U \
DS:ros10_o:COUNTER:600:0:U \
DS:ros0_i:COUNTER:600:0:U \
DS:ros0_o:COUNTER:600:0:U \
DS:ros6_i:COUNTER:600:0:U \
DS:ros6_o:COUNTER:600:0:U \
DS:ros7_i:COUNTER:600:0:U \
DS:ros7_o:COUNTER:600:0:U \
DS:ros8_i:COUNTER:600:0:U \
DS:ros8_o:COUNTER:600:0:U \
DS:ros9_i:COUNTER:600:0:U \
DS:ros9_o:COUNTER:600:0:U \
DS:ns_i:COUNTER:600:0:U \
DS:ns_o:COUNTER:600:0:U \
DS:stu9_i:COUNTER:600:0:U \
DS:stu9_o:COUNTER:600:0:U \
DS:stu1_i:COUNTER:600:0:U \
DS:stu1_o:COUNTER:600:0:U \
DS:stu2_i:COUNTER:600:0:U \
DS:stu2_o:COUNTER:600:0:U \
DS:stu3_i:COUNTER:600:0:U \
DS:stu3_o:COUNTER:600:0:U \
DS:stu4_i:COUNTER:600:0:U \
DS:stu4_o:COUNTER:600:0:U \
DS:www_i:COUNTER:600:0:U \
DS:www_o:COUNTER:600:0:U \
DS:fb10_i:COUNTER:600:0:U \
DS:fb10_o:COUNTER:600:0:U \
DS:fb0_i:COUNTER:600:0:U \
DS:fb0_o:COUNTER:600:0:U \
DS:fb5_i:COUNTER:600:0:U \
DS:fb5_o:COUNTER:600:0:U \
DS:sma0_i:COUNTER:600:0:U \
DS:sma0_o:COUNTER:600:0:U \
DS:sma5_i:COUNTER:600:0:U \
DS:sma5_o:COUNTER:600:0:U \
DS:sm0_i:COUNTER:600:0:U \
DS:sm0_o:COUNTER:600:0:U \
DS:sm5_i:COUNTER:600:0:U \
DS:sm5_o:COUNTER:600:0:U \
DS:s10_i:COUNTER:600:0:U \
DS:s10_o:COUNTER:600:0:U \
DS:s11_i:COUNTER:600:0:U \
DS:s11_o:COUNTER:600:0:U \
DS:s15_i:COUNTER:600:0:U \
DS:s15_o:COUNTER:600:0:U \
DS:s20_i:COUNTER:600:0:U \
DS:s20_o:COUNTER:600:0:U \
DS:s22_i:COUNTER:600:0:U \
DS:s22_o:COUNTER:600:0:U \
DS:s30_i:COUNTER:600:0:U \
DS:s30_o:COUNTER:600:0:U \
DS:s33_i:COUNTER:600:0:U \
DS:s33_o:COUNTER:600:0:U \
DS:t0_i:COUNTER:600:0:U \
DS:t0_o:COUNTER:600:0:U \
DS:t5_i:COUNTER:600:0:U \
DS:t5_o:COUNTER:600:0:U \
DS:t8_i:COUNTER:600:0:U \
DS:t8_o:COUNTER:600:0:U \
DS:tmpa_i:COUNTER:600:0:U \
DS:tmpa_o:COUNTER:600:0:U \
DS:tmpb_i:COUNTER:600:0:U \
DS:tmpb_o:COUNTER:600:0:U \
RRA:LAST:0.5:1:600 \
RRA:LAST:0.5:4:600 \
RRA:LAST:0.5:24:600 \
RRA:LAST:0.5:288:730 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:4:600 \
RRA:MAX:0.5:24:600 \
RRA:MAX:0.5:288:730 \
RRA:MIN:0.5:1:600 \
RRA:MIN:0.5:4:600 \
RRA:MIN:0.5:24:600 \
RRA:MIN:0.5:288:730 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:4:600 \
RRA:AVERAGE:0.5:24:600 \
RRA:AVERAGE:0.5:288:730



数据采集


#!/usr/local/bin/python
import datetime
import rrdtool
import netsnmp

debug=0
a='''
0       not debug
2       print to stdout
'''
NOW=datetime.datetime.now()



[*]生成数据保存变量last
rrdfile='/usr/local/www/rrdtool/rrd/netinfc.rrd'
tmp=str(rrdtool.last(rrdfile)-600)
a,t,t1=rrdtool.fetch(rrdfile,'LAST','--start',tmp,'--end',tmp)
last={}.fromkeys(t)
for a in range(len(t)) :
      last]= 0 if t1==None else int(t1)
for a in last.keys() :
      if debug==2 : print "%s\t->\t%s" % (a,last)


[*]定义数据影射表

map={

'sm.cn':
      (
                {'stu9':'em0'},
                {'stu1':'em1'},
                {'stu2':'rl0'},
                {'stu3':'rl1'},
      ),

'192.168.1.9':
      (
                {'nocw':'eth0'},
                {'nocl':'eth1'},
      ),

'192.168.0.2':
      (
                {'ns':'fxp0'},
      ),
}

'192.168.1.9':
      (
                {'nocw':'eth0'},
                {'nocl':'eth1'},
      ),
意思是:192.168.1.9这机共采集eth0、eth1网卡的数据,分别放到rrd库中的nocw_i、nocw_o、nocl_i、nocl_o中。

[*]采集数据并放到last中
for a in map :
      #name
      t=".1.3.6.1.2.1.2.2.1.2"
      oid=netsnmp.VarList(netsnmp.Varbind(t))
      res=netsnmp.snmpwalk(oid,Version=1,DestHost=a,Community='public')
      if len(res) :
                #In
                t=".1.3.6.1.2.1.2.2.1.10"
                oid=netsnmp.VarList(netsnmp.Varbind(t))
                In=netsnmp.snmpwalk(oid,Version=1,DestHost=a,Community='public')
                #out
                t=".1.3.6.1.2.1.2.2.1.16"
                oid=netsnmp.VarList(netsnmp.Varbind(t))
                Out=netsnmp.snmpwalk(oid,Version=1,DestHost=a,Community='public')
                tmp={}.fromkeys(res)
                for b in range(len(res)) :
                        tmp]=,Out]
                for b in map :
                        for c in b :
                              t='%s_i' % c
                              last=tmp]
                              t='%s_o' % c
                              last=tmp]

[*]根据last中的数据更新rrd库

b='N'
for a in (
'nocw_i','nocw_o','nocl_i','nocl_o',
'ros10_i','ros10_o','ros0_i','ros0_o','ros6_i','ros6_o','ros7_i','ros7_o','ros8_i','ros8_o','ros9_i','ros9_o',
'ns_i','ns_o',
'stu9_i','stu9_o','stu1_i','stu1_o','stu2_i','stu2_o','stu3_i','stu3_o','stu4_i','stu4_o',
'www_i','www_o',
'fb10_i','fb10_o','fb0_i','fb0_o','fb5_i','fb5_o',
'sma0_i','sma0_o','sma5_i','sma5_o',
'sm0_i','sm0_o','sm5_i','sm5_o',
'ss10_i','ss10_o','s10_i','s10_o','s11_i','s11_o','s12_i','s12_o','s15_i','s15_o',
's20_i','s20_o','s21_i','s21_o','s22_i','s22_o',
's30_i','s30_o','s31_i','s31_o','s32_i','s32_o',
't0_i','t0_o','t5_i','t5_o','t8_i','t8_o',
'tmpa_i','tmpa_o','tmpb_i','tmpb_o'
) :
      b=b+':%s' % (0 if last==None else int(last))
ret = rrdtool.update('/usr/local/www/rrdtool/rrd/netinfc.rrd',b)
if ret:
      print rrdtool.error()




绘图

tmp="%s" % NOW
ret=tmp.split('.')
tmp="-t 1 Day Net Download Traf(%s)" % ret
ret = rrdtool.graph( "/usr/local/www/apache22/data/rrd/neti_dayly.png",
"--start", "-1d",
"-h 145",
"-w 550",
tmp,
"--vertical-label=All Net Traf",
"DEF:nocwil=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocw_i:LAST",
"DEF:nocwim=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocw_i:MAX",
"DEF:nocwii=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocw_i:MIN",
"DEF:noclol=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocl_o:LAST",
"DEF:noclom=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocl_o:MAX",
"DEF:nocloi=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocl_o:MIN",
"DEF:ros10il=/usr/local/www/rrdtool/rrd/netinfc.rrd:ros10_i:LAST",
"DEF:ros10im=/usr/local/www/rrdtool/rrd/netinfc.rrd:ros10_i:MAX",
"DEF:ros10ii=/usr/local/www/rrdtool/rrd/netinfc.rrd:ros10_i:MIN",
"DEF:wwwil=/usr/local/www/rrdtool/rrd/netinfc.rrd:www_i:LAST",
"DEF:wwwim=/usr/local/www/rrdtool/rrd/netinfc.rrd:www_i:MAX",
"DEF:wwwii=/usr/local/www/rrdtool/rrd/netinfc.rrd:www_i:MIN",
"DEF:fbil=/usr/local/www/rrdtool/rrd/netinfc.rrd:fb10_i:LAST",
"DEF:fbim=/usr/local/www/rrdtool/rrd/netinfc.rrd:fb10_i:MAX",
"DEF:fbii=/usr/local/www/rrdtool/rrd/netinfc.rrd:fb10_i:MIN",
"DEF:nsol=/usr/local/www/rrdtool/rrd/netinfc.rrd:ns_o:LAST",
"DEF:nsom=/usr/local/www/rrdtool/rrd/netinfc.rrd:ns_o:MAX",
"DEF:nsoi=/usr/local/www/rrdtool/rrd/netinfc.rrd:ns_o:MIN",
"CDEF:cdefnocwil=nocwil,8,*",
"CDEF:cdefnocwim=nocwim,8,*",
"CDEF:cdefnocwii=nocwii,8,*",
"CDEF:cdefnoclol=noclol,8,*",
"CDEF:cdefnoclom=noclom,8,*",
"CDEF:cdefnocloi=nocloi,8,*",
"CDEF:cdefros10il=ros10il,8,*",
"CDEF:cdefros10im=ros10im,8,*",
"CDEF:cdefros10ii=ros10ii,8,*",
"CDEF:cdefwwwil=wwwil,8,*",
"CDEF:cdefwwwim=wwwim,8,*",
"CDEF:cdefwwwii=wwwii,8,*",
"CDEF:cdeffbil=fbil,8,*",
"CDEF:cdeffbim=fbim,8,*",
"CDEF:cdeffbii=fbii,8,*",
"CDEF:cdefnsol=nsol,8,*",
"CDEF:cdefnsom=nsom,8,*",
"CDEF:cdefnsoi=nsoi,8,*",
"AREA:cdefros10il#AFECED:ros10_in",
"STACK:cdefwwwil#0FF000:www_in",
"STACK:cdeffbil#000000:fb_in   Lan_out ",
"LINE1:cdefnocwil#FF0000:Wan_in ",
"AREA:cdefnsol#750F7D:ns_out               \\r",
"COMMENT:         Ros10         WWW         FB      Noc_L       Noc_W         ns\\n",
"COMMENT:LAST\:",
"GPRINT:cdefros10il:LAST:%9.0lf",
"GPRINT:cdefwwwil:LAST:%9.0lf",
"GPRINT:cdeffbil:LAST:%9.0lf",
"GPRINT:cdefnoclol:LAST:%9.0lf",
"GPRINT:cdefnocwil:LAST:%9.0lf",
"GPRINT:cdefnsol:LAST:%9.0lf\\n",
"COMMENT: MAX\:",
"GPRINT:cdefros10im:MAX:%9.0lf",
"GPRINT:cdefwwwim:MAX:%9.0lf",
"GPRINT:cdeffbim:MAX:%9.0lf",
"GPRINT:cdefnoclom:MAX:%9.0lf",
"GPRINT:cdefnocwim:MAX:%9.0lf",
"GPRINT:cdefnsom:MAX:%9.0lf\\n",
"COMMENT: MIN\:",
"GPRINT:cdefros10ii:MIN:%9.0lf",
"GPRINT:cdefwwwii:MIN:%9.0lf",
"GPRINT:cdeffbii:MIN:%9.0lf",
"GPRINT:cdefnocloi:MIN:%9.0lf",
"GPRINT:cdefnocwii:MIN:%9.0lf",
"GPRINT:cdefnsoi:MIN:%9.0lf\\n")





其他图片












zhxjfl 发表于 2015-06-04 16:31

楼主太强!!!!!
页: [1]
查看完整版本: py-rrdtool备忘