uykjgfh 发表于 2015-7-31 08:58:18

抽取Zabbix的图形整合到自己后台

大家应该都知道Zabbix的图是可以通过接口获取出来的,例如CPU性能图

把这图拖到新窗口可以看到这张图的地址:http://xxxxx.xxxx.com/chart2.php?graphid=78730&period=3600&stime=20160729160437&updateProfile=1&profileIdx=web.screens&profileIdx2=78730&sid=979aa31695be988f&width=1772&curtime=1438247260802
参数多得很,其实很多对我都没啥用的,精简一下可以裁剪成:http://xxxxx.xxxx.com/chart2.php?graphid=78730&period=3600&width=1772,其中参数意义:
graphid:这个是Zabbix的图形ID,下面再说怎么获取出来
period:这个是时间,以秒为单为,3600是指获取1个小时内的数据
width:这个就是图片的宽度
我只需要知道这三个参数,我就能把我想要的图形数据获取出来,嵌入到自己的后台中了。

graphid的查询方法:
1、先通用ip在hosts表中查出对应的hostid

2、根据hostid在items表里查找到对应监控项的itemid,倒如我要看流量图,可以查到net.if.in的itemid是395636

3、通过上面查找出来的itemid在graphs_items表里,可以找到graphid,这个ID就是生成图形的ID

利用这个graphid组合一下上面的URL:http://xxxxx.xxxx.com/chart2.php?graphid=78835&period=3600&width=1772,就能拿到图形

接下来就是嵌入到自己的后台里面去,业务代码都是很少,主要还是用jquery就OK了:
模板:我这里是用bootstrap的模拟框弹出展示,这是模拟框的代码

jquery代码:通过点击按钮弹出上面的模拟框,把URL填入四个img里面

ajax是通过异步获取我要生成图形的graphid,/yunwei/api/getgraphid/xxx/ 返回来的东西就是一个列表:

后端是django,view代码很简单:
urls.py:


1
url(r'^api/getgraphid/(\d+)/$',                         GetGrapidAPIView.as_view(),),




views.py:

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
28
29
class GetGrapidAPIView(View):

    def get(self, request, hostid):
      try:
            hostinfo = GameHost.objects.using('res').get(id=hostid)
            if hostinfo.platid == 34:
                hostip = hostinfo.ip2
            else:
                hostip = hostinfo.ip1
            print "hostip: %s" % hostip

            ret = []
            for key in ['system.cpu.util[,idle]', 'net.if.in|net.if.in', 'vm.memory.size', 'icmpping']:
                if '|' in key:
                  key1, key2 = key.split('|')
                  #获取itemid:
                  itemid = Items.objects.using('zab').get(Q(key_field=key1)|Q(key_field=key2), hostid__host=hostip).itemid         
                else:
                  #获取itemid:
                  itemid = Items.objects.using('zab').get(key_field=key, hostid__host=hostip).itemid
                print "itemid: %s" % itemid
                graphitems = GraphsItems.objects.using('zab').filter(itemid=itemid)
                graphid = graphitems.graphid.graphid
                print "graphid: %s" % graphid
                ret.append(graphid)
      except Exception,e:
            print e
            ret = []
      return HttpResponse(json.dumps(ret))





最后出来的结果:
这样做有个致命的缺点,就是浏览器需要登陆过zabbix才能看得到图形,不过我们也是运维自己看看,问题不大,方便自己而已~

页: [1]
查看完整版本: 抽取Zabbix的图形整合到自己后台