Skip to content

Instantly share code, notes, and snippets.

@q1x
Created June 18, 2015 15:11
Show Gist options
  • Save q1x/af166607cae881a26df0 to your computer and use it in GitHub Desktop.
Save q1x/af166607cae881a26df0 to your computer and use it in GitHub Desktop.
import sys
if not ('zabbix-tools-python-modules.zip' in sys.path):
sys.path.insert(0, 'packages.zip')
import getpass
from pyzabbix import ZabbixAPI
u = raw_input("Username: ")
p = getpass.getpass()
print "Connecting to zabbix"
zapi = ZabbixAPI("http://127.0.0.1/zabbix")
zapi.login(u, p)
print "Retrieving hosts"
hosts = zapi.host.get(**{"output" : "extend",
"selectInventory" : "extend",
"withInventory" : True,
"monitored_hosts" : True,
})
print "Updating hosts"
from pygeocoder import Geocoder
def resolve_location(address, zipcode, city):
searchstr = (city + ", " + address + ", " + zipcode).replace('.', '')
try:
geo = Geocoder.geocode(searchstr)
return geo
except:
print 'Err!'
pass
return None
for host in hosts:
if (host['inventory']['site_zip'] and
host['inventory']['site_address_a'] and
host['inventory']['site_city']):
if not host['inventory']['location_lat']:
obj=[host['inventory']['site_address_a'],
host['inventory']['site_zip'],
host['inventory']['site_city']]
print obj
geodata=resolve_location(*obj)
if geodata is None:
print host['host'] + ": no coordinates found"
continue
else:
lat, lon = geodata.coordinates
result = zapi.host.update(**{'hostid' : host['hostid'],
'inventory' : { 'location_lon' : lon, 'location_lat' : lat}
})
print result
else:
pass
print "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment