testes/app/routes/newzabbix.py

49 lines
2.1 KiB
Python

from pyzabbix import ZabbixAPI
# Connect to the Zabbix server
zapi = ZabbixAPI("https://mimir.itguys.com.br/zabbix/api_jsonrpc.php")
# Login to the Zabbix API
zapi.login(token="10925e9e452cade5270f4d930feb3b5e1aa2cc9b366c1c5b4d72996212b12c93")
# Retrieve a list of host groups (not hosts themselves)
groups = zapi.hostgroup.get(output="extend")
# Print group information and metrics
for group in groups:
print(f"Group ID: {group['groupid']}, Group Name: {group['name']}")
# Retrieve hosts in this group
hosts = zapi.host.get(groupids=group['groupid'], output="extend")
for host in hosts:
print(f" Host Name: {host['name']}")
# Retrieve items (metrics) for each host
items = zapi.item.get(hostids=host['hostid'], output="extend",
filter={"key_": ["system.uptime", "net.if.in", "net.if.out", "vm.memory.size", "vfs.fs.size"]})
# Print metrics for each host
for item in items:
# Check which metric it corresponds to and print it
if item['key_'] == "system.uptime":
print(f" Uptime: {item['lastvalue']} seconds")
elif item['key_'] == "net.if.in":
print(f" Network In: {item['lastvalue']} bytes")
elif item['key_'] == "net.if.out":
print(f" Network Out: {item['lastvalue']} bytes")
elif item['key_'] == "vm.memory.size":
print(f" Memory Usage: {item['lastvalue']} bytes")
elif item['key_'] == "vfs.fs.size":
print(f" Disk Usage: {item['lastvalue']} bytes")
# Retrieve the network packet loss (example metric)
# This can vary depending on your Zabbix configuration for ping or similar metrics
network_packet_loss = zapi.item.get(hostids=host['hostid'],
filter={"key_": "net.ping.loss"}, output="extend")
for loss in network_packet_loss:
print(f" Packet Loss: {loss['lastvalue']} %")
# Logout from the Zabbix API
zapi.user.logout()