Notify Monit alerts to Telegram
Monit is an open-source tool for monitoring your machine’s processes, files, directories, file systems, disks, etc.
Monit is controlled via a simple configuration file called MONITRC. I have taken an Ubuntu machine to install Monit. The config files of Monit reside in the Home directory.
In this article, we will configure Monit to send alerts to Telegram messenger.
There are only five steps to accomplish this setup
- Install MONIT on your machine
- Enable Monit web interface
- Create a Telegram bot
- Alert configuration
- Test with NGINX
1. Install Monit on an Ubuntu machine
The first thing we do is install Monit and make it run on our machine.
Step 1: Update your base system with the latest packages
$ sudo apt-get update
Step 2: Now install Monit using the command below
$ sudo apt-get install monit -y
Step 3: After installation, the service needs to be started and enabled.
$ systemctl start monit$ systemctl enable monit
Step 4: Monit Status check
$ systemctl status monit
2. Enabling Monit web interface
The web interface will help to list out all the resources which we monitor using Monit.
Step 1: Navigation
Default Monit port is 2812
The main config file of Monit resides in
/etc/monit/monitrc
Step 2: Edit monitrc
Either you can edit the following or you can add a new configuration here.
Step 4: Access Monit web
Now access the Monit web interface
3. Telegram bot
To receive the alert messages from Monit the Telegram bot is needed.
We need two id’s from Telegram.
- Token ID
- Chat ID
Step 1: Token ID
Create a telegram bot
Go to your Telegram app and search for Bot Father
**Type /newbot and give the bot a name
Once the bot is created it will give us a Token ID. Save that for later use
Step 2: Chat id
Now to get a chat id use the following URL
http://api.telegrams.org/bot<token-id>/getupdatesexample:
https://api.telegram.org/bot5184745265:AAFTwpyJRtpeFF6czU46-y34Qq8zGfBqypE/getupdates
Result:
The chat id will be on that web page. Just copy it to the clipboard for later use.
4. Alert configuration
Let us see a simple script that sends Monit alerts to the Telegram bot. When executing the script, Monit sets a few environment variables and transforms those variables into a text message, and pipes them to Telegram for delivery.
Step 1: Files
we need three files to configure the alert mechanism
i. monit2telegram.sh
ii. Sendtelegram.sh
iii. Telegramrc
git clone https://github.com/matriphe/monit2telegram
Clone the above repository onto your machine.
Step 2: Edit Telegramrc
Edit telegramrc file and provide your token and chat id.
$ sudo vi /etc/telegramrc
Step 3: Copy files and make them executable
cp sendtelegram.sh /usr/local/bin/sendtelegram
chmod+x /usr/local/bin/sendtelegramcp monit2telegram.sh /usr/local/bin/monit2telegram
chmod+x /usr/local/bin/monit2telegram
Step 4: Test the script
Test the sendtelegram
script by running this command.
# sendtelegram -c /etc/telegramrc -m “Hello from the other side!”
Output:
The message will be delivered to your telegram messenger as below
The connection between Monit to telegram has been made.
Now it’s time to check
5. Test with NGINX
Step 1: Install NGINX
Install NGINX in your machine
$ sudo apt-get install nginx
Step 2: Config monitrc
we are going to set some config in monitrc file to look for the change in ‘PID’ and if any changes occur it will alert to telegram
Now a process id is created in
var/run/Nginx.pid
monitrc:
Edit your monitrc file:
check process nginx with pidfile /var/run/nginx.pid
if changed pid then exec “/usr/local/bin/monit2telegram”
You can see the Nginx process in your web interface
Step 3: Test
Kill the Nginx service and restart it. When the ‘PID’ changes the alert message will be sent to Telegram.
Result:
There we go, we have configured the alert mechanism.
Francium Tech is a technology company laser-focused on delivering top-quality software of scale at extreme speeds. The numbers and Size of the data excite us. If you have any requirements in building a large-scale application or want a free health check of your systems or architecture, feel free to shoot an email to contact@francium.tech, we will get in touch with you!
Ten articles before and after
Bot Telegram. Por Prensa Glufco – Telegram 中文版
AMA with TooNFT. On the 19th of April, 2022 at 10:00… – Telegram 中文版
React Native Chat 2022. Reply on Swipe like in Telegram. Part 2 – Telegram 中文版
AMA with Konstellation. On the 8th of April, 2022 at 02:00 PM… – Telegram 中文版
data-rh=”true”>Useful Telegram Bot you should care – Moblize.IT LLC – Medium – Telegram 中文版
New Pangolin Telegram Group Explained – Telegram 中文版
La base de datos suprema. Solo escribo esto pa que Madi no me… – Telegram 中文版
Telegram OSINT: Generating a data ‘backbone’ for investigation – Telegram 中文版
Leef miner v0.01.5. Leef Miner is a community chat mining… – Telegram 中文版