免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10487 | 回复: 1

[FreeBSD] py-rrdtool备忘 [复制链接]

论坛徽章:
0
发表于 2013-01-04 11:28 |显示全部楼层
本帖最后由 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/STU  i386
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个数据:
  1. rrdtool create netinfc.rrd --start 0 --step 300 \
  2. DS:nocw_i:COUNTER:600:0:U \
  3. DS:nocw_o:COUNTER:600:0:U \
  4. DS:nocl_i:COUNTER:600:0:U \
  5. DS:nocl_o:COUNTER:600:0:U \
  6. DS:ros10_i:COUNTER:600:0:U \
  7. DS:ros10_o:COUNTER:600:0:U \
  8. DS:ros0_i:COUNTER:600:0:U \
  9. DS:ros0_o:COUNTER:600:0:U \
  10. DS:ros6_i:COUNTER:600:0:U \
  11. DS:ros6_o:COUNTER:600:0:U \
  12. DS:ros7_i:COUNTER:600:0:U \
  13. DS:ros7_o:COUNTER:600:0:U \
  14. DS:ros8_i:COUNTER:600:0:U \
  15. DS:ros8_o:COUNTER:600:0:U \
  16. DS:ros9_i:COUNTER:600:0:U \
  17. DS:ros9_o:COUNTER:600:0:U \
  18. DS:ns_i:COUNTER:600:0:U \
  19. DS:ns_o:COUNTER:600:0:U \
  20. DS:stu9_i:COUNTER:600:0:U \
  21. DS:stu9_o:COUNTER:600:0:U \
  22. DS:stu1_i:COUNTER:600:0:U \
  23. DS:stu1_o:COUNTER:600:0:U \
  24. DS:stu2_i:COUNTER:600:0:U \
  25. DS:stu2_o:COUNTER:600:0:U \
  26. DS:stu3_i:COUNTER:600:0:U \
  27. DS:stu3_o:COUNTER:600:0:U \
  28. DS:stu4_i:COUNTER:600:0:U \
  29. DS:stu4_o:COUNTER:600:0:U \
  30. DS:www_i:COUNTER:600:0:U \
  31. DS:www_o:COUNTER:600:0:U \
  32. DS:fb10_i:COUNTER:600:0:U \
  33. DS:fb10_o:COUNTER:600:0:U \
  34. DS:fb0_i:COUNTER:600:0:U \
  35. DS:fb0_o:COUNTER:600:0:U \
  36. DS:fb5_i:COUNTER:600:0:U \
  37. DS:fb5_o:COUNTER:600:0:U \
  38. DS:sma0_i:COUNTER:600:0:U \
  39. DS:sma0_o:COUNTER:600:0:U \
  40. DS:sma5_i:COUNTER:600:0:U \
  41. DS:sma5_o:COUNTER:600:0:U \
  42. DS:sm0_i:COUNTER:600:0:U \
  43. DS:sm0_o:COUNTER:600:0:U \
  44. DS:sm5_i:COUNTER:600:0:U \
  45. DS:sm5_o:COUNTER:600:0:U \
  46. DS:s10_i:COUNTER:600:0:U \
  47. DS:s10_o:COUNTER:600:0:U \
  48. DS:s11_i:COUNTER:600:0:U \
  49. DS:s11_o:COUNTER:600:0:U \
  50. DS:s15_i:COUNTER:600:0:U \
  51. DS:s15_o:COUNTER:600:0:U \
  52. DS:s20_i:COUNTER:600:0:U \
  53. DS:s20_o:COUNTER:600:0:U \
  54. DS:s22_i:COUNTER:600:0:U \
  55. DS:s22_o:COUNTER:600:0:U \
  56. DS:s30_i:COUNTER:600:0:U \
  57. DS:s30_o:COUNTER:600:0:U \
  58. DS:s33_i:COUNTER:600:0:U \
  59. DS:s33_o:COUNTER:600:0:U \
  60. DS:t0_i:COUNTER:600:0:U \
  61. DS:t0_o:COUNTER:600:0:U \
  62. DS:t5_i:COUNTER:600:0:U \
  63. DS:t5_o:COUNTER:600:0:U \
  64. DS:t8_i:COUNTER:600:0:U \
  65. DS:t8_o:COUNTER:600:0:U \
  66. DS:tmpa_i:COUNTER:600:0:U \
  67. DS:tmpa_o:COUNTER:600:0:U \
  68. DS:tmpb_i:COUNTER:600:0:U \
  69. DS:tmpb_o:COUNTER:600:0:U \
  70. RRA:LAST:0.5:1:600 \
  71. RRA:LAST:0.5:4:600 \
  72. RRA:LAST:0.5:24:600 \
  73. RRA:LAST:0.5:288:730 \
  74. RRA:MAX:0.5:1:600 \
  75. RRA:MAX:0.5:4:600 \
  76. RRA:MAX:0.5:24:600 \
  77. RRA:MAX:0.5:288:730 \
  78. RRA:MIN:0.5:1:600 \
  79. RRA:MIN:0.5:4:600 \
  80. RRA:MIN:0.5:24:600 \
  81. RRA:MIN:0.5:288:730 \
  82. RRA:AVERAGE:0.5:1:600 \
  83. RRA:AVERAGE:0.5:4:600 \
  84. RRA:AVERAGE:0.5:24:600 \
  85. RRA:AVERAGE:0.5:288:730
复制代码



数据采集


  1. #!/usr/local/bin/python
  2. import datetime
  3. import rrdtool
  4. import netsnmp

  5. debug=0
  6. a='''
  7. 0       not debug
  8. 2       print to stdout
  9. '''
  10. NOW=datetime.datetime.now()

复制代码

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

  • 定义数据影射表

    1. map={

    2. 'sm.cn':
    3.         (
    4.                 {'stu9':'em0'},
    5.                 {'stu1':'em1'},
    6.                 {'stu2':'rl0'},
    7.                 {'stu3':'rl1'},
    8.         ),

    9. '192.168.1.9':
    10.         (
    11.                 {'nocw':'eth0'},
    12.                 {'nocl':'eth1'},
    13.         ),

    14. '192.168.0.2':
    15.         (
    16.                 {'ns':'fxp0'},
    17.         ),
    18. }
    复制代码
    '192.168.1.9':
            (
                    {'nocw':'eth0'},
                    {'nocl':'eth1'},
            ),

    意思是:192.168.1.9这机共采集eth0、eth1网卡的数据,分别放到rrd库中的nocw_i、nocw_o、nocl_i、nocl_o中。
  • 采集数据并放到last中
    1. for a in map :
    2.         #name
    3.         t=".1.3.6.1.2.1.2.2.1.2"
    4.         oid=netsnmp.VarList(netsnmp.Varbind(t))
    5.         res=netsnmp.snmpwalk(oid,Version=1,DestHost=a,Community='public')
    6.         if len(res) :
    7.                 #In
    8.                 t=".1.3.6.1.2.1.2.2.1.10"
    9.                 oid=netsnmp.VarList(netsnmp.Varbind(t))
    10.                 In=netsnmp.snmpwalk(oid,Version=1,DestHost=a,Community='public')
    11.                 #out
    12.                 t=".1.3.6.1.2.1.2.2.1.16"
    13.                 oid=netsnmp.VarList(netsnmp.Varbind(t))
    14.                 Out=netsnmp.snmpwalk(oid,Version=1,DestHost=a,Community='public')
    15.                 tmp={}.fromkeys(res)
    16.                 for b in range(len(res)) :
    17.                         tmp[res[b]]=[In[b],Out[b]]
    18.                 for b in map[a] :
    19.                         for c in b :
    20.                                 t='%s_i' % c
    21.                                 last[t]=tmp[b[c]][0]
    22.                                 t='%s_o' % c
    23.                                 last[t]=tmp[b[c]][1]
    复制代码

  • 根据last中的数据更新rrd库

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




绘图

  1. tmp="%s" % NOW
  2. ret=tmp.split('.')[0]
  3. tmp="-t 1 Day Net Download Traf(%s)" % ret
  4. ret = rrdtool.graph( "/usr/local/www/apache22/data/rrd/neti_dayly.png",
  5. "--start", "-1d",
  6. "-h 145",
  7. "-w 550",
  8. tmp,
  9. "--vertical-label=All Net Traf",
  10. "DEF:nocwil=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocw_i:LAST",
  11. "DEF:nocwim=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocw_i:MAX",
  12. "DEF:nocwii=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocw_i:MIN",
  13. "DEF:noclol=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocl_o:LAST",
  14. "DEF:noclom=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocl_o:MAX",
  15. "DEF:nocloi=/usr/local/www/rrdtool/rrd/netinfc.rrd:nocl_o:MIN",
  16. "DEF:ros10il=/usr/local/www/rrdtool/rrd/netinfc.rrd:ros10_i:LAST",
  17. "DEF:ros10im=/usr/local/www/rrdtool/rrd/netinfc.rrd:ros10_i:MAX",
  18. "DEF:ros10ii=/usr/local/www/rrdtool/rrd/netinfc.rrd:ros10_i:MIN",
  19. "DEF:wwwil=/usr/local/www/rrdtool/rrd/netinfc.rrd:www_i:LAST",
  20. "DEF:wwwim=/usr/local/www/rrdtool/rrd/netinfc.rrd:www_i:MAX",
  21. "DEF:wwwii=/usr/local/www/rrdtool/rrd/netinfc.rrd:www_i:MIN",
  22. "DEF:fbil=/usr/local/www/rrdtool/rrd/netinfc.rrd:fb10_i:LAST",
  23. "DEF:fbim=/usr/local/www/rrdtool/rrd/netinfc.rrd:fb10_i:MAX",
  24. "DEF:fbii=/usr/local/www/rrdtool/rrd/netinfc.rrd:fb10_i:MIN",
  25. "DEF:nsol=/usr/local/www/rrdtool/rrd/netinfc.rrd:ns_o:LAST",
  26. "DEF:nsom=/usr/local/www/rrdtool/rrd/netinfc.rrd:ns_o:MAX",
  27. "DEF:nsoi=/usr/local/www/rrdtool/rrd/netinfc.rrd:ns_o:MIN",
  28. "CDEF:cdefnocwil=nocwil,8,*",
  29. "CDEF:cdefnocwim=nocwim,8,*",
  30. "CDEF:cdefnocwii=nocwii,8,*",
  31. "CDEF:cdefnoclol=noclol,8,*",
  32. "CDEF:cdefnoclom=noclom,8,*",
  33. "CDEF:cdefnocloi=nocloi,8,*",
  34. "CDEF:cdefros10il=ros10il,8,*",
  35. "CDEF:cdefros10im=ros10im,8,*",
  36. "CDEF:cdefros10ii=ros10ii,8,*",
  37. "CDEF:cdefwwwil=wwwil,8,*",
  38. "CDEF:cdefwwwim=wwwim,8,*",
  39. "CDEF:cdefwwwii=wwwii,8,*",
  40. "CDEF:cdeffbil=fbil,8,*",
  41. "CDEF:cdeffbim=fbim,8,*",
  42. "CDEF:cdeffbii=fbii,8,*",
  43. "CDEF:cdefnsol=nsol,8,*",
  44. "CDEF:cdefnsom=nsom,8,*",
  45. "CDEF:cdefnsoi=nsoi,8,*",
  46. "AREA:cdefros10il#AFECED:ros10_in  ",
  47. "STACK:cdefwwwil#0FF000:www_in  ",
  48. "STACK:cdeffbil#000000:fb_in   Lan_out ",
  49. "LINE1:cdefnocwil#FF0000:Wan_in ",
  50. "AREA:cdefnsol#750F7D:ns_out               \\r",
  51. "COMMENT:           Ros10         WWW         FB      Noc_L       Noc_W         ns\\n",
  52. "COMMENT:LAST\:",
  53. "GPRINT:cdefros10il:LAST:%9.0lf",
  54. "GPRINT:cdefwwwil:LAST:%9.0lf",
  55. "GPRINT:cdeffbil:LAST:%9.0lf",
  56. "GPRINT:cdefnoclol:LAST:%9.0lf",
  57. "GPRINT:cdefnocwil:LAST:%9.0lf",
  58. "GPRINT:cdefnsol:LAST:%9.0lf\\n",
  59. "COMMENT: MAX\:",
  60. "GPRINT:cdefros10im:MAX:%9.0lf",
  61. "GPRINT:cdefwwwim:MAX:%9.0lf",
  62. "GPRINT:cdeffbim:MAX:%9.0lf",
  63. "GPRINT:cdefnoclom:MAX:%9.0lf",
  64. "GPRINT:cdefnocwim:MAX:%9.0lf",
  65. "GPRINT:cdefnsom:MAX:%9.0lf\\n",
  66. "COMMENT: MIN\:",
  67. "GPRINT:cdefros10ii:MIN:%9.0lf",
  68. "GPRINT:cdefwwwii:MIN:%9.0lf",
  69. "GPRINT:cdeffbii:MIN:%9.0lf",
  70. "GPRINT:cdefnocloi:MIN:%9.0lf",
  71. "GPRINT:cdefnocwii:MIN:%9.0lf",
  72. "GPRINT:cdefnsoi:MIN:%9.0lf\\n")
复制代码


neti_dayly.png


其他图片

fbsshd_daily.png

onlineuser.png

temperture.png

temperture-year.png

rosi_dayly.png

roso_dayly.png

论坛徽章:
0
发表于 2015-06-04 16:31 |显示全部楼层
楼主太强!!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会

【架构革新 高效可控】2020年12月21日-23日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP