Blib 1.1 has been released and I would like to take a moment to discuss some of the enhancements as well as detail one application that I built using the library. I think the easiest way to accomplish this would be to include an excerpt form the release note in the package and then discuss the diskcheck utility I wrote based on blib.
Release the bash library a.k.a blib version 1.1
License: modified BSD license (re-licensing is strictly prohibited and the entire library plus all original documentation must be distributed intact.)
Currently the library contains the following four files;
base.blib debug.blib std.blib string.blib
Each bash library file has been crafted for a specific purpose and as the library grows these files will be expanded.
The main library file std.blib sets the foundation for the subsequent libraries thus is required for any of their usage. It is important to properly source this into the head of your script prior to accessing any of the other libraries. I hope to expand this file’s usage at some point as well as make it easier to implement further enhancements.
The base.blib file contains a handful of useful generic functions which over time could be branched out into their own repository. I am working to limit such branching but keep in mind that this is always a possibility. For instance the outputMsg and logOutput functions are prime candidates for relocation as well as anything tide to these methods.
String manipulation has been a goal and I have worked to include functions that I use for this purpose some are just encapsulations of bash built in constructs. I am still working on cleaning this library up and really could use some assistance with streamlining this file.
New in this build is the debug library which contains two functions I created to help me troubleshoot new scripts. Primarily I use the setCheckPoint function and only included the setBreakPoint function for the sake of completeness. I am sure over time this library will be enhanced but as of right now this is what I have.
Most of the funcitons in the library have been commented with phpdoc style comment markers. However there are several that I made up for the sake of clarity. Hopefully someone will consider creating a bashdoc utility that can parse these into some sort of useful documentation. As always I have included my usual commentary which sometimes explains what I was thinking when I wrote a particular block of code. This is especially true if I feel that I’ve take a short route that I just couldn’t riddle out a better way. The comments are there to remind me of this and hopefully spark some sort of epiphany at some later date.
So this naturally leads to the diskcheck utility which is a simplistic script that parse the output of a df report and then compares the usage percentages to preset values. If one of these usage exceeds the threshold then we throw an ‘err’ exception to syslog. The following is the entire script:
#!/usr/bin/env bash ############ # diskcheck - A simplistic disk usage threshold checker # # installation- Although I may include an install script, basically the application is installed as follows: # # run file- /usr/local/bin # blib files- /usr/local/lib # config files- /usr/local/etc # # @author Mikel King # @copyright 2010 Olivent Technologies, llc # @package diskcheck # @dependency blib version 1.1 # @version 1.0 # @license http://opensource.org/licenses/bsd-license.php New/ Simplified BSD License # Default threshold levels Warning=70 Error=80 Critical=90 CertainDoom=110 LogFacility="err" . /usr/local/lib/blib/std.blib require ${BlibPath}base.blib require ${BlibPath}string.blib getMyProcessName TmpFile=/tmp/${MyName} # include diskcheck.conf include ${ConfPath}${MyName}.conf # Store df output in temp file df -PH >${TmpFile} # Remember to set the four threshold variables in your source or config file before running this. # # @method setDiskErrMsgs # @descr a local wrapper for encapsulating the four error message functions. # @param string $MSG function setDiskErrMsgs() { setDoomMsg "You face certain DOOM as the free disk space threshold ${CertainDoom}% exceeded on ${Mount} only ${Avail} of ${Size} remaining, resulting in a Resume generating event." setCriticalMsg "Critical: Free disk space threshold ${Critical}% exceeded on ${Mount} only ${Avail} of ${Size} remaining." setErrorMsg "Error: Free disk space threshold ${Error}% exceeded on ${Mount} only ${Avail} of ${Size} remaining." setWarningMsg "Warning: Free disk space threshold ${Warning}% exceeded on ${Mount} only ${Avail} of ${Size} remaining." } function getDiskStats(){ while read Partition Size Used Avail Percent Mount; do case ${Partition} in Filesystem) ;; cdrom) ;; tmpfs) ;; devfs) ;; procfs) ;; map) ;; *) setDiskErrMsgs checkThreshold ${Percent} ;; esac done<${TmpFile} } setLogTag ${MyName} setLogFac ${LogFacility} setLogOptions getDiskStats # CleanUp your toys! rm ${TmpFile}
As you can see it’s rather simple in design. I have taken the liberty of wrapping the lines and colorizing the output to make for easier viewing. At the top of this script we define some default values which can be overridden in the associated config file.
At this point you may wonder why we need all those log tags and facility settings. The simple fact is that as useful as running this on the command line would be you could just run df -Ph yourself and figure it out. I wrote this script to monitor the disk usage levels and send a notice to syslog if a threshold was exceeded. This is particularly handy if you are sending your syslog messages to a remote host. So running diskcheck at the command line will just return an empty command prompt.
In reality it has done so much more as it performed all of the level checks and should something be amiss it is reported to syslog. On my machine I set the following entry in /etc/sysog.conf:
*.err @192.168.106.128
The machine on 192.168.106.128 is a virtual server running rsyslog that I setup to receive inbound syslog traffic on the default syslog UDP port 514. So on my machine I run diskcheck and the following appears in /var/log/messages on the remote syslog server:
2011-02-24T23:02:45+08:00 thoth diskcheck [21163]: Warning: Free disk space threshold 30% exceeded on / only 317G of 500G remaining.
As you can see this is not a particularly difficult script to create and I could have written everything from scratch without using the bash library (blib). However since I have completed this one I wrote a similar script to check load averages in about 2 hours using this one as a guide and blib as the foundation. Honestly this is exactly what prompted me to create blib in the first place.
I’ve been creating scripts for a very long time and I’ve always used bash or php-cli to do this. I know both of these tend to make people shutter in disbelief but if I am creating a script that I wish to run on both the web and cli then php will always be my language of choice. However that is a discussion for another day. I cobbled blib together after years of writing scripts and thought that there has to be a better way to reuse what I’ve already built.
Eventually through much trial and error, yes mostly error, I created a library the library I released in the article introducing Blib the bash library project. Eventually I will move the project into either git or svn and publish access to it via that method.I find that I am writing more scripts in bash again and honestly I believe it is because the library has made it not only easier but kind of fun to do. I hope that you give it a try as I really look forward to see what innovations other people come up with.
Until next time happy scripting…
Download the current copy of blib: https://www.jafdip.net/downloads/blib-1_1.tbz
Blib has been moved to GitHub as blp, so check it out here.