📜 ⬆️ ⬇️

SMS notifications from Nagios with little blood

Once the admin was standing in a smoking room and thinking - how could he find out during off-hours, away from the computer and the Internet, that a problem had arisen on one of his servers. You can of course put a student at the monitor with Nagios , so that if he called the admin and said, “Something red appeared here ...” Stop. I called. On the mobile phone. Nafig student SMS can be received. The thought stopped and the admin went to drink coffee. Then he came back and sat down at the workplace where the Google Calendar was loaded in the browser ... and the solution came by itself.

So, we have configured Nagios and the desire to receive SMS about the problems encountered on your phone.
For convenience, we register a separate Google account - something like nagios_alert@your-company-domain.com and go to set up a calendar for this account. We are interested in notifications for the calendar by default - set the notification 1 minute before the event by SMS.
You also need to bind your mobile number to this account (this is done in the same place in the calendar settings).
Next, follow this link: github.com/insanum/gcalcli and download GCalCLI, a console utility in Python that allows you to work with the Google calendar.
All that is required for the utility to work is described on the project page, there is literally a couple of Python modules.
For gcalcli to work properly, you need to finish it a bit (where without it).
Open the file for editing, go to line 226 and change it to this:
feedPrefix = 'https?://www.google.com/calendar/feeds/' 

Next, we put this script somewhere more comfortable, where the account from which the Nagios-demon is spinning could launch it. I chose / usr / bin.
Next to gcalcli we put a bash-script of the following content:
 #!/bin/bash # notify_sms for Nagios by Br0ziliy NOW_HOUR=`date +%-H` NOW_MIN=`date +%-M` [ $NOW_HOUR -gt 0 ] && H=$(( $NOW_HOUR-1 )) || H=23 [ $NOW_MIN -lt 55 ] && M=$(( $NOW_MIN+5 )) || M=59 /usr/bin/gcalcli --user nagios_alert@your-company-domain.com --pw yourpass quick "$H:$M $@" 

The script will add a “quick” event to the calendar of the account nagios_alert@your-company-domain.com with the text that will be transmitted in the parameters (see below).
I also note that the time on the Nagios server and my local time differ by 1 hour - this difference is taken into account in the script.
Next, configure Nagios itself.
Add the notify-service-by-sms command:

 define command{ command_name notify-service-by-sms command_line /usr/bin/notify_sms '$NOTIFICATIONTYPE$ $HOSTALIAS$/$SERVICEDESC$: $SERVICEOUTPUT$' } 

Add a separate contact for SMS-notifications (so easy to use):
 define contact{ contact_name mobile mobile host_notification_period never #      service_notification_period nonworkhours # SMS      service_notification_options c,r #     service_notification_commands notify-service-by-sms host_notification_commands notify-host-by-email } 

Add this contact to the right group, restart Nagios - everything. Now the admin will always know where the cho lies. My test message arrives exactly 8 minutes after the problem has appeared (you may have more / less depending on Nagios settings).

As for the mobile phone number - I tested it on the MTS - it works at 5.
')
As for paid SMS sending services, yes, I know about them, but in the title of the topic, therefore, there are words with “little blood”.

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


All Articles