zoukankan      html  css  js  c++  java
  • Python3 用requests调用Zabbix API

    环境版本:

    Python 3.6

    requests 2.21

    Zabbix 3.4

    #!/usr/bin/env python
    #_*_ coding:utf-8 _*_
    
    import requests
    import json
    
    class Zabbix_api():
    
        def __init__(self):
            self.url = 'http://192.168.110.110/zabbix/api_jsonrpc.php'
            self.username = 'Admin'
            self.password = '123456789'
            self.header = {"Content-Type": "application/json-rpc"}
    
        def Login(self):
            data = {
                "jsonrpc": "2.0",
                "method": "user.login",
                "params": {
                    "user": self.username,
                    "password": self.password
                },
                "id": 1
            }
            request = requests.post(url=self.url, headers=self.header, data=json.dumps(data))
            dict = json.loads(request.text)
            request.close()
            return dict['result']
        def HostList(self, token):
            data = {
                "jsonrpc": "2.0",
                "method": "host.get",
                "params": {
                    "output": [
                        "hostid"
                    ],
                    "selectInterfaces": [
                        "ip"
                    ]
                },
                "id": 2,
                "auth": token
            }
            request = requests.post(url=self.url, headers=self.header, data=json.dumps(data))
            dict = json.loads(request.text)
            request.close()
            return dict['result']
    
    if __name__ == '__main__':
        zabapi = Zabbix_api()
        token = zabapi.Login()
        zab_host_list = zabapi.HostList(token)
        for host in zab_host_list:
            print(host)

    文献:https://www.zabbix.com/documentation/3.4/zh/manual/api

  • 相关阅读:
    Some day some time we will do
    qemu-img 的使用
    虚拟化qemu-img的简单用法。
    linux 后台执行命令
    C#向服务器上传文件问题
    Canvas保存为图片
    一个Sql备注
    fabric Clone
    Js 正则获取Html元素
    Graphic 完成文字缩放
  • 原文地址:https://www.cnblogs.com/TaleG/p/10130321.html
Copyright © 2011-2022 走看看