美文网首页
Zabbix获取所有数据

Zabbix获取所有数据

作者: ToySun | 来源:发表于2017-12-21 16:34 被阅读89次
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "ToySun"
__date__ = "2017-12-20"
#version:1.0

from zabbix_client import ZabbixServerProxy
import json

class Zabbix():
    def __init__(self):
        self.zb = ZabbixServerProxy("http://192.168.10.143/zabbix")
        self.zb.user.login(user="Admin", password="zabbix")
    def get_hosts(self):
        host_data = {
        "output": ["hostid", "name"]
        }
        host_ret = self.zb.host.get(**host_data)
        return host_ret

    def item_get(self):
        key_dict = {}
        console = []
        for i in range(len(self.get_hosts())):
          item_data = {
          "output":["itemids","key_",],
          "hostids": self.get_hosts()[i]['hostid'],
          }
          item_ret = self.zb.item.get(**item_data)
          for x in range(len(item_ret)):
            if self.history_get(item_ret[x]['itemid']) is None:
              pass
            else:
              key_dict[item_ret[x]['key_']] = int(self.history_get(item_ret[x]['itemid']))

            key_dict['hostid'] = int(self.get_hosts()[i]['hostid'])
            key_dict['name'] = self.get_hosts()[i]['name']
          console.append(json.dumps(key_dict))
        return console

    def history_get(self, itemid):

        #history:Possible values: 0 - numeric float; 1 - character; 2 - log;
        #3 - numeric unsigned; 4 - text. Default: 3
        #limit:the number of value. Default/Must: 1
        data = { "output": "extend",
          "history": 3,
          "itemids": itemid,
          "sortfield": "clock",
          "sortorder": "DESC",
          "limit": 1
          }
        history_ret = self.zb.history.get(**data)
        if len(history_ret):
          return history_ret[0]['value']
        else:
          pass

if __name__ == "__main__": 
    zabbix_server = Zabbix()
    zabbix_server.item_get()

相关文章

网友评论

      本文标题:Zabbix获取所有数据

      本文链接:https://www.haomeiwen.com/subject/nzmawxtx.html