Reputation: 494
I'm attempting to run a simple bash script which checks the available space on the volumes attached to a Linux machine, then sends the result of any volumes to the output if it is over a particular threshold. The idea behind this is to monitor various servers so that we receive notifications when the volumes are getting full.
The mechanism we'd like to use is Jenkins, so what we need is a job that we can run every minute or so, and that job to fail if any volume is over 80%.
I'm using a script like this at the moment:
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ if ( $5+0 > 80 ) print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
done
This script successfully results in the output I need:
83% /dev/xvda1
92% /dev/xvdb
But this is where I am stuck. After much mucking around I've concluded my best bet would be to cause a failure if there is any output - as expected if I up the threshold to 95 (for example), the output is empty. However, I can't figure out a way to tell Jenkins this is the definition of a failure, because of course the exit status is 0, which Jenkins correctly interprets to mean success.
Can I change this so that this command has an exit status of 0 if there is no output, and a failure status if there is any output? I think that will allow me to do what I am trying to do.
Upvotes: 0
Views: 64
Reputation: 15273
Save the output to a temp var. If it has size, exit with a return code.
Also, drop that extraneous grep
. You're already using awk
.
flag=0
while read output
do flag=1
echo "$output"
done < <( df -H | awk '
/^Filesystem|tmpfs|cdrom/ { next; }
{ if ( $5+0 > 50 ) print $5 " " $1 }'
)
exit $flag
Better, with the help of smarter folk -
df -H | awk '
/^Filesystem|tmpfs|cdrom/ { next; }
{ if ( $5+0 > 80 ) print $5 " " $1; exit 1; }'
Upvotes: 3