Filippo Zanardo's Blog

to blog or not to blog

Posts Tagged ‘script

Nagios Plugin To Check XenServer License

with 4 comments


Hi guys,
since xenserver license is active for 1 year, happens sometimes that i forgot to reactivate the license, and then if u shut down the vm machine they don’t start up if license is expired, i create a nagios plugin to check the days remaining for the license.

here’s the script updated thanks to Martin:

#!/bin/bash
#
# retrieve remaining day for xenserver license
#
# Date: 2010-04-15
# Author: Filippo Zanardo
#

# get arguments

while getopts ‘w:c:hp’ OPT; do
case $OPT in
w) int_warn=$OPTARG;;
c) int_crit=$OPTARG;;
h) hlp=”yes”;;
*) unknown=”yes”;;
esac
done

# usage
HELP=”
usage: $0 [ -w value -c value -h ]

syntax:

-w –> Warning integer value
-c –> Critical integer value
-h –> print this help screen

if [ “$hlp” = “yes” -o $# -lt 1 ]; then
echo “$HELP”
exit 0
fi

# get license expiry date
EX=`xe host-license-view |grep expiry | awk ‘{ print $2; }’`
DEX=${EX:0:8}

today=`date +%Y%m%d`
timeStampToday=`date +%s -d $today`
timeStampOfPast=`date +%s -d $DEX`
secondsInDay=86400
dayDiff=`echo \($timeStampOfPast – $timeStampToday\) / $secondsInDay – 1 | bc`

#dayDiff is the day remaining

OUTPUT=”$dayDiff days remaining”

if [ -n “$int_warn” -a -n “$int_crit” ]; then

err=0

if (( $dayDiff <= $int_warn )); then
err=1
elif (( $dayDiff <= $int_crit )); then
err=2
fi

if (( $err == 0 )); then

echo -n "OK – $OUTPUT"
exit "$err"

elif (( $err == 1 )); then
echo -n "WARNING – $OUTPUT"
exit "$err"

elif (( $err == 2 )); then

echo -n "CRITICAL – $OUTPUT"
exit "$err"

fi

else

echo -n "no output from plugin"
exit 3

fi
exit

just copy it save with some name for example check_xenlicense.sh in the nagios plugins directory and configure to use for example with nrpe.
basic usage is:

check_xenlicense.sh -w 15 -c 5

where “w” is the days remaining for a warning message and “c” is the days remaining for a critical message

if you don’t use nagios you can use as a simple script in a cron job or whatever.

Maybe if someone find this useful i will add the ability to send an email notification.

Cheers
Post for suggestion comment etc etc

Written by pipposan

April 15, 2010 at 12:11

Bacula Script Reuse Tape

leave a comment »


I use bacula to backup my linux/win machine, i see that sometimes retention period or maximum volume job didn’t work as i expect (also recycle current volume) , so i create a script to autopurge/reuse Tape.

Here it is:

#!/bin/sh

console=”/usr/bin/bconsole -c /etc/bacula/bconsole.conf”
command=”$1″
TAPEDRIVE0=/dev/st0
storagename=”Tape”

#mount tape in the drive
PURGE=`echo “mount Tape” | $console`

#print wich tape is mounted
MOUNTED=`echo “status Storage=$storagename” | $console | grep $TAPEDRIVE0 | grep “is mounted with”`

if [ -z “$MOUNTED” ] ; then
echo “Drive is UNMOUNTED: $MOUNTED”
echo “0”
else
#echo “Drive is MOUNTED: $MOUNTED”
VOLUME=`echo “status Storage=$storagename” | $console | grep -A2 $TAPEDRIVE0 | grep “Volume:” | awk {‘print $2’}`
echo “Mounted Volume: $VOLUME”
VOLUMEDAUSARE=`echo “status Director” | $console | grep -A3 “Scheduled Jobs” |grep -A2 “Volume” | awk {‘print $7’} |tr “\n” ” ” |sed -e “s/ //g”`
#echo “VOLUME DA USARE :$VOLUMEDAUSARE”

STATOVOLUME=`echo “list volume=$VOLUME” | $console | grep -A3 “VolStatus” | grep $VOLUME| awk {‘print $6’}`
#echo “:$STATOVOLUME”
#echo “USED: $USED RECYCLE:$RECYCLE APPEND:$APPEND ERROR:$ERROR”

if [ “$STATOVOLUME” = “Append” ]; then
echo “APPEND”
exit
elif [ “$STATOVOLUME” = “Error” ]; then
PURGE=`echo “purge volume=$VOLUME” | $console`
echo “ERROR”
exit
elif [ “$STATOVOLUME” = “Used” ]; then
PURGE=`echo “purge volume=$VOLUME” | $console`
echo “USED”
exit
elif [ “$STATOVOLUME” = “Recycle” ]; then
PURGE=`echo “purge volume=$VOLUME” | $console`
echo “Recycle”
exit
elif [ “$STATOVOLUME” = “Purged” ]; then
echo “Purged”
exit
elif [ “$STATOVOLUME” = “Full” ]; then
PURGE=`echo “purge volume=$VOLUME” | $console`
echo “Full”
exit
fi
fi

PURGE=`echo “umount Tape” | $console`
PURGE=`echo “mount Tape” | $console`
exit 0

Basically check the tape inside the drive and purge it, if tape’s state is Full,Used,Error.

After volume is purged you lost all data, so use it carefully.

Save file with a name.sh and put in RunBeforeJob in every job’s configuration u need.
RunBeforeJob=/path/to/name.sh

Enjoy

Written by pipposan

October 23, 2009 at 10:43

Posted in Bacula

Tagged with , , ,