Looking for some guidance on how to get this code to point to the correct inventory within the Zabbix API.
Currently it is pulling all data from Inventory > Hosts > Latest Data.
Basically i'm trying to get this to change, to request the data grab to go to Inventory > Hosts > > Details and then grab the following 'Location latitude' and 'Location longitude'
My first assumption was the application within the def() getInventory was the culprit to change but it seems that even when I change that my output is the same.
If you need any further information please let me know.
import sys
import datetime
import csv
import re
import requests
import tkinter as tk
from tkinter import filedialog
from pyzabbix import ZabbixAPI,ZabbixAPIException
def initializeapi():
tries = 4
while tries >= 0:
user = "XXX"
password = "XXX"
if isinstance(user, str) == True and isinstance(password, str) == True:
try:
z.login(user=user,password=password)
print("Logged into ZabbixAPI version " + z.api_version() + ".")
return True
except ZabbixAPIException as e:
print(e)
tries -= 1
except requests.Timeout as f:
print(f, "\nProgram will now exit.")
sys.exit(2)
else:
print("Username and password must be strings.")
else:
print("Too many failed login attempts.")
return False
def getinventory(listname, hostid='',):
if isinstance(listname, list):
if len(hostid) != 0:
for i in z.item.get(output='extend', hostids=hostid, application='Monitoring'):
j = [i['hostid'], i['itemid'], i['name'], i['lastvalue'], i['units'], i['description'], i["location_lon"]]
listname.append(j)
else:
for i in z.item.get(output='extend', application='Monitoring'):
j = [i['hostid'], i['itemid'], i['name'], i['lastvalue'], i['units'], i['description']]
listname.append(j)
else:
print("Must pass list variable.")
return False
return True
def validateserver(serverip):
if re.search('http://', serverip):
return True
elif re.search('https://', serverip):
return True
else:
return False
def gethostdict(dictname):
if isinstance(dictname, dict):
for h in z.host.get(output="extend"):
dictname[h['hostid']] = h['name']
else:
print("Must pass dict variable.")
return False
return True
def hostchange(listname, dictname):
for index, item in enumerate(listname):
if isinstance(item, list):
hostchange(item, dictname)
elif item in dictname.keys():
listname[index] = dictname[item]
return
def writecsv(writelist):
with open(getfilepath(), 'w', newline='', encoding="utf-8") as result:
writer = csv.writer(result, dialect='excel')
header = ['Host', 'Item ID', 'Name', 'Value', 'Units', 'Description',]
writer.writerow(header)
writer.writerows(writelist)
def getfilepath():
root = tk.Tk()
return filedialog.asksaveasfilename(initialdir=r'XXX', defaultextension='.csv',
initialfile='Inventory ' + str(datetime.date.today()),
filetypes=(("Comma Separated Values",'*.csv'),("All Files", '*.*')))
if __name__ == '__main__':
retries = 4
while retries >= 0:
serverip = "XXX"
if validateserver(serverip):
timeout = 3.5
try:
z = ZabbixAPI(str(serverip), timeout=timeout)
except ZabbixAPIException as e:
print(e)
if initializeapi():
break
elif retries > 0:
retries -= 1
else:
print("Too many failed attempts.")
sys.exit(2)
list1 = []
dict1 = {}
getinventory(list1)
gethostdict(dict1)
hostchange(list1, dict1)
writecsv(list1)
print("Complete.")
Refer this Documentation.. https://www.zabbix.com/documentation/current/en/manual/api/reference/host/object#host-inventory.. below simple python script works for me
Here is the Python Script using username and password