Before setting up Nagios SMS alerts, ensure you have:
- Nagios Core or XI installed (version 3.x or later recommended)
- Server with outbound email capability (SMTP)
- SMS gateway service (e.g., Twilio, Nexmo, or carrier-specific email-to-SMS)
- Basic understanding of Nagios configuration files
The most common method uses your mobile carrier's email-to-SMS address format. Example for US carriers:
# Example contacts.cfg entry
define contact {
contact_name sms_admin
alias System Admin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-sms
host_notification_commands notify-host-by-sms
email [email protected]@txt.att.net # AT&T format
}
For more reliable delivery, integrate Twilio's API:
#!/bin/bash
# /usr/local/nagios/libexec/sms_notify.sh
# Twilio credentials
ACCOUNT_SID="YOUR_ACCOUNT_SID"
AUTH_TOKEN="YOUR_AUTH_TOKEN"
FROM_NUMBER="+15551234567"
TO_NUMBER="+15559876543"
# Nagios variables
MESSAGE="$1"
# Send via curl
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$ACCOUNT_SID/Messages.json" \
--data-urlencode "Body=$MESSAGE" \
--data-urlencode "From=$FROM_NUMBER" \
--data-urlencode "To=$TO_NUMBER" \
-u "$ACCOUNT_SID:$AUTH_TOKEN"
Then define the command in commands.cfg:
define command {
command_name notify-service-by-sms
command_line /usr/local/nagios/libexec/sms_notify.sh "Nagios - $NOTIFICATIONTYPE$ - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
Key verification steps:
- Test the SMS script manually from command line
- Check Nagios debug logs (/usr/local/nagios/var/nagios.log)
- Verify notification intervals in timeperiods.cfg
- Confirm contact group assignments
Other reliable methods include:
- Pushover: Mobile push notifications with dedicated app
- PagerDuty: Enterprise-grade alerting with on-call scheduling
- Telegram/Slack bots: For teams already using these platforms
Before configuring mobile alerts in Nagios, ensure you have:
- Nagios Core 4.x+ or Nagios XI installation
- SMS gateway service (like Twilio, Nexmo, or local provider)
- Server with outbound internet access
- Basic knowledge of Nagios command/config files
Here are three common approaches to enable SMS notifications:
1. Using Email-to-SMS Gateways
# Sample configuration in commands.cfg
define command {
command_name notify-host-by-sms
command_line /usr/bin/printf "%b" "***** Nagios *****\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\nDate/Time: $LONGDATETIME$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTPAGER$
}
Then map your provider's email-to-SMS address (e.g., 1234567890@txt.att.net for AT&T) to the contact's pager field.
2. Direct API Integration (Twilio Example)
# Create /usr/local/bin/send_sms_twilio
#!/bin/bash
TWILIO_ACCOUNT_SID="YOUR_SID"
TWILIO_AUTH_TOKEN="YOUR_TOKEN"
TWILIO_NUMBER="+15556667777"
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \
--data-urlencode "Body=$1" \
--data-urlencode "From=$TWILIO_NUMBER" \
--data-urlencode "To=$2" \
-u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"
Make it executable (chmod +x /usr/local/bin/send_sms_twilio
) and configure Nagios command:
define command {
command_name notify-by-twilio-sms
command_line /usr/local/bin/send_sms_twilio "$NOTIFICATIONTYPE$ alert for $HOSTNAME$/$SERVICEDESC$: $SERVICESTATE$" $CONTACTPAGER$
}
define contact {
contact_name jdoe-sms
alias John Doe (SMS)
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-by-twilio-sms
host_notification_commands notify-by-twilio-sms
email jdoe@example.com
pager +15551234567 # Must include country code
}
- Test notifications manually first by running the script from CLI
- Check Nagios debug logs (
tail -f /usr/local/nagios/var/nagios.log
) - Verify SMS gateway API credentials and rate limits
- Ensure timeouts don't block Nagios (consider async notifications)
For teams using mobile apps like Nagios XI Mobile or integrating with PagerDuty/AlertOps:
# Sample webhook integration
define command {
command_name notify-service-by-push
command_line /usr/bin/curl -X POST -H "Content-Type: application/json" -d '{"nagios_host":"$HOSTNAME$","service":"$SERVICEDESC$","state":"$SERVICESTATE$","output":"$SERVICEOUTPUT$"}' https://your.mobilegateway.com/api/v1/alerts
}