You can use the nagios API to query the list of contacts for a particular service if you're using 4.0.7 or higher. See
https://labs.nagios.com/2014/06/19/expl ... -7-part-1/ for details. You're going to hate me for this, but once you master this sort of thing below, then all the configuration information within Nagios is available to you at the command line as well.
Here is an example to grab the JSON data that shows the contacts for a service:
Code: Select all
http://NAGIOSSERVER/nagios/cgi-bin/objectjson.cgi?query=service&hostname=SOMEHOST&servicedescription=SOMESERVICE
This will return JSON data that looks like this the big list I've posted at the bottom. You could then parse this in your event handler to extract the line after "contacts" (in bash, that would be: contacts=`echo $JSON_OUTPUT | egrep -A 1 "\"contacts\""`), and then use the same APIs to go get each contact's email address with:
Code: Select all
http://NAGIOSSERVER/nagios/cgi-bin/objectjson.cgi?query=contact&contactname=SOMECONTACT
Then grab the "email" field, which would be an email addres and use it in your event handler to send email.
Code: Select all
http://NAGIOSSERVER/nagios/cgi-bin/objectjson.cgi?query=service&hostname=SOMEHOST&servicedescription=SOMESERVICE
{
"format_version": 0,
"result": {
"query_time": 1458734327000,
"cgi": "objectjson.cgi",
"user": "nagiosadmin",
"query": "service",
"query_status": "released",
"program_start": 1458643403000,
"last_data_update": 1458643403000,
"type_code": 0,
"type_text": "Success",
"message": ""
},
"data": {
"service": {
"host_name": "SOMEHOST",
"description": "SOMESERVICE",
"display_name": "SOMESERVICE",
"check_command": "check_nrpe!check_init_service!httpd",
"event_handler": "",
"initial_state": 0,
"check_interval": 5,
"retry_interval": 1,
"max_attempts": 4,
"parallelize": true,
"contact_groups": [
"voip-support"
],
"contacts": [
"SOMECONTACT"
],
"notification_interval": 60,
"first_notification_delay": 0,
"notifications_options": 15,
"stalking_options": 0,
"is_volatile": false,
"notification_period": "24x7",
"check_period": "24x7",
"flap_detection_enabled": true,
"low_flap_threshold": 0,
"high_flap_threshold": 0,
"flap_detection_options": -1,
"process_performance_data": true,
"check_freshness": false,
"freshness_threshold": 0,
"accept_passive_checks": true,
"event_handler_enabled": true,
"checks_enabled": true,
"retain_status_information": true,
"retain_nonstatus_information": true,
"notifications_enabled": true,
"obsess": true,
"hourly_value": 0,
"parents": [
],
"children": [
],
"notes": "",
"notes_url": "",
"action_url": "",
"icon_image": "",
"icon_image_alt": "",
"custom_variables": {
}
}
}
}