#!/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/
[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: 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. 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
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. 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.Source: https://habr.com/ru/post/133873/
All Articles