Solution
This is a description about how to connect Nagios to Asterisk.
So you can receive automatic phone calls if Nagios reports some failure.
There is also another script online that lets your Nagios box call thru the Asterisk Manager protocol (that way you can have your Nagios installation on a separate box).
Components
- Operating system: Red Hat, CentOS, Fedora or Ubuntu
- Monitoring software: Nagios
- VoIP software: Asterisk
- Text-to-speech software: Cepstral TTS
Tested scenario
- Ubuntu 8.04
- Nagios 3.0.1
- Cepstral Swift 4.2.1
- Asterisk 1.4.17
Nagios configuration
Contacts
/usr/local/nagios/etc/objects/contacts.cfg
[...]
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
service_notification_commands notify-service-by-phone
host_notification_commands notify-host-by-phone
email [email protected]
address2 SIP/1000
}
[...]
Notification command
vim /usr/local/nagios/etc/objects/commands.cfg
[...]
define command{
command_name notify-host-by-phone
command_line $USER1$/notify-by-phone.sh $CONTACTADDRESS2$ "Nagios Host Alarm. Notification Type: $NOTIFICATIONTYPE$. Host: $HOSTNAME$. State: $HOSTSTATE$. Address: $HOSTADDRESS$ Info: $HOSTOUTPUT$ Time: $LONGDATETIME$" _host_$HOSTEVENTID$
}
define command{
command_name notify-service-by-phone
command_line $USER1$/notify-by-phone.sh $CONTACTADDRESS2$ "Nagios Service Alarm. Notification Type: $NOTIFICATIONTYPE$. Service: $SERVICEDESC$. Host: $HOSTALIAS$. Address: $HOSTADDRESS$. State: $SERVICESTATE$. Time: $LONGDATETIME$. Additional Info: $SERVICEOUTPUT$" _service_$SERVICEEVENTID$
}
[...]
Asterisk configuration
/etc/asterisk/extensions.conf
[...]
[alarm]
exten => s,1,Answer
exten => s,2,NoOp(alarmid: ${alarmid})
exten => s,3,Playback(/var/spool/alarm/alarm${alarmid})
exten => s,4,Hangup
[...]
Connection script
see also Asterisk auto-dial out
/usr/local/nagios/libexec/notify-by-phone.sh
#!/bin/bash
channel=$1
alarmtext=$2
alarmid=$3
cd /var/spool/alarm
echo $alarmtext > alarm$alarmid.txt
/opt/swift/bin/swift -n Allison-8KHz -f alarm$alarmid.txt -o alarm$alarmid.tmp.wav
/usr/bin/sox alarm$alarmid.tmp.wav -r 8000 alarm$alarmid.ul
/bin/mv -f alarm$alarmid.ul alarm$alarmid.ulaw
/bin/rm alarm$alarmid.tmp.wav
echo "Channel: $1
MaxRetries: 2
RetryTime: 60
WaitTime: 30
Context: alarm
Extension: s
Priority: 1
Set: alarmid=$alarmid" > alarm$alarmid.call
/bin/chmod 777 alarm$alarmid.call
/bin/mv alarm$alarmid.call /var/spool/asterisk/outgoing
Setup procedure
prerequisites: Nagios, Asterisk and Cepstral installed
as root:
apt-get install sox
apt-get install libsox-fmt-all
mkdir /var/spool/alarm
chown nagios.asterisk /var/spool/alarm/
usermod -G nagcmd,asterisk nagios
vim /usr/local/nagios/libexec/notify-by-phone.sh # (copy and paste above script)
chown nagios.nagios /usr/local/nagios/libexec/notify-by-phone.sh
chmod 750 /usr/local/nagios/libexec/notify-by-phone.sh
vim /usr/local/nagios/etc/objects/contacts.cfg # (copy and paste above config)
vim /usr/local/nagios/etc/objects/commands.cfg # (copy and paste above config)
/etc/init.d/nagios reload
vim /etc/asterisk/extensions.conf # (copy and paste above config)
/etc/init.d/asterisk reload
Test via command line
su nagios
/usr/local/nagios/libexec/notify-by-phone.sh SIP/1000 "hello world" first_test_id