-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzabbix_api_host_create.py
More file actions
131 lines (120 loc) · 3.3 KB
/
Copy pathzabbix_api_host_create.py
File metadata and controls
131 lines (120 loc) · 3.3 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import requests
import json
import time
try:
hostname = sys.argv[1]
host_ip = sys.argv[2]
host_group_id = sys.argv[3]
template_id = sys.argv[4]
zabbix_username = sys.argv[5]
zabbix_password = sys.argv[6]
url = sys.argv[7]
except BaseException:
print('[hostname] [host_ip] [host_group_id] [template_id]' +
' [zabbix_username] [zabbix_password] [auth_url]')
sys.exit()
headers = {'content-type': 'application/json'}
def get_auth_key():
payload = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": zabbix_username,
"password": zabbix_password
},
"id": 1,
}
r = requests.post(url, data=json.dumps(payload), headers=headers)
if r.status_code != 200:
print('problem - get key')
print(r.status_code)
print(r.text)
sys.exit()
else:
result = r.json()
auth_key = result['result']
return auth_key
def create_host(auth_key):
payload = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host":
hostname,
"interfaces": [{
"type": 1,
"main": 1,
"useip": 1,
"ip": host_ip,
"dns": "",
"port": "10050"
}],
"groups": [{
"groupid": host_group_id
}],
"templates": [{
"templateid": template_id
}]
},
"auth": auth_key,
"id": 1
}
r = requests.post(url, data=json.dumps(payload), headers=headers)
if r.status_code != 200:
print('problem - host create')
sys.exit()
else:
try:
result = r.json()['result']
host_id = result['hostids'][0]
return host_id
except BaseException:
result = r.json()['error']
print('problem - host create response')
print(result)
sys.exit()
def set_maintenance(auth_key, host_id):
active_since = int(time.time())
active_till = int(time.time() + 600)
payload = {
"jsonrpc": "2.0",
"method": "maintenance.create",
"params": {
"name":
'new server initialization period_' + str(active_till),
"active_since":
active_since,
"active_till":
active_till,
"hostids": [host_id],
"timeperiods": [{
"timeperiod_type": 0,
"start_time": active_since,
"period": 1800
}]
},
"auth": auth_key,
"id": 1
}
r = requests.post(url, data=json.dumps(payload), headers=headers)
if r.status_code != 200:
print('problem - set maintenance')
sys.exit()
else:
try:
result = r.json()['result']
print(result)
except BaseException:
result = r.json()['error']
print('problem - set maintenance response')
print(result)
sys.exit()
if __name__ == "__main__":
os.chdir(sys.path[0])
auth_key = get_auth_key()
host_id = create_host(auth_key)
set_maintenance(auth_key, host_id)