前面一篇写了数据同步和模板绑定,zabbix其实能作的事还蛮多。
前端
zabbix提供了一个很是好的前端展现页面,可是咱们总以为不太好看;咱们能够进一步调用他的api经过获取每个监控项的历史数据,而后打到咱们的监控平台上;主流的有rrdtool方式和highcharts方式;rrdtool略显复杂,还要学习rrdtool之类的几个聚合方式。相对而言我更喜欢highcharts的方式,出图简便,只须要提供数据和时间戳组成的json数据就够了,以前也介绍过具体;那么这里咱们先拿出咱们想要的数据。python
zabbix的api读取方式以下:json
一、经过post方式传入用户名密码获取token秘钥。api
二、获取全部主机的hostid和name。ide
三、经过hostid获取每一个监控项目的item和对应的key。post
四、经过传入的itemid获取相对应的历史数据。学习
以上步骤都是须要传入token才能执行操做,pipy已经提供了第三方插件,让咱们省去了这一部分的操做;具体参考:https://pypi.python.org/pypi/zabbix-client/0.1.1;zabbix api文档请参考官方文档。想多写点代码能够看小马哥的博客:http://www.xiaomastack.com/2014/08/17/rrdtool-1/。url
一、获取主机的代码:.net
def get_hosts(self): data = { "output": ["hostid", "name"] } ret = self.zb.host.get(**data) return ret
执行结果:插件
二、获取每一个主机对应的监控项和监控项具体名称:
def item_get(self, hostids="10109"): data = { "output":["itemids","key_"], "hostids": hostids, } ret = self.zb.item.get(**data) return ret
执行结果:
三、获取对应的历史数据:
def history_get(self, itemid, i ,limit=10): data = { "output": "extend", "history": i, "itemids": itemid, "sortfield": "clock", "sortorder": "DESC", "limit": limit } ret = self.zb.history.get(**data) return ret
具体代码:
from zabbix_client import ZabbixServerProxy class Zabbix(): def __init__(self): self.zb = ZabbixServerProxy("http://192.168.10.100/zabbix") self.zb.user.login(user="Admin", password="zabbix") def get_hosts(self): data = { "output": ["hostid", "name"] } ret = self.zb.host.get(**data) return ret def item_get(self, hostids="10109"): data = { "output":["itemids","key_"], "hostids": hostids, } ret = self.zb.item.get(**data) return ret def history_get(self, itemid, i ,limit=10): ###history参数中有0,1,2,3,4表示:float,string,log,integer,text data = { "output": "extend", "history": i, "itemids": itemid, "sortfield": "clock", "sortorder": "DESC", "limit": limit } ret = self.zb.history.get(**data) return ret if __name__ == "__main__": zabbix_server = Zabbix() # print zabbix_server.get_hosts() print zabbix_server.history_get("24519",3)