Nagios::Plugin::Simple - Simple and Minimalistic Nagios Plugin Package


Nagios-Plugin-Simple documentation Contained in the Nagios-Plugin-Simple distribution.

Index


Code Index:

NAME

Top

Nagios::Plugin::Simple - Simple and Minimalistic Nagios Plugin Package

SYNOPSIS

Top

  use Nagios::Plugin::Simple;
  my $nps=Nagios::Plugin::Simple->new;
  $nps->ok("I'm OK") if &ok;
  $nps->warning("I'm a bit sickly") if &sick;
  $nps->critical("Barf...");
  $nps->unknown("Huh?");

In the true spirit of Perl you can even do a one-liner.

  perl -MNagios::Plugin::Simple -e 'Nagios::Plugin::Simple->new->ok("")';echo $?




DESCRIPTION

Top

This is the package that I use mostly because I feel the Nagios::Plugin is too encompassing. I feel that it is the scripts responsibility to handle arguments and thus this package does not do that nor will do that. If you want argument handling use one of the GetOpt packages.

USAGE

Top

  use Nagios::Plugin::Simple;
  my $nps=Nagios::Plugin::Simple->new;
  if (&ok) {$nps->ok("good!")} else {$nps->critical("bad!")};




CONSTRUCTOR

Top

new

  my $nps=Nagios::Plugin::Simple->new();

METHODS

Top

ok

Exits script with ok status code.

  $nps->ok("I'm OK");

Prints "OK: %s" and exits with a code 0.

  STDOUT => "OK: I'm OK\n",  EXIT=>0

warning

Exits script with warning status code.

  $nps->warning("I'm a bit sickly");

Prints "Warning: %s" and exits with a code 1.

  STDOUT => "Warning: I'm a bit sickly\n",  EXIT=>1

critical

Exits script with critical status code.

  $nps->critical("Barf...");

Prints "Critical: %s" and exits with a code 2.

  STDOUT => "Critical: Barf...\n",  EXIT=>2

unknown

Exits script with unknown status code.

  $nps->unknown("Huh?")

Prints "Unknown: %s" and exits with a code 3.

  STDOUT => "Unknown: Huh?\n",  EXIT=>3

code

Exits script by status code. This works best if your status is actually stored as a code 0, 1, 2, or 3 in a variable.

  $nps->code($code => $string);

Examples:

  $nps->code(0 => "I'm OK!");
  $nps->code(1 => "I'm a bit sickly");
  $nps->code(2 => "Barf...");
  $nps->code(3 => "Huh?")

Prints ``$status: %s'' and exits with $code.

status

Exits script by status string. This works best if your string is actually stored as "OK", "Warning", etc in a variable

  $nps->status($status    => $string);

Examples:

  $nps->status("OK"       => "I'm OK!");
  $nps->status("Warning"  => "I'm a bit sickly");
  $nps->status("Critical" => "Barf...");
  $nps->status("Unknown"  => "Huh?")

Prints ``$status: %s'' and exits with correct code.

codes

Returns a hash of the Nagios status codes.

  my %codes=$nps->codes;           #(OK=>0, Warning=>1, Critical=>2, Unknown=>3)
  my $codes=$nps->codes;           #{OK=>0, Warning=>1, Critical=>2, Unknown=>3}
  my %status=reverse $self->codes; #(0=>"OK", 1=>"Warning", ...)

BUGS

Top

SUPPORT

Top

AUTHOR

Top

    Michael R. Davis
    CPAN ID: MRDVT
    STOP, LLC
    account=>perl,tld=>com,domain=>michaelrdavis
    http://www.stopllc.com/

COPYRIGHT

Top

SEE ALSO

Top

Nagios::Plugin, Getopt::Std, Getopt::Long


Nagios-Plugin-Simple documentation Contained in the Nagios-Plugin-Simple distribution.
package Nagios::Plugin::Simple;
use strict;
use warnings;

our $VERSION='0.05';

sub new {
  my $this = shift();
  my $class = ref($this) || $this;
  my $self = {};
  bless $self, $class;
  $self->initialize(@_);
  return $self;
}

sub initialize {
  my $self=shift;
  %$self=@_;
}

sub ok {
  my $self=shift;
  my $string=shift;
  $self->status(OK=>$string);
}

sub warning {
  my $self=shift;
  my $string=shift;
  $self->status(Warning=>$string);
}

sub critical {
  my $self=shift;
  my $string=shift;
  $self->status(Critical=>$string);
}

sub unknown {
  my $self=shift;
  my $string=shift;
  $self->status(Unknown=>$string);
}

sub code {
  my $self=shift;
  my $code=shift;
  my $string=shift;
  my %status=reverse $self->codes;
  my $status=$status{$code};
  $self->status($status, $string);
}

sub status {
  my $self=shift;
  my $status=shift;
  my $string=shift;
  $string='' unless defined($string);
  my %codes=map {uc($_)} $self->codes;
  #use Data::Dumper;
  #print Dumper([\%codes]);
  my $code=$codes{uc($status)};
  die(qq{Error: Exit code not defined for "$status"}) unless defined($code);
  printf "%s: %s\n", $status, $string;
  exit $code;
}

sub codes {
 #my $self=shift;
  my @data=(OK=>0, Warning=>1, Critical=>2, Unknown=>3);
  return wantarray ? @data : {@data};
}

1;