| 1 | # Functional interface to basic Nagios::Monitoring::Plugin constants, exports, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # and functions | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | package Nagios::Monitoring::Plugin::Functions; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 22 | ▶ | use Params::Validate qw(:types validate); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 22 | ▶ | use Math::Calc::Units; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Remember to update Nagios::Monitoring::Plugins as well | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 22 | our $VERSION = "0.51"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 22 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 22 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 22 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 22 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 22 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state max_state_alt convert $value_re); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 22 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | all => [ @EXPORT, @EXPORT_OK ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | codes => [ @STATUS_CODES ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | functions => [ qw(nagios_exit nagios_die check_messages max_state max_state_alt convert) ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 22 | ▶ | use constant OK => 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 22 | ▶ | use constant WARNING => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 22 | ▶ | use constant CRITICAL => 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 22 | ▶ | use constant UNKNOWN => 3; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 22 | ▶ | use constant DEPENDENT => 4; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 22 | our %ERRORS = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 'OK' => OK, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 'WARNING' => WARNING, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 'CRITICAL' => CRITICAL, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 'UNKNOWN' => UNKNOWN, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 'DEPENDENT' => DEPENDENT, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 22 | our %STATUS_TEXT = reverse %ERRORS; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 22 | my $value = qr/[-+]?[\d\.]+/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 22 | our $value_re = qr/$value(?:e$value)?/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # _fake_exit flag and accessor/mutator, for testing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 22 | my $_fake_exit = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | ▶ | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fake_exit: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # _use_die flag and accessor/mutator, so exceptions can be raised correctly | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 22 | my $_use_die = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | sub _use_die { @_ ? $_use_die = shift : $_use_die }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_use_die: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 76 | ▶ | sub get_shortname { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_shortname: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 76 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 76 | my $shortname = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 76 | ▶ | return $arg->{shortname} if (defined($arg->{shortname})); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 72 | ▶ | $shortname = $arg->{plugin} if (defined( $arg->{plugin})); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 72 | ▶ | $shortname = uc basename($shortname || $ENV{NAGIOS_PLUGIN} || $0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$shortname || $ENV{'NAGIOS_PLUGIN'} || $0
$shortname or $ENV{"NAGIOS_PLUGIN"}
MC/DC0%0/3
$shortname || $ENV{'NAGIOS_PLUGIN'} || $0
$shortname
$ENV{"NAGIOS_PLUGIN"}
$0
Truth table50%2/4
A or B || C
A= $shortnameB= $ENV{"NAGIOS_PLUGIN"}C= $0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 72 | $shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_] | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 72 | $shortname =~ s/\..*$//; # Remove any trailing suffix | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 72 | return $shortname; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | sub max_state { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
max_state: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | return CRITICAL if grep { $_ == CRITICAL } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | return WARNING if grep { $_ == WARNING } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | return OK if grep { $_ == OK } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return UNKNOWN if grep { $_ == UNKNOWN } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return DEPENDENT if grep { $_ == DEPENDENT } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | return UNKNOWN; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub max_state_alt { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
max_state_alt: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return CRITICAL if grep { $_ == CRITICAL } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return WARNING if grep { $_ == WARNING } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return UNKNOWN if grep { $_ == UNKNOWN } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return DEPENDENT if grep { $_ == DEPENDENT } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | return OK if grep { $_ == OK } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return UNKNOWN; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # nagios_exit( $code, $message ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 97 | ▶ | sub nagios_exit { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nagios_exit: called CC=23 SCAR=32.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 97 | my ($code, $message, $arg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # Handle named parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 97 | ▶ | if (defined $code && ($code eq 'return_code' || $code eq 'message')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $code and $code eq "return_code" || $code eq "message"
$code eq "return_code" or $code eq "message"
MC/DC66%2/3
defined $code and $code eq "return_code" || $code eq "message"
defined $code
$code eq "return_code"
$code eq "message"
Truth table75%3/4
A and B or C
A= defined $codeB= $code eq "return_code"C= $code eq "message"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # Remove last argument if odd no and last is ref | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 9 | ▶ | if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
int @_ / 2 != @_ / 2 and ref $_[$#_]
MC/DC0%0/2
int @_ / 2 != @_ / 2 and ref $_[$#_]
int @_ / 2 != @_ / 2
ref $_[$#_]
Truth table33%1/3
A and B
A= int @_ / 2 != @_ / 2B= ref $_[$#_]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 9 | $arg = pop @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | undef $arg; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 9 | my %arg = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 9 | $code = $arg{return_code}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 9 | $message = $arg{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 97 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg ||= {}
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # Handle string codes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 97 | ▶ | $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $code and exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$code}
MC/DC100%2/2
defined $code and exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$code}
defined $code
exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$code}
Truth table100%3/3
A and B
A= defined $codeB= exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$code}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Set defaults | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 97 | ▶ | $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $code and exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$code}
MC/DC100%2/2
defined $code and exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$code}
defined $code
exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$code}
Truth table100%3/3
A and B
A= defined $codeB= exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$code}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 97 | ▶ | $message = '' unless defined $message; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 97 | ▶ | if (ref $message && ref $message eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $message and ref $message eq "ARRAY"
MC/DC0%0/2
ref $message and ref $message eq "ARRAY"
ref $message
ref $message eq "ARRAY"
Truth table33%1/3
A and B
A= ref $messageB= ref $message eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $message = join(' ', map { chomp; $_ } @$message); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 97 | chomp $message; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Setup output | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 97 | my $output = "$STATUS_TEXT{$code}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 97 | ▶ | $output .= " - $message" if defined $message && $message ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $message and $message ne ""
MC/DC50%1/2
defined $message and $message ne ""
defined $message
$message ne ""
Truth table66%2/3
A and B
A= defined $messageB= $message ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 97 | ▶ | my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 97 | ▶ | $shortname ||= get_shortname(); # Should happen only if funnctions are called directly | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$shortname ||= get_shortname()
MC/DC0%0/2
$shortname ||= get_shortname()
$shortname
get_shortname()
Truth table66%2/3
A ||= B
A= $shortnameB= get_shortname()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 97 | ▶ | $output = "$shortname $output" if $shortname; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 97 | ▶ | if ($arg->{plugin}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 53 | my $plugin = $arg->{plugin}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 53 | ▶ | $output .= " | ". $plugin->all_perfoutput | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$plugin->perfdata and $plugin->all_perfoutput
MC/DC50%1/2
$plugin->perfdata and $plugin->all_perfoutput
$plugin->perfdata
$plugin->all_perfoutput
Truth table66%2/3
A and B
A= $plugin->perfdataB= $plugin->all_perfoutput
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | if $plugin->perfdata && $plugin->all_perfoutput; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 97 | $output .= "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # Don't actually exit if _fake_exit set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 97 | ▶ | if ($_fake_exit) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 89 | require Nagios::Monitoring::Plugin::ExitResult; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 89 | return Nagios::Monitoring::Plugin::ExitResult->new($code, $output); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 8 | _nagios_exit($code, $output); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 23 | ▶ | sub _nagios_exit { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nagios_exit: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 23 | my ($code, $output) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # Print output and exit; die if flag set and called via a die in stack backtrace | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 23 | ▶ | if ($_use_die) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 15 | for (my $i = 0;; $i++) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 32 | @_ = caller($i); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 32 | ▶ | last unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 32 | ▶ | if ($_[3] =~ m/die/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 15 | $! = $code; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 15 | die($output); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 8 | print $output; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 8 | exit $code; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Default $code: UNKNOWN | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 57 | ▶ | sub nagios_die { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nagios_die: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 57 | my ($arg1, $arg2, $rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Named parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 57 | ▶ | if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition83%5/6
defined $arg1 and $arg1 eq "return_code" || $arg1 eq "message"
$arg1 eq "return_code" or $arg1 eq "message"
MC/DC66%2/3
defined $arg1 and $arg1 eq "return_code" || $arg1 eq "message"
defined $arg1
$arg1 eq "return_code"
$arg1 eq "message"
Truth table75%3/4
A and B or C
A= defined $arg1B= $arg1 eq "return_code"C= $arg1 eq "message"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 6 | return nagios_exit(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # ($code, $message) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | ▶ | elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $arg1 and exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg1} || exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg1}
exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg1} or exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg1}
MC/DC100%3/3
defined $arg1 and exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg1} || exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg1}
defined $arg1
exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg1}
exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg1}
Truth table100%4/4
A and B or C
A= defined $arg1B= exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg1}C= exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg1}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 20 | return nagios_exit(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # ($message, $code) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | ▶ | elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $arg2 and exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg2} || exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg2}
exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg2} or exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg2}
MC/DC100%3/3
defined $arg2 and exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg2} || exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg2}
defined $arg2
exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg2}
exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg2}
Truth table100%4/4
A and B or C
A= defined $arg2B= exists $Nagios::Monitoring::Plugin::Functions::ERRORS{$arg2}C= exists $Nagios::Monitoring::Plugin::Functions::STATUS_TEXT{$arg2}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 21 | return nagios_exit($arg2, $arg1, $rest); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # Else just assume $arg1 is the message and hope for the best | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 10 | return nagios_exit( UNKNOWN, $arg1, $arg2 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # For backwards compatibility | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub die { nagios_die(@_); } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
die: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # Utility functions | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # Simple wrapper around Math::Calc::Units::convert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | sub convert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my ($value, $from, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | return $newval; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # check_messages - return a status and/or message based on a set of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # message arrays. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # Returns a nagios status code in scalar context. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # Returns a code and a message in list context. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # The message is join($join, @array) for the relevant array for the code, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # or join($join_all, $message) for all arrays if $join_all is set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 53 | ▶ | sub check_messages { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_messages: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 53 | my %arg = validate( @_, { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | critical => { type => ARRAYREF }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | warning => { type => ARRAYREF }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | ok => { type => ARRAYREF | SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 'join' => { default => ' ' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | join_all => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 50 | ▶ | $arg{join} = ' ' unless defined $arg{join}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # Decide $code | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 50 | my $code = OK; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 50 | ▶ | $code ||= CRITICAL if @{$arg{critical}}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$code ||= 2
MC/DC0%0/1
$code ||= 2
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 50 | ▶ | $code ||= WARNING if @{$arg{warning}}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$code ||= 1
MC/DC100%1/1
$code ||= 1
$code
Truth table100%2/2
A
A= $code
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 50 | ▶ | return $code unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # Compose message | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 44 | my $message = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 44 | ▶ | if ($arg{join_all}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | $message = join( $arg{join_all}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 36 | ▶ | map { @$_ ? join( $arg{'join'}, @$_) : () } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | $arg{critical}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | $arg{warning}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 12 | $arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : [] | |||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 32 | ▶ | $message ||= join( $arg{'join'}, @{$arg{critical}} ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$message ||= join($arg{"join"}, @{$arg{"critical"};})
MC/DC0%0/2
$message ||= join($arg{"join"}, @{$arg{"critical"};})
$message
join($arg{"join"}, @{$arg{"critical"};})
Truth table33%1/3
A ||= B
A= $messageB= join($arg{"join"}, @{$arg{"critical"};})
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | if $code == CRITICAL; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 32 | ▶ | $message ||= join( $arg{'join'}, @{$arg{warning}} ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$message ||= join($arg{"join"}, @{$arg{"warning"};})
MC/DC0%0/2
$message ||= join($arg{"join"}, @{$arg{"warning"};})
$message
join($arg{"join"}, @{$arg{"warning"};})
Truth table33%1/3
A ||= B
A= $messageB= join($arg{"join"}, @{$arg{"warning"};})
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | if $code == WARNING; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 8 | $message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok} | |||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 32 | ▶ | if $arg{ok}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$message ||= ref $arg{"ok"} ? join($arg{"join"}, @{$arg{"ok"};}) : $arg{"ok"}
MC/DC100%2/2
$message ||= ref $arg{"ok"} ? join($arg{"join"}, @{$arg{"ok"};}) : $arg{"ok"}
$message
ref $arg{"ok"} ? join($arg{"join"}, @{$arg{"ok"};}) : $arg{"ok"}
Truth table100%3/3
A ||= B
A= $messageB= ref $arg{"ok"} ? join($arg{"join"}, @{$arg{"ok"};}) : $arg{"ok"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 44 | return ($code, $message); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 22 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # vim:sw=4:sm:et | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||