Send Nagios Alert Notification Using WhatsApp

Introduction

One of the most important feature of the monitoring platform is the notification system; by default  they use the local mail daemon (postfix, mail, exim) installed on the server to send the notifications.

Using local mail daemon has many issues :

  • You need to install and configure your smtp server.
  • Maybe your mail server go down so you can’t receive the alert.
  • Maybe this kind of emails from a not verified source like nagios can be filtered by the same company antispam system.

In order to avoid all issues, In this article I am going to explain how to send alert from monitoring tool Nagios Core using WhatsApp.

This task was not so easy since communicating with WhatsApp through a linux server was requiring to install Yowsup library.

If you still didn’t install Nagios Core , check the following articles.

Introduction to WhatApp

WhatsApp Messenger is a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS.

 

WhatsApp Messenger is available for iPhone, BlackBerry, Android, Windows Phone and Nokia and yes, those phones can all message each other! Because WhatsApp Messenger uses the same internet data plan that you use for email and web browsing, there is no cost to message and stay in touch with your friends.

Installing and Configuring Yowsup Under Nagios

Server

Yowsup is an opened WhatsApp service under platforms like Linux, MAC and windows.

Yowsup is a python library that enables you build application which use WhatsApp service. Yowsup has been used to create an unofficial WhatsApp client Nokia N9 through the Wazapp project which was in use by 200K + users as well as another fully featured unofficial client for Blackberry 10.

Yowsup Architecture

A running Yowsup app is a stack of several layers. The previous figure is a breakdown of core Yowsup Stack. A layers is a bidirectional channel that usually transforms the data passing through it before passing it to underlying or the above layer.

Yowsup Installation

Before we start looking under the configuration of nagios we need to install Yowsup library that enables us to send alert using WhatsApp service.

First step to do is to install all dependencies using this command as root user:

apt-get install python python-dateutil python-argparse python-dev python-setuptools

Then download WhatsAPI Yowsup hosted on Github

git clone https://github.com/tgalal/yowsup

Then go to yowsup file and give the script setup.py permission

cd yowsup
chmod +x setup.py

then run this command

python setup.py install

After the command finish the installation you can start your whatsApp configuration by:

chmod +x yowsup-cli

Yowsup Cli client

Yowsup comes with a command line client exposes many yowsup capabilities for quick access in command line. At the moment it supports only a smaller set of yowsup function as it’s still in under heavy development, but eventually it will expose every single yowsup capabilities.

For developers this tool is useful if you’d like to debug some yowsup features and monitor WhatsApp traffic being transmitted. Also useful for developers to see how it implements different types of functionality for use as reference while working on their own project.

For non devs, the tools is also easy to use for quick command line WhatsApp magic.

Yowsup Configuration

Yowsup-cli is a command line interface to Yowsup library. It provides you with the options of registration, and provides a few demos such as a command line client.

First you need to register using yowsup in order to have a valid phone number and password.

./yowsup-cli 
Available commands:
===================
demos, registration, version

Then register your phone number using the command yowsup with the registration option

./yowsup-cli registration --requestcode sms --phone xxxxxxxx --cc 49 --mcc 24 --mnc 07 
INFO:yowsup.common.http.warequest:{"status":"sent","length":6,"method":"sms","retry_after":1805}
status: sent
retry_after: 1805
length: 6
method: sms

WhatsApp Registration options

-m MCC, --mcc MCC Mobile Country Code. Check your mcc here: https://en.wikipedia.org/wiki/Mobile_country_code
-n MNC, --mnc MNC Mobile Network Code. Check your mnc from https://en.wikipedia.org/wiki/Mobile_country_code
-p PHONE, --phone PHONE Your full phone number including the country code you
defined in 'cc', without preceeding '+' or '00'
-C CC, --cc CC Country code. See http://www.ipipi.com/help/telephone-country-codes.htm.

Then you need to finish the registration using this command:

./yowsup-cli registration --register 263-541 --phone xxxxxxxxxxxxxxx --cc 49
INFO:yowsup.common.http.warequest:{"status":"ok","login":"xxxxxxxxxxxxxxx","pw":"0pJj5cLaGSk6pDTa6rJR/5bDiR0=","type":"new","expiration":1471273284,"kind":"free","price":"0,89 \u20ac","cost":"0.89","currency":"EUR","price_expiration":1442376976}
status: ok
kind: free
pw: 0pJj5cLaGSk6pDTa6rJR/5bDiR0=
price: 0,89 €
price_expiration: 1442376976
currency: EUR
cost: 0.89
expiration: 1471273284
login: xxxxxxxxxxxxxx
type: new

Create config file like this example:

nano yowsup-cli.conf

Then add content like in this example:

cc=49 #Your country code
phone=xxxxxxxxxxxx #Your phone number
id= #This is where you insert your IMEI number (not needed)
password=0pJj5cLaGSk6pDTa6rJR/5bDiR0= #your password which you got from registration command

Then you test your configuration by sending sms using this command where “******************” is the receiver number.

./yowsup-cli demos -c yowsup-cli.config -s ************ “test”

Nagios Configuration

Start Nagios configuration by copying the yowsup file to /usr/local/nagios/libexec/.

cp yowsup/ /usr/local/nagios/libexec/

After this go under /usr/local/nagios/etc/objects/ and edit the file commands.cfg but that perform a backup of the config file:

cd /usr/local/nagios/etc/objects/
cp commands.cfg commands.cfg.old
Edit the commands.cfg file and you will add two commands ‘notify-host-by-whatsapp and ‘notify-service-by-whatsapp’
 
notify-service-by-whatsapp
$USER1$/yowsup/yowsup-cli -c $USER1$/yowsup/yowsup-cli.config -s $_CONTACTWHATSAPP$ “$NOTIFICATIONTYPE$ Host : $HOSTNAME$ - Service : $SERVICEDESC$ is $SERVICESTATE$ @ $LONGDATETIME$”

notify-host-by-whatsapp

$USER1$/yowsup/yowsup-cli -c $USER1$/yowsup/yowsup-cli.config -s $_CONTACTWHATSAPP$ “$NOTIFICATIONTYPE$ Host : $HOSTNAME$ is $HOSTSTATE$ @ $LONGDATETIME$”

And we should define the contact under contacts.cfg file like this example

        contact_name                    admin
        use                             generic-contact
        alias                           Admin 
        email                           admin@unixmen.com
        _whatsapp                       xxxxxxxxxxxxxxxxxxxxx
        service_notification_commands   notify-service-by-email notify-service-by-whatsapp
        host_notification_commands      notify-host-by-email notify-host-by-whatsapp

Now the configuration is finished. Before applying the new configuration, run the following command to check for any error or mistake:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, restart the nagios service:

service nagios restart

And you should get alert notification from nagios like this example:

That all!.

Thanks for reading my article.