• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解python3-zabbixapi的使用

python 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

这篇文章详解python3-zabbixapi的使用

python3 使用zabbix api的一些案例。。具体可以去zabbix官网找API借口,替换一下就可以。

#!/usr/bin/env python# -*- coding: utf-8 -*- import urllib.requestimport jsonimport re url = 'http://xxxxxxxxxxxxxxxx/api_jsonrpc.php'username = 'xxxxxxxxxxxxxxx'password = 'xxxxxx'  # 登陆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)    #    print output    try:        message = output['result']    except:        message = output['error']['data']        print(message)        quit()     return output['result']  ##登陆的APIdef 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的值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  # b = groups(auth)# print(b) ##查询主机  {'hostid': '10108',def hosts(auth):    values = {        "jsonrpc": "2.0",        "method": "host.get",        "params": {            "output": ["groupid", "name"],            "groupids": "8",        },        'auth': auth,        'id': '1'    }    output = requestJson(url, values)    return output  host = hosts(auth)host1 = []host2 = []for i in range(len(host)):    host1.append(host[i]['name'])    host2.append(host[i]['hostid']) host3 = dict(zip(host1, host2))  ##查询主机项目 {'key_': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'itemid': '26399'}def item(auth):    values = {        "jsonrpc": "2.0",        "method": "item.get",        "params": {            "output": ["itemids", "key_"],            "hostids": "10108",        },        'auth': auth,        'id': '1'    }    output = requestJson(url, values)    return output  # print(item(auth)) ##查询项目的历史值  '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  # print(his(auth,26399)) ##查询触发项目值和监控项目  {'description': 'xxxxxxxxxxxxxxx', 'hostname': 'xxxxxxxxxxxxxxx', 'items': [{'itemid': '26399'}], 'triggerid': '17030'}def trigger(auth, hostid):    values = {        "jsonrpc": "2.0",        "method": "trigg<strong>本文来源gaodaima#com搞(代@码$网6</strong>er.get",        "params": {            "output": [                "description",            ],            "filter": {                "hostid": hostid,            },            "selectItems": "",            "sortfield": "hostname",            "sortorder": "DESC"        },        'auth': auth,        'id': '1'    }    output = requestJson(url, values)    return output       ###简单使用案例,可查考,根据触发器查找历史。        t1 = trigger(auth, host3[msg['Content']])        t2 = []        t3 = []        for i in range(len(t1)):            t5 = t1[i]['items'][0]  ##   'items': [{'itemid': '26399'}]            t6 = his(auth, t5['itemid'])  ##   his(auth,26399)            t2.append(t1[i]['description'])  ##监控项目描述            t3.append(round(float(t6[0]['lastvalue'])) / 1000)  ##项目ID 的值        t4 = dict(zip(t2, t3))        t8 = []        for k in t4:            t7 = k + ":" + "{}".format(t4[k]) + "db"            t8.append(t7)        t9 = "\n".join(t8)

以上就是详解python3-zabbixapi的使用的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解python3-zabbixapi的使用
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址