New XIV plugin

Esteban Monge esteban at nuevaeralatam.com
Tue Apr 2 01:55:09 CEST 2013


>> Hello:
>>
>> I am writing one plugin for check XIV API, at the moment I can check:
>> CF
>> DIMM
>> Ethernet Cable
>> FAN
>> MODULES
>> PSU
>> UPS
>>
>> I am using Perl with Nagios::Plugins, also I am using IBM::XCLI(1)
>>
>> I add to original IBM::XCLI module the possibility of obtain only
>> modules
>> in raw format. But you can try with other checks.
>>
>> But I have problems with output, when I run the plugin in bash I get OK,
>> CRITICAL and UNKNOW results, but when Nagios run the check I get "Return
>> code of 9 is out of bounds."
>>
>>
>> I am not a Perl writers, it's my first plugin, I paste the code:
>>
>> #!/usr/bin/perl
>> # Copyright: Ton Voon, 2006
>> # Modified by Esteban Monge, 2013
>> #
>> # This is the script used in the Nagios Plugin lightning talk
>> # given by Ton Voon at FOSDEM 2007.
>> # See http://www.nagioscommunity.org for more details
>> #
>> # This script is available under GPLv2, though the ideas are
>> # public domain :)
>> #
>> # You will need to set the permissions of the script so it is executable
>>
>> # Get these plugins from CPAN
>> use Nagios::Plugin;
>> use Nagios::Plugin::Functions;
>> use IBM::XCLI;
>> use warnings;
>> use Switch;
>>
>> #Function for evaluate if any component isn't OK
>> sub print_array {
>>                 $end_code = 0;
>>                 local (@array) = @_;
>>                 foreach (@array) {
>>                         s/^"\|"$//g;
>>                         my(@array) = split /","/;
>>                         substr($array[0], 0, 1, '');
>>                         if ( $array[1] ne "Status" ) {
>>                                 print
>> "Component:\t$array[0]\tStatus:\t$array[1]\n";
>>                                 if ($array[1] ne "OK" ) {
>>                                         $end_code = 2;
>>                                 }
>>                         }
>>                 }
>>                 if ( $end_code eq 0 ) {
>>                         nagios_exit( 'OK', 'Full, full, perfect' );
>>                 }
>>                 if ( $end_code eq 2 ) {
>>                         nagios_exit( 'CRITICAL', 'Something is Wrong' );
>>                 }
>> }
>>
>> #Define xcli path usually /opt/XIVGUI/xcli
>> $xcli_path = "/opt/XIVGUI/xcli";
>> $np = Nagios::Plugin->new(
>>         shortname => "Check_XIV.pl",
>>         usage => "Usage: %s [-m] [-u] [-p] [-c]",
>>          );
>>
>> $np->add_arg(
>>         spec => "machine|m=s",
>>         help => "-m, --machine=IP. IP of XIV machine",
>>         required => 1,
>>         );
>>
>> $np->add_arg(
>>         spec => "username|u=s",
>>         help => "-u, --username=someuser. User account with read
>> permissions in XIVGUI",
>>         required => 1,
>>         );
>>
>> $np->add_arg(
>>         spec => "password|p=s",
>>         help => "-p, --password=somepassword. Valid password for the
>> user
>> of XIVGUI",
>>         required => 1,
>>         );
>>
>> $np->add_arg(
>>         spec => "command|c=s",
>>         help => "-c, --command=somecommand. One command from list_dimm",
>>         required => 1,
>>         );
>>
>> $np->getopts;
>>
>> my $xiv = IBM::XCLI->new(
>>                       ip_address      =>      $np->opts->machine,
>>                       username        =>      $np->opts->username,
>>                       password        =>      $np->opts->password,
>>                       xcli            =>      $xcli_path,
>>                 );
>>
>> switch ($np->opts->command) {
>>         case cf_list {
>>                 my @cf     = $xiv->cf_list_raw();
>>                 print_array(@cf);
>>         }
>>         case dimm_list {
>>                 my @dimms     = $xiv->dimm_list_raw();
>>                 print_array(@dimms);
>>         }
>>         case ethernet_cable_list {
>>                 my @ethernet_cable     =
>> $xiv->ethernet_cable_list_raw();
>>                 print_array(@ethernet_cable);
>>         }
>>         case fan_list {
>>                 my @fan     = $xiv->fan_list_raw();
>>                 print_array(@fan);
>>         }
>>         case module_list {
>>                 my @module     = $xiv->module_list_raw();
>>                 print_array(@module);
>>         }
>>         case psu_list {
>>                 my @psu     = $xiv->psu_list_raw();
>>                 print_array(@psu);
>>         }
>>         case ups_list {
>>                 my @ups     = $xiv->ups_list_raw();
>>                 print_array(@ups);
>>         }
>>         else {
>>                 print "Invalid command\n";
>>                 nagios_exit( UNKNOWN, 'Verify XIV command' );
>>         }
>> }
>>
>>
>>
>> (1) http://search.cpan.org/~ltp/IBM-XCLI-0.5/lib/IBM/XCLI.pm
>>
>
> I continued work in this script, I took check_stuff.pl and adapted it to
> my XVI script.
>
> I executed in console with nagios and recover last return code with echo
> $? and receive 0, but in Nagios still with "(Return code of 9 is out of
> bounds)"
>
> I don't understand what cause the problem.
>
> The adapted script to:
> #!/usr/bin/perl -w
>
> ###  check_stuff.pl
>
> # an example Nagios plugin using the Nagios::Plugin modules.
>
> # Originally by Nathan Vonnahme, n8v at users dot sourceforge
> # dot net, July 19 2006
>
> # Please modify to your heart's content and use as the basis for all
> # the really cool Nagios monitoring scripts you're going to create.
> # You rock.
>
> ##############################################################################
> # prologue
> use strict;
> use warnings;
> use Switch;
>
> use Nagios::Plugin;
> use IBM::XCLI;
>
> use vars qw($VERSION $PROGNAME $xcli_path $return_code $array @array
> $message $result);
> $VERSION = '1.0';
>
> # get the base name of this script for use in the examples
> use File::Basename;
> $PROGNAME = basename($0);
>
> #Define xcli path usually /opt/XIVGUI/xcli
> $xcli_path = "/opt/XIVGUI/xcli";
>
> ##############################################################################
> # define and get the command line options.
> #   see the command line option guidelines at
> #
> http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS
>
>
> # Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory)
> my $p = Nagios::Plugin->new(
>
>     usage => "Usage: %s  [-m <host>]
>     [ -u|--username=<username> ]
>     [ -p|--password=<password> ]
>     [ -c|--command = <XIV command> ]",
>     version => $VERSION,
>     blurb => 'This plugin is an example of a Nagios plugin written in Perl
> using the Nagios::Plugin modules.  It will generate a random integer
> between 1 and 20 (though you can specify the number with the -n option
> for testing), and will output OK, WARNING or CRITICAL if the resulting
> number is outside the specified thresholds.',
>
>         extra => "
>
> THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max'
> (or 'max'). If specified '\@min:max', a warning status will be generated
> if the count *is* inside the specified range.
>
> See more threshold examples at http
>   : // nagiosplug
>   . sourceforge
>   . net / developer-guidelines
>   . html    #THRESHOLDFORMAT
>
>   Examples:
>
>   $PROGNAME -w 10 -c 18 Returns a warning
>   if the resulting number is greater than 10,
>   or a critical error
>   if it is greater than 18.
>
>   $PROGNAME -w 10 : -c 4 : Returns a warning
>   if the resulting number is less than 10,
>   or a critical error
>   if it is less than 4.
>
>   "
> );
>
>
> # Define and document the valid command line options
> # usage, help, version, timeout and verbose are defined by default.
>
> $p->add_arg(
>         spec => 'machine|m=s',
>
>         help =>
> qq{-m, --machine=INTEGER
>    XIV IP address},
>
> #       required => 1,
> #       default => 10,
> );
>
> $p->add_arg(
>         spec => 'username|u=s',
>         help =>
> qq{-u, --username=INTEGER
>    XIV username},
> );
>
> $p->add_arg(
>         spec => 'password|p=s',
>         help =>
> qq{-p, --password=INTEGER
>    Password of the XIV username.},
> );
>
> $p->add_arg(
>         spec => 'command|c=s',
>         help =>
> qq{-c, --command=INTEGER
>    XIV command, the same that you use with XCLI.},
> );
>
> # Parse arguments and process standard ones (e.g. usage, help, version)
> $p->getopts;
>
> my $xiv = IBM::XCLI-> new(
>                       ip_address      =>      $p->opts->machine,
>                       username        =>      $p->opts->username,
>                       password        =>      $p->opts->password,
>                       xcli            =>      $xcli_path,
>                 );
> #Function for evaluate if any component isn't OK
> sub print_array {
>
>                 $return_code = 0;
>                 local (@array) = @_;
>                 foreach (@array) {
>                         s/^"\|"$//g;
>                         my(@array) = split /","/;
>                         substr($array[0], 0, 1, '');
>                         if ( $array[1] ne "Status" ) {
>                                 $message =
> "Component:\t$array[0]\tStatus:\t$array[1]\n";
> #                               print
> "Component:\t$array[0]\tStatus:\t$array[1]\n";
>                                 if ($array[1] ne "OK" ) {
>                                         $return_code = 2;
>                                 }
>                         }
>                 }
> }
>
>
> ##############################################################################
> # check stuff.
>
> # THIS is where you'd do your actual checking to get a real value for
> $result
> #  don't forget to timeout after $p->opts->timeout seconds, if applicable.
>
> switch ($p->opts->command) {
>         case "cf_list" {
>                 my @cf     = $xiv->cf_list_raw();
>                 print_array(@cf);
>         }
>         case "dimm_list" {
>                 my @dimms     = $xiv->dimm_list_raw();
>                 print_array(@dimms);
>         }
>         case "ethernet_cable" {
>                 my @ethernet_cable     = $xiv->ethernet_cable_list_raw();
>                 print_array(@ethernet_cable);
>         }
>         case "fan_list" {
>                 my @fan     = $xiv->fan_list_raw();
>                 print_array(@fan);
>         }
>         case "module_list" {
>                 my @module     = $xiv->module_list_raw();
>                 print_array(@module);
>         }
>         case "psu_list" {
>                 my @psu     = $xiv->psu_list_raw();
>                 print_array(@psu);
>         }
>         case "ups_list" {
>                 my @ups     = $xiv->ups_list_raw();
>                 print_array(@ups);
>         }
>         else {
>                 print "Invalid command\n";
>                 nagios_exit( UNKNOWN, 'Verify XIV command' );
>         }
> }
>
> ##############################################################################
> # check the result against the defined warning and critical thresholds,
> # output the result and exit
> $p->nagios_exit(
>          return_code => $return_code,
>          message => $message
> );
>
>
Finally a trash Perl and return to my loved Bash. I finished the script:
#!/bin/bash
# Sources:
# http://blog.felixbrezo.com/?p=473
# http://wiki.bash-hackers.org/howto/getopts_tutorial
#
http://www.linuxquestions.org/questions/linux-general-1/store-multi-line-output-into-an-array-in-a-linux-bash-script-706878/
#
http://stackoverflow.com/questions/4667509/problem-accessing-a-global-variable-from-within-a-while-loop

# Help and usage
showHelp () {
echo -e "USE:"
echo -e "\t$0 [-H -u -p -C -w -c | -h]"
echo -e "OPTIONS:"
echo -e "\t-H\tIP Address"
echo -e "\t-u\tUsername"
echo -e "\t-p\tPassword"
echo -e "\t-C\tXIV Command"
echo -e "\t-w\tWarning threshold"
echo -e "\t-c\tCritical threshold"
echo -e "\t-h\tThis Help"
}

xcli_path='/opt/XIVGUI';

while getopts "H: u: p: C: w:c:h" OPTION; do
  case $OPTION in
    H)
      HOSTNAME=$OPTARG
      ;;
    u)
      USERNAME=$OPTARG
      ;;
    p)
      PASSWORD=$OPTARG
      ;;
    C)
      COMMAND=$OPTARG
      ;;
    w)
      WARNING=$OPTARG
      ;;
    c)
      CRITICAL=$OPTARG
      ;;
    h)
      showHelp
      ;;
    \?)
      showHelp
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 3
      ;;
  esac
done

case $COMMAND in
     cf_list) ;;
     dimm_list) ;;
     ethernet_cable_list) ;;
     fan_list) ;;
     module_list) ;;
     psu_list) ;;
     ups_list) ;;
     *) echo "UNKNOWN: Command $COMMAND not supported or not exist"
        exit 3;;
esac

i=0
while read line
do
    array[$i]="$line"
    if [  $i != 0 ]
    then
        case "${array[$i]}" in
                *"OK"*) CHECK_STATE=0
                ;;
                *) CHECK_STATE=2
                PROBLEM="$PROBLEM "`echo "${array[$i]}" | awk '{ print $1
" " $4; }'`
                ;;
        esac    fi
    (( i++ ))
done < <($xcli_path/xcli -m $HOSTNAME -u $USERNAME -p $PASSWORD $COMMAND)

case "$CHECK_STATE" in
        0) echo "OK: $COMMAND Full full perfect"
           exit $CHECK_STATE
           ;;
        2) echo "CRITICAL: $COMMAND problem found $PROBLEM"
           exit $CHECK_STATE
           ;;
        *) echo "UNKNOWN: $COMMAND with config problems"
           exit $CHECK_STATE
           ;;
esac

When I have time going to upload to gitorius. I love Nagios!!!



>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_mar
> _______________________________________________
> Nagios-users mailing list
> Nagios-users at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-users
> ::: Please include Nagios version, plugin version (-v) and OS when
> reporting any issue.
> ::: Messages without supporting info will risk being sent to /dev/null
>



------------------------------------------------------------------------------
Own the Future-Intel® Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest.
Compete for recognition, cash, and the chance to get your game 
on Steam. $5K grand prize plus 10 genre and skill prizes. 
Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list