diff options
Diffstat (limited to 'lib/Nagios')
-rw-r--r-- | lib/Nagios/Plugin.pm | 9 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Base.pm | 45 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Range.pm | 44 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 4 |
5 files changed, 71 insertions, 34 deletions
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 2acc6ea..4e95e98 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm | |||
@@ -23,7 +23,7 @@ use Exporter; | |||
23 | our @ISA = qw(Exporter Nagios::__::Plugin); | 23 | our @ISA = qw(Exporter Nagios::__::Plugin); |
24 | our @EXPORT_OK = qw(%ERRORS); | 24 | our @EXPORT_OK = qw(%ERRORS); |
25 | 25 | ||
26 | our $VERSION = '0.12'; | 26 | our $VERSION = '0.13'; |
27 | 27 | ||
28 | sub add_perfdata { | 28 | sub add_perfdata { |
29 | my ($self, %args) = @_; | 29 | my ($self, %args) = @_; |
@@ -59,7 +59,8 @@ Nagios::Plugin - Object oriented helper routines for your Nagios plugin | |||
59 | use Nagios::Plugin qw(%ERRORS); | 59 | use Nagios::Plugin qw(%ERRORS); |
60 | $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); | 60 | $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); |
61 | 61 | ||
62 | $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" ); | 62 | $threshold = $p->set_thresholds( warning => "10:25", critical => "~:25" ); |
63 | # Critical if outside -INF to 25, ie > 25. Warn if outside 10-25, ie < 10 | ||
63 | 64 | ||
64 | # ... collect current metric into $value | 65 | # ... collect current metric into $value |
65 | if ($trouble_getting_metric) { | 66 | if ($trouble_getting_metric) { |
@@ -120,7 +121,7 @@ Initializes a new Nagios::Plugin object. Can specify the shortname here. | |||
120 | 121 | ||
121 | =head1 OBJECT METHODS | 122 | =head1 OBJECT METHODS |
122 | 123 | ||
123 | =item set_thresholds( warning => "10:25", critical => "25:" ) | 124 | =item set_thresholds( warning => "10:25", critical => "~:25" ) |
124 | 125 | ||
125 | Sets the thresholds, based on the range specification at | 126 | Sets the thresholds, based on the range specification at |
126 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. | 127 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. |
@@ -148,6 +149,8 @@ http://nagiosplug.sourceforge.net | |||
148 | 149 | ||
149 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | 150 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> |
150 | 151 | ||
152 | Thanks to Nathan Vonnahme for loads of extra tests and subsequent fixes. | ||
153 | |||
151 | =head1 COPYRIGHT AND LICENSE | 154 | =head1 COPYRIGHT AND LICENSE |
152 | 155 | ||
153 | Copyright (C) 2006 by Nagios Plugin Development Team | 156 | Copyright (C) 2006 by Nagios Plugin Development Team |
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm index f3a7f7c..d1678a5 100644 --- a/lib/Nagios/Plugin/Base.pm +++ b/lib/Nagios/Plugin/Base.pm | |||
@@ -5,6 +5,9 @@ package Nagios::Plugin::Base; | |||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | 7 | ||
8 | use Nagios::Plugin; | ||
9 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
10 | |||
8 | use Exporter; | 11 | use Exporter; |
9 | our @ISA = qw(Exporter); | 12 | our @ISA = qw(Exporter); |
10 | our @EXPORT = qw(%ERRORS); | 13 | our @EXPORT = qw(%ERRORS); |
@@ -13,28 +16,38 @@ our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); | |||
13 | 16 | ||
14 | our %STATUS_TEXT = reverse %ERRORS; | 17 | our %STATUS_TEXT = reverse %ERRORS; |
15 | 18 | ||
19 | |||
16 | my $exit_on_die = 1; | 20 | my $exit_on_die = 1; |
17 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | 21 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; |
18 | my $print_on_die = 1; | 22 | my $print_on_die = 1; |
19 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | 23 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; |
20 | 24 | ||
21 | sub die { | 25 | sub die { |
22 | my ($class, $args, $plugin) = @_; | 26 | my ($class, $args, $plugin) = @_; |
23 | my $return_code = $args->{return_code} || 3; | 27 | my $return_code; |
24 | my $message = $args->{message} || "Internal error"; | 28 | |
25 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); | 29 | if ( exists $args->{return_code} |
26 | if ($plugin) { | 30 | && exists $STATUS_TEXT{$args->{return_code}} |
27 | $output = $plugin->shortname." $output" if $plugin->shortname; | 31 | ) { |
28 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; | 32 | $return_code = $args->{return_code}; |
29 | } | 33 | } |
30 | if ($print_on_die) { | 34 | else { |
31 | print $output, $/; | 35 | $return_code = $ERRORS{UNKNOWN}; |
32 | } | 36 | } |
33 | if ($exit_on_die) { | 37 | my $message = $args->{message} || "Internal error"; |
34 | exit $return_code; | 38 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); |
35 | } else { | 39 | if ($plugin) { |
36 | return $output; | 40 | $output = $plugin->shortname." $output" if $plugin->shortname; |
37 | } | 41 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; |
42 | } | ||
43 | if ($print_on_die) { | ||
44 | print $output, $/; | ||
45 | } | ||
46 | if ($exit_on_die) { | ||
47 | exit $return_code; | ||
48 | } else { | ||
49 | return $output; | ||
50 | } | ||
38 | } | 51 | } |
39 | 52 | ||
40 | 1; | 53 | 1; |
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 45109ae..2af22bd 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm | |||
@@ -7,6 +7,9 @@ use warnings; | |||
7 | 7 | ||
8 | use Carp; | 8 | use Carp; |
9 | use Nagios::Plugin::Threshold; | 9 | use Nagios::Plugin::Threshold; |
10 | use Nagios::Plugin; | ||
11 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
12 | |||
10 | use Class::Struct; | 13 | use Class::Struct; |
11 | struct "Nagios::Plugin::Performance" => { | 14 | struct "Nagios::Plugin::Performance" => { |
12 | label => '$', | 15 | label => '$', |
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index c03001a..c0c47e4 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm | |||
@@ -4,6 +4,10 @@ use 5.008004; | |||
4 | 4 | ||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | use Carp; | ||
8 | |||
9 | use Nagios::Plugin; | ||
10 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
7 | 11 | ||
8 | use overload | 12 | use overload |
9 | '""' => sub { shift->stringify }; | 13 | '""' => sub { shift->stringify }; |
@@ -17,8 +21,8 @@ struct "Nagios::Plugin::Range" => { | |||
17 | alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set | 21 | alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set |
18 | }; | 22 | }; |
19 | 23 | ||
20 | my $outside = 0; | 24 | use constant OUTSIDE => 0; |
21 | my $inside = 1; | 25 | use constant INSIDE => 1; |
22 | 26 | ||
23 | sub stringify { | 27 | sub stringify { |
24 | my $self = shift; | 28 | my $self = shift; |
@@ -49,22 +53,32 @@ sub set_range_end { | |||
49 | sub parse_range_string { | 53 | sub parse_range_string { |
50 | my ($class, $string) = @_; | 54 | my ($class, $string) = @_; |
51 | my $valid = 0; | 55 | my $valid = 0; |
52 | my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside); | 56 | my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE); |
57 | |||
58 | $string =~ s/\s//g; # strip out any whitespace | ||
59 | # check for valid range definition | ||
60 | unless ( $string =~ /[\d~]/ && $string =~ m/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) { | ||
61 | carp "invalid range definition '$string'"; | ||
62 | return undef; | ||
63 | } | ||
53 | 64 | ||
54 | if ($string =~ s/^\@//) { | 65 | if ($string =~ s/^\@//) { |
55 | $range->alert_on($inside); | 66 | $range->alert_on(INSIDE); |
56 | } | 67 | } |
57 | if ($string =~ s/^~//) { | 68 | |
58 | $range->start_infinity(1); | 69 | if ($string =~ s/^~//) { # '~:x' |
70 | $range->start_infinity(1); | ||
59 | } | 71 | } |
60 | if (($_) = $string =~ /^([-\d\.]+)?:/) { | 72 | if ( $string =~ m/^([\d\.-]+)?:/ ) { # '10:' |
61 | $range->set_range_start($_) if defined $_; | 73 | my $start = $1; |
62 | $string =~ s/^([-\d\.]+)?://; | 74 | $range->set_range_start($start) if defined $start; |
63 | $valid++ | 75 | $range->end_infinity(1); # overridden below if there's an end specified |
76 | $string =~ s/^([-\d\.]+)?://; | ||
77 | $valid++; | ||
64 | } | 78 | } |
65 | if ($string =~ /^([-\d\.]+)$/) { | 79 | if ($string =~ /^([-\d\.]+)$/) { # 'x:10' or '10' |
66 | $range->set_range_end($string); | 80 | $range->set_range_end($string); |
67 | $valid++; | 81 | $valid++; |
68 | } | 82 | } |
69 | 83 | ||
70 | if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { | 84 | if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { |
@@ -78,7 +92,7 @@ sub check_range { | |||
78 | my ($self, $value) = @_; | 92 | my ($self, $value) = @_; |
79 | my $false = 0; | 93 | my $false = 0; |
80 | my $true = 1; | 94 | my $true = 1; |
81 | if ($self->alert_on == $inside) { | 95 | if ($self->alert_on == INSIDE) { |
82 | $false = 1; | 96 | $false = 1; |
83 | $true = 0; | 97 | $true = 0; |
84 | } | 98 | } |
@@ -89,7 +103,7 @@ sub check_range { | |||
89 | return $true; | 103 | return $true; |
90 | } | 104 | } |
91 | } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { | 105 | } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { |
92 | if ($self->start <= $value) { | 106 | if ( $value >= $self->start ) { |
93 | return $false; | 107 | return $false; |
94 | } else { | 108 | } else { |
95 | return $true; | 109 | return $true; |
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 1b332b9..494383f 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm | |||
@@ -7,6 +7,8 @@ use warnings; | |||
7 | 7 | ||
8 | use Nagios::Plugin::Range; | 8 | use Nagios::Plugin::Range; |
9 | use Nagios::Plugin::Base; | 9 | use Nagios::Plugin::Base; |
10 | use Nagios::Plugin; | ||
11 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
10 | 12 | ||
11 | use Class::Struct; | 13 | use Class::Struct; |
12 | struct "Nagios::Plugin::Threshold" => { | 14 | struct "Nagios::Plugin::Threshold" => { |
@@ -44,6 +46,7 @@ sub set_thresholds { | |||
44 | 46 | ||
45 | sub get_status { | 47 | sub get_status { |
46 | my ($self, $value) = @_; | 48 | my ($self, $value) = @_; |
49 | |||
47 | if ($self->critical->is_set) { | 50 | if ($self->critical->is_set) { |
48 | if ($self->critical->check_range($value) == 1) { | 51 | if ($self->critical->check_range($value) == 1) { |
49 | return $ERRORS{CRITICAL}; | 52 | return $ERRORS{CRITICAL}; |
@@ -54,6 +57,7 @@ sub get_status { | |||
54 | return $ERRORS{WARNING}; | 57 | return $ERRORS{WARNING}; |
55 | } | 58 | } |
56 | } | 59 | } |
60 | return $ERRORS{OK}; | ||
57 | } | 61 | } |
58 | 62 | ||
59 | 1; | 63 | 1; |