-2

Can't seem to find the correct syntax to get the request I need.

At this point I am able to connect to my Zabbix server via an API. I'm able to see all the data that I need which is confirmed by a simple print() function.

However, I am blanking at the way to get the data from the variable into a csv file.

from pyzabbix import ZabbiXAPI

        zapi = ZabbixAPI("example")
        zapi.login("example", "example")
    
    for hostInventory in zapi.host.get():
        print(hostInventory)

This then spits out about 500 lines of this approx - obviously redacted sensitive information:

{'hostid': 'xxx', 'proxy_hostid': 'xxx', 'host': 'xxx', 'status': 'x', 'lastaccess': 'x', 'ipmi_authtype': 'x', 'ipmi_privilege': 'x', 'ipmi_username': 'x', 'ipmi_password': 'x', 'maintenanceid': 'x', 
'maintenance_status': 'x', 'maintenance_type': 'x', 'maintenance_from': 'x', 'name': 'x', 'flags': 'x', 'templateid': 'x', 'description': 'x', 'tls_connect': 'x', 'tls_accept': 'x', 'tls_issuer': 'x', 'tls_subject': 'x', 'proxy_address': 'x', 'auto_compress': 'x', 'custom_interfaces': 'x', 'uuid': 'x', 'inventory_mode': 'x'}

If I need to provide any further information please let me know. First time poster.

Lachlan
  • 17
  • 1
  • 6
  • you have to learn standard module [csv](https://docs.python.org/3/library/csv.html) or popular `pandas` - and this problem has nothing to do directly with Zabbix. – furas Apr 04 '22 at 05:06
  • @furas I understand its not a problem with zabbix itself, however i'm more looking for the solution or the missing code that will allow me to write the data once i have it. Everything that I seem to have tried doesnt seem to be working. – Lachlan Apr 04 '22 at 05:11
  • if you found something and it doesn't work then you should show it in question - so we wouldn't have to waste time for methods which don't work for you. OR maybe we could fix these methods - if we know what error they gives. – furas Apr 04 '22 at 05:12
  • I do apologise, i can only give a rundown of what I have tried as I have scrubbed it everytime the outcome either resulted in syntax errors or doesn't get me what I needed. I can edit the main post with this information if you think that would be helpful – Lachlan Apr 04 '22 at 05:19

1 Answers1

0

You have to learn standard module csv or popular pandas
(and this problem has nothing to do directly with Zabbix).

With pandas it should be much simpler.

import pandas as pd

data = list( zapi.host.get() )
df = pd.DataFrame(data)

df.to_csv('output.csv')

Eventually you may have to use own code to create dictionaries

data = []

for hostInventory in zapi.host.get():
    item = {"hostid": hostInventory.hostid, ...}
    data.append(item)
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks for that Furas, seem to have run into an issue where it tells me that the object of type set is not JSON serializable. I have tried using JSONpickle to encode and decode this however the end result always ends in the same output. – Lachlan Apr 04 '22 at 05:16
  • so you may have to create own code to convert it to normal dictionary with strings: `{"hostid": hostInventory.hostid, ...}` and this will need to use `for`-loop to create list with all dictionares. – furas Apr 04 '22 at 05:43