Monitoring the server is crucial to have uninterrupted services. There are different ways to configure alerts from the server. Here, in this blog, we will explore an interesting way to get the notification from the server right on your Telegram Messenger application.
What is telegram?
Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed. For more details, please visit the official website.
Telegram Bot
Bots are nothing but telegram accounts that are operated by software. They can do almost everything that a user can do in the chat application. For more details, refer to the official website. Here, we will be using this bot feature to send notifications from the server to the Telegram Messenger application.
How do you create the bot?
- By using Botfather in Telegram, we can create a new bot. Go to the search field in Telegram and type botfather.
- Now, go to the message field and click on start.
- Type ‘/newbot’ without quotes in the message field and hit enter
- Now bot’s father asks you to name the new bot. Give any name of your choice. In this case, we named it ‘Jarvis’.
- Bot requires a unique username. Type a username of your choice. If it is already taken, try a different one.
- Once a username is successfully generated, you’ll get an acknowledgment message from Botfather with the token ID of the bot. This token ID is used in the Telegram API to send messages.
- Next, we need to get the chat ID. Search for the newly created bot in the search field and start a chat session.
- To get the chat ID, use the URL https://api.telegram.org/bot$token/getupdates (Replace $token with actual token ID obtained during bot creation) in the browser which returns with string of values.
In the screenshot, the highlighted section has the chat ID.
Incase of group chat, the bot has to be invited to the group and then run the getupdates API to get the chat ID. The group chat ID will have a negative sign at the beginning.
- To test if the bot is working, run the URL https://api.legram.org/bot$token/sendMessage?chat_id=$chaitid&text=Hello+World in the browser. (Replace $token and $chatid with corresponding IDs). The message ‘Hello World’ will be received in the Telegram.
Simple bash script to send notification via telegram bot
#!/bin/bashchat_id=”123456789″ token=”112233445:AABbcd1FfggHHi22jkLmmn0PPQQRstu” msg=”Test Message”curl -s -F chat_id=$chat_id -F text=”$msg” https://api.telegram.org/bot$token/sendMessage > /dev/null
Bash Script to send report to telegram if high resource usage is found in the server
Here is a bash script that can monitor server load and memory usage at regular intervals. It has been tested on Centos 7. The telegram API is used to send an attachment instead of just a text message. This is because sending text has a character limit. Resource usage statistics can be sent in the attachment instead of the plain message.
#!/bin/bash
#This script monitors server load and memory usage for every 10 seconds and sends notification to telegram in case of high usage.
#Include telegram chat id and bot token ID here
chat_id=”123456789″
token=”112233445:AABbcd1FfggHHi22jkLmmn0PPQQRstu”
#Temporary files to store data
resource_usage_info=/tmp/resource_usage_info.txt
msg_caption=/tmp/telegram_msg_caption.txt
#Set threshold levels for memory usage and load here. If the usage exceeds these values, the notification will be sent.
mem_threshold=90 #Should be interger. This is in percentage
load_threshold=$(nproc) #Should be integer. Usually total number of cores.
#Telegram API to send notificaiton.
function telegram_send
{
curl -s -F chat_id=$chat_id -F document=@$resource_usage_info -F caption=”$caption” https://api.telegram.org/bot$token/sendDocument > /dev/null 2&>1
}
#Monitoring Load on the server
while :
do
min_load=$(cat /proc/loadavg | cut -d . -f1)
if [ $min_load -ge $load_threshold ]
then
echo -e “High CPU usage detected on $(hostname)\n$(uptime)” > $msg_caption
echo -e “CPU usage report from $(hostname)\nServer Time : $(date +”%d%b%Y
%T”)\n\n\$uptime\n$(uptime)\n\n%CPU %MEM USER\tCMD” > $resource_usage_info
ps -eo pcpu,pmem,user,cmd | sed ‘1d’ | sort -nr >> $resource_usage_info
caption=$(<$msg_caption)
telegram_send
rm -f $resource_usage_info
rm -f $msg_caption
sleep 900 #stop executing script for 15 minutes
fi
sleep 10
#Monitoring Memory usage on the server
mem=$(free -m)
mem_usage=$(echo “$mem” | awk ‘NR==2{printf “%i\n”, ($3*100/$2)}’)
if [ $mem_usage -gt $mem_threshold ]
then
echo -e “High CPU usage detected on $(hostname)\n$(uptime)” > $msg_caption
echo -e “CPU usage report from $(hostname)\nServer Time : $(date +”%d%b%Y%T”)\n\n\$free -m output\n$mem\n\n%MEM %CPU USER\tCMD” > $resource_usage_info
ps -eo pmem,pcpu,user,cmd | sed ‘1d’ | sort -nr >> $resource_usage_info
caption=$(<$msg_caption)
telegram_send
rm -f $resource_usage_info
rm -f $msg_caption
sleep 900 #stop executing script for 15 minutes
fi
sleep 10
done
Video Tutorial
Thank you for taking the time to read our blog on “How to get server notifications on the Telegram application?.” We hope you found the information valuable and insightful. If you find any issues with the information provided in this blog don’t hesitate to contact us (info@assistanz.com).
Optimize server speed and never lose a valuable customer again!
Our mission is to ensure that your server remains lightning-fast and protected by monitoring and maintaining it 24×7 with our experts.