路虽远,行则将至;|

Security

园龄:5年2个月粉丝:24关注:37

获取zabbix全量主机

修改URL和username password为自己的系统即可

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python3
# -*- coding: utf-8 -*
# Copyright (C) 2023 - 2023 zhoujt, Inc. All Rights Reserved
# @Date    : 2023/5/15 18:03
# @Author  : zhoujt
# @Email   : zhouxiaobu1022@gmail.com
# @FileName: ZabbixGet_all_host.py
# @IDE     : PyCharm
import requests
import json
 
 
# 获取token
class Zabbix:
    def __init__(self, user, password):
        self.user = user
        self.password = password
        self.session = requests.Session()
        self.host = 'http://zabbix.zhoujt.com'
        self.url = self.host + '/api_jsonrpc.php'
        self.login()
 
    def login(self):
        data = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": self.user,
                "password": self.password
            },
            "id": 1,
            "auth": None
        }
        response = self.session.post(self.url, json=data)
        # 获取结果中的token
        self.auth = response.json()['result']
 
    def get_hosts(self):
        auth_token = self.auth
        # 构造API请求的数据
        data = {
            'jsonrpc': '2.0',
            'method': 'host.get',
            'params': {
                'output': ['hostid', 'host', 'status'],
                'selectInterfaces': ['ip']
            },
            'auth': auth_token,
            'id': 1
        }
        url = self.url
        headers = {'Content-Type': 'application/json'}
        # 发送API请求
        response = requests.post(url, headers=headers, data=json.dumps(data))
 
        # 解析响应数据
        result = json.loads(response.text)
        stop_host = []
        # 提取主机信息
        if 'result' in result:
            hosts = result['result']
            sum_1 = 0
            for host in hosts:
                sum_1 += 1
                host_id = host['hostid']
                host_name = host['host']
                ip_address = host['interfaces'][0]['ip']
                status = host['status']
                print(f"Host ID: {host_id}, Name: {host_name}, IP: {ip_address}, status: {status}")
                if int(status) == 1:
                    stop_host.append(ip_address)
        else:
            print("Failed to retrieve hosts.")
        print("Host sum: %s" % sum_1)
        stop_1 = 0
        print("=======The stop hosts=======\n")
        for i in stop_host:
            stop_1 += 1
            print(i)
        print("Stop hosts: %s" % stop_1)
 
 
zab = Zabbix(user="zhoujt", password="password")
zab.get_hosts()

 

本文作者:Security

本文链接:https://www.cnblogs.com/security-guard/p/get_zabbix_host.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Security  阅读(82)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 像风一样 এ萘狸.🦩
  2. 2 你(吉他甜蜜版) এ萘狸.🦩
  3. 3 黑月光 এ萘狸.🦩
  4. 4 永不失联的爱 এ萘狸.🦩
  5. 5 你不是第一个离开的人 এ萘狸.🦩
像风一样 - এ萘狸.🦩
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.