2020-11-25 15:47:41 +01:00
# notify-by-telegram
2020-11-26 08:28:40 +01:00
Send Nagios notifications to a [Telegram Messenger ](https://telegram.org/ ) channel.
2020-11-25 17:13:16 +01:00
## Telegram bot
2020-11-26 08:28:40 +01:00
This [tutorial ](https://takersplace.de/2019/12/19/telegram-notifications-with-nagios/ ) explains how to create a Telegram bot. You'll need the `chat_id` and `auth_key` for the next section.
2020-11-25 17:13:16 +01:00
## Installation
_This guide has been written for [Debian ](https://www.debian.org/ ). Some commands might slightly change depending on your distribution._
Clone the repository:
```
2020-11-26 08:28:40 +01:00
git clone https://github.com/jouir/notify-by-telegram.git /opt/notify-by-telegram
2020-11-25 17:13:16 +01:00
```
Install dependencies using the package manager:
```
2020-11-26 14:48:46 +01:00
sudo apt install python3-jinja2 python3-requests python3-jsonschema
2020-11-25 17:13:16 +01:00
```
## Configuration
Copy and update the configuration file example:
```
2020-11-26 14:48:46 +01:00
cp -p config.example.json telegram.json
2020-11-26 08:28:40 +01:00
vim telegram.json
sudo mv telegram.json /etc/nagios4/telegram.json
2020-11-26 13:02:08 +01:00
sudo chown root:nagios /etc/nagios4/telegram.json
sudo chmod 640 /etc/nagios4/telegram.json
2020-11-25 17:13:16 +01:00
```
Ensure Nagios reads the configuration file:
```
2020-11-26 08:28:40 +01:00
echo "cfg_file=/opt/notify-by-telegram/nagios.cfg" >> /etc/nagios4/nagios.cfg
2020-11-25 17:13:16 +01:00
```
Then reload service:
```
systemctl reload nagios4
```
2020-11-26 14:58:40 +01:00
## Configuration file
Format used is JSON with the following keys:
* `chat_id` : where to send message on Telegram
* `auth_key` : key used to authenticate on Telegram
* `host_template` (optional): path to Markdown template file used for sending host notifications
* `service_template` (optional): path to Markdown template file used for sending service notifications
2020-11-26 15:06:25 +01:00
## Logging
2020-11-25 17:13:16 +01:00
Errors logs can be set with the `--logfile` argument.
Example:
```
tail -f /var/log/nagios4/telegram.log
```
Log level can be raised using `--verbose` or even more with `--debug` arguments.
2020-11-26 12:57:32 +01:00
## Message format
`notify-by-telegram` script uses the `MarkdownV2` format to generate Telegram messages.
[Jinja ](https://jinja.palletsprojects.com ) is used for templating (eg. replace `{{host_name}}` placeholders by the value submitted by Nagios).
Default **host** and **service** templates can be found in the [templates ](templates ) directory.
They can be overriden in the configuration file:
```json
{
"host_template": "/etc/nagios4/host.md.j2",
"service_template": "/etc/nagios4/service.md.j2"
}
```
Both options are optional.
2020-11-26 15:06:25 +01:00
### Host variables
Variables replaced in the host template:
* `notification_type` (= `$NOTIFICATIONTYPE$` )
* `host_name` (= `$HOSTNAME$` )
* `host_state` (= `$HOSTSTATE$` )
* `host_address` (= `$HOSTADDRESS$` )
* `host_output` (= `$HOSTOUTPUT$` )
* `long_date_time` (= `$LONGDATETIME$` )
### Service variables
Variables replaced in the service template:
* `notification_type` (= `$NOTIFICATIONTYPE$` )
* `service_desc` (= `$SERVICEDESC$` )
* `host_alias` (= `$HOSTALIAS$` )
* `host_address` (= `$HOSTADDRESS$` )
* `service_state` (= `$SERVICESTATE$` )
* `long_date_time` (= `$LONGDATETIME$` )
* `service_output` (= `$SERVICEOUTPUT$` )
2020-11-26 15:23:52 +01:00
## How to contribute
Contributions are welcomed! Feel free to update the code and create a pull-request.
Be sure to lint the code before:
```
docker build -t pre-commit .
docker run -it -v $(pwd):/mnt/ --rm pre-commit bash
# cd /mnt/
# pre-commit run --all-files
# exit
```