File Coverage

blib/lib/Nagios/Plugin/DieNicely.pm
Criterion Covered Total %
statement 18 25 72.0
branch 4 10 40.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 27 40 67.5


line stmt bran cond sub pod time code
1             package Nagios::Plugin::DieNicely;
2              
3 1     1   28876 use warnings;
  1         3  
  1         32  
4 1     1   6 use strict;
  1         2  
  1         58  
5              
6 1     1   6 use vars qw($VERSION);
  1         5  
  1         356  
7             $VERSION = '0.05';
8              
9             our ($wanted_exit, $exit_description);
10              
11             sub import {
12 1     1   9 my ($class, $exit) = @_;
13 1         4 my $translation = {
14             'OK' => 0,
15             'WARNING' => 1,
16             'CRITICAL' => 2,
17             'UNKNOWN' => 3
18             };
19 1 50       5 if (not defined $exit){
20             # by default we will exit critical
21 1         2 $exit = 'CRITICAL';
22             }
23 1 50       3 if (not defined $translation->{$exit}){
24 0         0 print "Nagios::Plugin::DieNicely doesn't know how to exit $exit\n";
25 0         0 exit 3;
26             }
27            
28 1         2 $wanted_exit = $translation->{$exit};
29 1         10 $exit_description = $exit;
30             }
31              
32              
33             sub _nagios_die {
34 2 50   2   14 die @_ if $^S;
35 2 50       48 die @_ if (not defined $^S);
36              
37 0 0         if (not defined $wanted_exit){
38             # If someone only requires the module, and import is not called,
39             # wanted_exit would be undefined. We also get here when the
40             # parameter passed to the class is not valid
41 0           $wanted_exit = 2;
42 0           $exit_description = 'CRITICAL';
43             }
44              
45 0           print "$exit_description - ", @_;
46 0           exit $wanted_exit;
47             }
48              
49             $SIG{__DIE__} = \&_nagios_die;
50              
51             =head1 NAME
52              
53             Nagios::Plugin::DieNicely - Die in a Nagios output compatible way
54              
55             =head1 SYNOPSIS
56              
57             use Nagios::Plugin::DieNicely;
58              
59             ... your plugin code goes here ...
60              
61              
62             use Nagios::Plugin::DieNicely 'WARNING';
63              
64             ... now if you die, you will get a Nagios WARNING state ...
65              
66             =head1 DESCRIPTION
67              
68             When your Nagios plugins, or the modules that they use raise an unhandled exception with I, I or I, 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.
69              
70             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)
71              
72             =head1 USE
73              
74             Just I the module. If you want a Nagios error code other that B, then use the module passing one of: I, I, I. I can be passed too (just for completeness).
75              
76             use Nagios::Plugin::DieNicely 'WARNING';
77             use Nagios::Plugin::DieNicely 'UNKNOWN';
78             use Nagios::Plugin::DieNicely 'CRITICAL';
79             use Nagios::Plugin::DieNicely 'OK';
80              
81             =head1 TODO
82              
83             - Get the shortname of the module through Nagios::Plugin if it is beeing used
84             - Issue perl warnings to STDOUT, and possbily issue WARNING or CRITICAL
85              
86             =head1 AUTHOR
87              
88             Jose Luis Martinez
89             CPAN ID: JLMARTIN
90             CAPSiDE
91             jlmartinez@capside.com
92             http://www.pplusdomain.net
93              
94             =head1 COPYRIGHT
95              
96             This program is free software; you can redistribute
97             it and/or modify it under the same terms as Perl itself.
98              
99             The full text of the license can be found in the
100             LICENSE file included with this module.
101              
102              
103             =cut
104              
105             #################### main pod documentation end ###################
106              
107              
108             1;
109             # The preceding line will help the module return a true value
110