others linux服务器运维 django3 监控 k8s golang 数据库 大数据 前端 devops 理论基础 java oracle 运维日志

zabbix api 批量删除监控项

访问量:1469 创建时间:2021-03-22

因为自动发现导致重复无用的监控项目太多,直接删除机器已经删除失败,无法删除主机重建主机,通过脚本将机器的监控项目删除。下图显示一台机器4W多监控项目。

编写python脚本如下:

#!/bin/env python
#coding:utf-8
import json
import os
import requests

url = "http://127.0.0.1/zabbix/api_jsonrpc.php"
headers = {
    'Content-Type': 'application/json-rpc'
}

def tokenget():
    data = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": "账号",
                "password": "密码"
            },
            "auth": None,
            "id": 0
}
    json_data = json.dumps(data)
    req = requests.post(url,data=json_data,headers=headers)
    js_req = req.json()
    return js_req['result']

def hostget(token,hostname):
    data = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            "filter": {
                "host": [
                    #"127.0.0.1"
                    #"Zabbix server"
                    hostname
                ]   
            }
        },
        "auth": token,
        "id": 1
    }
    json_data = json.dumps(data)
    req = requests.post(url,data=json_data,headers=headers)
    js_req = req.json()
    return js_req['result']

def applicationget(hostid,token):
    data = {
        "jsonrpc": "2.0",
        "method": "application.get",
        "params": {
            "output": "extend",
            "hostids": hostid,
            "sortfield": "name"
        },
        "auth": token,
        "id": 1
    }
    json_data = json.dumps(data)
    req = requests.post(url,data=json_data,headers=headers)
    js_req = req.json()
    return js_req['result']



def itemget(hostid,token,applicationids):
    data = {
        "jsonrpc": "2.0",
        "method": "item.get",
        "params": {
            "output": "extend",
            "hostids": hostid,
            "applicationids": applicationids,
            "limitSelects": 50
            #"sortfield": "name"
        },
        "auth": token,
        "id": 1
    }
    json_data = json.dumps(data)
    req = requests.post(url,data=json_data,headers=headers)
    js_req = req.json()
    return js_req['result']


def itemdelete(token,itemid):
    data = {
        "jsonrpc": "2.0",
        "method": "item.delete",
        "params": [
            itemid
        ],
        "auth": token,
        "id": 1
    }
    json_data = json.dumps(data)
    req = requests.post(url,data=json_data,headers=headers)
    js_req = req.json()
    print "delete"
    print js_req['result']


if __name__=='__main__':
    token = tokenget()
    #print token
    hos= hostget(token,"172.16.106.3")
    #print hos
    hostid = hos[0]['hostid']
    #print hostid
    apps=applicationget(hostid,token)
    #print apps 
    for a in apps:
        if a["name"] == 'Filesystems':
            applicationids=a["applicationid"]
    items=itemget(hostid,token,applicationids)
    for item in items:
        #print item["itemid"],item["name"]
        itemdelete(token,item["itemid"])
登陆评论: 使用GITHUB登陆