Trying to remove values from a list like I have done previously however I'm running into the above error when running it.
import requests
import json
import pandas as pd
import csv
import numpy
url = 'http://XXX/api_jsonrpc.php'
payload = '{"jsonrpc": "2.0", "method": "event.get", "params": {"output": "extend", "selectAcknowledges": "extend", "selectTags": "extend", "selectSuppressionData": "extend", "selectHosts": ["hostid", "host", "name"], "recent": "true", "sortorder": "DESC"}, "auth": "XXX", "id": 1 }'
headers = {'content-type': 'application/json-rpc'}
r = requests.post(url, data=payload, headers=headers, )
geteventlist = r.json()['result']
print(type(geteventlist))
for i in geteventlist:
i['host'] = i['hosts']['host']
i['hostid'] = i['hosts']['hostid']
i['location'] = i['hosts']['name']
del i['hosts']
file = open('event.csv', 'w+', newline='', encoding="utf_8")
with file:
header = ['hosts', 'eventid', 'userid', 'acknowledged', 'opdata', 'object', 'name', 'suppressed', 'c_eventid', 'clock', 'source', 'objectid', 'severity', 'urls', 'r_eventid', 'value', 'ns', 'suppression_data', 'correlationid', 'tags']
writer = csv.DictWriter(file, fieldnames = header)
writer.writeheader()
writer.writerows(geteventlist)
This is out putting the following:
TypeError: list indices must be integers or slices, not str
The actual data that should be outputted is the following:
[{'hostid': '10519', 'proxy_hostid': '0', 'host': 'XXX', 'status': '0', 'lastaccess': '0', 'ipmi_authtype': '-1', 'ipmi_privilege': '2', 'ipmi_username': '', 'ipmi_password': '', 'maintenanceid': '0', 'maintenance_status': '0', 'maintenance_type': '0', 'maintenance_from': '0', 'name': 'XXX', 'flags': '0', 'templateid': '0', 'description': '', 'tls_connect': '1', 'tls_accept': '1', 'tls_issuer': '', 'tls_subject': '', 'proxy_address': '', 'auto_compress': '1', 'custom_interfaces': '0', 'uuid': '', 'inventory_mode': '1'}]
I understand that it is coming from a list, however I previously was able to specify the dict in order to byapss this but now I cant get around this error.
Thanks in advance for the help.
I have just tried to update this with further reading by using a range(len()) addition like the following:
for i in range(len(geteventlist)):
i['host'] = i['hosts']['host']
i['hostid'] = i['hosts']['hostid']
i['location'] = i['hosts']['name']
del i['hosts']
However this also produces an error of - "TypeError: 'int' object is not subscriptable"
and if I were to remove the len() from the range() section:
for i in range(geteventlist):
i['host'] = i['hosts']['host']
i['hostid'] = i['hosts']['hostid']
i['location'] = i['hosts']['name']
del i['hosts']
I get error - "TypeError: 'list' object cannot be interpreted as an integer"