Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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="480631913"
- token="918463820:AAECzo6XYA1jVLxnYs-0qctWdzHQyhXT628"
- #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 /var/log/snort/alert | egrep -wi --color 'Ada|Serangan|ping')
- if [ $min_load -ge $load_threshold ]
- then
- echo -e "High CPU usage detected on $(hostname)\n$(uptime)" > $msg_caption
- echo -e '\E[32m'"ADA SERANGAN :" $tecreset $min_load | 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement