-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_maintenance.py
executable file
·208 lines (184 loc) · 6.22 KB
/
zabbix_maintenance.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
import argparse
import configparser
import datetime
import json
import os.path
import urllib.parse
import urllib.request
import socket
import sys
class ZabbixCliConfig:
def __init__(self):
zabbix_config = "~/.zabbix-cli/zabbix-cli.conf"
zabbix_auth = "~/.zabbix-cli_auth_token"
# find URL
try:
config = configparser.ConfigParser()
config.read(os.path.expanduser(zabbix_config))
self.url = config["zabbix_api"]["zabbix_api_url"]
except KeyError:
self.url = None
# find auth token
try:
with open(os.path.expanduser(zabbix_auth), "r") as f:
content = f.read()
if len(content) == 37 and content.startswith("cli::"):
self.auth = content[5:]
except FileNotFoundError:
self.auth = None
class ZabbixSession:
def __init__(self, url, user=None, password=None, auth=None):
if auth:
self.user = None
self.password = None
self.auth = auth
else:
self.user = user
self.password = password
self.auth = None
self.logged_in = False
self.url = url
self._request_id = 0
def _request(self, data):
data["jsonrpc"] = "2.0"
data["auth"] = self.auth
data["id"] = self.get_request_id()
encoded = json.dumps(data).encode("utf-8")
r = urllib.request.Request(self.url,
data=encoded,
headers={"Content-Type": "application/json-rpc"},
method="POST")
return json.load(urllib.request.urlopen(r))
def get_request_id(self):
self._request_id += 1
return self._request_id
def login(self):
# login by username and password
data = {
"method": "user.login",
"params": {
"user": self.user,
"password": self.password
}
}
response = self._request(data)
self.auth = response["result"]
self.logged_in = True
def logout(self):
data = {
"method": "user.logout",
"params": []
}
self._request(data)
def __enter__(self):
if not self.auth:
self.login()
return self
def __exit__(self, type, value, traceback):
if self.logged_in:
# logged in by username and password
self.logout()
def get_host_id(self, host_name):
data = {
"method": "host.get",
"params": {
"output": ["hostid"],
"selectGroups": "extend",
"filter": {
"host": [host_name]
}
}
}
response = self._request(data)
try:
return response["result"][0]["hostid"]
except IndexError:
return None
except KeyError:
return None
def maintenance_create(self, host_name, duration_minutes):
duration_seconds = duration_minutes * 60
host_id = self.get_host_id(host_name)
if not host_id:
raise ValueError("Unknown host: " + str(host_name))
dt = datetime.datetime.now()
time_start = int(dt.timestamp())
time_stop = time_start + duration_seconds
maintenance_name = dt.strftime("Automatic {} min (since %Y-%m-%d %H:%M:%S)".format(duration_minutes))
data = {
"method": "maintenance.create",
"params": {
"name": maintenance_name,
"description": "Host: {}".format(host_name),
"active_since": time_start,
"active_till": time_stop,
"hostids": [host_id],
"groupids": [],
"timeperiods": [
{
"period": duration_seconds
}
],
"tags": []
}
}
result = self._request(data)
if "error" in result:
raise ValueError("API error:\n{} {}".format(result["error"].get("message", ""),
result["error"].get("data", "")))
def maintenance_delete_expired(self):
now = int(datetime.datetime.now().timestamp())
data = {
"method": "maintenance.get",
"params": {
"output": "extend",
"selectGroups": "extend",
"selectTimeperiods": "extend",
"selectTags": "extend"
}
}
response = self._request(data)
expired = [elem["maintenanceid"] for elem in response["result"]
if elem["name"].startswith("Automatic ") and int(elem["active_till"]) < now]
data = {
"method": "maintenance.delete",
"params": expired
}
self._request(data)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Sets the host into Zabbix maintenance mode. \
Config of zabbix-cli is used if user, password or URL are undefined.")
parser.add_argument("--url", help="Zabbix API URL")
parser.add_argument("--user", help="API user name")
parser.add_argument("--password", help="API password")
parser.add_argument("minutes", type=int, help="Length of the maintenance window")
args = parser.parse_args()
config = ZabbixCliConfig()
if args.url:
api_url = args.url
elif config.url:
api_url = config.url
else:
print("URL undefined.", file=sys.stderr)
sys.exit(1)
if args.user:
user = args.user
password = args.password
auth = None
elif config.auth:
user = None
password = None
auth = config.auth
else:
user = None
password = None
auth = None
host = socket.gethostname()
with ZabbixSession(url=api_url, user=user, password=password, auth=auth) as zabbix:
try:
zabbix.maintenance_create(host, args.minutes)
zabbix.maintenance_delete_expired()
except ValueError as e:
print(e, file=sys.stderr)
sys.exit(2)