From 12c4b63473fe8e8320c1cf8faf4da7c3bf3a97c5 Mon Sep 17 00:00:00 2001 From: Russ Long Date: Mon, 30 Mar 2015 19:07:34 -0400 Subject: [PATCH] Initial github commit --- README | 0 swapupdate.sh | 0 swapwatch.sh | 98 +++++++++++++++++++++++++++++++++++++++++++++ swapwatchinstall.sh | 75 ++++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 README create mode 100644 swapupdate.sh create mode 100644 swapwatch.sh create mode 100644 swapwatchinstall.sh diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/swapupdate.sh b/swapupdate.sh new file mode 100644 index 0000000..e69de29 diff --git a/swapwatch.sh b/swapwatch.sh new file mode 100644 index 0000000..c323927 --- /dev/null +++ b/swapwatch.sh @@ -0,0 +1,98 @@ +#!/bin/bash +#version number +#2.0.0 +#rewrite by rlong + +LOGDIR="/usr/local/lp/logs" +TMPFILE="/usr/local/lp/temp/swapwatch.results" + +#Create and clear TMPFILE +touch $TMPFILE +cat /dev/null > $TMPFILE + +#check if it's already running +if [ "$(ps ax | grep "$(basename $0)" -c )" -gt 4 ] +then + exit 1 +else + + #Find Current Load + load=$(awk '{print $1}' /proc/loadavg | cut -d. -f1) + + #Find Current Free Swap + swap_free=$(grep SwapFree /proc/meminfo | awk '{print $2}') + + #Find Swap Total + swap_total=$(grep SwapTotal /proc/meminfo | awk '{print $2}') + + #get thresholds + source /usr/local/lp/configs/swapwatch/swapwatch.conf + + #if that fails (file got rm'd?), use some defaults + if [ "$?" -ne 0 ] + then + loadthreshold=5 + swapthreshold=524288 + contact="support@liquidweb.com" + fi + + #check if we should run. either can trigger it + #make sure server has a swap partition > 0 too + if [[ "$load" -gt "$loadthreshold" || ("$swap_free" -lt "$swapthreshold" && "$swap_total" -ne 0) ]] + then + + #Echo everything into a temp file, to help with ZD compatibility + echo "Load at $load" >> $TMPFILE + echo "" >> $TMPFILE + echo "Swap free at $swap_free" >> $TMPFILE + echo "" >> $TMPFILE + echo "Swap threshold is at $swapthreshold" >> $TMPFILE + echo "" >> $TMPFILE + echo "Apache Status" >> $TMPFILE + echo "" >> $TMPFILE + /usr/bin/lynx -dump -width 500 -connect_timeout=10 http://127.0.0.1/whm-server-status | grep -v ___ | grep -v OPTIONS >> $TMPFILE 2>&1 + echo "" >> $TMPFILE + echo "" >> $TMPFILE + echo "Busiest Sites" >> $TMPFILE + echo "" >> $TMPFILE + /usr/bin/lynx -dump -width 500 -connect_timeout=5 http://127.0.0.1/whm-server-status | awk 'BEGIN { FS = " " } ; { print $12 }' | sed '/^$/d' | sort | uniq -c | sort -rn >> $TMPFILE 2>&1 + echo "" >> $TMPFILE + echo "" >> $TMPFILE + echo "Netstat for Port 80 Abuse" >> $TMPFILE + echo "" >> $TMPFILE + netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -f1 -d: | sort | uniq -c | sort -rn | head >> $TMPFILE 2>&1 + echo "" >> $TMPFILE + echo "" >> $TMPFILE + echo "MySQL Process List" >> $TMPFILE + echo "" >> $TMPFILE + mysqladmin proc stat --verbose >> $TMPFILE 2>&1 + echo "" >> $TMPFILE + echo "Process List" >> $TMPFILE + echo "" >> $TMPFILE + /bin/ps faux >> $TMPFILE 2>&1 + echo "" >> $TMPFILE + echo "" >> $TMPFILE + echo "Processes sorted by RESIDENT memory" >> $TMPFILE + echo "" >> $TMPFILE + ps -e -o pid,rss,vsz,args --sort -rss,-vsz >> $TMPFILE 2>&1 + + + + #Send the email to the $contact address + subject="[LW] Swapwatch.sh on $(hostname)" + contents=$(cat $TMPFILE) + + /usr/sbin/sendmail "$contact" < /dev/null 2>&1 + + #log that swapwatch triggered + echo "[ $(date +%c) ] swapwatch triggered - Load $load - Free swap $swap_free" >> /usr/local/lp/logs/swapwatch.log + fi +#remove the tempfile +rm -f $TMPFILE +fi diff --git a/swapwatchinstall.sh b/swapwatchinstall.sh new file mode 100644 index 0000000..b7fab8e --- /dev/null +++ b/swapwatchinstall.sh @@ -0,0 +1,75 @@ +#!/bin/bash +#this will create swapwatch.sh, swapwatch.conf, and crontab to run swapwatch.sh, and swapwatchupdater + +############################################################################### + +APPDIR="/usr/local/lp/apps/swapwatch" +LOGDIR="/usr/local/lp/logs" +CONFDIR="/usr/local/lp/configs/swapwatch" +swapurl="http://layer3.liquidweb.com/monitoring/swapwatch.sh" #this will be appserv or nagilink +updateurl="http://layer3.liquidweb.com/monitoring/swapupdate.sh" #this will be appserv or nagilink + +############################################################################### + +#set default values, these can be changed in swapwatch.conf, but they are +#better than ridgid default values + +#cpus is an easy count +cpus=$(grep processor /proc/cpuinfo -c) +#memory in kB +memory=$(grep MemTotal /proc/meminfo |awk '{print $2}') + +#load trigger is 5 * CPUs +loadthresh=$( expr $cpus \* 5 ) + +#memory is: 2G thresh for 7+, 1G for 2+, .5G for less than that +G=1048576 #your a gigabyte +if [ $memory -gt $( expr 7 \* $G ) ] +then + swapthresh=$( expr 2 \* $G ) +elif [ $memory -gt $( expr 2 \* $G ) ] +then + swapthresh=$G +else + swapthresh=$( expr $G / 2 ) +fi + +############################################################################### + +##create the config file +#loadthreshold=5 +#swapthreshold=1048576 +#update=yes +mkdir -p "$CONFDIR" +cat > "$CONFDIR/swapwatch.conf" << EOM +loadthreshold=$loadthresh +swapthreshold=$swapthresh +update=yes +contact="support@liquidweb.com" +EOM + +############################################################################### + +#install +mkdir -p "$APPDIR" +echo "downloading swapwatch.sh" +wget -O "$APPDIR/swapwatch.sh" $swapurl +chmod +x "$APPDIR/swapwatch.sh" +echo "*/3 * * * * root $APPDIR/swapwatch.sh > /dev/null 2>&1" > /etc/cron.d/swapwatch + +############################################################################### + +##get update script +wget -O "$APPDIR/swapupdate.sh" $updateurl +chmod +x "$APPDIR/swapupdate.sh" +##check randomly once a week, to prevent tons of simmul conns to appserv +min=$[ ( $RANDOM % 60 ) ] #0-59 +hour=$[ ( $RANDOM % 24 ) ] #0-23 +dow=$[ ( $RANDOM % 7 ) ] #0-6 +# $min $hour * * $dow +echo "$min $hour * * $dow root $APPDIR/swapupdate.sh > /dev/null 2>&1" > /etc/cron.d/swapupdate + +############################################################################### + +#log install +echo "[ $(date +%c) ] Swapwatch installed" >> $LOGDIR/swapwatch.log