diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-06-08 12:27:44 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-06-08 12:27:44 +0000 |
commit | 2c6651034f76e2bccb549a867485f8fabbf07cb1 (patch) | |
tree | 8de53d7efe4564017e1ebce73353c7e3a3c8b548 | |
parent | 83e29d795dddf34a652cda8665179b77372ddcfe (diff) | |
download | monitoring-plugin-perl-2c6651034f76e2bccb549a867485f8fabbf07cb1.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1419 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | .cvsignore | 3 | ||||
-rw-r--r-- | Changes | 9 | ||||
-rw-r--r-- | MANIFEST | 13 | ||||
-rw-r--r-- | Makefile.PL | 12 | ||||
-rw-r--r-- | README | 29 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 160 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Base.pm | 65 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 119 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Range.pm | 126 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 96 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 63 | ||||
-rw-r--r-- | t/Nagios-Plugin-Range.t | 84 | ||||
-rw-r--r-- | t/Nagios-Plugin-Threshold.t | 32 | ||||
-rw-r--r-- | t/Nagios-Plugin.t | 32 |
14 files changed, 843 insertions, 0 deletions
diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..0b5bd39 --- /dev/null +++ b/.cvsignore | |||
@@ -0,0 +1,3 @@ | |||
1 | Makefile | ||
2 | blib | ||
3 | pm_to_blib | ||
@@ -0,0 +1,9 @@ | |||
1 | Revision history for Perl extension Nagios::Plugin. | ||
2 | |||
3 | 0.10 8th June 2006 | ||
4 | First release to CPAN | ||
5 | |||
6 | 0.01 Fri Jun 2 14:10:58 2006 | ||
7 | - original version; created by h2xs 1.23 with options | ||
8 | -X -n Nagios::Plugin | ||
9 | |||
diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..336daab --- /dev/null +++ b/MANIFEST | |||
@@ -0,0 +1,13 @@ | |||
1 | Changes | ||
2 | Makefile.PL | ||
3 | MANIFEST | ||
4 | README | ||
5 | t/Nagios-Plugin.t | ||
6 | t/Nagios-Plugin-Performance.t | ||
7 | t/Nagios-Plugin-Range.t | ||
8 | t/Nagios-Plugin-Threshold.t | ||
9 | lib/Nagios/Plugin.pm | ||
10 | lib/Nagios/Plugin/Performance.pm | ||
11 | lib/Nagios/Plugin/Range.pm | ||
12 | lib/Nagios/Plugin/Threshold.pm | ||
13 | lib/Nagios/Plugin/Base.pm | ||
diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..c10568c --- /dev/null +++ b/Makefile.PL | |||
@@ -0,0 +1,12 @@ | |||
1 | use 5.008004; | ||
2 | use ExtUtils::MakeMaker; | ||
3 | # See lib/ExtUtils/MakeMaker.pm for details of how to influence | ||
4 | # the contents of the Makefile that is written. | ||
5 | WriteMakefile( | ||
6 | NAME => 'Nagios::Plugin', | ||
7 | VERSION_FROM => 'lib/Nagios/Plugin.pm', # finds $VERSION | ||
8 | PREREQ_PM => {}, # e.g., Module::Name => 1.1 | ||
9 | ($] >= 5.005 ? ## Add these new keywords supported since 5.005 | ||
10 | (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module | ||
11 | AUTHOR => 'Ton Voon <ton.voon@altinity.com>') : ()), | ||
12 | ); | ||
@@ -0,0 +1,29 @@ | |||
1 | Nagios::Plugin | ||
2 | ============== | ||
3 | |||
4 | These modules are meant for perl developers of plugins for Nagios | ||
5 | (http://nagiosplug.sourceforge.net). It is meant to simplify a lot | ||
6 | of the common functions required to do checking of a particular | ||
7 | service. | ||
8 | |||
9 | The modules are still in an experimental stage and will be considered | ||
10 | stable when it reaches version 1.0. | ||
11 | |||
12 | INSTALLATION | ||
13 | |||
14 | To install this module type the following: | ||
15 | |||
16 | perl Makefile.PL | ||
17 | make | ||
18 | make test | ||
19 | make install | ||
20 | |||
21 | COPYRIGHT AND LICENCE | ||
22 | |||
23 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
24 | |||
25 | This library is free software; you can redistribute it and/or modify | ||
26 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
27 | at your option, any later version of Perl 5 you may have available. | ||
28 | |||
29 | |||
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm new file mode 100644 index 0000000..3c6e8d7 --- /dev/null +++ b/lib/Nagios/Plugin.pm | |||
@@ -0,0 +1,160 @@ | |||
1 | # This is only because Class::Struct doesn't allow subclasses | ||
2 | # Trick stolen from Class::DBI | ||
3 | package Nagios::__::Plugin; | ||
4 | |||
5 | use 5.008004; | ||
6 | use Class::Struct; | ||
7 | struct "Nagios::__::Plugin" => { | ||
8 | perfdata => '@', | ||
9 | }; | ||
10 | |||
11 | package Nagios::Plugin; | ||
12 | |||
13 | use Nagios::Plugin::Base; | ||
14 | use Nagios::Plugin::Performance; | ||
15 | use Nagios::Plugin::Threshold; | ||
16 | |||
17 | use strict; | ||
18 | use warnings; | ||
19 | |||
20 | use Carp; | ||
21 | |||
22 | use Exporter; | ||
23 | our @ISA = qw(Exporter Nagios::__::Plugin); | ||
24 | our @EXPORT_OK = qw(%ERRORS); | ||
25 | |||
26 | our $VERSION = '0.10'; | ||
27 | |||
28 | sub add_perfdata { | ||
29 | my ($self, %args) = @_; | ||
30 | my $perf = Nagios::Plugin::Performance->new(%args); | ||
31 | push @{$self->perfdata}, $perf; | ||
32 | } | ||
33 | |||
34 | sub all_perfoutput { | ||
35 | my $self = shift; | ||
36 | return join(" ", map {$_->perfoutput} (@{$self->perfdata})); | ||
37 | } | ||
38 | |||
39 | sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); } | ||
40 | |||
41 | my $shortname; | ||
42 | sub shortname { shift; @_ ? $shortname = shift : $shortname } | ||
43 | |||
44 | sub die { | ||
45 | my $self = shift; | ||
46 | my %args = @_; | ||
47 | Nagios::Plugin::Base->die(\%args, $self); | ||
48 | } | ||
49 | |||
50 | 1; | ||
51 | __END__ | ||
52 | |||
53 | =head1 NAME | ||
54 | |||
55 | Nagios::Plugin - Object oriented helper routines for your Nagios plugin | ||
56 | |||
57 | =head1 SYNOPSIS | ||
58 | |||
59 | use Nagios::Plugin qw(%ERRORS); | ||
60 | $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); | ||
61 | |||
62 | $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" ); | ||
63 | |||
64 | # ... collect current metric into $value | ||
65 | if ($trouble_getting_metric) { | ||
66 | $p->die( return_code => $ERRORS{UNKNOWN}, message => "Could not retrieve page"); | ||
67 | # Output: PAGESIZE UNKNOWN Could not retrieve page | ||
68 | # Return code: 3 | ||
69 | } | ||
70 | |||
71 | $p->add_perfdata( label => "size", | ||
72 | value => $value, | ||
73 | uom => "kB", | ||
74 | threshold => $threshold, | ||
75 | ); | ||
76 | $p->add_perfdata( label => "time", ... ); | ||
77 | |||
78 | $p->die( return_code => $threshold->get_status($value), message => "page size at http://... was ${value}kB" ); | ||
79 | # Output: PAGESIZE OK: page size at http://... was 36kB | size=36kB;10:25;25: time=... | ||
80 | # Return code: 0 | ||
81 | |||
82 | =head1 DESCRIPTION | ||
83 | |||
84 | This is the place for common routines when writing Nagios plugins. The idea is to make it as | ||
85 | easy as possible for developers to conform to the plugin guidelines | ||
86 | (http://nagiosplug.sourceforge.net/developer-guidelines.html). | ||
87 | |||
88 | =head1 DESIGN | ||
89 | |||
90 | To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data | ||
91 | (ie, thresholds, ranges, performance). However, a plugin developer does not need to know about the | ||
92 | different types - a "use Nagios::Plugin" should be sufficient. | ||
93 | |||
94 | There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables | ||
95 | when use'ing Nagios::Plugin | ||
96 | |||
97 | use Nagios::Plugin qw(%ERRORS) | ||
98 | print $ERRORS{WARNING} # prints 1 | ||
99 | |||
100 | =head1 VERSIONING | ||
101 | |||
102 | Only methods listed in the documentation for each module is public. | ||
103 | |||
104 | These modules are experimental and so the interfaces may change up until Nagios::Plugin | ||
105 | hits version 1.0, but every attempt will be made to make backwards compatible. | ||
106 | |||
107 | =over 4 | ||
108 | |||
109 | =head1 STARTING | ||
110 | |||
111 | =item use Nagios::Plugin qw(%ERRORS) | ||
112 | |||
113 | Imports the %ERRORS hash. This is currently the only symbol that can be imported. | ||
114 | |||
115 | =head1 CLASS METHODS | ||
116 | |||
117 | =item Nagios::Plugin->new( shortname => $$ ) | ||
118 | |||
119 | Initializes a new Nagios::Plugin object. Can specify the shortname here. | ||
120 | |||
121 | =head1 OBJECT METHODS | ||
122 | |||
123 | =item set_thresholds( warning => "10:25", critical => "25:" ) | ||
124 | |||
125 | Sets the thresholds, based on the range specification at | ||
126 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. | ||
127 | Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold | ||
128 | is crossed. | ||
129 | |||
130 | =item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold ) | ||
131 | |||
132 | Adds to an array a Nagios::Plugin::Performance object with the specified values. | ||
133 | |||
134 | This needs to be done separately to allow multiple perfdata output. | ||
135 | |||
136 | =item die( return_code => $ERRORS{CRITICAL}, message => "Output" ) | ||
137 | |||
138 | Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified | ||
139 | return code. Also prints performance data if any have been added in. | ||
140 | |||
141 | =back | ||
142 | |||
143 | =head1 SEE ALSO | ||
144 | |||
145 | http://nagiosplug.sourceforge.net | ||
146 | |||
147 | =head1 AUTHOR | ||
148 | |||
149 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
150 | |||
151 | =head1 COPYRIGHT AND LICENSE | ||
152 | |||
153 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
154 | |||
155 | This library is free software; you can redistribute it and/or modify | ||
156 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
157 | at your option, any later version of Perl 5 you may have available. | ||
158 | |||
159 | |||
160 | =cut | ||
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm new file mode 100644 index 0000000..f3a7f7c --- /dev/null +++ b/lib/Nagios/Plugin/Base.pm | |||
@@ -0,0 +1,65 @@ | |||
1 | # This module holds all exported variables | ||
2 | # and base functions | ||
3 | package Nagios::Plugin::Base; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Exporter; | ||
9 | our @ISA = qw(Exporter); | ||
10 | our @EXPORT = qw(%ERRORS); | ||
11 | |||
12 | our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); | ||
13 | |||
14 | our %STATUS_TEXT = reverse %ERRORS; | ||
15 | |||
16 | my $exit_on_die = 1; | ||
17 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | ||
18 | my $print_on_die = 1; | ||
19 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | ||
20 | |||
21 | sub die { | ||
22 | my ($class, $args, $plugin) = @_; | ||
23 | my $return_code = $args->{return_code} || 3; | ||
24 | my $message = $args->{message} || "Internal error"; | ||
25 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); | ||
26 | if ($plugin) { | ||
27 | $output = $plugin->shortname." $output" if $plugin->shortname; | ||
28 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; | ||
29 | } | ||
30 | if ($print_on_die) { | ||
31 | print $output, $/; | ||
32 | } | ||
33 | if ($exit_on_die) { | ||
34 | exit $return_code; | ||
35 | } else { | ||
36 | return $output; | ||
37 | } | ||
38 | } | ||
39 | |||
40 | 1; | ||
41 | __END__ | ||
42 | |||
43 | =head1 NAME | ||
44 | |||
45 | Nagios::Plugin::Base - Base functions for Nagios::Plugins | ||
46 | |||
47 | =head1 DESCRIPTION | ||
48 | |||
49 | See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate | ||
50 | common backend functionality. | ||
51 | |||
52 | =head1 AUTHOR | ||
53 | |||
54 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
55 | |||
56 | =head1 COPYRIGHT AND LICENSE | ||
57 | |||
58 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
59 | |||
60 | This library is free software; you can redistribute it and/or modify | ||
61 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
62 | at your option, any later version of Perl 5 you may have available. | ||
63 | |||
64 | |||
65 | =cut | ||
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm new file mode 100644 index 0000000..eee1bee --- /dev/null +++ b/lib/Nagios/Plugin/Performance.pm | |||
@@ -0,0 +1,119 @@ | |||
1 | package Nagios::Plugin::Performance; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Carp; | ||
9 | use Nagios::Plugin::Threshold; | ||
10 | use Class::Struct; | ||
11 | struct "Nagios::Plugin::Performance" => { | ||
12 | label => '$', | ||
13 | value => '$', | ||
14 | uom => '$', | ||
15 | threshold => 'Nagios::Plugin::Threshold', | ||
16 | min => '$', | ||
17 | max => '$', | ||
18 | }; | ||
19 | |||
20 | sub perfoutput { | ||
21 | my $self = shift; | ||
22 | my $output = $self->label."=".$self->value.$self->uom.";".$self->threshold->warning.";".$self->threshold->critical; | ||
23 | return $output; | ||
24 | } | ||
25 | |||
26 | sub _parse { | ||
27 | my $class = shift; | ||
28 | my $string = shift; | ||
29 | my $p = $class->new; | ||
30 | $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)? *//; | ||
31 | return undef unless ($1 && $2); | ||
32 | $p->label($1); | ||
33 | $p->value($2+0); | ||
34 | $p->uom($3); | ||
35 | $p->threshold(Nagios::Plugin::Threshold->set_thresholds(warning => $4, critical => $5)); | ||
36 | $p->min($6); | ||
37 | $p->max($7); | ||
38 | return ($p, $string); | ||
39 | } | ||
40 | |||
41 | sub parse_perfstring { | ||
42 | my ($class, $perfstring) = @_; | ||
43 | my @perfs; | ||
44 | my $obj; | ||
45 | while ($perfstring) { | ||
46 | ($obj, $perfstring) = $class->_parse($perfstring); | ||
47 | return undef unless $obj; | ||
48 | push @perfs, $obj; | ||
49 | } | ||
50 | return undef unless @perfs; | ||
51 | return @perfs; | ||
52 | } | ||
53 | |||
54 | 1; | ||
55 | __END__ | ||
56 | |||
57 | =head1 NAME | ||
58 | |||
59 | Nagios::Plugin::Performance - Performance information in a perl object | ||
60 | |||
61 | =head1 SYNOPSIS | ||
62 | |||
63 | use Nagios::Plugin::Performance; | ||
64 | |||
65 | @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); | ||
66 | print "1st label = ", $p[0]->label, $/; | ||
67 | print "1st uom = ", $p[0]->uom, $/; | ||
68 | print "2nd crit = ", $p[1]->threshold->critical, $/; | ||
69 | |||
70 | =head1 DESCRIPTION | ||
71 | |||
72 | Handles common Nagios Plugin performance data. This has a public interface because it could be | ||
73 | used by performance graphing routines, such as nagiostat (http://nagiostat.sourceforge.net), | ||
74 | perfparse (http://perfparse.sourceforge.net), nagiosgraph (http://nagiosgraph.sourceforge.net) or | ||
75 | NagiosGrapher (http://www.nagiosexchange.org/NagiosGrapher.84.0.html). | ||
76 | |||
77 | Once the performance string has been parsed, you can query the label, value, uom, or thresholds. | ||
78 | |||
79 | =head1 CLASS METHODS | ||
80 | |||
81 | =over 4 | ||
82 | |||
83 | =item Nagios::Plugin::Performance->parse_perfstring($string) | ||
84 | |||
85 | Returns an array of Nagios::Plugin::Performance objects based on the string entered. | ||
86 | If there is an error parsing the string, undef is returned. | ||
87 | |||
88 | =head1 OBJECT METHODS | ||
89 | |||
90 | =item label, value, uom, min, max | ||
91 | |||
92 | These all return scalars. min and max are not well supported yet. | ||
93 | |||
94 | =item threshold | ||
95 | |||
96 | This returns a Nagios::Plugin::Threshold object. | ||
97 | |||
98 | =back | ||
99 | |||
100 | =head1 SEE ALSO | ||
101 | |||
102 | Nagios::Plugin for information about versioning. | ||
103 | |||
104 | http://nagiosplug.sourceforge.net | ||
105 | |||
106 | =head1 AUTHOR | ||
107 | |||
108 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
109 | |||
110 | =head1 COPYRIGHT AND LICENSE | ||
111 | |||
112 | Copyright (C) 2006 by Altinity Limited | ||
113 | |||
114 | This library is free software; you can redistribute it and/or modify | ||
115 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
116 | at your option, any later version of Perl 5 you may have available. | ||
117 | |||
118 | |||
119 | =cut | ||
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm new file mode 100644 index 0000000..3d6f613 --- /dev/null +++ b/lib/Nagios/Plugin/Range.pm | |||
@@ -0,0 +1,126 @@ | |||
1 | package Nagios::Plugin::Range; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use overload | ||
9 | '""' => sub { shift->stringify }; | ||
10 | |||
11 | use Class::Struct; | ||
12 | struct "Nagios::Plugin::Range" => { | ||
13 | start => '$', | ||
14 | end => '$', | ||
15 | start_infinity => '$', # TRUE / FALSE | ||
16 | end_infinity => '$', # TRUE / FALSE | ||
17 | alert_on => '$', # OUTSIDE 0, INSIDE 1 | ||
18 | }; | ||
19 | |||
20 | my $outside = 0; | ||
21 | my $inside = 1; | ||
22 | |||
23 | sub stringify { | ||
24 | my $self = shift; | ||
25 | return (($self->alert_on) ? "@" : "") . | ||
26 | (($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) . | ||
27 | (($self->end_infinity == 1) ? "" : $self->end); | ||
28 | } | ||
29 | |||
30 | sub set_range_start { | ||
31 | my ($self, $value) = @_; | ||
32 | $self->start($value+0); # Force scalar into number | ||
33 | $self->start_infinity(0); | ||
34 | } | ||
35 | |||
36 | sub set_range_end { | ||
37 | my ($self, $value) = @_; | ||
38 | $self->end($value+0); # Force scalar into number | ||
39 | $self->end_infinity(0); | ||
40 | } | ||
41 | |||
42 | # Returns a N::P::Range object if the string is a conforms to a Nagios Plugin range string, otherwise null | ||
43 | sub parse_range_string { | ||
44 | my ($class, $string) = @_; | ||
45 | my $valid = 0; | ||
46 | my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside); | ||
47 | |||
48 | if ($string =~ s/^\@//) { | ||
49 | $range->alert_on($inside); | ||
50 | } | ||
51 | if ($string =~ s/^~//) { | ||
52 | $range->start_infinity(1); | ||
53 | } | ||
54 | if (($_) = $string =~ /^([-\d\.]+)?:/) { | ||
55 | $range->set_range_start($_) if defined $_; | ||
56 | $string =~ s/^([-\d\.]+)?://; | ||
57 | $valid++ | ||
58 | } | ||
59 | if ($string =~ /^([-\d\.]+)$/) { | ||
60 | $range->set_range_end($string); | ||
61 | $valid++; | ||
62 | } | ||
63 | |||
64 | if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { | ||
65 | return $range; | ||
66 | } | ||
67 | return undef; | ||
68 | } | ||
69 | |||
70 | # Returns 1 if an alert should be raised, otherwise 0 | ||
71 | sub check_range { | ||
72 | my ($self, $value) = @_; | ||
73 | my $false = 0; | ||
74 | my $true = 1; | ||
75 | if ($self->alert_on == $inside) { | ||
76 | $false = 1; | ||
77 | $true = 0; | ||
78 | } | ||
79 | if ($self->end_infinity == 0 && $self->start_infinity == 0) { | ||
80 | if ($self->start <= $value && $value <= $self->end) { | ||
81 | return $false; | ||
82 | } else { | ||
83 | return $true; | ||
84 | } | ||
85 | } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { | ||
86 | if ($self->start <= $value) { | ||
87 | return $false; | ||
88 | } else { | ||
89 | return $true; | ||
90 | } | ||
91 | } elsif ($self->start_infinity == 1 && $self->end_infinity == 0) { | ||
92 | if ($value <= $self->end) { | ||
93 | return $false; | ||
94 | } else { | ||
95 | return $true; | ||
96 | } | ||
97 | } else { | ||
98 | return $false; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | 1; | ||
103 | __END__ | ||
104 | |||
105 | =head1 NAME | ||
106 | |||
107 | Nagios::Plugin::Range - Common range functions for Nagios::Plugin | ||
108 | |||
109 | =head1 DESCRIPTION | ||
110 | |||
111 | Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces. | ||
112 | |||
113 | =head1 AUTHOR | ||
114 | |||
115 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
116 | |||
117 | =head1 COPYRIGHT AND LICENSE | ||
118 | |||
119 | Copyright (C) 2006 by Altinity Limited | ||
120 | |||
121 | This library is free software; you can redistribute it and/or modify | ||
122 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
123 | at your option, any later version of Perl 5 you may have available. | ||
124 | |||
125 | |||
126 | =cut | ||
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm new file mode 100644 index 0000000..9c5d042 --- /dev/null +++ b/lib/Nagios/Plugin/Threshold.pm | |||
@@ -0,0 +1,96 @@ | |||
1 | package Nagios::Plugin::Threshold; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Nagios::Plugin::Range; | ||
9 | use Nagios::Plugin::Base; | ||
10 | |||
11 | use Class::Struct; | ||
12 | struct "Nagios::Plugin::Threshold" => { | ||
13 | warning => 'Nagios::Plugin::Range', | ||
14 | critical => 'Nagios::Plugin::Range', | ||
15 | }; | ||
16 | |||
17 | sub set_thresholds { | ||
18 | my ($class, %args) = @_; | ||
19 | my $t = $class->new; | ||
20 | if (defined $args{warning}) { | ||
21 | my $r = Nagios::Plugin::Range->parse_range_string($args{warning}); | ||
22 | if (defined $r) { | ||
23 | $t->warning($r); | ||
24 | } else { | ||
25 | Nagios::Plugin::Base->die( { | ||
26 | return_code => $ERRORS{UNKNOWN}, | ||
27 | message => "Warning range incorrect: '$args{warning}'" | ||
28 | } ); | ||
29 | } | ||
30 | } | ||
31 | if (defined $args{critical}) { | ||
32 | my $r = Nagios::Plugin::Range->parse_range_string($args{critical}); | ||
33 | if (defined $r) { | ||
34 | $t->critical($r); | ||
35 | } else { | ||
36 | Nagios::Plugin::Base->die( { | ||
37 | return_code => $ERRORS{UNKNOWN}, | ||
38 | message => "Critical range incorrect: '$args{critical}'" | ||
39 | } ); | ||
40 | } | ||
41 | } | ||
42 | return $t; | ||
43 | } | ||
44 | |||
45 | sub get_status { | ||
46 | my ($self, $value) = @_; | ||
47 | if ($self->critical) { | ||
48 | if ($self->critical->check_range($value) == 1) { | ||
49 | return $ERRORS{CRITICAL}; | ||
50 | } | ||
51 | } | ||
52 | if ($self->warning) { | ||
53 | if ($self->warning->check_range($value) == 1) { | ||
54 | return $ERRORS{WARNING}; | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | 1; | ||
60 | __END__ | ||
61 | |||
62 | =head1 NAME | ||
63 | |||
64 | Nagios::Plugin::Threshold - Threshold information in a perl object | ||
65 | |||
66 | =head1 DESCRIPTION | ||
67 | |||
68 | Handles common Nagios Plugin threshold data. See Nagios::Plugin or Nagios::Plugin::Performance for | ||
69 | creation of this object. | ||
70 | |||
71 | =head1 OBJECT METHODS | ||
72 | |||
73 | =over 4 | ||
74 | |||
75 | =item warning, critical | ||
76 | |||
77 | Returns the warning or critical range as a Nagios::Plugin::Range object. | ||
78 | |||
79 | =item get_status($value) | ||
80 | |||
81 | Given a value, will see if the value breeches the critical or the warning range. Returns the status code. | ||
82 | |||
83 | =head1 AUTHOR | ||
84 | |||
85 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
86 | |||
87 | =head1 COPYRIGHT AND LICENSE | ||
88 | |||
89 | Copyright (C) 2006 by Altinity Limited | ||
90 | |||
91 | This library is free software; you can redistribute it and/or modify | ||
92 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
93 | at your option, any later version of Perl 5 you may have available. | ||
94 | |||
95 | |||
96 | =cut | ||
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t new file mode 100644 index 0000000..2fe2326 --- /dev/null +++ b/t/Nagios-Plugin-Performance.t | |||
@@ -0,0 +1,63 @@ | |||
1 | |||
2 | use strict; | ||
3 | use Test::More tests => 42; | ||
4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; | ||
5 | |||
6 | use Nagios::Plugin::Base; | ||
7 | Nagios::Plugin::Base->exit_on_die(0); | ||
8 | |||
9 | my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); | ||
10 | cmp_ok( $p[0]->label, 'eq', "/", "label okay"); | ||
11 | cmp_ok( $p[0]->value, '==', 382, "value okay"); | ||
12 | cmp_ok( $p[0]->uom, 'eq', "MB", "uom okay"); | ||
13 | cmp_ok( $p[0]->threshold->warning->end, "==", 15264, "warn okay"); | ||
14 | cmp_ok( $p[0]->threshold->critical->end, "==", 15269, "crit okay"); | ||
15 | ok( ! defined $p[0]->min, "min okay"); | ||
16 | ok( ! defined $p[0]->max, "max okay"); | ||
17 | |||
18 | cmp_ok( $p[1]->label, 'eq', "/var", "label okay"); | ||
19 | cmp_ok( $p[1]->value, '==', 218, "value okay"); | ||
20 | cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay"); | ||
21 | cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); | ||
22 | cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); | ||
23 | |||
24 | ok( ! defined Nagios::Plugin::Performance->parse_perfstring("rubbish"), "Errors correctly"); | ||
25 | ok( ! defined Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); | ||
26 | |||
27 | @p = Nagios::Plugin::Performance->parse_perfstring( | ||
28 | "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); | ||
29 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); | ||
30 | cmp_ok( $p[0]->value, "==", 0.001229, "value okay"); | ||
31 | cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); | ||
32 | cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay"); | ||
33 | cmp_ok( $p[0]->threshold->critical, "eq", "0", "crit okay"); | ||
34 | |||
35 | @p = Nagios::Plugin::Performance->parse_perfstring( | ||
36 | "load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;"); | ||
37 | cmp_ok( $p[0]->label, "eq", "load1", "label okay"); | ||
38 | cmp_ok( $p[0]->value, "eq", "0", "value okay with 0 as string"); | ||
39 | cmp_ok( $p[0]->uom, "eq", "", "uom empty"); | ||
40 | cmp_ok( $p[0]->threshold->warning, "eq", "5", "warn okay"); | ||
41 | cmp_ok( $p[0]->threshold->critical, "eq", "9", "crit okay"); | ||
42 | cmp_ok( $p[1]->label, "eq", "load5", "label okay"); | ||
43 | cmp_ok( $p[2]->label, "eq", "load15", "label okay"); | ||
44 | |||
45 | @p = Nagios::Plugin::Performance->parse_perfstring( "users=4;20;50;0" ); | ||
46 | cmp_ok( $p[0]->label, "eq", "users", "label okay"); | ||
47 | cmp_ok( $p[0]->value, "==", 4, "value okay"); | ||
48 | cmp_ok( $p[0]->uom, "eq", "", "uom empty"); | ||
49 | cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay"); | ||
50 | cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay"); | ||
51 | |||
52 | @p = Nagios::Plugin::Performance->parse_perfstring( | ||
53 | "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" ); | ||
54 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); | ||
55 | cmp_ok( $p[0]->value, "eq", "0.2153", "value okay"); | ||
56 | cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); | ||
57 | cmp_ok( $p[0]->threshold->warning, 'eq', "5", "warn okay"); | ||
58 | cmp_ok( $p[0]->threshold->critical, 'eq', "10", "crit okay"); | ||
59 | cmp_ok( $p[1]->label, "eq", "size", "label okay"); | ||
60 | cmp_ok( $p[1]->value, "==", 426, "value okay"); | ||
61 | cmp_ok( $p[1]->uom, "eq", "B", "uom okay"); | ||
62 | ok( ! defined $p[1]->threshold->warning, "warn okay"); | ||
63 | ok( ! defined $p[1]->threshold->critical, "crit okay"); | ||
diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t new file mode 100644 index 0000000..13667de --- /dev/null +++ b/t/Nagios-Plugin-Range.t | |||
@@ -0,0 +1,84 @@ | |||
1 | |||
2 | use strict; | ||
3 | use Test::More tests => 60; | ||
4 | BEGIN { use_ok('Nagios::Plugin::Range') }; | ||
5 | |||
6 | |||
7 | my $r = Nagios::Plugin::Range->parse_range_string("6"); | ||
8 | isa_ok( $r, "Nagios::Plugin::Range"); | ||
9 | ok( defined $r, "'6' is valid range"); | ||
10 | cmp_ok( $r->start, '==', 0, "Start correct"); | ||
11 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
12 | cmp_ok( $r->end, '==', 6, "End correct"); | ||
13 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
14 | cmp_ok( $r, 'eq', "6", "Stringification back to original"); | ||
15 | |||
16 | $r = Nagios::Plugin::Range->parse_range_string("-7:23"); | ||
17 | ok( defined $r, "'-7:23' is valid range"); | ||
18 | cmp_ok( $r->start, '==', -7, "Start correct"); | ||
19 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
20 | cmp_ok( $r->end, '==', 23, "End correct"); | ||
21 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
22 | cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); | ||
23 | |||
24 | $r = Nagios::Plugin::Range->parse_range_string(":5.75"); | ||
25 | ok( defined $r, "':5.75' is valid range"); | ||
26 | cmp_ok( $r->start, '==', 0, "Start correct"); | ||
27 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
28 | cmp_ok( $r->end, '==', 5.75, "End correct"); | ||
29 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
30 | cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); | ||
31 | |||
32 | $r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); | ||
33 | ok( defined $r, "'~:-95.99' is valid range"); | ||
34 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); | ||
35 | cmp_ok( $r->end, '==', -95.99, "End correct"); | ||
36 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
37 | cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); | ||
38 | |||
39 | $r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); | ||
40 | ok( defined $r, "'123456789012345:' is valid range"); | ||
41 | cmp_ok( $r->start, '==', 123456789012345, "Start correct"); | ||
42 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
43 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); | ||
44 | cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); | ||
45 | |||
46 | $r = Nagios::Plugin::Range->parse_range_string("~:0"); | ||
47 | ok( defined $r, "'~:0' is valid range"); | ||
48 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); | ||
49 | cmp_ok( $r->end, '==', 0, "End correct"); | ||
50 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
51 | cmp_ok( $r->alert_on, '==', 0, "Will alert on outside of range"); | ||
52 | cmp_ok( $r, 'eq', "~:0", "Stringification back to original"); | ||
53 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); | ||
54 | ok( $r->check_range(-10) == 0, "-10 - no alert"); | ||
55 | ok( $r->check_range(0) == 0, "0 - no alert"); | ||
56 | |||
57 | $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); | ||
58 | ok( defined $r, '"@0:657.8210567" is a valid range'); | ||
59 | cmp_ok( $r->start, '==', 0, "Start correct"); | ||
60 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
61 | cmp_ok( $r->end, '==', 657.8210567, "End correct"); | ||
62 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
63 | cmp_ok( $r->alert_on, '==', 1, "Will alert on inside of range"); | ||
64 | cmp_ok( $r, 'eq', '@657.8210567', "Stringification to simplified version"); | ||
65 | ok( $r->check_range(32.88) == 1, "32.88 - alert"); | ||
66 | ok( $r->check_range(-2) == 0, "-2 - no alert"); | ||
67 | ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); | ||
68 | ok( $r->check_range(0) == 1, "0 - alert"); | ||
69 | |||
70 | $r = Nagios::Plugin::Range->parse_range_string('1:1'); | ||
71 | ok( defined $r, '"1:1" is a valid range'); | ||
72 | cmp_ok( $r->start, '==', 1, "Start correct"); | ||
73 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
74 | cmp_ok( $r->end, '==', 1, "End correct"); | ||
75 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | ||
76 | cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version"); | ||
77 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); | ||
78 | ok( $r->check_range(1) == 0, "1 - no alert"); | ||
79 | ok( $r->check_range(5.2) == 1, "5.2 - alert"); | ||
80 | |||
81 | $r = Nagios::Plugin::Range->parse_range_string('2:1'); | ||
82 | ok( ! defined $r, '"2:1" is rejected'); | ||
83 | |||
84 | # TODO: Need more tests for invalid data | ||
diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t new file mode 100644 index 0000000..764f7b0 --- /dev/null +++ b/t/Nagios-Plugin-Threshold.t | |||
@@ -0,0 +1,32 @@ | |||
1 | |||
2 | use strict; | ||
3 | use Test::More tests => 18; | ||
4 | BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; | ||
5 | |||
6 | Nagios::Plugin::Base->exit_on_die(0); | ||
7 | Nagios::Plugin::Base->print_on_die(0); | ||
8 | |||
9 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); | ||
10 | ok( defined $t, "Threshold ('', '80') set"); | ||
11 | ok( ! defined $t->warning, "Warning not set"); | ||
12 | cmp_ok( $t->critical->end, '==', 80, "Critical set correctly"); | ||
13 | |||
14 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); | ||
15 | ok( defined $t, "Threshold ('5:33', '') set"); | ||
16 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); | ||
17 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); | ||
18 | ok( ! defined $t->critical, "Critical not set"); | ||
19 | |||
20 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60"); | ||
21 | ok( defined $t, "Threshold ('30', '60') set"); | ||
22 | cmp_ok( $t->warning->end, '==', 30, "Warning end set"); | ||
23 | cmp_ok( $t->critical->end, '==',60, "Critical end set"); | ||
24 | cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok"); | ||
25 | cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning"); | ||
26 | cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical"); | ||
27 | |||
28 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish"); | ||
29 | ok( defined $t, "Threshold object created although ..."); | ||
30 | ok( ! defined $t->warning, "Warning not set"); | ||
31 | ok( ! defined $t->critical, "Critical not set"); | ||
32 | |||
diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t new file mode 100644 index 0000000..38e792d --- /dev/null +++ b/t/Nagios-Plugin.t | |||
@@ -0,0 +1,32 @@ | |||
1 | |||
2 | use strict; | ||
3 | use Test::More tests => 5; | ||
4 | BEGIN { use_ok('Nagios::Plugin') }; | ||
5 | |||
6 | use Nagios::Plugin::Base; | ||
7 | Nagios::Plugin::Base->exit_on_die(0); | ||
8 | Nagios::Plugin::Base->print_on_die(0); | ||
9 | |||
10 | my $p = Nagios::Plugin->new; | ||
11 | isa_ok( $p, "Nagios::Plugin"); | ||
12 | |||
13 | $p->shortname("PAGESIZE"); | ||
14 | |||
15 | my $t = $p->set_thresholds( warning => "10:25", critical => "25:" ); | ||
16 | |||
17 | $p->add_perfdata( | ||
18 | label => "size", | ||
19 | value => 1, | ||
20 | uom => "kB", | ||
21 | threshold => $t, | ||
22 | ); | ||
23 | |||
24 | cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct"); | ||
25 | |||
26 | my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" ); | ||
27 | cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay"); | ||
28 | |||
29 | cmp_ok( $p->die( return_code => $t->get_status(30), message => "page size at http://... was 30kB" ), | ||
30 | "eq", 'PAGESIZE WARNING page size at http://... was 30kB | size=1kB;10:25;25:', "Output okay"); | ||
31 | |||
32 | |||