I manage this with a combination of Python and the NagiosXI API (v1). This is by no means the complete source or meant to be a production solution.
I have a function that's called NagiosXI Generic API that submits the API request and returns the JSON results. If you follow information in the help about the API you'll get the basics of what's being done
This function takes 6 arguments and spits back the results.
The rest of the code will be a reference to this function.
Code: Select all
##NAGIOSXI GENERIC API CALL
def nagiosxiGenericAPI(resource,endpoint,modifier,method,myurl,mykey):
#AND THEN A MIRICAL HAPPENS
# ~tobtieker
url = ("https://{turl}/nagiosxi/api/v1/{resource}/{endpoint}?{modifier}&apikey={akey}".format(turl=myurl,akey=mykey,resource=resource,endpoint=endpoint,modifier=modifier))
#GET
if method == "get":
try:
r = requests.get(url=url,verify=False)
except Exception as e:
print.error("%s",e)
r = False
elif method == "post":
try:
r = requests.post(url=url,verify=False)
except Exception as e:
print.error("%s",e)
r = False
elif method == "put":
try:
r = requests.put(url=url,verify=False)
except Exception as e:
print.error("%s",e)
r = False
elif method == "delete":
try:
r = requests.delete(url=url,verify=False)
except Exception as e:
print.error("%s",e)
r = False
else:
print.error("%s","NO METHOD PROVIDED")
r = False
return r
I have logic that retrieves my user API key and associates it to mykey
I have logic that inserts my server URL as myurl.
I have logic that will parse the results and create a csv of the JSON results.
--
First I pull the current downtime list using no query modifier passed to the function
resource = objects
endpoint = downtime
method = get
modifier = none
mykey
myurl
This will spit back the record count and a list of all downtime entries.
Next, In my case I create a CSV file and write the results into the file.
I should mention that if you are parsing this list that it's one of the few times that there is a mismatch in the endpoint and the data returned.
Code: Select all
#COUNT IT
ic = 0
#API RESULTS
items = r.json()
#MAKE A CSV
mycsv = open('yourfile.csv','w',newline='',encoding='utf-8')
csv_writer = csv.writer(mycsv)
#WRITE OUR DOWNTIME RESULTS INTO THE CSV
for i in item['scheduleddowntime']:
if ic == 0:
header = i.keys()
csv_writer.writerow(header)
ic+=1
else:
line = i.values()
csv_writer.writerow(line)
ic+=1
In the CSV you'll get everything that's available including the internal_id of each entry which is what we need to move to the next step of deleting the downtime using the API.
To delete a downtime is really simple we just send a DELETE with the internal_id to the SYSTEM/SCHEDULEDDOWNTIME enpoint.
Code: Select all
#Using the nagiosXIGenericAPI function
resource = system
endpoint = scheduleddowntime
method = delete
modifier = <your internal_id>
myurl
mykey
d = nagiosXIGenericAPI(resource,endpoint,modifier,myurl,mykey)
print(d)
To delete a lot of them, make a list and loop through it making a call to delete them.
This is what I'm doing and similar to how I'm bulk managing host/service comments.
--SN