Server Room Environmental Monitoring Using Graphs Sent to Telegram

  • Published In: Tutorial
  • Created Date: 2018-09-11
  • Hits: 1945

We continue publishing a series of articles on implementing the integration of microclimate monitoring devices and devices for remote power distribution NetPing with a «Telegram» messenger. Earlier in our blog, we examined the process of bot creation для «Telegram», controlling sockets in IP PDU NetPingcontrolling an air conditioner in a server room and controlling IO lines of a NetPing device using a «Telegram» messenger in enough details. In this article, we will regard a process of creating a tool for remote monitoring a microclimate in a server room using graphs sent to «Telegram».

Required Equipment and Preparation for the Solution Implementation:

In this article, UniPing server solution v4/SMS server room environmental monitoring device is used that is configured to work with temperature sensors and humidity sensors. More detailed information about configuring a device can be found here. A UniPing server solution v4/SMS monitoring device must be connected to a Zabbix 3.4.8 monitoring system. More detailed information about a connection can be read here. It is supposed that you have already created bots according to the recommendations of this article. In this example, we created a bot named @NetPing_Graph_bot.

A programming language interpreter Python 2.7.5 is installed in the system CentOS Linux release 7.4.1708 (Core) on default. Let's install a pip package manager using the next commands: «# cd /usr/src && wget https://bootstrap.pypa.io/get-pip.py» and «# python get-pip.py»:

CentOS downloading Pip

CentOS installing Pip

Also, a pi module «requests» is necesary for the operation. Let's install it using a command «# pip install requests»:

CentOS installing requests

On the website github.com, there is a free script that can send graphs from a Zabbix monitoring system to «Telegram». Download script sources and copy files «zbxtg.py» and «zbxtg_settings.example.py» of the downloaded archive from our site to the directory «/usr/lib/zabbix/alertscripts» of a Zabbix server. Rename a file «zbxtg_settings.example.py» to «zbxtg_settings.py». Make the contents of «zbxtg_settings.py» look nearly like this:

# -*- coding: utf-8 -*-

tg_key = "624993224:AAE74nYdfK3aDgej_AowQ0Gc0rXDFCSvZQk" # telegram bot api key

zbx_tg_prefix = "zbxtg" # variable for separating text from script info
zbx_tg_tmp_dir = "/var/tmp/" + zbx_tg_prefix # directory for saving caches, uids, cookies, etc.
zbx_tg_signature = False

zbx_tg_update_messages = True
zbx_tg_matches = {
  "problem": "PROBLEM: ",
  "ok": "OK: "
}

zbx_server = "http://192.168.1.211/zbx/" # zabbix server full url
zbx_api_user = "Admin"
zbx_api_pass = "12345"
zbx_api_verify = False # True - do not ignore self signed certificates, False - ignore

zbx_basic_auth = False
zbx_basic_auth_user = ""
zbx_basic_auth_pass = ""

proxy_to_zbx = None
proxy_to_tg = None

#proxy_to_zbx = "proxy.local:3128"
#proxy_to_tg = "proxy.local:3128"

google_maps_api_key = None # get your key, see https://developers.google.com/maps/documentation/geocoding/intro

#zbx_tg_daemon_enabled = False
#zbx_tg_daemon_wl_ids = [509559395, ]
#zbx_tg_daemon_wl_u = ["zeroxzed", ]

zbx_db_host = "localhost"
zbx_db_database = "zabbix"
zbx_db_user = "root"
zbx_db_password = "12"

emoji_map = {
  "OK": "✅",
  "PROBLEM": "❗",
  "info": "ℹ️",
  "WARNING": "⚠️",
  "DISASTER": "❌",
}


To make the script work successfully, you have to fill in the next variables with the correct values:

tg_key – is a token that was issued by a bot @BotFather when registering a new bot;
zbx_server – is a full address of a Zabbix server;
zbx_api_user – is a login of a Zabbix account;
zbx_api_pass – is a password of a Zabbix account;
zbx_db_database –  is a name of a Zabbix database;
zbx_db_user – is a user for accessing a Zabbix database;
zbx_db_password – is a password for accessing a Zabbix database.

Make the main script file of a «zbxtg.py» script an executable one using a command «# chmod +x /usr/lib/zabbix/alertscripts/zbxtg.py»

CentOS changing rights for launching a script

Configuring Zabbix for Sending Notifications in «Telegram»

Go to a web interface of a Zabbix monitoring system and add a new type of notifications. To do this, go to the section «Administration → Media types» and click the button «Create media type»:

Zabbix adding a new type of a notification

Fill in the fields:

Zabbix parameters of notifications

where:
Name – is a name of a notification type. For our example, «Telegram»;
Type – is a notification type. The next types are available: «Email», «Script», «SMS», «Jabber», «Ez Texting». There is a need to choose a type «Script»;
Script name – is a name of a script file for sending SMS notifications, that is located via the path: «/usr/lib/zabbix/alertscripts». For our example, «zbxtg.py»;
Script parameters – are parameters of a script. Specify parameters the same as in our example. In this case, {ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE} - are parameters that we are going to apply to the script.;
Enabled – is a checkbox for enabling this notification type.

After filling in all notification parameters, click the button «Add».

Then, go to the settings of a user Admin (Administration → Users) and add a new notification type on the tab «Media». Click the button «Add» and specify parameters the same as in our example in the pop-up window «Media» that appears:

Zabbix adding a new notification type for a user

Zabbix adding a new notification for a user. Filling in the parameters

where:
Type – is a parameter for choosing notification types that were previously created in a Zabbix monitoring system (Administration → Media Types). In our case, select a notification type «Telegram» from the list;
Send to – indicate a «Telegram» account of a notification receiver. It is possible to indicate a text login or a numeric ID. In our example, a textual login was indicated;
When active – is the time of triggering of this notification. A configuration can be left on default;
Use if severity – is an importance of a trigger, checkboxes test at which trigger importance this notification will work. In the example, all checkboxes are checked for a better visualization o this parameter;
Enabled – is a parameter for switching this notification on and off.

Save the changes by clicking the buttons «Add» and «Update».

Then, proceed to creating the action, when performing which notifications will be sent to «Telegram». Go to the section «Configuration → Action» and click the button «Create Action»:

Zabbix creating the action

In a window that opens, on the tab «Action», fill in the fields the same in our example. Here, we need to sort out triggers that send notifications to «Telegram» when triggered. This action will be performed when a temperature deviated from a normal temperature range.

Zabbix choosing triggers for the action

where:
Name – is a unique name of the action. «temp_error» in our example;
Type of calculation – is a logic rule type for triggers. «Custom expression», in our example;
Conditions – are conditions at which the action will operate. Names of the triggers that are going to send a graphic image to «Telegram» when triggered, must contain the phrases «Temperature Sensor» and one of the conditions is «Above Safe Range» or «Below Safe Range»;
New condition – is a field for creating a new condition. Adding rules is supported by clicking the link «Add» in the field «New condition»;
Enabled – is a parameter for switching on and off this action.

Then, go to the tab «Operations, and click an SMS text that will be sent to «Telegram» when a trigger that is described in the tab «Action»:

Zabbix configuring a notification

where:
Default operation step duration – is a duration of a single operation step. For example, an operation step duration of one hour means that if the first operation is performed in this action, then the next operation will begin in an hour. In our example, an operation of sending a message is the only one, therefore it is possible to use a default value;
Default subject – is a default notification subject. In our example, it is «{{WARNING}} Problem: {TRIGGER.NAME}»;
Default message – is a default notification. A notification can contain macros. In this field, we write a notification text that will be received by a monitoring system administrator when a trigger worked.

Let's examine the variables that have been used in the notification in a bigger detail:

zbxtg;graphs – indicates that we will send a graph;
zbxtg;graphs_period – is a time period that will be represented on a graph. Time is indicated in seconds;
zbxtg;itemid – is a parameter that determines itemid for a graph. We indicated a necessary itemid manually;
zbxtg;title – sets a name for a heading of a graph.

To determine the itemid that is necessary for you, go to the page «Configuration → Hosts →Items»:

Zabbix Configuration - Hosts - Items

And find a parameter that interests you. For example, a temperature value on the first temperature sensor of a device for a microclimate monitoring of a server room UniPing server solution v4/SMS:

Zabbix selecting a parameter

Open a configuration of the parameter necessary for you. The value of itemid will be in the address bar of a browser:

Zabbix parameter configuration


In the field «Operations», click the link «New» and fill in the form for an action description that opens as in our example. Add a user «Admin» in the field «Sent to Users». In the field «Send only to» choose a notification type «Telegram». Confirm the changes by clicking the link «Add» (1):

Zabbix form for an action description

Confirm creating a new action by clicking the button «Add» (2).

Now repeat creating the action for events of a humidity sensor. Go to the section «Configuration → Action» and click the button «Create Action»:

Zabbix creating the action

In a window that opens, fill in the fields the same as in our example in the tab «Action»:

Zabbix selecting triggers for the action

Then, go to the tab «Operations» and specify the notification text that is going to be sent to «Telegram» when a trigger described on the tab «Action» works:

Zabbix form for describing the action

Confirm creating a new action by clicking the button «Add».

As a result of these configurations, a system administrator will receive the next notifications to «Telegram»:

Telegram General view of notifications with graphs

If you mouse-click the picture with a graph, it will open in a bigger size:

Telegram enlarged notification

Thus, we got one more tool that can make a work of a system administrator a little easier in the part of controlling a server room microclimate.


comments powered by Disqus