Nagios::Plugin::DieNicely - Die in a Nagios output compatible way


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

Index


Code Index:

NAME

Top

Nagios::Plugin::DieNicely - Die in a Nagios output compatible way

SYNOPSIS

Top

  use Nagios::Plugin::DieNicely;

  ... your plugin code goes here ...




  use Nagios::Plugin::DieNicely 'WARNING';

  ... now if you die, you will get a Nagios WARNING state ...

DESCRIPTION

Top

When your Nagios plugins, or the modules that they use raise an unhandled exception with die, croak or confess, the exception gets lost, and Nagios treats the output as an UNKNOWN state with no output from the plugin, as STDERR gets discarded by Nagios.

This module overrides perl's default behaviour of using exit code 255 and printing the error to STDERR (not Nagios friendly). Just using for exit code 2 (Nagios CRITICAL), and outputing the error to STDOUT with "CRITICAL - " prepended to the exception. Note that you can change the CRITICAL for WARNING, or even OK (not recommended)

USE

Top

Just use the module. If you want a Nagios error code other that CRITICAL, then use the module passing one of: WARNING, OK, UNKNOWN. CRITICAL can be passed too (just for completeness).

  use Nagios::Plugin::DieNicely 'WARNING';
  use Nagios::Plugin::DieNicely 'UNKNOWN';
  use Nagios::Plugin::DieNicely 'CRITICAL';
  use Nagios::Plugin::DieNicely 'OK';

TODO

Top

 - Get the shortname of the module through Nagios::Plugin if it is beeing used
 - Issue perl warnings to STDOUT, and possbily issue WARNING or CRITICAL

AUTHOR

Top

    Jose Luis Martinez
    CPAN ID: JLMARTIN
    CAPSiDE
    jlmartinez@capside.com
    http://www.pplusdomain.net

COPYRIGHT

Top


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

package Nagios::Plugin::DieNicely;

use warnings;
use strict;

use vars qw($VERSION);
$VERSION     = '0.05';

our ($wanted_exit, $exit_description);

sub import {
    my ($class, $exit) = @_;
    my $translation = {
        'OK'       => 0,
	'WARNING'  => 1,
	'CRITICAL' => 2,
	'UNKNOWN'  => 3
    };
    if (not defined $exit){
        # by default we will exit critical
        $exit = 'CRITICAL';
    }
    if (not defined $translation->{$exit}){
        print "Nagios::Plugin::DieNicely doesn't know how to exit $exit\n";
        exit 3;
    } 
    
    $wanted_exit = $translation->{$exit};
    $exit_description = $exit;
}


sub _nagios_die {
    die @_ if $^S;
    die @_ if (not defined $^S);

    if (not defined $wanted_exit){
        # If someone only requires the module, and import is not called,
	# wanted_exit would be undefined. We also get here when the 
	# parameter passed to the class is not valid
        $wanted_exit = 2;
	$exit_description = 'CRITICAL';
    }

    print "$exit_description - ", @_;
    exit $wanted_exit;
}

$SIG{__DIE__} = \&_nagios_die;

#################### main pod documentation end ###################


1;
# The preceding line will help the module return a true value