Skip to content

Instantly share code, notes, and snippets.

@slavikme
Last active March 28, 2018 10:19
Show Gist options
  • Save slavikme/884007881de4cce64b8b81437c949e15 to your computer and use it in GitHub Desktop.
Save slavikme/884007881de4cce64b8b81437c949e15 to your computer and use it in GitHub Desktop.
On high system loads turn off http service and send Telegram notification
#!/bin/bash
MAX_THRESHOLD=${MAX_THRESHOLD:-4}
MIN_THRESHOLD=${MIN_THRESHOLD:-2}
TELEGRAM_API="https://api.telegram.org/bot$BOT_API_TOKEN"
monit=`cat /proc/loadavg`
load=`echo $monit | cut -d' ' -f1`
pid=`echo $monit | cut -d' ' -f5`
pinfo=`ps -p $pid u`
notify () {
SEND_CHAT_ID=${2:-$CHAT_ID}
curl "$TELEGRAM_API/sendMessage?chat_id=$SEND_CHAT_ID&parse_mode=HTML&text=$1"
}
msg=""
if (( $(awk 'BEGIN {print ('$load' > '$MAX_THRESHOLD')}') )); then
if [[ $(ps -A | grep httpd) ]]; then
msg="Load is at $load, which is higher than allowed.%0A<pre>$monit</pre>"
/sbin/service httpd stop
notify "The website went offline" $CHANNEL_CHAT_ID
msg="$msg%0A<code>httpd</code><b> service has been stopped</b>"
fi
fi
if (( $(awk 'BEGIN {print ('$load' < '$MIN_THRESHOLD')}') )); then
if [[ ! $(ps -A | grep httpd) ]]; then
msg="Load is normal at $load.%0A<pre>$monit</pre>"
/sbin/service httpd start
notify "The website has been restored" $CHANNEL_CHAT_ID
msg="$msg%0A<code>httpd</code><b> service has been restored</b>"
fi
fi
if [[ "$msg" ]]; then
# if [ -d /proc/$pid ]; then
# msg="$msg%0A<pre>$(echo $text|head -1)%0A$(echo $text|tail -1)</pre>"
# else
# msg="$msg%0AThe process with the PID $pid is no longer alive"
# fi
notify "$msg"
echo "$msg"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment