加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 教程 > 正文

Zabbix及RRDtool绘图篇之用RRDtool绘制图形

发布时间:2022-06-18 05:45:46 所属栏目:教程 来源:互联网
导读:下面来给各位介绍一个Zabbix与RRDtool绘图篇之用RRDtool绘制图形例子,希望文章对各位会有所帮助. RRDtool在python里面的绘图函数是rrdtool.graph(),函数的各个参数代表什么意义就不讲了,主要讲思路,后面贴上绘图函数的代码就会明白了,在成千上万的监控图的海
  下面来给各位介绍一个Zabbix与RRDtool绘图篇之用RRDtool绘制图形例子,希望文章对各位会有所帮助.
 
  RRDtool在python里面的绘图函数是rrdtool.graph(),函数的各个参数代表什么意义就不讲了,主要讲思路,后面贴上绘图函数的代码就会明白了,在成千上万的监控图的海洋里怎样用最短和最通用的方法将这些图片绘出了是个问题,我目前由于水平的限制能够想到的解决问题的方法是这样的.
 
  1、所有的图形都有有限的item,将相同数目item的图片用同一个函数来绘制.一些特别的图形如内存堆栈图(stack),可以在该函数中写个判断语句单独配置相应的绘图参数.关于item的颜色和绘制方式(LINE1,LINE2,AREA),需要特别照顾的图形可以在DrawDef这张表中定义,其它的图形的item就采用自动默认就好。
 
  2、成千上万的item绘图时怎样真确的找到rrd文件中对应的DS,取得正确的数据,这个一开始就设计好了,每台主机的所有rrd文件放在一个文件夹里,文件夹以主机id命名,每个rrd文件以图形id命名并带上.rrd的后缀,每个rrd文件的DS用itemid命名,这样做就能正确的找到数据源了.
 
  解决了这些逻辑上的麻烦后,就可以堆绘图代码了,首先根据GET请求的hostid和type找到要进行绘图的rrd文件,rrdtooldraw02,处理这个GET请求的views的函数是这样的:
 
  def zabbixdraw(request):
      dir = r"/opt/rrd/"
      pngdir = r"/opt/www/diewu/static/images/rrdpng/"
      #value = request.GET
      hostid = request.GET['hostid']
      type = request.GET['type']
      sql = "select hostid, graphid, graphname, hostname from zabbixapp_drawtree where hostid="+hostid+" and type="+type+" and draw='1'"
      graphs = DrawTree.objects.getclass(sql)
      pngs = []
      gdatas = []
      strtime = str(int(time.time()- 86400))
      for graph in graphs:
          hostid = graph[0]
          graphid = graph[1]
          graphname = graph[2]
          hostname = graph[3]
          rpath = dir + hostid + r"/" + graphid + r".rrd"
          ############################################
          if not os.path.exists(pngdir + hostid + r"/"):
              os.makedirs(pngdir + hostid + r"/")
          pngname = pngdir + hostid + r"/" + graphid + r".png"
          sql = "select itemid,itemname,units from zabbixapp_drawgraphs where graphid="+graphid
          pitem = DrawGraphs.objects.getdata(sql)
          #####取自定义颜色和绘制方法
          sql = "select cols from zabbixapp_drawdef where graphid="+graphid
          cols = DrawDef.objects.getdata(sql)
          if cols:
              cols = (cols[0][0].split(":"),)
          sql = "select types from zabbixapp_drawdef where graphid="+graphid
          itypes = DrawDef.objects.getdata(sql)
          if itypes:
              itypes = (itypes[0][0].split(":"),)
          gdata = {'pname':pngname, 'gname':graphname, 'rrdpath':rpath, 'pitem':pitem, 'graphid':graphid,
              'cols':cols, 'itypes':itypes, 'host':hostname, 'stime':strtime, 'flag':'Daily'}
          gdatas.append(gdata)
          pngs.append({'pngpath':str(pngname).replace(r"/opt/www/diewu", ''), 'graphid':graphid}) --phpfensi.com
      drawrrd.drawmain(gdatas)
      #value = gdatas
      #avg = {'privatetitle': 'Zabbix监控数据展示', 'STATIC_URL': '/static', 'value':value, 'pngs':pngs}
      avg = {'privatetitle': 'Zabbix监控数据展示', 'STATIC_URL': '/static', 'pngs':pngs}
      return render_to_response('zabbixapp/zabbixdraw.html', avg)
  其中自定义的绘图函数drawrrd.drawmain()函数主要是用来遍历需要绘制的图形,根据图形数据提供的item个数,再调用相应的绘图函数绘制出对应的png图形.
 
  贴上真正的绘图函数,绘制一个item图形的和绘制2个item的图形的函数.
 
  如果有24个item的图形需要绘图,貌似我是想不到更好的方法了,只得慢慢细心的堆代码了,如果想到更优秀的方法,请告诉我一下,谢谢.Zabbix及RRDtool绘图篇之用RRDtool绘制图形
 
 

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读