summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.cvsignore3
-rw-r--r--Changes9
-rw-r--r--MANIFEST13
-rw-r--r--Makefile.PL12
-rw-r--r--README29
-rw-r--r--lib/Nagios/Plugin.pm160
-rw-r--r--lib/Nagios/Plugin/Base.pm65
-rw-r--r--lib/Nagios/Plugin/Performance.pm119
-rw-r--r--lib/Nagios/Plugin/Range.pm126
-rw-r--r--lib/Nagios/Plugin/Threshold.pm96
-rw-r--r--t/Nagios-Plugin-Performance.t63
-rw-r--r--t/Nagios-Plugin-Range.t84
-rw-r--r--t/Nagios-Plugin-Threshold.t32
-rw-r--r--t/Nagios-Plugin.t32
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 @@
1Makefile
2blib
3pm_to_blib
diff --git a/Changes b/Changes
new file mode 100644
index 0000000..9095567
--- /dev/null
+++ b/Changes
@@ -0,0 +1,9 @@
1Revision history for Perl extension Nagios::Plugin.
2
30.10 8th June 2006
4 First release to CPAN
5
60.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 @@
1Changes
2Makefile.PL
3MANIFEST
4README
5t/Nagios-Plugin.t
6t/Nagios-Plugin-Performance.t
7t/Nagios-Plugin-Range.t
8t/Nagios-Plugin-Threshold.t
9lib/Nagios/Plugin.pm
10lib/Nagios/Plugin/Performance.pm
11lib/Nagios/Plugin/Range.pm
12lib/Nagios/Plugin/Threshold.pm
13lib/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 @@
1use 5.008004;
2use ExtUtils::MakeMaker;
3# See lib/ExtUtils/MakeMaker.pm for details of how to influence
4# the contents of the Makefile that is written.
5WriteMakefile(
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);
diff --git a/README b/README
new file mode 100644
index 0000000..bde0f80
--- /dev/null
+++ b/README
@@ -0,0 +1,29 @@
1Nagios::Plugin
2==============
3
4These modules are meant for perl developers of plugins for Nagios
5(http://nagiosplug.sourceforge.net). It is meant to simplify a lot
6of the common functions required to do checking of a particular
7service.
8
9The modules are still in an experimental stage and will be considered
10stable when it reaches version 1.0.
11
12INSTALLATION
13
14To install this module type the following:
15
16 perl Makefile.PL
17 make
18 make test
19 make install
20
21COPYRIGHT AND LICENCE
22
23Copyright (C) 2006 by Nagios Plugin Development Team
24
25This library is free software; you can redistribute it and/or modify
26it under the same terms as Perl itself, either Perl version 5.8.4 or,
27at 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
3package Nagios::__::Plugin;
4
5use 5.008004;
6use Class::Struct;
7struct "Nagios::__::Plugin" => {
8 perfdata => '@',
9 };
10
11package Nagios::Plugin;
12
13use Nagios::Plugin::Base;
14use Nagios::Plugin::Performance;
15use Nagios::Plugin::Threshold;
16
17use strict;
18use warnings;
19
20use Carp;
21
22use Exporter;
23our @ISA = qw(Exporter Nagios::__::Plugin);
24our @EXPORT_OK = qw(%ERRORS);
25
26our $VERSION = '0.10';
27
28sub add_perfdata {
29 my ($self, %args) = @_;
30 my $perf = Nagios::Plugin::Performance->new(%args);
31 push @{$self->perfdata}, $perf;
32}
33
34sub all_perfoutput {
35 my $self = shift;
36 return join(" ", map {$_->perfoutput} (@{$self->perfdata}));
37}
38
39sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); }
40
41my $shortname;
42sub shortname { shift; @_ ? $shortname = shift : $shortname }
43
44sub die {
45 my $self = shift;
46 my %args = @_;
47 Nagios::Plugin::Base->die(\%args, $self);
48}
49
501;
51__END__
52
53=head1 NAME
54
55Nagios::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
84This is the place for common routines when writing Nagios plugins. The idea is to make it as
85easy as possible for developers to conform to the plugin guidelines
86(http://nagiosplug.sourceforge.net/developer-guidelines.html).
87
88=head1 DESIGN
89
90To 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
92different types - a "use Nagios::Plugin" should be sufficient.
93
94There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables
95when use'ing Nagios::Plugin
96
97 use Nagios::Plugin qw(%ERRORS)
98 print $ERRORS{WARNING} # prints 1
99
100=head1 VERSIONING
101
102Only methods listed in the documentation for each module is public.
103
104These modules are experimental and so the interfaces may change up until Nagios::Plugin
105hits 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
113Imports 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
119Initializes 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
125Sets the thresholds, based on the range specification at
126http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT.
127Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold
128is crossed.
129
130=item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold )
131
132Adds to an array a Nagios::Plugin::Performance object with the specified values.
133
134This needs to be done separately to allow multiple perfdata output.
135
136=item die( return_code => $ERRORS{CRITICAL}, message => "Output" )
137
138Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified
139return code. Also prints performance data if any have been added in.
140
141=back
142
143=head1 SEE ALSO
144
145http://nagiosplug.sourceforge.net
146
147=head1 AUTHOR
148
149Ton Voon, E<lt>ton.voon@altinity.comE<gt>
150
151=head1 COPYRIGHT AND LICENSE
152
153Copyright (C) 2006 by Nagios Plugin Development Team
154
155This library is free software; you can redistribute it and/or modify
156it under the same terms as Perl itself, either Perl version 5.8.4 or,
157at 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
3package Nagios::Plugin::Base;
4
5use strict;
6use warnings;
7
8use Exporter;
9our @ISA = qw(Exporter);
10our @EXPORT = qw(%ERRORS);
11
12our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
13
14our %STATUS_TEXT = reverse %ERRORS;
15
16my $exit_on_die = 1;
17sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die };
18my $print_on_die = 1;
19sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die };
20
21sub 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
401;
41__END__
42
43=head1 NAME
44
45Nagios::Plugin::Base - Base functions for Nagios::Plugins
46
47=head1 DESCRIPTION
48
49See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate
50common backend functionality.
51
52=head1 AUTHOR
53
54Ton Voon, E<lt>ton.voon@altinity.comE<gt>
55
56=head1 COPYRIGHT AND LICENSE
57
58Copyright (C) 2006 by Nagios Plugin Development Team
59
60This library is free software; you can redistribute it and/or modify
61it under the same terms as Perl itself, either Perl version 5.8.4 or,
62at 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 @@
1package Nagios::Plugin::Performance;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use Carp;
9use Nagios::Plugin::Threshold;
10use Class::Struct;
11struct "Nagios::Plugin::Performance" => {
12 label => '$',
13 value => '$',
14 uom => '$',
15 threshold => 'Nagios::Plugin::Threshold',
16 min => '$',
17 max => '$',
18 };
19
20sub 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
26sub _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
41sub 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
541;
55__END__
56
57=head1 NAME
58
59Nagios::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
72Handles common Nagios Plugin performance data. This has a public interface because it could be
73used by performance graphing routines, such as nagiostat (http://nagiostat.sourceforge.net),
74perfparse (http://perfparse.sourceforge.net), nagiosgraph (http://nagiosgraph.sourceforge.net) or
75NagiosGrapher (http://www.nagiosexchange.org/NagiosGrapher.84.0.html).
76
77Once 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
85Returns an array of Nagios::Plugin::Performance objects based on the string entered.
86If there is an error parsing the string, undef is returned.
87
88=head1 OBJECT METHODS
89
90=item label, value, uom, min, max
91
92These all return scalars. min and max are not well supported yet.
93
94=item threshold
95
96This returns a Nagios::Plugin::Threshold object.
97
98=back
99
100=head1 SEE ALSO
101
102Nagios::Plugin for information about versioning.
103
104http://nagiosplug.sourceforge.net
105
106=head1 AUTHOR
107
108Ton Voon, E<lt>ton.voon@altinity.comE<gt>
109
110=head1 COPYRIGHT AND LICENSE
111
112Copyright (C) 2006 by Altinity Limited
113
114This library is free software; you can redistribute it and/or modify
115it under the same terms as Perl itself, either Perl version 5.8.4 or,
116at 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 @@
1package Nagios::Plugin::Range;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use overload
9 '""' => sub { shift->stringify };
10
11use Class::Struct;
12struct "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
20my $outside = 0;
21my $inside = 1;
22
23sub 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
30sub set_range_start {
31 my ($self, $value) = @_;
32 $self->start($value+0); # Force scalar into number
33 $self->start_infinity(0);
34}
35
36sub 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
43sub 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
71sub 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
1021;
103__END__
104
105=head1 NAME
106
107Nagios::Plugin::Range - Common range functions for Nagios::Plugin
108
109=head1 DESCRIPTION
110
111Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces.
112
113=head1 AUTHOR
114
115Ton Voon, E<lt>ton.voon@altinity.comE<gt>
116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright (C) 2006 by Altinity Limited
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself, either Perl version 5.8.4 or,
123at 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 @@
1package Nagios::Plugin::Threshold;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use Nagios::Plugin::Range;
9use Nagios::Plugin::Base;
10
11use Class::Struct;
12struct "Nagios::Plugin::Threshold" => {
13 warning => 'Nagios::Plugin::Range',
14 critical => 'Nagios::Plugin::Range',
15 };
16
17sub 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
45sub 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
591;
60__END__
61
62=head1 NAME
63
64Nagios::Plugin::Threshold - Threshold information in a perl object
65
66=head1 DESCRIPTION
67
68Handles common Nagios Plugin threshold data. See Nagios::Plugin or Nagios::Plugin::Performance for
69creation of this object.
70
71=head1 OBJECT METHODS
72
73=over 4
74
75=item warning, critical
76
77Returns the warning or critical range as a Nagios::Plugin::Range object.
78
79=item get_status($value)
80
81Given a value, will see if the value breeches the critical or the warning range. Returns the status code.
82
83=head1 AUTHOR
84
85Ton Voon, E<lt>ton.voon@altinity.comE<gt>
86
87=head1 COPYRIGHT AND LICENSE
88
89Copyright (C) 2006 by Altinity Limited
90
91This library is free software; you can redistribute it and/or modify
92it under the same terms as Perl itself, either Perl version 5.8.4 or,
93at 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
2use strict;
3use Test::More tests => 42;
4BEGIN { use_ok('Nagios::Plugin::Performance') };
5
6use Nagios::Plugin::Base;
7Nagios::Plugin::Base->exit_on_die(0);
8
9my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448");
10cmp_ok( $p[0]->label, 'eq', "/", "label okay");
11cmp_ok( $p[0]->value, '==', 382, "value okay");
12cmp_ok( $p[0]->uom, 'eq', "MB", "uom okay");
13cmp_ok( $p[0]->threshold->warning->end, "==", 15264, "warn okay");
14cmp_ok( $p[0]->threshold->critical->end, "==", 15269, "crit okay");
15ok( ! defined $p[0]->min, "min okay");
16ok( ! defined $p[0]->max, "max okay");
17
18cmp_ok( $p[1]->label, 'eq', "/var", "label okay");
19cmp_ok( $p[1]->value, '==', 218, "value okay");
20cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay");
21cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay");
22cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay");
23
24ok( ! defined Nagios::Plugin::Performance->parse_perfstring("rubbish"), "Errors correctly");
25ok( ! 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");
29cmp_ok( $p[0]->label, "eq", "time", "label okay");
30cmp_ok( $p[0]->value, "==", 0.001229, "value okay");
31cmp_ok( $p[0]->uom, "eq", "s", "uom okay");
32cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay");
33cmp_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;");
37cmp_ok( $p[0]->label, "eq", "load1", "label okay");
38cmp_ok( $p[0]->value, "eq", "0", "value okay with 0 as string");
39cmp_ok( $p[0]->uom, "eq", "", "uom empty");
40cmp_ok( $p[0]->threshold->warning, "eq", "5", "warn okay");
41cmp_ok( $p[0]->threshold->critical, "eq", "9", "crit okay");
42cmp_ok( $p[1]->label, "eq", "load5", "label okay");
43cmp_ok( $p[2]->label, "eq", "load15", "label okay");
44
45@p = Nagios::Plugin::Performance->parse_perfstring( "users=4;20;50;0" );
46cmp_ok( $p[0]->label, "eq", "users", "label okay");
47cmp_ok( $p[0]->value, "==", 4, "value okay");
48cmp_ok( $p[0]->uom, "eq", "", "uom empty");
49cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay");
50cmp_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" );
54cmp_ok( $p[0]->label, "eq", "time", "label okay");
55cmp_ok( $p[0]->value, "eq", "0.2153", "value okay");
56cmp_ok( $p[0]->uom, "eq", "s", "uom okay");
57cmp_ok( $p[0]->threshold->warning, 'eq', "5", "warn okay");
58cmp_ok( $p[0]->threshold->critical, 'eq', "10", "crit okay");
59cmp_ok( $p[1]->label, "eq", "size", "label okay");
60cmp_ok( $p[1]->value, "==", 426, "value okay");
61cmp_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
2use strict;
3use Test::More tests => 60;
4BEGIN { use_ok('Nagios::Plugin::Range') };
5
6
7my $r = Nagios::Plugin::Range->parse_range_string("6");
8isa_ok( $r, "Nagios::Plugin::Range");
9ok( defined $r, "'6' is valid range");
10cmp_ok( $r->start, '==', 0, "Start correct");
11cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
12cmp_ok( $r->end, '==', 6, "End correct");
13cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
14cmp_ok( $r, 'eq', "6", "Stringification back to original");
15
16$r = Nagios::Plugin::Range->parse_range_string("-7:23");
17ok( defined $r, "'-7:23' is valid range");
18cmp_ok( $r->start, '==', -7, "Start correct");
19cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
20cmp_ok( $r->end, '==', 23, "End correct");
21cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
22cmp_ok( $r, 'eq', "-7:23", "Stringification back to original");
23
24$r = Nagios::Plugin::Range->parse_range_string(":5.75");
25ok( defined $r, "':5.75' is valid range");
26cmp_ok( $r->start, '==', 0, "Start correct");
27cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
28cmp_ok( $r->end, '==', 5.75, "End correct");
29cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
30cmp_ok( $r, 'eq', "5.75", "Stringification to simplification");
31
32$r = Nagios::Plugin::Range->parse_range_string("~:-95.99");
33ok( defined $r, "'~:-95.99' is valid range");
34cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
35cmp_ok( $r->end, '==', -95.99, "End correct");
36cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
37cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original");
38
39$r = Nagios::Plugin::Range->parse_range_string("123456789012345:");
40ok( defined $r, "'123456789012345:' is valid range");
41cmp_ok( $r->start, '==', 123456789012345, "Start correct");
42cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
43cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity");
44cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original");
45
46$r = Nagios::Plugin::Range->parse_range_string("~:0");
47ok( defined $r, "'~:0' is valid range");
48cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
49cmp_ok( $r->end, '==', 0, "End correct");
50cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
51cmp_ok( $r->alert_on, '==', 0, "Will alert on outside of range");
52cmp_ok( $r, 'eq', "~:0", "Stringification back to original");
53ok( $r->check_range(0.5) == 1, "0.5 - alert");
54ok( $r->check_range(-10) == 0, "-10 - no alert");
55ok( $r->check_range(0) == 0, "0 - no alert");
56
57$r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567');
58ok( defined $r, '"@0:657.8210567" is a valid range');
59cmp_ok( $r->start, '==', 0, "Start correct");
60cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
61cmp_ok( $r->end, '==', 657.8210567, "End correct");
62cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
63cmp_ok( $r->alert_on, '==', 1, "Will alert on inside of range");
64cmp_ok( $r, 'eq', '@657.8210567', "Stringification to simplified version");
65ok( $r->check_range(32.88) == 1, "32.88 - alert");
66ok( $r->check_range(-2) == 0, "-2 - no alert");
67ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert");
68ok( $r->check_range(0) == 1, "0 - alert");
69
70$r = Nagios::Plugin::Range->parse_range_string('1:1');
71ok( defined $r, '"1:1" is a valid range');
72cmp_ok( $r->start, '==', 1, "Start correct");
73cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
74cmp_ok( $r->end, '==', 1, "End correct");
75cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
76cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version");
77ok( $r->check_range(0.5) == 1, "0.5 - alert");
78ok( $r->check_range(1) == 0, "1 - no alert");
79ok( $r->check_range(5.2) == 1, "5.2 - alert");
80
81$r = Nagios::Plugin::Range->parse_range_string('2:1');
82ok( ! 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
2use strict;
3use Test::More tests => 18;
4BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') };
5
6Nagios::Plugin::Base->exit_on_die(0);
7Nagios::Plugin::Base->print_on_die(0);
8
9my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80");
10ok( defined $t, "Threshold ('', '80') set");
11ok( ! defined $t->warning, "Warning not set");
12cmp_ok( $t->critical->end, '==', 80, "Critical set correctly");
13
14$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "");
15ok( defined $t, "Threshold ('5:33', '') set");
16cmp_ok( $t->warning->start, '==', 5, "Warning start set");
17cmp_ok( $t->warning->end, '==', 33, "Warning end set");
18ok( ! defined $t->critical, "Critical not set");
19
20$t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60");
21ok( defined $t, "Threshold ('30', '60') set");
22cmp_ok( $t->warning->end, '==', 30, "Warning end set");
23cmp_ok( $t->critical->end, '==',60, "Critical end set");
24cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok");
25cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning");
26cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical");
27
28$t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish");
29ok( defined $t, "Threshold object created although ...");
30ok( ! defined $t->warning, "Warning not set");
31ok( ! 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
2use strict;
3use Test::More tests => 5;
4BEGIN { use_ok('Nagios::Plugin') };
5
6use Nagios::Plugin::Base;
7Nagios::Plugin::Base->exit_on_die(0);
8Nagios::Plugin::Base->print_on_die(0);
9
10my $p = Nagios::Plugin->new;
11isa_ok( $p, "Nagios::Plugin");
12
13$p->shortname("PAGESIZE");
14
15my $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
24cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct");
25
26my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" );
27cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay");
28
29cmp_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