xen nagios plugin

Introduction

In this post we will share about the “Xen Server License Nagios Plugin” written in bash script. It was good experience to create the nagios plugin for Xen server license.

Task

To show how many days are left for Xen Server expiration.

Requirement

You must have following items to be installed in system.

1. Nagios Plugins
2. NRPE

Create check_xenlicense nagios plugin

Create a bash script file called check_xenlicense by using your favorite file editor. We generally prefer vi/vim.

vi /usr/lib/nagios/plugins/check_xenlicense

Paste the given below contents in the bash script file.

#!/bin/bash
#
# Author Sharad Kumar Chhetri
# Date -6 /June/2010

# get arguments

while getopts 'w:c:h' 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

EX=`sudo xe host-license-view |grep expiry | awk '{ print $2 }'`
EXPIRYDATE=`echo $EX | sed 's/([0-9]*).*/1/'`
today=`date +%Y%m%d`
timeStampToday=`date +%s -d $today`
DateOfExpire=`date +%s -d $EXPIRYDATE`
secondsInDay=86400
dayDiff=`echo ($DateOfExpire -$timeStampToday) / $secondsInDay | bc`
#echo $dayDiff

#echo "$x days are left to expire"
OUTPUT="$dayDiff days are left to expire Xen License"

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

if [ "$perform" = "yes" ]; then
echo -n "OK - $OUTPUT"
exit "$err"
else
echo -n "OK - $OUTPUT"
exit "$err"
fi

elif (( $err == 1 )); then
if [ "$perform" = "yes" ]; then
echo -n "WARNING - $OUTPUT"
exit "$err"
else
echo -n "WARNING - $OUTPUT"
exit "$err"
fi

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

if [ "$perform" = "yes" ]; then
echo -n "CRITICAL - $OUTPUT"
exit "$err"
else
echo -n "CRITICAL - $OUTPUT"
exit "$err"
fi

fi

else

echo -n "no output from plugin"
exit 3

fi
exit 0

Give /bin/bash shell to nagios user in client machine

usermod -s /bin/bash nagios

In nrpe client machine, edit /etc/sudoers file.And do the following editing.

nagios ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/check_xenlicense , /usr/lib/nagios/plugins/check_by_ssh , /bin/bash ,/opt/xensource/bin/xe, /usr/bin/xe

Note : The value of $perform is not set in this script but script will work , you can customize this section.

Read Some More Articles

3 Comments

    1. ok, I got the error.

      Line 33 EX=`sudo xe host-license-view |grep expiry | awk ‘{ print $2 }’ | cut -d”T” -f1`
      Line 34 — not required now —–
      Line 39 dayDiff=`echo “($DateOfExpire -$timeStampToday) / $secondsInDay” | bc`

    2. Hi Pankasj,

      When I used to work almost 8 years back at that time I have written and it was working. Right now I do not have this setup.
      May be there is workaround on command output sudo xe host-license-view

      Regards
      Sharad

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.