码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-30 23:38:52      阅读:930      评论:0      收藏:0      [点我收藏+]

标签:zabbix   django   

大家应该都知道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[eth0]的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/ 返回来的东西就是一个列表:[30308, 33636, 30310, 30305]


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

urls.py:

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

views.py:

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[eth0]|net.if.in[em1]‘, ‘vm.memory.size[available]‘, ‘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)[0]
                graphid = graphitems.graphid.graphid
                print "graphid: %s" % graphid
                ret.append(graphid)
        except Exception,e:
            print e
            ret = []
        return HttpResponse(json.dumps(ret))


最后出来的结果:

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

本文出自 “运维笔记” 博客,请务必保留此出处http://lihuipeng.blog.51cto.com/3064864/1680230

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

标签:zabbix   django   

原文地址:http://lihuipeng.blog.51cto.com/3064864/1680230

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!