3.9 KiB
Alias | Tag | Date | DocType | Hierarchy | TimeStamp | location | Source | CollapseMetaTable | ||
---|---|---|---|---|---|---|---|---|---|---|
|
2022-03-10 | https://github.com/matriphe/monit2telegram | true |
Parent:: Selfhosting, VPS Console Dialogue, Configuring Monit
name Save
type command
action Save current file
id Save
^button-GitHubmatriphemonit2telegramAsimplescriptNSave
GitHub - matriphemonit2telegram A simple script to send Monit alerts using Telegram bot.
title: Summary
collapse: open
Note Description
style: number
A simple script to send Monit alerts using Telegram bot.
By default, Monit only sends alert notifications via email. But we can also set a hook to execute a script. When executing the script, Monit sets a few environment variables about the alert.
This tiny script transforms those variables into a text message and pipes them to Telegram using a bash script for delivery.
This script is inspired by Monit2Twilio.
Requirements
- Bash
- CURL
- jq
- Configuring Telegram bots
- Configuring Monit
Create Telegram Bot
If you don't have a Telegram Bot, just create one. By using a Telegram bot you don’t have to use a real Telegram client or reuse your Telegram account.
Getting Bot Token
You will get a Telegram Bot Token after bot created. Keep this token, we will use it later. The bot token is looked like this.
123456789:aBcDeFgHiJkLmN-OpQrStUvWXyZ12345678
Getting Chat ID
To send messages to a Telegram chat, you must first needs to start a chat with the bot. Clicking on the bot link after creation should be enough, it will automatically send a message of /start
to the bot.
To get the Chat ID from Telegram bot, execute this command using getUpdates function of Telegram API.
title: Get Chat ID
~~~bash
curl --silent "https://api.telegram.org/bot{TOKEN}/getUpdates" | jq
~~~
title: Output
~~~javascript
{
"ok": true,
"result": [
{
"update_id": 17082016,
"message": {
"message_id": 17,
"from": {
"id": 22031984,
"first_name": "User"
},
"chat": {
"id": 22031984,
"first_name": "User",
"type": "private"
},
"date": 1471402800,
"text": "Hello from the other side~"
}
}
]
}
~~~
In this example the Chat ID to look out for is 22031984. Replace {TOKEN}
with your Telegram bot token.
Usage
Clone this repo or download the zipped file.
~~~bash
git clone https://github.com/matriphe/monit2telegram.git
cd monit2telegram
~~~
Put your Telegram Bot ID and Chat ID in telegramrc
and save it to the /etc
directory (/etc/telegramrc
).
~~~bash
cp telegramrc /etc/telegramrc
~~~
Put sendtelegram.sh
and monit2telegram.sh
to /usr/local/bin
and make them executable.
~~~bash
cp sendtelegram.sh /usr/local/bin/sendtelegram
chmod +x /usr/local/bin/sendtelegram
cp monit2telegram.sh /usr/local/bin/monit2telegram
chmod +x /usr/local/bin/monit2telegram
~~~
Test the sendtelegram
script by running this command.
~~~bash
sendtelegram -c /etc/telegramrc -m "Hello from the other side!"
~~~
title: output
~~~bash
Sending message 'Hello from the other side!' to 22031984
Done!
~~~
You should see Telegram message sent by your Telegram bot.
Set Up Monit
Now you can add Monit alert by adding this line to Monit configuration file.
title: monitrc
~~~bash
check file nginx.pid with path /var/run/nginx.pid
if changed pid then exec "/usr/local/bin/monit2telegram"
~~~