-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
254 lines (205 loc) · 9.02 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import json
import os
import requests
import argparse
import pandas as pd
from tabulate import tabulate
from utils import zabbix_url
help_command = [["help", "Show all commands"], ["groups", "Show all groups and ID"], ["hosts", "Show all hosts and ID"],
["templates", "Show all templates and ID"], ["template", "Show ID one template"],
["proxy groups", "Show all proxy groups"], ["proxies", "Show all proxies"],
["import hosts", "Import hosts from file to Zabbix server"], ["author", "About author"],
["exit", "Close script"]]
parser = argparse.ArgumentParser(description="Zabbix import host | More info: https://github.com/Udeus/zabbix-import-hosts/")
parser.add_argument("--url", type=str, help="Zabbix url address")
parser.add_argument("--token", type=str, help="API token")
args = parser.parse_args()
try:
terminal_width = os.get_terminal_size().columns
except OSError:
terminal_width = 80
if args.url:
api_url = zabbix_url(args.url)
else:
api_url = zabbix_url(input("Zabbix url address: "))
if args.token:
api_token = args.token
else:
api_token = input("Zabbix API token: ")
print(tabulate(help_command, headers=["Command", "Description"], tablefmt="psql"))
print("How to use: https://github.com/Udeus/zabbix-import-hosts")
def connect_api(api_date):
request_header = {'Authorization': 'Bearer ' + api_token, 'Content-Type': 'application/json-rpc'}
response = requests.post(api_url, data=api_date, headers=request_header)
response = response.json()["result"]
return response
def get_groups():
api_date = '{"jsonrpc": "2.0","method": "hostgroup.get","params": {"output": ["name", "groupid"]},"id": 1}'
response = connect_api(api_date)
print(tabulate(response, headers="keys", tablefmt="psql"))
def get_hosts_list():
api_date = '{"jsonrpc": "2.0","method": "host.get","params": {"output": ["name", "groupid"]},"id": 1}'
response = connect_api(api_date)
print(tabulate(response, headers="keys", tablefmt="psql"))
def get_templates():
api_date = '{"jsonrpc": "2.0","method": "template.get","params": {"output": ["name", "groupid"]},"id": 1}'
response = connect_api(api_date)
print(tabulate(response, headers="keys", tablefmt="psql"))
def get_template():
template_name = input("Template name: ")
api_date = f'{{"jsonrpc": "2.0","method": "template.get","params": {{"output": ["name", "groupid"],"filter": {{"host": ["{template_name}"]}}}},"id": 1}}'
response = connect_api(api_date)
if response:
print(tabulate(response, headers="keys", tablefmt="psql"))
else:
print("Template not found")
def get_proxies_groups():
api_date = '{"jsonrpc": "2.0","method": "proxygroup.get","params": {"output": ["name", "proxy_groupid"]},"id": 1}'
response = connect_api(api_date)
if response:
print(tabulate(response, headers="keys", tablefmt="psql"))
else:
print("Proxy group not found")
def get_proxies():
api_date = '{"jsonrpc": "2.0","method": "proxy.get","params": {"output": ["name", "proxyid", "address"]},"id": 1}'
response = connect_api(api_date)
if response:
print(tabulate(response, headers="keys", tablefmt="psql"))
else:
print("Proxy not found")
def import_hosts():
file_name = input("File to import: ")
file = pd.read_excel(file_name)
for index, row in file.iterrows():
file_hostname = row["Hostname*"]
file_address_ip = row["IP address"]
file_dns = row["DNS"]
file_interface = row["Interface"]
file_port = row["Port"]
groups = str(row["Group ID*"])
file_template_id = row["Template ID"]
file_proxy_id = row["Proxy ID"]
useip = 1
interface_id = 1
port = 10050
interface_detals = ""
# Required hostname and group
if pd.isna(file_hostname) or pd.isna(groups):
break
else:
groups = groups.split(';')
groups = [{"groupid": num.strip()} for num in groups]
groups = json.dumps(groups)
# Check adress ip/dns
if pd.isna(file_address_ip):
file_address_ip = ""
if pd.isna(file_dns):
file_dns = ""
if file_dns and not file_address_ip:
useip = 0
if file_interface == "Agent" or pd.isna(file_interface):
interface_id = "1"
if pd.isna(file_port):
port = "10050"
else:
port = round(file_port)
elif file_interface == "SNMPv1" or file_interface == "SNMPv2" or file_interface == "SNMPv3":
interface_id = "2"
if file_interface == "SNMPv1":
interface_detals = ', "details": {"version": 1, "community": "{$SNMP_COMMUNITY}"}'
elif file_interface == "SNMPv2":
interface_detals = ', "details": {"version": 2, "community": "{$SNMP_COMMUNITY}"}'
elif file_interface == "SNMPv3":
interface_detals = ', "details": {"version": 3}'
if pd.isna(file_port):
port = "161"
else:
port = round(file_port)
elif file_interface == "JMX":
interface_id = "4"
if pd.isna(file_port):
port = "12345"
else:
port = round(file_port)
elif file_interface == "IPMI":
interface_id = "3"
if pd.isna(file_port):
port = "623"
else:
port = round(file_port)
if file_address_ip == "" and file_dns == "":
interface = ''
else:
interface = f',"interfaces": [{{"type": "{interface_id}","main": 1,"useip": "{useip}","ip": "{file_address_ip}","dns": "{file_dns}","port": "{port}" {interface_detals}}}]'
if pd.isna(file_template_id):
template = ""
else:
template = f',"templates": [{{"templateid": "{round(file_template_id)}"}}]'
if pd.isna(file_proxy_id):
proxy_id = ""
else:
proxy_id = f', "proxyid": "{round(file_proxy_id)}"'
api_data = f'{{"jsonrpc": "2.0","method": "host.create","params": {{"host": "{file_hostname}"{proxy_id}{interface},"groups": {groups}{template}}},"id": 1}}'
request_header = {'Authorization': 'Bearer ' + api_token, 'Content-Type': 'application/json-rpc'}
response = requests.post(api_url, data=api_data, headers=request_header)
response = response.json()
result = response.get("result", {}).get("hostids")
error = response.get("error", {}).get("data")
print('-' * terminal_width)
print("Hostname:", file_hostname)
if result is not None:
print("New host ID:", result)
elif error is not None:
print("Error message:", error)
else:
print("Error")
if api_token and api_url:
while True:
# Check URL API
try:
data = '{"jsonrpc":"2.0","method":"apiinfo.version","params":{},"id":1}'
header = {'Content-Type': 'application/json-rpc'}
response_api = requests.post(api_url, data=data, headers=header)
response_api.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error API: {e}")
break
except Exception as e:
print(f"Error: {e}")
break
# Check API Token
try:
data = '{"jsonrpc": "2.0","method": "token.get","params": {"output": "extend"},"id": 1}'
header = {'Authorization': 'Bearer ' + api_token, 'Content-Type': 'application/json-rpc'}
response_api = requests.post(api_url, data=data, headers=header)
response_api = response_api.json()["result"]
command = input("Command: ")
except Exception:
print("Error API: Correct your token")
break
# Commands
if command == "help":
print(tabulate(help_command, headers=["Command", "Description"], tablefmt="psql"))
print("How to use: https://github.com/Udeus/zabbix-import-hosts")
elif command == "groups":
get_groups()
elif command == "hosts":
get_hosts_list()
elif command == "templates":
get_templates()
elif command == "template":
get_template()
elif command == "proxy groups":
get_proxies_groups()
elif command == "proxies":
get_proxies()
elif command == "import hosts":
import_hosts()
elif command == "author":
print("Created by Andrzej Pietryga")
print("Github: https://github.com/Udeus/")
elif command == "exit":
print("Closing the script...")
break
else:
print("Command not found")