| 1 | package Nagios::Plugin::DieNicely; | ||||||||||
| 2 | |||||||||||
| 3 | 56 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 56 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 56 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 56 | $VERSION = '0.05'; | |||||||||
| 8 | |||||||||||
| 9 | 56 | our ($wanted_exit, $exit_description); | |||||||||
| 10 | |||||||||||
| 11 | 55 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||
| 12 | 55 | my ($class, $exit) = @_; | |||||||||
| 13 | 55 | my $translation = { | |||||||||
| 14 | 'OK' => 0, | ||||||||||
| 15 | 'WARNING' => 1, | ||||||||||
| 16 | 'CRITICAL' => 2, | ||||||||||
| 17 | 'UNKNOWN' => 3 | ||||||||||
| 18 | }; | ||||||||||
| 19 | 55 | ▶ | if (not defined $exit){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | # by default we will exit critical | ||||||||||
| 21 | 18 | $exit = 'CRITICAL'; | |||||||||
| 22 | } | ||||||||||
| 23 | 55 | ▶ | if (not defined $translation->{$exit}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | print "Nagios::Plugin::DieNicely doesn't know how to exit $exit\n"; | |||||||||
| 25 | 1 | exit 3; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 54 | $wanted_exit = $translation->{$exit}; | |||||||||
| 29 | 54 | $exit_description = $exit; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 56 | ▶ | sub _nagios_die { | ||||||||
|
Subroutine
_nagios_die: called CC=4 SCAR=14.3
| |||||||||||
| 34 | 56 | ▶ | die @_ if $^S; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 50 | ▶ | die @_ if (not defined $^S); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | 45 | ▶ | if (not defined $wanted_exit){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 45 | print "$exit_description - ", @_; | |||||||||
| 46 | 45 | exit $wanted_exit; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 56 | $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<die>, I<croak> or I<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. | ||||||||||
| 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<use> the module. If you want a Nagios error code other that B<CRITICAL>, then use the module passing one of: I<WARNING>, I<OK>, I<UNKNOWN>. I<CRITICAL> 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 | 56 | 1; | |||||||||
| 109 | # The preceding line will help the module return a true value | ||||||||||
| 110 | |||||||||||