Last active
January 10, 2021 16:47
-
-
Save igorhrq/ca56fed09bee568f1bb8581b06642dd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# | |
# This is script is a fork project from assistanz and modified/adapted for monitoring Tibia by Igor Andrade | |
# | |
# This script monitors server load, memory usage and Tibia daemon for every 10 seconds and sends notification | |
# to telegram in case of high usage or server offline. | |
# | |
# Get the status of bot after you start it and get chat id : https://api.telegram.org/bot[TOKENIDHERE]/getupdates | |
# | |
# Send hello world to chat id for test token + chatid : | |
# https://api.telegram.org/bot[TOKENIDAQUI]/sendmessage?chat_id=[CHATIDHERE]&text=Hello+World2 | |
# | |
# Just replace [TOKENIDHERE] for your token and [CHATIDHERE] for chat id that you get on getupdates when you start bot | |
# | |
# Just execute with ./monitor.sh & for runs in background | |
# | |
# will run like https://uploaddeimagens.com.br/images/002/750/716/full/Captura_de_tela_de_2020-07-05_22-54-08.png | |
#Include telegram chat id and bot token ID here | |
chat_id="CHATIDHERE" | |
token="TOKENIDHERE" | |
#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 | |
#Monitoring to see if TFS is online | |
tibia=$(lsof -i:7171| egrep -i LISTEN) | |
if [ -z "$tibia" ] | |
then | |
echo -e "Tibia is offline on $(hostname)\n PSFAUX grepando start.sh:\n $(ps faux | egrep start.sh)" > $msg_caption | |
echo -e "What is running on $(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 | |
if [ -z "$tibia" ] | |
then | |
echo -e "\n\nALERT: There is nothing listening on port 7171, Your OTServ is Offline." >> $resource_usage_info | |
fi | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment