利用zabbixapi帶入組ID和監(jiān)控項目鍵值獲取所有主機的值-創(chuàng)新互聯(lián)

def  chaxun(groupid,items_value):  是自己寫的,帶入組ID 和 監(jiān)控項目鍵值,返回 主機和值的結(jié)果。

做了判斷,如果這個主機沒有想要的監(jiān)控項目,就去除掉。
#!/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

##查詢觸發(fā)項目值和監(jiān)控項目
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:    ##篩選,  有監(jiān)控項目的   符合要求的主機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  #返回主機和值的對應(yīng)關(guān)系

ret = chaxun(5, "ProcThreadCount.[2]")#  組ID 和 想要查詢的監(jiān)控項目
print(ret)

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、萬全網(wǎng)絡(luò)推廣、成都小程序開發(fā)、萬全網(wǎng)絡(luò)營銷、萬全企業(yè)策劃、萬全品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供萬全建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

標(biāo)題名稱:利用zabbixapi帶入組ID和監(jiān)控項目鍵值獲取所有主機的值-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article40/dejieo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)、微信小程序網(wǎng)站設(shè)計公司、靜態(tài)網(wǎng)站、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)