Receive notifications from Zabbix on WhatsApp

There are many ways to get notifications from Zabbix on the web. It seemed to me convenient to receive alerts on WhatsApp - it is cheaper than sms and, in my case, more convenient than mail - we use a corporate mail account to receive notifications, access to which is limited from the outside, and the alert can be lost among other letters .

The setup is pretty simple. We will need:

1) yowsup and its dependencies:
1.1) python 2.6+
1.2) python-dateutil
1.3) argparse for python <2.7
1.4) libxml2 if yowsup will be used with the old version of the API (flag --v1)
2) Zabbix server access to the Internet
3) A phone with a SIM card, to the number of which a WhatsApp account will be registered

Step 1: Install yowsup dependencies

First of all, you need to install the dependencies for yowsup.

On Debian-like distributions or using the DEB package format, this is done like this:

# aptitude install python python-dateutil python-argparse


On RedHat-based distributions or using the RPM package format:

# yum install python python-dateutil python-argparse


In my case, the python-argparse package was not in the repository, I had to download it separately and install it manually. For RHEL 6, this can be done, for example, here .

Installation:

# rpm -i python-argparse-1.2.1-5.1.noarch.rpm


Step 2: Install yowsup

After installing the dependencies, put yowsup itself. The official project page is located on GitHub . Directly from Zabbix’s server, yowsup can be downloaded like this:

# wget https://github.com/tgalal/yowsup/archive/master.zip


Unpack the archive and go to the yowsup-master / src directory:

# unzip master.zip
# cd yowsup-master/src


Copy the example config to the working config:

# cp config.example yowsup-cli.config


It has only four lines:

cc=7
phone=
id=
password=


cc is the country code. A list of codes can be found on CountryCode.org . The code of Russia is 7;

phone - the phone number to which the WhatsApp account will be linked. Must start with a country code;

id - this field is necessary if you want to use an existing WhatsApp account and an old version of the API (launch yowsup with the --v1 flag). If the account was created on a Nokia or Android device, then in the id field you need to write the device’s IMEI. If the device is based on iOS, then you need to write the MAC address of the WLAN interface. In newer versions of the API, this field is not used; you can leave it blank;

password- WhatsApp password, which will be received automatically after registration. In principle, you can pull this password from an existing account, then it will be possible to use one account on two devices, but I have not tried it.

So, fill in the config fields:

cc=7
phone=79123456789
id=
password=


Next, you need to go through the registration procedure, during which sms with a confirmation code will be sent to the phone number specified in the config. In general, problems at this step should not arise. Make sure the Zabbix server has access to the WhatsApp server using the https protocol. If Zabbix is ​​behind the firewall and the security policy does not allow it to have full access to the Internet, you can get the address list by running tcpdump on the server’s network interface and see where yowsup is accessing by starting the registration procedure. I will not dwell on this in detail, there are a lot of tcpdump manuals on the network, I can only say that such a command was useful to me (all tcp traffic on eth0 interface that does not go within the local network 10.0.0.0/8):

# tcpdump -i eth0 -n tcp and  not src net 10.0.0.0/8 and dst net 10.0.0.0/8


We send a request for registration:

# ./yowsup-cli -c yowsup-cli.config -r sms


If everything went fine, we will receive an SMS with a six-digit confirmation code (for example, 123-456). We send this code to the WhatsApp server:

# ./yowsup-cli -c yowsup-cli.config -R 123456


After that, the password should appear in the password field of our config. This completes the registration procedure, you can try sending a message to another WhatsApp user.

You can send a message by the command

# ./yowsup-cli --send 79123456780 "Test message" --wait --config yowsup-cli.config


Receive message:

# ./yowsup-cli --listen --autoack --keepalive --config yowsup-cli.config


And you can generally chat in chat mode:

# ./yowsup-cli --interactive 79123456780 --wait --autoack --keepalive --config yowsup-cli.config


image

This completes the yowsup setup.

Step 3: writing an alert sending script

To make yowsup and Zabbix friends, you need to write a notification sending script. Let me remind you that Zabbix takes notice of the scripts directory specified in the variable AlertScriptsPath config zabbix_server.conf were . In mine, this directory was located in / usr / local / share / zabbix / alertscripts :

# cat /usr/local/etc/zabbix_server.conf | grep AlertScriptsPath
### Option: AlertScriptsPath
# AlertScriptsPath=/usr/local/share/zabbix/alertscripts


We put the script there (I called it whatsapp ), which consists of literally three lines:

#!/bin/bash
DIR='/usr/local/share/zabbix/alertscripts/yowsup-master/src/' # путь к файлам yowsup 
$DIR/yowsup-cli --send $1 "$2 $3" --wait --config $DIR/yowsup-cli.config


I put the files in a directory yowsup alertscripts - maybe it's not the best solution, but it seemed to me that it's more convenient to look for the configs / scripts in case anything happens.

$ 1 $ 2 $ 3 is the value of Zabbix variables, meaning, respectively, “to whom to send a notification”, “subject” and “notification body”. We will configure these fields further from the Zabbix interface.

We make the script executable:

# chmod +x /usr/local/share/zabbix/alertscripts/whatsup 


Step 4: Configuring Zabbix

We follow the standard algorithm:

1) Create an alert method
2) Create an action associated with this alert
3) Connect a notification to users

1) Create a notification method

In the Administration -> Alerts menu (Administration -> Media types), click the Create media type button .



Name: Any
type you want (Type): - Script (Script)
Name of the script (Script Name): - Must match the name of the script created in the previous step ( whatsapp in our case)

2) Create an action

By default, an action is set up in Zabbix that sends newsletters to all administrators with all types of alerts:



If you don’t have such an action, you need to create your own. Menu Settings -> Actions (Configuration -> Action) , create a new action using the Create Action button .



On the Action tab, you can define the format of the message that will be sent to WhatsApp. I left everything by default.



On the Conditions tab, the conditions are set under which the action will be performed. Also left by default.



Well, on the tab Operations (Operations) , we specify what to do Zabbix: send a message (Send message)a group of users or an individual user using the WhatsApp notification method.

Save the settings and proceed to the next step.

3) We connect the notification to users

We go into the user settings (Administration -> Users or Administration -> Users) , select the desired user and configure it for him. Alert method (Media) :



Type of alert (Type): WhatsApp
Send to (Send to): Phone number with country code (for example, 79123456789)
Choose the time when you can send messages and the degree of their criticality. Save the settings.

This completes the setup, but you still need to verify that the messages are leaving and successfully delivered. You can track the sending of notifications in the Administration -> Audit menu (Administration -> Audit) by selecting Actions :



We see that the notification was sent successfully:



Checking WhatsApp on the smartphone:



Works!

UPDATE:In the comments they ask why WhatsApp, and not something else, for example sms. The fact is that now our Zabbix generates quite a few alerts, including false ones. I tried to screw a 3G modem to it and send sms from it, but it turned out pretty expensive in terms of the cost of their payment. And since almost all colleagues have WhatsApp, I thought it would be nice to send notifications there, moreover, it is free (the first year) and does not require the installation of any applications (in our case). It’s clear that this is not the most reliable way - they can change the API, the phone may be without the Internet, the channel between Zabbix and the Internet, etc. will drop, but to send non-critical alerts, IMHO, it can be used to save less on SMS. If you suddenly close the shop, we will use something else, such as Pushover or Boxcar,suggested by kemko

Also popular now: