码迷,mamicode.com
首页 > 编程语言 > 详细

Python调用Zabbix API接口批量修改(禁用/启用)触发器trigger

时间:2020-03-25 19:17:27      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:主机   bin   磁盘io   port   账号   禁用   desktop   启用   tput   

#!/bin/env python3 #coding:utf-8 import json import os import requests url = "http://192.168.1.1/zabbix/api_jsonrpc.php" # 此处域名修改为相应的地址 headers = { ‘Content-Type‘: ‘application/json-rpc‘ } def token_get(): #根据账号密码获取token data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "UserName", # zabbix管理员用户名 "password": "PASSWORD" # 账户密码 }, "auth": None, "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 hosts_get(token): #获取所有主机信息 data = { "jsonrpc": "2.0", "method": "host.get", "params": { "output":["hostid","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 triggers_get(token,hostids):#通过指定的主机IDs获取所有相关Trigger信息 data = { "jsonrpc": "2.0", "method": "trigger.get", "params": { "output": "extend", "hostids": hostids, "search": { "description": "Disk I/O" #修改此处更改触发器匹配字符串,我这边针对的是Disk I/O触发器进行处理 } }, "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 trigger_update(token,triggerid): #根据triggerid修改trigger状态:enable/disable data = { "jsonrpc": "2.0", "method": "trigger.update", "params": { "triggerid": triggerid, #此处只能为单个id "status": 1 }, "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 pending(): path = ‘C:/Users/38997/Desktop/hostlist.txt‘ #需要处理的主机列表 f = open(path,‘r‘) return f.read() f.close() if __name__==‘__main__‘: trigger_count = 0 trigger_ids = [] hostids = [] token = token_get() hostlist = hosts_get(token) print(‘读取主机列表...‘) unhosts = pending() print("获取主机ID...") for hostinfo in hostlist: if hostinfo[‘name‘] in unhosts: hostids.append(hostinfo[‘hostid‘]) print("主机IDs:",hostids,"\n","获取所有触发器IDs...") triggerlist = triggers_get(token,hostids) for i in triggerlist: trigger_count += 1 trigger_ids.append(i[‘triggerid‘]) print(trigger_ids,‘\n共‘,trigger_count,‘条磁盘IO触发器‘) select = input("开始处理 ‘y‘ or ‘n‘ ?\n:") if select == ‘y‘ or select == ‘‘: for triggerid in trigger_ids: print("开始禁用触发器:",triggerid,"\n结果:",trigger_update(token,triggerid),"\n","-"*30) elif select == ‘n‘: print("退出!") exit() else: print("请输入:‘y‘ or ‘n‘!") exit()

Python调用Zabbix API接口批量修改(禁用/启用)触发器trigger

标签:主机   bin   磁盘io   port   账号   禁用   desktop   启用   tput   

原文地址:https://blog.51cto.com/9499607/2481726

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!