审计Zabbix模板自动发现规则是否报错

审计Zabbix模板自动发现规则是否报错

话不多说直接上脚本,自用的话替换一下脚本里面的API地址以及zabbix的用户名和密码即可。

#!/usr/bin/env python
# encoding=utf-8

import datetime
import json
import time

import requests


def getToken(url, post_headers, url_user, url_password):
    post_data = {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": url_user,
            "password": url_password
        },
        "id": 1
    }
    ret = requests.post(url, data=json.dumps(post_data), headers=post_headers)
    return json.loads(ret.text).get("result")


def timestamp_to_string(timestamp):
    # 转换成localtime
    time_local = time.localtime(timestamp)
    # 转换成新的时间格式(2016-05-05 20:28:54)
    dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    return dt

def get_host_discovery_list(url, auth, post_headers,hostid):
    post_data = {
    "jsonrpc": "2.0",
    "method": "discoveryrule.get",
    "params": {
        "output": "extend",
        "hostids": hostid,
    },
    "auth": auth,
    "id": 1
}
    ret = requests.post(url, data=json.dumps(post_data), headers=post_headers)
    ret_dict = json.loads(ret.text)
    return ret_dict

def get_all_host_list(url, auth, post_headers):
    post_data = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {"output":["hostid", "name"], "filter": {"status": 0}},
    "auth": auth,
    "id": 1
}
    ret = requests.post(url, data=json.dumps(post_data), headers=post_headers)
    ret_dict = json.loads(ret.text)
    return ret_dict


if __name__ == '__main__':
    # zabbix监控系统接口地址
    url = "http://xxxx.com/zabbix/api_jsonrpc.php"
    post_headers = {'Content-Type': 'application/json'}
    url_user = ""
    url_passwd = ""
    auth = getToken(url, post_headers, url_user, url_passwd)  # 获取token值
    # print(get_host_discovery_list(url,auth,post_headers))
    all_host_list = get_all_host_list(url,auth,post_headers)['result']
    # print(all_host_list)
    count = 0
    error_host_list = []
    error_msg_list = []
    for host in all_host_list:
        host_id = host['hostid']
        host_name = host['name']
        print(count)
        count= count + 1
        auto_discovery_list = get_host_discovery_list(url,auth,post_headers, host_id)['result']
        for auto in auto_discovery_list:
            is_error = auto['error']
            if is_error:
                if 'Unsupported item key' in is_error:
                    error_host_list.append(host_name)
                    error_msg_list.append(is_error)
    error_host_list_unique = set(error_host_list)
    for error in error_host_list_unique:
        print(error)

 

0 评论 在 “审计Zabbix模板自动发现规则是否报错

发表评论

电子邮件地址不会被公开。 必填项已用*标注

captcha