当前位置:   article > 正文

python获取zabbix指标信息_pyzabbix

pyzabbix

问题

python获取zabbix指标信息时,用三方包py-zabbix(即from pyzabbix import ZabbixAPI)不能成功连接zabbix的api。所以选择用最原始的requests三方包实现

实现过程

1、加载需要的三方库

import requests
import json
headers = {
    "Content-Type": "application/json-rpc",
}
  • 1
  • 2
  • 3
  • 4
  • 5

2、先获取token,即在访问zabbix中的任何数据之前,需要登录并获取身份认证token。我们假设你想要以标准Zabbix Admin用户身份登录,那么,你的json请求将如下所示:

{
           "jsonrpc": "2.0",
           "method": "user.login",
           "params": {
               "user": "Admin",
               "password": "zabbix"
           },
           "id": 1,
           "auth": null
       }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

以上json请求是zabbix官方文档给出的,在python中将null替换成None。如果不能成功连接zabbix,需要将请求中的user修改为username,即完整的json请求如下:

 {
           "jsonrpc": "2.0",
           "method": "user.login",
           "params": {
               "username": "Admin",
               "password": "zabbix"
           },
           "id": 1,
           "auth": null
       }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

python实现过程如下:

data = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "username": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": None
}
data = json.dumps(data)
response = requests.post(url, data=data, headers=headers)
token = json.loads(response.text)['result']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

如果正确提供了凭据,API返回的响应将包含用户身份认证token:

{
           "jsonrpc": "2.0",
           "result": "0424bd59b807674191e7d77572075f33",
           "id": 1
       }
  • 1
  • 2
  • 3
  • 4
  • 5

即result字段对应的结果就是token。

2、接下来获取主机id, 从zabbix官方文档中获取hot.get方法的json请求体:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "filter": {
                   "host": [
                       "Zabbix server",
                       "Linux server"
                   ]
               }
           },
           "auth": "0424bd59b807674191e7d77572075f33",
           "id": 1
       }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

将其中的auth值用上述获取的token替换,filter部分按照自己的需求来设置,获取主机id的代码如下:

host_get_query = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "filter": {
            'flags': [0, 4]
        }
    },
    "auth": token,
    "id": 1
}
response_host = requests.post(url, data=json.dumps(host_get_query), headers=headers)
host_result = json.loads(response_host.text)['result']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3、有了主机id,就可以获取这些主机对应监控的指标id的信息了,同样,从zabbix官方文档获取json请求的格式:

{
           "jsonrpc": "2.0",
           "method": "item.get",
           "params": {
               "output": "extend",
               "hostids": "10084",
               "search": {
                   "key_": "system"
               },
               "sortfield": "name"
           },
           "auth": "0424bd59b807674191e7d77572075f33",
           "id": 1
       }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

同样,auth对应的值用上述的token结果替换,hostids用步骤2获取的主机id替换,获取主机对应的指标的信息的代码如下所示:

item_ls = []
for host in host_result:
    param = {
        "output": ["itemid", "delay", "units"],
        "hostids": host["hostid"],
        "with_triggers": False,
        "filter": {'flags': [0, 4]},
        "tags": [{"tag": "component", "value": "cpu"},
                 {"tag": "component", "value": "memory"},
                 {"tag": "component", "value": "network"}],
        # "search": {
        #     "key_": "system.cpu"  # 通配符搜索。
        # },
        "sortfield": "name"
    }

    item_get_query = {
        "jsonrpc": "2.0",
        "method": "item.get",
        "params": param,
        "auth": token,
        "id": 1
    }
    response_item = requests.post(url, data=json.dumps(item_get_query), headers=headers)
    item_list = json.loads(response_item.text)['result']
    item_ls.extend(item_list)
print(len(item_ls))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

代码中我只获取了指标的itemid,delay, units字段的信息,循环的将获取到的主机的所有监控指标满足flags是0和4的,以及tag的值为‘cpu’, “memory”, “network”的指标提取出来。

完整代码如下:

import requests
import json

url = r'http://zabbix.com.cn/api_jsonrpc.php' # 该处根据自己的实际情况修改。
headers = {
    "Content-Type": "application/json-rpc",
}

data = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "username": "Admin",  # 该处用户名根据实际用户名修改
        "password": "zabbix"  # 该处密码根据实际密码修改
    },
    "id": 1,
    "auth": None
}
data = json.dumps(data)
response = requests.post(url, data=data, headers=headers)
token = json.loads(response.text)['result']
host_get_query = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "filter": {
            'flags': [0, 4]
        }
    },
    "auth": token,
    "id": 1
}
response_host = requests.post(url, data=json.dumps(host_get_query), headers=headers)
host_result = json.loads(response_host.text)['result']

item_ls = []
for host in host_result:
    param = {
        "output": ["itemid", "delay", "units"],
        "hostids": host["hostid"],
        "with_triggers": False,
        "filter": {'flags': [0, 4]},
        "tags": [{"tag": "component", "value": "cpu"},
                 {"tag": "component", "value": "memory"},
                 {"tag": "component", "value": "network"}],
        # "search": {
        #     "key_": "system.cpu"  # 通配符搜索。
        # },
        "sortfield": "name"
    }

    item_get_query = {
        "jsonrpc": "2.0",
        "method": "item.get",
        "params": param,
        "auth": token,
        "id": 1
    }
    response_item = requests.post(url, data=json.dumps(item_get_query), headers=headers)
    item_list = json.loads(response_item.text)['result']
    item_ls.extend(item_list)
print(len(item_ls))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/782508
推荐阅读
相关标签
  

闽ICP备14008679号