How to receive Linux server alerts on your phone via WhatsApp
In my last article, I wrote about how to receive Linux server alerts on your phone via telegram. While telegram has a rich feature set, it is still not as popular as whatsapp at least in most regions. Almost, everyone I know is using whatsapp. With the recent addition of end-to-end ecryption, I’ve since relaxed my negative opinions of this service. Therefore, I decided to write a follow-up to my previous article, and show you how to get alerts from your Linux server via whatsapp. I will not delve into the setup of icinga2, however, once you are able to send messages to whatsapp, things should be pretty much self explanatory.
Thanks to Tarek Galal, who wrote a python library called yowsup to interface with whatsapp API.
First install it as follows:
pip install yowsup2
If installation fails, ensure that your have installed all the required dependencies on your Linux box which include python2.6+, python-dateutil, argparse,readline, pillow. If you intend to use encryption, you will need to install protobuf, pycrypto, python-axolotl-curve25519
Yowsup comes with a command-line tool appropriately named; yowsup-cli. For a full understanding of its usage, please visit the yowsup-cli wiki
Registration
As expected, you will need to register your phone. This can be done simply as follows:
yowsup-cli registration -d -E android -m 641 -n 10 -p 256XXXXXXXXX -C 256 -r sms
The arguments explained:
-d For debugging
-E This is the environment. Specifically set this to android. The default will cause you to run into ‘no_route’ errors.
-m This is the MCC for your country. In my case 641 is for Uganda. Use the site: http://mcclist.com/mobile-network-codes-country-codes.asp to locate yours
-n This is the MNC for your Telecom provider. Again, use the site: http://mcclist.com/mobile-network-codes-country-codes.asp to locate yours
-p This is your phone number. Include the country code specified in next option -C
-C This is the country telephone code. In my case 256 is for Uganda.
-r (sms|voice) This is method by which pin code will be sent to you
If execution of the above command is successful, you will receive, either a text message or voice call with the PIN code. Use this code to complete the registration process as follows:
yowsup-cli registration -d -E android -p 2567XXXXX -C 256 -R PIN-CODE
The arguments are similar to above, except for:
– R which allows you to register using the provided PIN-CODE
Once the command above has been executed successfully, the results displayed will look like the following
kind: free
pw: dfggHHSGGdIcdddRN567gjy=
price: US$0.99
price_expiration: 147567899
currency: USD
cost: 0.99
expiration: 4444444444.0
login: 256XXXXXXXXXX
type: new
Take note of the 2 fields: pw and login
Sending Messages
You should now be able to send messages using the command line. Test sending as follows:
yowsup-cli demos -d -l "256XXXXXXXX:dfggHHSGGdIcdddRN567gjy=" -s 256XXXXXXXXX "This is a test – Hello World"
The arguments explained:
-d For debugging
-l login. Enter in format [login:password]
-s The recipient’s phone number
At this point your recipient should be able to receive your message.
For monitoring with icinga2, you will need to create a script using the above command for sending messages. Make it executable and place it in location of your scripts. From, here, setup of icinga2 is like any other as shown in my previous article.
If you need more assistance or are interested in Linux and Open Source stuff, follow me @jzikusooka.
How to receive Linux server alerts on your phone via telegram
To monitor my Linux servers, I have been using icinga since its forking from the popular Nagios. Icinga has really matured over the last few years and currently at version 2.4.4, I would recommend anyone serious at network and server monitoring to check this open source project. Installation and setup of icinga2 is somewhat complex, but if you are an experienced command line sort of guy, its not that hard at all. In fact the introduction of icingaweb2 has made it possible to have an easy to use interface to setup your monitoring needs. I will not delve into the nitty gritty of install and setup. For that please take a look at the detailed documentation.
Several alerts are possible when using Icinga including e-mail, sms, XMPP, IRC, twitter etc. I wanted a way to get notified of host and service failures using telegram. Luckily, telegram provides an API to do just that.
The secret source in all of this; is to install a command line interface for telegram called telegram-cli . In order for telegram-cli to compile without any issues, ensure that all dependencies are installed
For Fedora:
sudo dnf -y install
lua-devel openssl-devel libconfig-devel readline-devel libevent-devel
jansson-devel python-devel
For Ubuntu/Debian:
sudo apt-get install
libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev
libevent-dev libjansson-dev libpython-dev make
Now install telegram-cli
cd /usr/src && git clone –recursive https://github.com/vysheng/tg.git
Note: Make sure you add the recursive flag to git clone, otherwise make will fail
cd tg
./configure && make
The binary is under ./bin/telegram-cli. Simply create a symbolic link to it.
cd /usr/sbin && ln -s /usr/src/tg/bin/telegram-cli .
Thats it for the installation of telegram-cli. Please read the README file in the sources root folder, especially on how to use this application.
Icinga2 telegram-cli setup
First ensure that the user icinga (created during icinga install process) can send a message using telegram-cli. Remember all alerts go out under the icinga user, so you have to get this part working first, lest you spend hours troubleshooting.
Temporarily change the shell for icinga from ‘/sbin/nologin’ to ‘/bin/bash.’ Remember to change back to nologin for icinga user when testing is completed.
usermod -s /bin/bash icinga
Now change to user icinga: Assumming that you are root user, if not you will be prompted for a password, which you would setup as root.
su -l icinga
telegram-cli
Enter your phone number and SMS code sent when prompted
Then test to see if you can send a message:
telegram-cli -D -W -e "msg [PEER_NAME] 'Hello World'"
If it works, then you can proceed to configure icinga2.
Create notification script for services
cat > /etc/icinga2/scripts/notify_services_via_telegram.sh << EOF
#!/usr/bin/env bash
TELEGRAM_CMD=/usr/sbin/telegram-cli
TELEGRAM_OPTS=”-D -W”
TELEGRAM_PEER=[PEER_NAME]
TELEGRAM_MSG=$(cat <<TEMPLATE
[$HOSTALIAS] $NOTIFICATIONTYPE: $SERVICEDESC is $SERVICESTATE
TEMPLATE
)
$TELEGRAM_CMD $TELEGRAM_OPTS -e “msg $TELEGRAM_PEER ‘$TELEGRAM_MSG'”
EOF
Ensure that above script is executable:
chmod 755 /etc/icinga2/scripts/notify_services_via_telegram.sh
Proceed to configure commands, notifications, and hosts config files: Note: Am only setting up notifications for services. You can do the same for hosts information.
cat >> /etc/icinga2/conf.d/commands.conf <<EOF
/** Telegram */
object NotificationCommand “telegram-service-notification” {
import “plugin-notification-command”
command = [ SysconfDir + “/icinga2/scripts/notify_services_via_telegram.sh” ]
env = {
“NOTIFICATIONTYPE” = “$notification.type$”
“SERVICEDESC” = “$service.name$”
“HOSTALIAS” = “$host.display_name$”,
“HOSTADDRESS” = “$address$”,
“SERVICESTATE” = “$service.state$”,
“LONGDATETIME” = “$icinga.long_date_time$”,
“SERVICEOUTPUT” = “$service.output$”,
“NOTIFICATIONAUTHORNAME” = “$notification.author$”,
“NOTIFICATIONCOMMENT” = “$notification.comment$”,
“HOSTDISPLAYNAME” = “$host.display_name$”,
“SERVICEDISPLAYNAME” = “$service.display_name$”,
“USEREMAIL” = “$user.email$”
}
}
EOF
cat >> /etc/icinga2/conf.d/notifications.conf <<EOF
/** Telegram */
apply Notification “telegram-icingaadmin” to Service {
import “telegram-service-notification”
user_groups = host.vars.notification.telegram.groups
users = host.vars.notification.telegram.users
assign where host.vars.notification.telegram
}
EOF
To get alerts on services for a host, add the following stanza to the host configuration in /etc/icinga2/conf.d/hosts.conf
/* Telegram */
vars.notification[“telegram”] = {
groups = [ “icingaadmins” ]
}
Finally, check your config files
icinga2 daemon -C
Restart icinga if all is OK. You will now be notified via telegram when services fail for the above configured host.