I am trying to create a plugin (in python) for nagios that would fetch a json file from a remote server and then according to the contents of that file will generate alerts.
The specific file is actually alerts generated from a separate monitoring system hence my goal is to generate nagios notifications by polling the alerts form the other system.
Assuming the json file could contains, lets say three alerts, i would like my script to generate three different alert entries.
So far i have a loop in my script that checks if the script has at least one alert and if so, it will print the alertname and exit with status code of two ( 2 ) so it can indicate a CRITICAL to nagios.
data = urllib2.urlopen("http://127.0.0.1:880").read()
alerts = json.loads(data)
if len(alerts["data"]) == 0:
exit(0)
else:
print "Alerts: "
for alert in alerts["data"]:
print " " + alert["labels"]["alertname"]
exit(2)
Is it possible to generate multiple alerts on nagios using a single script?
I mean, if you can only have a single exit code how could you generate multiple different alerts?
Nagios service check will generate single alert only. As you know, you can control this by the exit code in addition to the printed text message.
If you want to use a single script to generate multiple different alerts, you can modify your script to take an additional parameter to specify the alert name / ID or whatever field you can use to match your alert. Then, you can define multiple services to use the same check script/command but with different parameters.
You cannot generate multiple alerts/notifications using a single active service check.
But: you can fake this with a passive check set to volatile. That way, each non-OK check result you submit would generate an alert.
Then you'd use NSCA/NRDP/equivalent to have your script submit the passive results, one at a time.