📜 ⬆️ ⬇️

The notification from Nagios by phone through Asterisk

I read the Google translate + Asterisk IVR article yesterday and thought, “Cool! You can use it to quickly create sound files when making new hosts and services in Nagios! ”.

Then I wondered if it was described somewhere how to set up phone alerts for Nagios? Googling a bit I found a couple of articles, read them and realized that the solutions described there have a number of drawbacks:So I decided to share my decision, which I have been using for more than a year. In general, the solution is as follows. In Nagios, contact objects are created with the phone number in the pager parameter and the notify-by-phone and host-notify-by-phone commands that run the /etc/nagios3/notify_by_phone.sh script with the parameters to whom to call and which service has dropped. The script, in turn, via ssh sends the parameters to the Asterisk server.

On the Asterisk side, instead of the BESH, the /etc/asterisk/call_from_nagios.sh script is run , which generates the call files for Asterisk with a variable containing a list of phrases to be voiced. Here, in general, and everything, then it is described in detail.
')
I will not describe here the installation process of Nagios and Asterisk, suppose that both of these services already work for you. Let's start with the Asterisk server part. Create the /etc/asterisk/call_from_nagios.sh script:
#!/bin/bash data=`cat <&0` _TMP=/var/tmp callfile="${_TMP}/nag_callfile_$$" echo "`date '+%Y.%m.%d %H:%M:%S'` ${data}" >> ${_TMP}/nag.log number=`echo "${data}" | cut -f 1 -d " "` data=`echo ${data} | cut -f 1 -d ":" | cut -d " " -f 2-100 | tr "[:upper:]" "[:lower:]" | sed 's/ /\&/g' ` echo "Channel: Local/${number}@from-internal Context: custom-nagios-say Extension: s Priority: 1 MaxRetries: 0 WaitTime: 40 Setvar: play=${data} Account: NAGIOS CallerId: \"NAGIOS\" <168> " > $callfile chmod 666 $callfile mv $callfile /var/spool/asterisk/outgoing/ 

Create a context in extensions.conf :
 [custom-nagios-say] exten => s,1,Answer() exten => s,n,Wait(1) exten => s,n,Set(CHANNEL(language)=nag) exten => s,n,Playback(intro) exten => s,n,Playback(${play}) exten => s,n,Playback(${play}) exten => s,n,Playback(end) exten => s,n,Hangup() 
Create the / var / lib / asterisk / sounds / nag / directory and put the following files into it:
critical.mp3 - critical
down.mp3 - not available
end.mp3 - detailed information by email
host.mp3 - server
intro.mp3 - attention!
problem.mp3 - problem
service.mp3 - service
status.mp3 - status
warning.mp3 - warning
As well as the names of servers and services. The files themselves can be voiced by your voice or by the method described in the topic, which led me to the idea of ​​this article:
 wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5" "http://translate.google.com/translate_tts?q=&tl=ru" -O host.mp3 
Then there are two options: install the format_mp3 module in Asterisk or convert each file into a digestible asterisk format. I preferred the first method, but for an example I will give the command for conversion:
 sox host.mp3 -r 8000 -c 1 host.wav 
Now you can run a test call with the command:
 echo "168 PROBLEM Host ISP status DOWN" | /etc/asterisk/call_from_nagios.sh 
After you caught all the shoals and got a call and pronouncing “Attention! Problem. Host - Internet service provider. Status is unavailable, you can go to the next step and prepare the possibility of receiving alerts via ssh.

We generate a separate ssh certificate without passphrase:
 ssh-keygen -t rsa -b 4096 
Now add the contents of id_rsa.pub to /root/.ssh/authorized_keys and add the command parameter to the beginning of the line. Do not forget to do
 chmod 600 /root/.ssh/authorized_keys 
The file will look like this:
 command="/etc/asterisk/call_from_nagios.sh" ssh-rsa AAAAB3NA0PCGAC/8kZU= root@nagios 
Again, check the next command and go on.
 echo "168 PROBLEM Host ISP status DOWN" | ssh -l root -i id_rsa localhost 

Now let's work with the Nagios server. Suppose we have some contact:
 define contact{ contact_name vasea alias Vasea Pupkin service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,r service_notification_commands notify-service-by-email host_notification_commands notify-host-by-email email vasea@mydoamin.ru } 
Copy it and modify it a bit:
 define contact{ contact_name vasea_phone alias Vasea Pupkin phone service_notification_period dayhours host_notification_period dayhours service_notification_options w,c,r host_notification_options d,r service_notification_commands notify-by-phone host_notification_commands host-notify-by-phone pager 163 } 
Here I replaced the notification period with a daytime (previously described with the directive define timeperiod ), which corresponds to the time from 7 to 22 o'clock. If Vasya is ready to receive calls at night, you can leave 24x7. Well, in fact, I added the phone number 168. I use the internal phones, and the redirection to the mobile is already configured in the asterisk itself. But, in principle, you can immediately specify the mobile number.

We add the vasea_phone contact to the list of responsible persons for the corresponding service or to the corresponding contactgroup . Further we create teams:
 define command{ command_name notify-by-phone command_line [ "$NOTIFICATIONTYPE$" = "PROBLEM" ] && /etc/nagios3/notify_by_phone.sh "$CONTACTPAGER$ $NOTIFICATIONTYPE$ Host $HOSTNAME$ Service $SERVICEDESC$ status $SERVICESTATE$ : $SERVICEOUTPUT$" } define command{ command_name host-notify-by-phone command_line [ "$NOTIFICATIONTYPE$" = "PROBLEM" ] && /etc/nagios3/notify_by_phone.sh "$CONTACTPAGER$ $NOTIFICATIONTYPE$ Host $HOSTNAME$ status $HOSTSTATE$ : $HOSTOUTPUT$" } 
And the script itself /etc/nagios3/notify_by_phone.sh read as follows:
 #!/bin/bash data=$@ date=`/bin/date '+%Y.%m.%d %H:%M:%S'` echo "${date} ${data}" >> /tmp/nag.log echo "${data}" | ssh -i /etc/nagios3/id_rsa root@10.1.5.61 
10.1.5.61 is the IP address of my Asterisk server. And the file / etc / nagios3 / id_rsa is the one that we generated on the Asterisk server. The first time you connect to a new ssh server, it asks for confirmation before entering its fingerprint into known_hosts. Therefore, we need to become a nagios user (if the Nagios daemon is working under this user, this is usually the case) and manually run the /etc/nagios3/notify_by_phone.sh script:
 su - nagios /etc/nagios3/notify_by_phone.sh 168 PROBLEM Host ISP status DOWN 
Before that, you may need to replace / bin / false with / bin / bash in / etc / password for user nagios. After executing the above command, it can be returned.

That's all! Maybe some details I missed. If so - write in the comments and I will update the article.

Now you will be the first to know that a problem has arisen and to fix it before any of the clients or management noticed it.

Source: https://habr.com/ru/post/133873/


All Articles