利用zabbix api 带入 组ID 和 监控项目键值 获取所有主机的值
更新:HHH   时间:2023-1-7



def  chaxun(groupid,items_value):  是自己写的,带入组ID 和 监控项目键值,返回 主机和值的结果。

做了判断,如果这个主机没有想要的监控项目,就去除掉。
#!/usr/bin/env python
#coding=utf-8
import urllib.request
import json
import re

url = 'http://XXXXXXXXXXXXXXXX/api_jsonrpc.php'
username = 'Admin'
password = 'XXXXX'

#登陆
def requestJson(url,values):
    data = json.dumps(values).encode('utf-8')
    req = urllib.request.Request(url, data, {'Content-Type': 'application/json-rpc'})
    response = urllib.request.urlopen(req, data)
    a = response.read().decode(encoding='utf-8')
    output = json.loads(a)
    try:
        message = output['result']
    except:
        message = output['error']['data']
        print(message)
        quit()
    return output['result']


##登陆的API
def authenticate(url, username, password):
    values = {'jsonrpc': '2.0',
              'method': 'user.login',
              'params': {
                  'user': username,
                  'password': password
              },
              'id': '0'
              }
    idvalue = requestJson(url,values)
    return idvalue

auth=authenticate(url,username,password)



##查询组ID {'groupid': '8', 'name': 'Switch'}
def groups(auth):
    values = {
            "jsonrpc":"2.0",
            "method":"hostgroup.get",
              "params":{
                    "output":["groupid","name"],
            },
              'auth': auth,
              'id': '1'
              }
    output = requestJson(url,values)
    return output

##查询主机
def hosts(auth,a):
    values = {
            "jsonrpc":"2.0",
            "method":"host.get",
              "params":{
                    "output":["groupid","name"],
                    "groupids": a,
            },
              'auth': auth,
              'id': '1'
              }
    output = requestJson(url,values)
    return output




##查询主机项目
def items(auth,a):
    values = {
            "jsonrpc":"2.0",
            "method":"item.get",
              "params":{
                  "output": ["itemids", "key_"],
                  "hostids": a,
                  },
              'auth': auth,
              'id': '1'
              }
    output = requestJson(url,values)
    return output

##查询项目的历史值  'lastvalue': '-14760.0000'
def his(auth,itemids):
    values = {
            "jsonrpc":"2.0",
            "method":"item.get",
              "params":{
                    "output": "extend",
                      "history": 0,
                      "itemids": itemids,
                      "sortfield": "itemid",
                      "sortorder": "DESC",
                      "limit": 1
                  },
              'auth': auth,
              'id': '1'
              }
    output = requestJson(url,values)
    return output

##查询触发项目值和监控项目
def trigger(auth):
    values = {
              "jsonrpc":"2.0",
              "method": "trigger.get",
              "params": {
                      "output": [
                        "description",
              ],
              "filter": {
                  "hostid": "10108",
              },
              "selectItems":"",
              "sortfield": "hostname",
              "sortorder": "DESC"
              },
              'auth': auth,
              'id': '1'
              }
    output = requestJson(url,values)
    return output


def  chaxun(groupid,items_value):

    host = hosts(auth, groupid) ##获取所有主机

    host_name = []
    host_id = []

    for i in host:
        host_id.append(i['hostid'])
        host_name.append(i['name'])
    hostall = dict(zip(host_id, host_name))  ## 所有主机     '10118': '云主机:10.8.8.65','10119': '云主机:10.8.8.66',


    host_names = []
    host_ids = []
    for i in host_id:    ##筛选,  有监控项目的   符合要求的主机ID,和主机名字
        host_id_value = items(auth, i) ## [{'itemid': '29272', 'key_': 'BiosDate'},{'itemid': '29272', 'key_': 'BiosDate'}]
        for index, value in enumerate(host_id_value):
            for k, v in value.items():
                if  items_value  in v:           ## items_value
                    host_ids.append(host_id_value[index]['itemid'])
                    host_names.append(i)

                    
                    

    host_names_list = []
    for i in   host_names:  ## 用符合要求的主机  获取它的名字
        for k, v in hostall.items():
            if i in k:
                host_names_list.append(v)


    history_value = []
    for i in host_ids:         ##查询主机ID的最新历史值
        history = his(auth, i)
        history_value.append(history[0]['lastvalue'])
    host_value = dict(zip(host_names_list, history_value))

    return  host_value  #返回主机和值的对应关系

ret = chaxun(5, "ProcThreadCount.[2]")#  组ID 和 想要查询的监控项目
print(ret)


返回开发技术教程...