Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (52 sloc) 2.3 KB
#!/usr/bin/env python
# Autor: Waltenne Carvalho
# Github: https://github.com/waltenne
# -*- coding: utf-8 -*-
import re
from pyzabbix import ZabbixAPI
from requests import get
# Zabbix Server Connection Data
url = 'http://IPZABBIXSERVER/zabbix/api_jsonrpc.php'
username = 'USERNAME'
password = 'PASSWORD'
try:
zapi = ZabbixAPI(url, timeout=4)
zapi.login(username, password)
except Exception as err:
print('Failed to connect to Zabbix API')
print('Error: {}'.format(err))
# Searching for the Hostname defined within the zabbix_agentd.conf
file = open("/etc/zabbix/zabbix_agentd.conf").readlines()
for lines in file:
if 'Hostname=' in lines:
host_name1 = lines.split(":")[-1].strip()
host_name2 = re.findall(r"=(.*)",(host_name1))
host_name = (host_name2[0])
# With Hostname Found, I connect to the Zabbix API to collect Hostid.
req_host_get = zapi.host.get(filter={"host": host_name})
host_id = (req_host_get[0]['hostid'])
# With Hostname Found, I connect to the Zabbix API to collect Hostid.
req_host_get = zapi.host.get(filter={"host": host_name})
host_id = (req_host_get[0]['hostid'])
# With Hostid Found, I connect to the Zabbix API to collect IP Address from the host
req_interface_get = zapi.hostinterface.get(filter={"hostid": host_id})
ip_hostzabbix_srv = (req_interface_get[0]['ip'])
# With Hostid Found, I connect to the Zabbix API to collect interfaceid from host
req_interface_id = zapi.hostinterface.get(filter={"hostid": host_id})
interfaceid = (req_interface_id[0]['interfaceid'])
# Now I collect or public ip from the local machine
ip_public = get('https://api.ipify.org').text
# Now I Compare Current Host IP Address on Zabbix Server to Current Public IP Address
# If this is different, I update the host's IP address with Zabbix with the public IP address
if ip_hostzabbix_srv != ip_public:
req_interfaceid_upd = zapi.hostinterface.update({"interfaceid": interfaceid,"ip": ip_public})
print("This Hostname " + host_name + " This HostID " + host_id)
print("IP address On Zabbix Server " + ip_hostzabbix_srv)
print("IP address Public " + ip_public)
print("IP address are not the same, Updated IP Address")
else:
print("IP Address equal")
print("This Hostname " + host_name + " This HostID " + host_id)
print("IP address On Zabbix Server " + ip_hostzabbix_srv)
print("IP address Public " + ip_public)
# The End
You can’t perform that action at this time.