-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZabbix_host_export.py
50 lines (40 loc) · 1.56 KB
/
Zabbix_host_export.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
#!/usr/bin/env python3
import pandas as pd
from pyzabbix import ZabbixAPI
import dotenv
config = dotenv.dotenv_values()
# Connect to Zabbix
try:
zabbix = ZabbixAPI(config['ZABBIX_URL'])
zabbix.login(api_token=config['ZABBIX_API_TOKEN'])
print(f"ZABBIX_API_TOKEN: {config.get('ZABBIX_API_TOKEN')}")
print("Successfully connected to Zabbix.")
except Exception as e:
print(f"Connection failed: {e}")
exit()
# Fetch hosts
try:
hosts = zabbix.host.get(
output=["hostid", "host", "name", "status"],
selectInterfaces=["ip", "port"] # Fetching network interface information including port
)
print(f"Fetched {len(hosts)} hosts.")
# Prepare data for export
host_data = []
for host in hosts:
for interface in host.get('interfaces', []): # Loop over interfaces if there are multiple
host_data.append({
"Host ID": host["hostid"],
"Host": host["host"],
"Name": host["name"],
"Status": host["status"],
"IP Address": interface.get("ip", "N/A"), # Fallback if no IP is available
"Port": interface.get("port", "N/A") # Fallback if no port is available
})
# Convert the data into a DataFrame
df = pd.DataFrame(host_data)
# Export to Excel
df.to_excel('zabbix_hosts_inventory_export_demo.xlsx', index=False)
print("Hosts and inventory details exported successfully to 'zabbix_hosts_inventory_export_demo.xlsx'.")
except Exception as e:
print(f"Failed to fetch hosts: {e}")