summaryrefslogtreecommitdiffstats
path: root/lib/Nagios
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2006-08-04 20:22:31 +0000
committerTon Voon <tonvoon@users.sourceforge.net>2006-08-04 20:22:31 +0000
commit96933fd2e1f53aff9c9ef26639fafe9a84ec754e (patch)
tree307d23df452b83c4d6799303379e208c7adb6317 /lib/Nagios
parentc0f29b373c0339eec73e325fb15ebef6dd24d230 (diff)
downloadmonitoring-plugin-perl-96933fd2e1f53aff9c9ef26639fafe9a84ec754e.tar.gz
Lots of extra tests and subsequent fixes (Nathan Vonnahme)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1466 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios')
-rw-r--r--lib/Nagios/Plugin.pm9
-rw-r--r--lib/Nagios/Plugin/Base.pm45
-rw-r--r--lib/Nagios/Plugin/Performance.pm3
-rw-r--r--lib/Nagios/Plugin/Range.pm44
-rw-r--r--lib/Nagios/Plugin/Threshold.pm4
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;
23our @ISA = qw(Exporter Nagios::__::Plugin); 23our @ISA = qw(Exporter Nagios::__::Plugin);
24our @EXPORT_OK = qw(%ERRORS); 24our @EXPORT_OK = qw(%ERRORS);
25 25
26our $VERSION = '0.12'; 26our $VERSION = '0.13';
27 27
28sub add_perfdata { 28sub 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
125Sets the thresholds, based on the range specification at 126Sets the thresholds, based on the range specification at
126http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. 127http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT.
@@ -148,6 +149,8 @@ http://nagiosplug.sourceforge.net
148 149
149Ton Voon, E<lt>ton.voon@altinity.comE<gt> 150Ton Voon, E<lt>ton.voon@altinity.comE<gt>
150 151
152Thanks to Nathan Vonnahme for loads of extra tests and subsequent fixes.
153
151=head1 COPYRIGHT AND LICENSE 154=head1 COPYRIGHT AND LICENSE
152 155
153Copyright (C) 2006 by Nagios Plugin Development Team 156Copyright (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;
5use strict; 5use strict;
6use warnings; 6use warnings;
7 7
8use Nagios::Plugin;
9our ($VERSION) = $Nagios::Plugin::VERSION;
10
8use Exporter; 11use Exporter;
9our @ISA = qw(Exporter); 12our @ISA = qw(Exporter);
10our @EXPORT = qw(%ERRORS); 13our @EXPORT = qw(%ERRORS);
@@ -13,28 +16,38 @@ our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
13 16
14our %STATUS_TEXT = reverse %ERRORS; 17our %STATUS_TEXT = reverse %ERRORS;
15 18
19
16my $exit_on_die = 1; 20my $exit_on_die = 1;
17sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; 21sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die };
18my $print_on_die = 1; 22my $print_on_die = 1;
19sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; 23sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die };
20 24
21sub die { 25sub 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
401; 531;
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
8use Carp; 8use Carp;
9use Nagios::Plugin::Threshold; 9use Nagios::Plugin::Threshold;
10use Nagios::Plugin;
11our ($VERSION) = $Nagios::Plugin::VERSION;
12
10use Class::Struct; 13use Class::Struct;
11struct "Nagios::Plugin::Performance" => { 14struct "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
5use strict; 5use strict;
6use warnings; 6use warnings;
7use Carp;
8
9use Nagios::Plugin;
10our ($VERSION) = $Nagios::Plugin::VERSION;
7 11
8use overload 12use 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
20my $outside = 0; 24use constant OUTSIDE => 0;
21my $inside = 1; 25use constant INSIDE => 1;
22 26
23sub stringify { 27sub stringify {
24 my $self = shift; 28 my $self = shift;
@@ -49,22 +53,32 @@ sub set_range_end {
49sub parse_range_string { 53sub 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
8use Nagios::Plugin::Range; 8use Nagios::Plugin::Range;
9use Nagios::Plugin::Base; 9use Nagios::Plugin::Base;
10use Nagios::Plugin;
11our ($VERSION) = $Nagios::Plugin::VERSION;
10 12
11use Class::Struct; 13use Class::Struct;
12struct "Nagios::Plugin::Threshold" => { 14struct "Nagios::Plugin::Threshold" => {
@@ -44,6 +46,7 @@ sub set_thresholds {
44 46
45sub get_status { 47sub 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
591; 631;