Monitoring Server With Telegram Bot

Hai teman-teman penggiat IT dan selamat datang kembali di blog saya,di artikel kali ini saya akan memberikan contoh script untuk monitoring server dengan bot telegram.

Setup pertama kalian harus memilik akun telegram terlebih dahulu,kemudian chat bot father untuk membuat bot.

Nah,setelah kalian mendapatkan bot tokennya silahkan masukkan token ke script dan chat_id kalian.

untuk mengetahui chat_id kalian dengan mudah,kalian bisa melakukannya dengan mengirim pesan kepada bot yang akan kalian gunakan. Setelah itu kalian bisa mengunjungi URL ini :

https://api.telegram.org/bot653769905:AAHOdS0wa0Hoietq_YWZelZk1_vHTtk7mOg/getupdates

Ganti text yang bercetak tebal dengan bot token kalian,seperti gambar dibawah kalian akan mendapatkan chat_id kalian

Script nya silahkan kalian copy paste dan simpan dengan extension file *.sh khusus untuk linux.

Nah kalian bisa combine script tersebut dengan pm2 agar pm2 bisa melakukan pada task background sytem. yaa bisa juga kalian kalian konfigurasikan ketika startup dll.

Script :

#!/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="Your Chat ID"
token="Your Telegram Bot Token"


#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
}

function bot_started
{
    curl -s -F chat_id=$chat_id -F text="I'am Started" https://api.telegram.org/bot$token/sendMessage > /dev/null 2&>1
}

bot_started

#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

Refrence https://www.assistanz.com/get-server-notification-telegram-app/

Leave a Reply

Your email address will not be published. Required fields are marked *