详解python3-zabbixapi的使用

高洛峰
发布: 2017-03-10 18:45:20
原创
1668人浏览过

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

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

SpeakingPass-打造你的专属雅思口语语料
SpeakingPass-打造你的专属雅思口语语料

使用chatGPT帮你快速备考雅思口语,提升分数

SpeakingPass-打造你的专属雅思口语语料 25
查看详情 SpeakingPass-打造你的专属雅思口语语料
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import urllib.request
import json
import 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']
 
 
##登陆的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的值
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": "trigger.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的使用的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号