Last active
January 31, 2023 10:16
-
-
Save mslepko/f570ef591da44e18e59bcab0a30c463d to your computer and use it in GitHub Desktop.
Bash load average check and restart
This file contains hidden or 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 | |
function post_to_slack () { | |
# format message as a code block ```${msg}``` | |
SLACK_MESSAGE="\`\`\`$1\`\`\`" | |
SLACK_URL=https://hooks.slack.com/services/UPDATE_THE_URL | |
case "$2" in | |
INFO) | |
SLACK_ICON=':slack:' | |
;; | |
WARNING) | |
SLACK_ICON=':warning:' | |
;; | |
ERROR) | |
SLACK_ICON=':bangbang:' | |
;; | |
*) | |
SLACK_ICON=':slack:' | |
;; | |
esac | |
curl -X POST --data "payload={\"text\": \"${SLACK_ICON} ${SLACK_MESSAGE}\"}" ${SLACK_URL} | |
mail -s "$1" [email protected] <<< " | |
$1 | |
Thank you." | |
} | |
loadavg=`uptime | awk '{print $10+0}'` | |
# bash doesn't understand floating point | |
# so convert the number to an interger | |
thisloadavg=`echo $loadavg|awk -F \. '{print $1}'` | |
if [ "$thisloadavg" -ge "40" ]; then | |
echo "Busy - Load Average $loadavg ($thisloadavg) " | |
ps aux > /tmp/psaux.tmp | |
post_to_slack "Busy - Load Average $loadavg ($thisloadavg)" "ERROR" | |
~/bin/kill-httpd.sh | |
sleep 30 | |
service httpd restart | |
loadavg=`uptime | awk '{print $10+0}'` | |
thisloadavg=`echo $loadavg|awk -F \. '{print $1}'` | |
post_to_slack "After Restart - Load Average $loadavg ($thisloadavg)" "INFO" | |
else | |
echo "Okay - Load Average $loadavg ($thisloadavg) " | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment