Get Server Notification on Telegram App

Get Server Notification on Telegram App

[vc_row][vc_column][vc_column_text]Monitoring the server is crucial to have uninterrupted services. There are different ways to configure alerts from the server. Here is an interesting way to get the notification from the server right on your Telegram Messenger application.

Telegram Messenger
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 simply Telegram accounts operated by software. They can do almost everything that a user can do in the chat application. For more details, refer to this link

Here, we will be using this bot feature to send notifications from server to Telegram Messenger application.

Creating Telegram Bot

  • By using botfather in the Telegram, we can create a new bot. Go to the search field in the Telegram and type botfather.
  • Go to message field and click on start.

Telegram-1

  • Type ‘/newbot’ without quotes in the message field and hit enter
  •  Now bot father asks you to name the new bot. Give any name of your choice. In this example, it is named as ‘Jarvis’.

Telegram-2

  • 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 a congratulatory message from botfather with the token id of the bot. This token ID is used in the Telegram API to send messages.

Telegram-3

 

  • Next we need to get the chat ID. Search for the newly created bot in the search field and start a chat session.

Telegram-4

Telegram-5

          In the screenshot, the highlighted section has the chat id.
          In case of group chat, the bot has to be invited to the group and then run getupdates API to get the chat ID. The group chat ID will have a negative sign at the beginning.

Telegram-6

Simple bash script to send notification via telegram bot.

#!/bin/bash chat_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 attachment instead of just text message. This is because sending text has character limit. Resource usage statistics can be sent in attachment instead of 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 Memory usage detected on $(hostname)\n$(echo $mem_usage% memory usage)" > $msg_caption
        echo -e "Memory consumption 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

Thanks for reading the article. If you have any questions, leave the comments below.

[/vc_column_text][/vc_column][/vc_row]

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn
More Interested Related Posts
zfs pool usage
ZFS Pool and Dataset Usage

I am back with another blog to provide the useful script to the Freebsd users to determine the zpool and it’s datasets usage report. This

DirectAdmin Server Management Plans

Premium Support

24/7 End User Support from your Helpdesk
$ 99 Monthly / Server
  • Unlimited Support Plan
  • 24/7 Emergency Phone
  • Chat Support for Admin
  • Separate Account Manager
  • NDA & SLA
  • SLA Review Meetings
  • FREE Consultancy Services
  • Simple SignUp Process
  • Instant Account Activation
  •  

Platinum Support

24/7 Proactive Support
$ 49 Monthly / Server
  • Unlimited Support Plan
  • 24/7 Emergency Phone
  • Chat Support for Admin
  • Separate Account Manager
  • Advance Proactive Monitoring
  • Guaranteed SLA
  • SLA Review Meetings
  • 3rd Party Application Support
  • FREE Consultancy Services
  • Server Migration Support
  • Weekly Status Report
  • No End User Support
Popular

Unlimited Support

24/7 Support

$ 30 Monthly / Server
  • Unlimited Support Plan
  • 24/7 Support
  • Basic Monitoring
  • 30 Minutes Response Time
  • 4 Hours Resolution time for the Possible Issues
  • Security and Performance Optimization
  • React to Customers queries
  • Simple SignUp Process
  • Instant Account Activation
  • No Third party application support
  • No Migration and End User Support

Cpanel Server Management Plans

Premium Support

24/7 End User Support from your Helpdesk

$ 99 Monthly / Server
  • 24/7 End User White Label Support
  • Unlimited Number of Tickets
  • Chat Support for Admin
  • Separate Account Manager
  • Guaranteed SLA
  • Weekly Status Reports
  • FREE Consultancy Services
  • Simple SignUp Process
  • Instant Account Activation

Platinum Support

24/7 Proactive Server Management
$ 49 Monthly / Server
  • Unlimited Support Plan
  • 24/7 Emergency Phone
  • Chat Support for Admin
  • Separate Account Manager
  • Advance Proactive Monitoring
  • Guaranteed SLA
  • SLA Review Meetings
  • 3rd Party Application Support
  • FREE Consultancy Services
  • Server Migration Support
  • Weekly Status Report
  • No End User Support
Popular

Unlimited Support

24/7 Server Management

$ 29 Monthly / Server
  • Unlimited Admin Tasks
  • 24/7 Support
  • Basic Monitoring
  • 30 Minutes Response time
  • 4 hours Response time for possible issues
  • Security and Performance Optimization
  • React to Customers queries
  • No Third party application support
  • No Migration and End User Support

Cpanel Server Management Plans

Premium Support

24/7 End User Support from your Helpdesk

$ 99 Monthly / Server
  • 24/7 End User White Label Support
  • Unlimited Number of Tickets
  • Chat Support for Admin
  • Separate Account Manager
  • Guaranteed SLA
  • Weekly Status Reports
  • FREE Consultancy Services
  • Simple SignUp Process
  • Instant Account Activation

Platinum Support

24/7 Proactive Server Management
$ 49 Monthly / Server
  • Unlimited Support Plan
  • 24/7 Emergency Phone
  • Chat Support for Admin
  • Separate Account Manager
  • Advance Proactive Monitoring
  • Guaranteed SLA
  • SLA Review Meetings
  • 3rd Party Application Support
  • FREE Consultancy Services
  • Server Migration Support
  • Weekly Status Report
  • No End User Support
Popular

Unlimited Support

24/7 Server Management

$ 29 Monthly / Server
  • Unlimited Admin Tasks
  • 24/7 Support
  • Basic Monitoring
  • 30 Minutes Response time
  • 4 hours Response time for possible issues
  • Security and Performance Optimization
  • React to Customers queries
  • No Third party application support
  • No Migration and End User Support