[Gllug] Simple Bash Script

John Edwards john at cornerstonelinux.co.uk
Tue Jun 3 07:49:45 UTC 2008


On Mon, Jun 02, 2008 at 10:41:16AM +0100, william pink wrote:
> Hello All,
> 
> I have a script that checks for system load then sends a email if it is over
> a certain threshold ( Its a cut down version from the one on Nixcraft )
> Here is the script
> 
> 
> #!/bin/bash
> 
> # Script to check system load and remove from cluster if higher than 6.0
> 
> TEMPFILE="$(mktemp)"
> NOTIFY="6.0"
> HOST=`hostname`
> RCLUSTER=`/usr/local/bin/in-cluster no`
> #load at 1 minute
> TRUE="1"
> LOAD=`uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\),
> \(.*\...\)/\1/" -e "s/ //g"`
> 
> #input in tempfile
> echo "Load average to high removed from cluster, Load currently at $LOAD
> allowed limit $NOTIFY." > $TEMPFILE
> 
> # Compare results to benchmark
> RESULT=$(echo "$LOAD > $NOTIFY" | bc)
> 
> # IF so do your thing
> 
> if [ "$RESULT == $TRUE" ]; then
>         $RCLUSTER
>         mail -s "$HOST has been removed from the cluster" "$EMAIL" <
> $TEMPFILE
> fi
> 
> rm -f $TEMPFILE

Hi

I see that others have helped you with the if statements and other
things, but I would like to ask about if the TEMPFILE is really
neccesary.

You should be able to send an email by echo'ing the contents to
mail (or mutt), eg something like:

echo "Load average to high removed from cluster
Load currently at $LOAD
allowed limit $NOTIFY" | mail -s "$HOST has been removed from the cluster" "$EMAIL"


Getting rid of tempfiles is a little personnal mission of mine after
seeing thousands of them generated on some systems. Not only are they
messy if the script ends halfways through, but can also be a security
risk if the name is not suitably random or the contents are not
sanitised.


Apart from that the other thing that might be cleaned up is the maths
evaluations. Rather than defining $TRUE and using bc to generate
$RESULT, I wonder if it's possible to use something like:

if [ $LOAD -gt $NOTIFY ]


That could make the code a little easier to read and remove the need
for the external bc command.


-- 
#---------------------------------------------------------#
|    John Edwards   Email: john at cornerstonelinux.co.uk    |
#---------------------------------------------------------#
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20080603/117d91b6/attachment.pgp>
-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list