Nagios Event Handler for py-phone-caller¶
This directory contains a Bash script designed to be used as a Nagios Core event handler. It triggers a phone call through the asterisk_caller API when a monitored service reaches a confirmed CRITICAL / HARD state.
Files¶
nagios_event_handler_call.sh: Event handler script that posts toasterisk_caller.
Prerequisites¶
bashandcurlavailable on the Nagios server.asterisk_callerreachable from the Nagios server.- A callable API endpoint, normally
http://py-phone-caller.lan:8081/call_to_queue.
Installation¶
- Copy
nagios_event_handler_call.shto your Nagios plugins or scripts directory, for example/usr/local/nagios/libexec/or/usr/local/nagios/etc/objects/scripts/. -
Make the script executable:
-
Configure the target API URL with
PY_PHONE_CALLER_API_BASE_URLor pass it as argument 7 from the Nagios command definition. Optionally setPY_PHONE_CALLER_TARGET_PHONEor pass the target as argument 8.
Configuration values¶
| Name | Required | Default | Description |
|---|---|---|---|
PY_PHONE_CALLER_API_BASE_URL |
Yes, unless argument 7 is passed | none | Full asterisk_caller queue endpoint, for example http://py-phone-caller.lan:8081/call_to_queue. |
PY_PHONE_CALLER_TARGET_PHONE |
No | oncall |
Destination phone number or the address-book oncall alias. |
PY_PHONE_CALLER_CURL_TIMEOUT |
No | 10 |
Maximum request time in seconds. Keep this low so Nagios workers do not hang. |
Do not include phone or message in PY_PHONE_CALLER_API_BASE_URL. The script appends those query parameters with curl --data-urlencode and sends the request as POST, which matches the current asterisk_caller API.
Nagios configuration¶
1. Define the command¶
Add the following command definition to your Nagios configuration, for example commands.cfg:
define command {
command_name notify-by-phone-call
command_line /path/to/nagios_event_handler_call.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ "$HOSTNAME$" "$SERVICEDESC$" "$SERVICEOUTPUT$" "http://py-phone-caller.lan:8081/call_to_queue" "oncall"
}
Alternatively, export the values in the Nagios service environment if your Nagios packaging preserves custom environment variables:
export PY_PHONE_CALLER_API_BASE_URL="http://py-phone-caller.lan:8081/call_to_queue"
export PY_PHONE_CALLER_TARGET_PHONE="oncall"
export PY_PHONE_CALLER_CURL_TIMEOUT="10"
2. Enable event handler for a service¶
Add the event_handler directive to the service definition you want to monitor:
define service {
host_name myserver
service_description CPU Load
check_command check_load
event_handler notify-by-phone-call
event_handler_enabled 1
...
}
Runtime behavior¶
When the service CPU Load on myserver enters a CRITICAL state and the state type is HARD, the script will:
- Construct a message:
Alert. Host myserver. Service CPU Load is CRITICAL. <Check Output>. - Send a
POSTrequest to the configured endpoint. - Add URL-encoded
phoneandmessagequery parameters usingcurl.
The script intentionally ignores SOFT states and OK recovery states by default to avoid alert noise. Recovery-call examples are left commented in the script if you want to enable them later.
Manual test¶
Run this from the Nagios server to verify connectivity without editing the script:
/path/to/nagios_event_handler_call.sh \
CRITICAL \
HARD \
1 \
"demo-host" \
"Demo Service" \
"This is a test alert from Nagios" \
"http://py-phone-caller.lan:8081/call_to_queue" \
"oncall"
Expected result: asterisk_caller returns JSON similar to {"status": 200} and the call is queued.
Customization¶
You can configure nagios_event_handler_call.sh without editing the file:
- Set
PY_PHONE_CALLER_API_BASE_URLor pass argument 7 to change the API endpoint. - Set
PY_PHONE_CALLER_TARGET_PHONEor pass argument 8 to change the target phone number. - Set
PY_PHONE_CALLER_CURL_TIMEOUTto tune the HTTP timeout. - Adjust the script logic if you want to alert on
WARNINGor recoveryOKstates.
Troubleshooting¶
Error: PY_PHONE_CALLER_API_BASE_URL or argument 7 must be set: pass the endpoint in the command definition or export the variable in the Nagios runtime environment.Error: curl is required but was not found in PATH: installcurlon the Nagios server.curl: (22): the API returned an HTTP error such as400or500; check theasterisk_callerservice logs.curl: (7)or timeout errors: verify DNS, firewall rules, and thatasterisk_calleris listening on the configured host and port.