Swapmon - dynamically add/remove swapspace as needed

This shell script will dynamically add/remove swapspace as needed. It is designed for FreeBSD but can probably made to work easily on other BSDs
The script has configureable low and high watermark values (in percent). If the high watermark is reached (a configureable amount of) new swapspace is added. If the low watermark is reached previously added swapspace is removed. Whenever swapspace is adjusted a message is emitted and delivered via cron.

Limitations

The script can only react when it is executed which is typically once or twice per minute. So your system can still run into out-of-swap-space if the script is not called in the time the system reached low watermark to out-of-swap-space.

Usage

Place the script somewhere in your system, for example into /usr/local/sbin/, make it executeable (chmod +x swapmon) and add either add a symlink to swapmon in /usr/local/etc/rc.d or place swapmon into the crontab:
* *     * * *   /usr/local/sbin/swapmon
If you want to run it twice per minute and don't want to use the daemon mode you can simply add another entry:
* *     * * *   sleep 30 && /usr/local/sbin/swapmon

Current version:

swapmon-1.5.tbz 7.660 bytes (SHA1 = 3768e3d10d22d8fa8324b35dd75b1e29643285c4)

ToDo

  • find a better trigger method
  • Changelog

    1.5  Jun 26th 2012
    	- clean up leftover swapfiles upon startup
    	- create minimal swapfile if no swap is configured
    
    1.4  Aug 4th 2010
    	- had to split up the startup script and main script
    	- do a testrun instead of simply demanding to run as UID==0
    	- got rid of bunch of duplicate code as I had to introduce
    	  some new code duplication with the startup wrapper ;)
    	- replaced all repetitive commands by variables
    	- port accepted *yay*
    
    1.3  Aug 4th 2010
    	- implemented use of /etc/rc.subr and all the usual startup commands
    	  stop/start/restart/status
    	- got rid of distfile
    
    1.2  Jul 23rd 2010
    	- implemented daemon mode and startscript support
    	- wrote manpage
    	- submitted port
    
    1.1  May 2nd 2010
    	- replace some awk and bc calls by shell substitution
    	- add version to notifications
    
    1.0  May 1st 2010
    	- initial release