-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzabbix.py
153 lines (139 loc) · 4.11 KB
/
zabbix.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
from configparser import ConfigParser
import requests
import json
""" credenciais de acesso ao zabbix no arquivo 'config.ini'"""
"""
[auth]
url = http://<ip_do_zabbix>/zabbix/api_jsonrpc.php
user = <user_do_zabbix>
password = <senha_do_zabbix>
"""
config = ConfigParser()
config.read('./config.ini')
url = config.get('auth', 'url')
user = config.get('auth', 'user')
password = config.get('auth', 'password')
def login(tipo_de_login):
if tipo_de_login == 1:
try:
r = requests.post(url, json={
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": user,
"password": password},
"id": 1
})
AUTHTOKEN = r.json()["result"]
print(f"\nLogado com Usuario e Senha\n")
print(f"\n>>>>>>>>>> Login: {AUTHTOKEN}\n")
return AUTHTOKEN
except Exception:
exit(f"Nao consegui logar")
else:
token = config.get('auth', 'token')
print(f"\nLogado via API Token\n")
print(f"\n>>>>>>>>>> Login: {token}\n")
return token
def criarGrupo(AUTHTOKEN, nome_grupo):
try:
r = requests.post(url, json={
"jsonrpc": "2.0",
"method": "hostgroup.create",
"params": {
"name": nome_grupo
},
"auth": AUTHTOKEN,
"id": 2
})
res = r.json()
return res
except Exception:
print(f"\nerror\n")
def getGroups(AUTHTOKEN):
try:
r = requests.post(url, json={
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"status": 0
},
"auth": AUTHTOKEN,
"id": 1
})
res = r.json()
return res
except Exception:
print(f"\nerror\n")
def getTemplates(AUTHTOKEN):
try:
r = requests.post(url, json={
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": []
}
},
"auth": AUTHTOKEN,
"id": 1
})
res = r.json()
return res
except Exception:
print(f"\nerror\n")
def criarHosts(AUTHTOKEN, nome, dns, tipo_interface, port_interface, templates, id_grupo):
# print(AUTHTOKEN, nome, ip, snmp_c, tipo_interface, port_interface, templates, id_grupo)
try:
r = requests.post(url, json={
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": nome,
"interfaces": [
{
"type": tipo_interface,
"main": 1,
"useip": 0,
"ip": "",
"dns": dns,
"port": port_interface,
}
],
"groups": [
{
"groupid": id_grupo
}
],
"templates": templates,
},
"auth": AUTHTOKEN,
"id": 5
})
res = r.json()
if 'error' in res:
print(
f'Host {nome} => { json.dumps(r.json()["error"]["data"]) }')
else:
print(
f'Host "{nome}" => Criado com o ID { json.dumps(r.json()["result"]["hostids"]) }')
except Exception:
print(f"\nerror\n")
def logout(AUTHTOKEN, tipo_de_login):
if tipo_de_login == 1:
try:
r = requests.post(url, json={
"jsonrpc": "2.0",
"params": {},
"method": "user.logout",
"id": 100,
"auth": AUTHTOKEN
})
print(f"\n>>>>>>>>>> Logout: {json.dumps(r.json()['result'])}\n")
# print(f"Fim da aplicação\n")
except Exception as err:
print(f"\nlogout: ", err)
# else:
# print(f"\nFim da aplicação\n")