-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
132 lines (111 loc) · 3.86 KB
/
main.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
import requests
import json
import sys
from requests.auth import HTTPBasicAuth
import datetime
import time
if ( len(sys.argv) < 7 ):
print '[hostname] [host_ip] [host_group_id] [zabbix_username] [zabbix_password] [http_auth_username] [http_auth_password] [templeate_id]'
sys.exit(1)
else:
hostname=sys.argv[1]
host_ip=sys.argv[2]
host_group_id=sys.argv[3]
zabbix_username=sys.argv[4]
zabbix_password=sys.argv[5]
http_auth_username=sys.argv[6]
http_auth_password=sys.argv[7]
templeate_id=sys.argv[8]
url=sys.argv[9]
headers = {'content-type': 'application/json'}
def get_aut_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, verify=False, auth=HTTPBasicAuth(http_auth_username,http_auth_password))
if r.status_code != 200:
print 'problem -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": templeate_id
}
],
},
"auth": auth_key,
"id": 1
}
r = requests.post(url, data=json.dumps(payload), headers=headers, verify=False, auth=HTTPBasicAuth(http_auth_username,http_auth_password))
if r.status_code != 200:
print 'problem -request'
sys.exit()
else:
try:
result=r.json()['result']
host_id=result['hostids'][0]
return host_id
except:
result=r.json()['error']
print 'error'
print result
sys.exit()
def set_maintenance(auth_key, host_id):
active_till=int(time.time()+600)
payload={
"jsonrpc": "2.0",
"method": "maintenance.create",
"params": {
"name": 'new server initialization period_'+str(active_till),
"hostids": [ host_id ],
"active_till": active_till,
"timeperiods": [
{
"timeperiod_type": 0,
"period": 1800
}
]
},
"auth": auth_key,
"id": 1
}
r = requests.post(url, data=json.dumps(payload), headers=headers, verify=False, auth=HTTPBasicAuth(http_auth_username,http_auth_password))
if r.status_code != 200:
print 'problem -request'
sys.exit()
else:
try:
result=r.json()['result']
print result
except:
result=r.json()['error']
print 'error'
print result
sys.exit()
auth_key=get_aut_key()
host_id=create_host(auth_key)
set_maintenance(auth_key,host_id)