-
Notifications
You must be signed in to change notification settings - Fork 1
/
enable_disable-host.py
executable file
·123 lines (98 loc) · 3.26 KB
/
enable_disable-host.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2022 Opensource ICT Solutions B.V.
# https://oicts.com
#
#version: 2.0.0
#date: 23-01-2022
import sys
import requests
import json
import os
import time
url = 'https://example.com/zabbix/api_jsonrpc.php?'
token = "PUT_YOUR_TOKEN_HERE"
headers = {'Content-Type': 'application/json'}
state = sys.argv[1]
hostname = sys.argv[2] # enable and disable are possible
def main():
hostid = hostid_get(token)
if sys.argv[1].lower() == 'disable':
event_dict,eventid_array = get_problems(hostid,token)
toggle_host(hostid,token)
close_problems(eventid_array, token)
print("Host " + hostid + " disabled")
elif sys.argv[1].lower() == 'enable':
toggle_host(hostid, token)
print("Host " + hostid + " enabled")
else:
print("DIE!")
# os.system('zabbix_server -R config_cache_reload')
def hostid_get(token):
payload = {}
payload['jsonrpc'] = '2.0'
payload['method'] = 'host.get'
payload['params'] = {}
payload['params']['output'] = ['hostid']
payload['params']['filter'] = {}
payload['params']['filter']['host'] = hostname
payload['auth'] = token
payload['id'] = 1
#Doing the request
request = requests.post(url, data=json.dumps(payload), headers=headers)
data = request.json()
hostid = data["result"][0]["hostid"]
return hostid
def get_problems(hostid,token):
payload = {}
payload['jsonrpc'] = '2.0'
payload['method'] = 'problem.get'
payload['params'] = {}
payload['params']['output'] = ['eventid','name','severity']
payload['params']['hostids'] = hostid
payload['params']['sortfield'] = ['eventid']
payload['params']['sortorder'] = "DESC"
payload['params']['filter'] = {}
payload['params']['filter']['r_eventid'] = "0"
payload['auth'] = token
payload['id'] = 1
request = requests.post(url, data=json.dumps(payload), headers=headers)
data = request.json()
eventid_array = []
for eventid in data["result"]:
eventid_array.append(str(eventid["eventid"]))
event_dict = {}
for x in data["result"]:
event_dict[x["eventid"]] = {}
event_dict[x["eventid"]]['name'] = x["name"]
event_dict[x["eventid"]]['name'] = x["name"]
event_dict[x["eventid"]]['severity'] = x["severity"]
return event_dict, eventid_array
def toggle_host(hostid, token):
if sys.argv[1].lower() == 'disable':
set_status = '1'
elif sys.argv[1].lower() == 'enable':
set_status = '0'
payload = {}
payload['jsonrpc'] = '2.0'
payload['method'] = 'host.update'
payload['params'] = {}
payload['params']['hostid'] = hostid
payload['params']['status'] = set_status
payload['auth'] = token
payload['id'] = 1
request = requests.post(url, data=json.dumps(payload), headers=headers)
def close_problems(eventid_array,token):
payload = {}
payload['jsonrpc'] = '2.0'
payload['method'] = 'event.acknowledge'
payload['params'] = {}
payload['params']['eventids'] = eventid_array
payload['params']['action'] = '1'
payload['auth'] = token
payload['id'] = 1
request = requests.post(url, data=json.dumps(payload), headers=headers)
if __name__ == '__main__':
# Call to main
main()