Difference between revisions of "SwapClean"

From James Dooley's Wiki
Jump to: navigation, search
(Created page with "==Overview== Script that is run via cron every ~10 minutes. Main purpose is to clear any swap usage if the system has the enough free ram and load is below a set value. ==Script...")
 
(Script)
Line 3: Line 3:
  
 
==Script==
 
==Script==
<code>[bash,n]#!/bin/bash
+
<code>[bash,n]
 +
#!/bin/bash
 
#Swap Cleaner
 
#Swap Cleaner
 
#Watches swap usage
 
#Watches swap usage
Line 14: Line 15:
 
loadthresh=0 #Load threshold, will not clear if above this value (unless 0)
 
loadthresh=0 #Load threshold, will not clear if above this value (unless 0)
 
logrotate=1000 #Max number of lines in the log file
 
logrotate=1000 #Max number of lines in the log file
 
 
####
 
####
  
Line 25: Line 25:
 
}
 
}
  
curswap=`free | fgrep "Swap:" | awk '{print $3}'`
+
function checkswap {
curfree=`free | fgrep "buffers/cache:" | awk '{print $4}'`
+
 
realload=`top -b -n1 | fgrep "load average" | awk '{print $12}' | cut -d ',' -f1`
+
curswap=`free | fgrep "Swap:" | awk '{print $3}'`
curload=`echo "$realload * 100" | bc | sed 's/[.].*//'`
+
curfree=`free | fgrep "buffers/cache:" | awk '{print $4}'`
loadthresh=`echo "$loadthresh *100" | bc | sed 's/[.].*//'`
+
realload=`top -b -n1 | fgrep "load average" | awk '{print $12}' | cut -d ',' -f1`
swapdif=`expr $curfree - $curswap`
+
curload=`echo "$realload * 100" | bc | sed 's/[.].*//'`
 +
loadthresh=`echo "$loadthresh *100" | bc | sed 's/[.].*//'`
 +
swapdif=`expr $curfree - $curswap`
  
if [ "$swapthresh" -lt "$curswap" ]
+
if [ "$swapthresh" -lt "$curswap" ]
then
 
#Server has swapped and is above its threshold
 
if [ "$freethresh" -lt "$swapdif" ]
 
 
then
 
then
#Free - Swap is greater then freethresh
+
#Server has swapped and is above its threshold
if [ "$curload" -lt "$loadthresh" -o "$loadthresh" -eq 0 ]
+
if [ "$freethresh" -lt "$swapdif" ]
 
then
 
then
#Load is below threshold
+
#Free - Swap is greater then freethresh
if [ ! -e ".swapoff" ]  
+
if [ "$curload" -lt "$loadthresh" -o "$loadthresh" -eq 0 ]
 
then
 
then
plog "Clearing swap"
+
#Load is below threshold
#Not currently clearing swap, nuke it.
+
if [ ! -e ".swapoff" ]
touch .swapoff
+
then
swapoff -a && swapon -a
+
plog "Clearing swap"
rm -f .swapoff
+
#Not currently clearing swap, nuke it.
 +
touch .swapoff
 +
swapoff -a && swapon -a
 +
rm -f .swapoff
 +
plog "Swap Cleared"
 +
else
 +
plog "Lock file found, swap may be already clearing"
 +
fi
 
else
 
else
plog "Lock file found, swap may be already clearing"
+
plog "High load, waiting to clear swap"
 
fi
 
fi
 
else
 
else
plog "High load, waiting to clear swap"
+
plog "Not enough free memory, waiting to clear swap"
 
fi
 
fi
else
 
plog "Not enough free memory, waiting to clear swap"
 
 
fi
 
fi
fi
+
 
 +
}
 +
 
 +
function enablecron {
 +
 
 +
echo "What time would you like to set the cron to"
 +
echo "[IE: */10 * * * * ]"
 +
read crontime;
 +
if [ ! $crontime ]
 +
then
 +
crontime="*/10 * * * *"
 +
fi
 +
echo "SHELL=/bin/bash" > /etc/cron.d/swapclean.sh
 +
echo "$crontime $(readlink -f $0)" >> /etc/cron.d/swapclean.sh
 +
chmod 0755 /etc/cron.d/swapclean.sh
 +
echo "Cron enabled [$crontime $(readlink -f $0)]"
 +
}
 +
 
 +
function disablecron {
 +
 
 +
rm -f /etc/cron.d/swapclean.sh
 +
echo "Cron disabled"
 +
 
 +
}
 +
 
 +
 
 +
case $1 in
 +
        --cron)
 +
                case $2 in
 +
on)
 +
enablecron
 +
;;
 +
off)
 +
disablecron
 +
;;
 +
esac
 +
                ;;
 +
        --help)
 +
                echo "Check swap usage:"
 +
echo " --cron [on, off]"
 +
echo " on: Turns on cron job and asks for time"
 +
echo " off: Turns off the cron job"
 +
                ;;
 +
        *)
 +
                checkswap
 +
                ;;
 +
  esac
 
</code>
 
</code>
  

Revision as of 15:43, 15 April 2011

Overview

Script that is run via cron every ~10 minutes. Main purpose is to clear any swap usage if the system has the enough free ram and load is below a set value.

Script

[bash,n]

  1. !/bin/bash
  2. Swap Cleaner
  3. Watches swap usage
  4. If there is swapping going on and there is enough free (+cache) ram, then turn swap off and back on.
  1. User Assigned Variables

swapthresh=0 #Swap threshold for clearing swap. Will not clear until this value is hit freethresh=0 #Real free (free+cached) - current swap threshold. Ensures that atleast x bytes are free before clearing loadthresh=0 #Load threshold, will not clear if above this value (unless 0) logrotate=1000 #Max number of lines in the log file

function plog { echo "[ `date` ][ $curswap; $curfree; $realload ] $1" >> /var/log/swapclean if [ "`wc -l /var/log/swapclean | awk '{print $1}'`" -gt "$logrotate" ] then sed -i -e "1d" /var/log/swapclean fi }

function checkswap {

curswap=`free | fgrep "Swap:" | awk '{print $3}'` curfree=`free | fgrep "buffers/cache:" | awk '{print $4}'` realload=`top -b -n1 | fgrep "load average" | awk '{print $12}' | cut -d ',' -f1` curload=`echo "$realload * 100" | bc | sed 's/[.].*//'` loadthresh=`echo "$loadthresh *100" | bc | sed 's/[.].*//'` swapdif=`expr $curfree - $curswap`

if [ "$swapthresh" -lt "$curswap" ] then #Server has swapped and is above its threshold if [ "$freethresh" -lt "$swapdif" ] then #Free - Swap is greater then freethresh if [ "$curload" -lt "$loadthresh" -o "$loadthresh" -eq 0 ] then #Load is below threshold if [ ! -e ".swapoff" ] then plog "Clearing swap" #Not currently clearing swap, nuke it. touch .swapoff swapoff -a && swapon -a rm -f .swapoff plog "Swap Cleared" else plog "Lock file found, swap may be already clearing" fi else plog "High load, waiting to clear swap" fi else plog "Not enough free memory, waiting to clear swap" fi fi

}

function enablecron {

echo "What time would you like to set the cron to" echo "[IE: */10 * * * * ]" read crontime; if [ ! $crontime ] then crontime="*/10 * * * *" fi echo "SHELL=/bin/bash" > /etc/cron.d/swapclean.sh echo "$crontime $(readlink -f $0)" >> /etc/cron.d/swapclean.sh chmod 0755 /etc/cron.d/swapclean.sh echo "Cron enabled [$crontime $(readlink -f $0)]" }

function disablecron {

rm -f /etc/cron.d/swapclean.sh echo "Cron disabled"

}


case $1 in

       --cron)
               case $2 in

on) enablecron ;; off) disablecron ;; esac

               ;;
       --help)
               echo "Check swap usage:"

echo " --cron [on, off]" echo " on: Turns on cron job and asks for time" echo " off: Turns off the cron job"

               ;;
       *)
               checkswap
               ;;
 esac

What to change

swapthresh

This value allows swap to get to a certain level before looking to swap.

freethresh

This is the level of extra free ram the script will require before attempting to clean swap

loadthresh

This is the load level threshold at which the script will allow swap to be cleaned.

logrotate

This is the number of lines that will be stored in the log file. When reached the top line will be deleted.