📜 ⬆️ ⬇️

We send Nagios notifications to Skype chat

So, once the thought occurred to me that it would be great to receive notifications from Nagios on skype.
In short: use Skype4Py, send notifications with a script.
Bold minus: skype does not work without X.
Plus: reduced reaction time to problems.

Interesting? Details under the cut.

Googling revealed only one similar option of sending notifications to Skype, but for Zabbix .
Thanks to the author, convinced that the idea is feasible!

So, further operations were performed under Gentoo, but it should be quite easily reproduced for other distributions.
')
We will need a whole set of packages:
- X
# emerge x11-apps/xdm x11-apps/xsm x11-base/xorg-drivers x11-base/xorg-server x11-base/xorg-x11 -pv

- skype and Skype4Py, which allows you to send messages
# emerge skype skype4py -pv

When installing skype, you will be prompted to accept the license, for this you need to add it in ACCEPT_LICENSE in make.conf.
In addition, if the system is 64-bit, you can make sure that the kernel has the option CONFIG_IA32_EMULATION = y, which allows you to run 32-bit applications, otherwise, 32-bit Skype will not start.
I also needed to rebuild udev with X support:
USE="X" emerge udev -pv

While everything is compiled, you can read Habr, go on vacation, build a house, plant a tree, raise a son ...

Hooray! We are halfway there.
Now the fun part.

We register an account for our nagios: here .

The most convenient option is to set up a skype account on a separate host (for example, on your laptop), and then copy ~ / .Skype / to the server in the nagios user's home folder.
What is useful to do:
- enable automatic user login when skype is started;
- disable all pop-up and sound notifications about events;
- add our user to the chat, where we plan to continue to write "letters of happiness."

In addition, you must authorize Skype4Py, allowing him to perform actions on behalf of our user. For this
1) run python2.7;
2) connect to the already working Skype, with a logged in user nagiosa
>>> import Skype4Py <br>
>>> import sys <br>
>>> skype = Skype4Py.Skype(Transport='x11')
>>> skype.Attach()

We confirm in the appeared window that we allow Skype4Py to connect, put a tick so that they no longer ask;
3) the first chat, in which we were added, is visible as skype.Chats [0] - we will write something:
>>> msg = "Ololo"
>>> skype.Chats[0].SendMessage(msg)

Has come Ok, so you can move on.

By the way, the Skype4Py documentation can inspire a much wider range of actions than just sending a chat message.

Turn off Skype, copy ~ / .Skype / to the target server with nagios in the home folder of the user nagios.

Note: these actions can be done immediately on the right server by running x11vnc once, then you need to install x11vnc, xterm, twm, as the easiest option. The result will not change, but I don’t want to install extra applications initially.

Check what we are on the target server:
# su - nagios
$ export DISPLAY=:0
$ Xvfb :0 -screen 0 800x600x16 &
$ skype &

If there are no errors in the output, everything is in order. We check if Xvfb and skype are dead - we are looking for them in the processes.
Then we try to connect and send something.
If you could not connect (Connection refused) and the account is not online - then autologin was disabled, you need to turn it on and copy ~ / .Skype / to the server with nagios again.

So, there is a working Skype, through which we can send messages. Teach this nagios.
Create in the home folder (for example, / var / nagios / home /) 2 files:
alert.sh:
#!/bin/bash
export DISPLAY=:0
/usr/bin/python2.7 /var/nagios/home/alert.py "$*"

and
alert.py:
#!/usr/bin/python2.7
import Skype4Py
import sys
skype = Skype4Py.Skype(Transport='x11')
skype.Attach()
msg=' '.join(sys.argv[1:])
skype.Chats[0].SendMessage(msg)

Do not forget to make the shell script executable (chmod + x alert.sh) and go to the nagios customization.
Add a contact to /etc/nagios/contacts.cfg:
define contact{
contact_name skype
alias Skype
use generic-contact
service_notification_commands notify-service-by-skype
host_notification_commands notify-host-by-skype
}

We add our contact to the admins group or any other that receives notifications of problems from nagios.

And decrypt the notify - * - by-skype commands in /etc/nagios/objects/commands.cfg
# 'notify-host-by-skype'
define command{
command_name notify-host-by-skype
command_line /var/nagios/home/alert.sh "$NOTIFICATIONTYPE$ Host: $HOSTNAME$ State: $HOSTSTATE$ Info: $HOSTOUTPUT$"
}

# 'notify-service-by-skype'
define command{
command_name notify-service-by-skype
command_line /var/nagios/home/alert.sh "$NOTIFICATIONTYPE$ Host: $HOSTNAME$ Service: $SERVICEDESC$ Info: $SERVICEOUTPUT$"
}

Please note that you can display any necessary information, for example, you can add a variable with a description of the host.
Do not forget to re-read the nagios config (/etc/init.d/nagios reload) and admire the results!

Useful links:

1. Nagios documentation: www.nagios.org/documentation
2. Skype4Py documentation: skype4py.sourceforge.net/doc/html

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


All Articles