New XIV plugin

Esteban Monge esteban at nuevaeralatam.com
Thu Mar 21 20:22:56 CET 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
);



------------------------------------------------------------------------------
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





More information about the Users mailing list