Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.42 KB | None | 0 0
  1. #!/bin/bash
  2. #This script monitors server load and memory usage for every 10 seconds and sends notification to telegram in case of high usage.
  3.  
  4. #Include telegram chat id and bot token ID here
  5. chat_id="480631913"
  6. token="918463820:AAECzo6XYA1jVLxnYs-0qctWdzHQyhXT628"
  7.  
  8. #Temporary files to store data
  9. resource_usage_info=/tmp/resource_usage_info.txt
  10. msg_caption=/tmp/telegram_msg_caption.txt
  11.  
  12. #Set threshold levels for memory usage and load here. If the usage exceeds these values, the notification will be sent.
  13. mem_threshold=90 #Should be interger. This is in percentage
  14. load_threshold=$(nproc) #Should be integer. Usually total number of cores.
  15.  
  16. #Telegram API to send notificaiton.
  17. function telegram_send
  18. {
  19. 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
  20. }
  21.  
  22. #Monitoring Load on the server
  23. while :
  24. do
  25.     min_load=$(cat /var/log/snort/alert | egrep -wi --color 'Ada|Serangan|ping')
  26.     if [ $min_load -ge $load_threshold ]
  27.         then
  28.         echo -e "High CPU usage detected on $(hostname)\n$(uptime)" > $msg_caption
  29.         echo -e '\E[32m'"ADA SERANGAN :" $tecreset $min_load | sed '1d' | sort -nr >> $resource_usage_info
  30.         caption=$(<$msg_caption)
  31.         telegram_send
  32.         rm -f $resource_usage_info
  33.         rm -f $msg_caption
  34.         sleep 900 #stop executing script for 15 minutes
  35.     fi
  36.     sleep 10
  37. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement