From 2c6651034f76e2bccb549a867485f8fabbf07cb1 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 8 Jun 2006 12:27:44 +0000 Subject: Initial revision git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1419 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..0b5bd39 --- /dev/null +++ b/.cvsignore @@ -0,0 +1,3 @@ +Makefile +blib +pm_to_blib diff --git a/Changes b/Changes new file mode 100644 index 0000000..9095567 --- /dev/null +++ b/Changes @@ -0,0 +1,9 @@ +Revision history for Perl extension Nagios::Plugin. + +0.10 8th June 2006 + First release to CPAN + +0.01 Fri Jun 2 14:10:58 2006 + - original version; created by h2xs 1.23 with options + -X -n Nagios::Plugin + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..336daab --- /dev/null +++ b/MANIFEST @@ -0,0 +1,13 @@ +Changes +Makefile.PL +MANIFEST +README +t/Nagios-Plugin.t +t/Nagios-Plugin-Performance.t +t/Nagios-Plugin-Range.t +t/Nagios-Plugin-Threshold.t +lib/Nagios/Plugin.pm +lib/Nagios/Plugin/Performance.pm +lib/Nagios/Plugin/Range.pm +lib/Nagios/Plugin/Threshold.pm +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 @@ +use 5.008004; +use ExtUtils::MakeMaker; +# See lib/ExtUtils/MakeMaker.pm for details of how to influence +# the contents of the Makefile that is written. +WriteMakefile( + NAME => 'Nagios::Plugin', + VERSION_FROM => 'lib/Nagios/Plugin.pm', # finds $VERSION + PREREQ_PM => {}, # e.g., Module::Name => 1.1 + ($] >= 5.005 ? ## Add these new keywords supported since 5.005 + (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module + AUTHOR => 'Ton Voon ') : ()), +); diff --git a/README b/README new file mode 100644 index 0000000..bde0f80 --- /dev/null +++ b/README @@ -0,0 +1,29 @@ +Nagios::Plugin +============== + +These modules are meant for perl developers of plugins for Nagios +(http://nagiosplug.sourceforge.net). It is meant to simplify a lot +of the common functions required to do checking of a particular +service. + +The modules are still in an experimental stage and will be considered +stable when it reaches version 1.0. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +COPYRIGHT AND LICENCE + +Copyright (C) 2006 by Nagios Plugin Development Team + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.4 or, +at your option, any later version of Perl 5 you may have available. + + 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 @@ +# This is only because Class::Struct doesn't allow subclasses +# Trick stolen from Class::DBI +package Nagios::__::Plugin; + +use 5.008004; +use Class::Struct; +struct "Nagios::__::Plugin" => { + perfdata => '@', + }; + +package Nagios::Plugin; + +use Nagios::Plugin::Base; +use Nagios::Plugin::Performance; +use Nagios::Plugin::Threshold; + +use strict; +use warnings; + +use Carp; + +use Exporter; +our @ISA = qw(Exporter Nagios::__::Plugin); +our @EXPORT_OK = qw(%ERRORS); + +our $VERSION = '0.10'; + +sub add_perfdata { + my ($self, %args) = @_; + my $perf = Nagios::Plugin::Performance->new(%args); + push @{$self->perfdata}, $perf; +} + +sub all_perfoutput { + my $self = shift; + return join(" ", map {$_->perfoutput} (@{$self->perfdata})); +} + +sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); } + +my $shortname; +sub shortname { shift; @_ ? $shortname = shift : $shortname } + +sub die { + my $self = shift; + my %args = @_; + Nagios::Plugin::Base->die(\%args, $self); +} + +1; +__END__ + +=head1 NAME + +Nagios::Plugin - Object oriented helper routines for your Nagios plugin + +=head1 SYNOPSIS + + use Nagios::Plugin qw(%ERRORS); + $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); + + $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" ); + + # ... collect current metric into $value + if ($trouble_getting_metric) { + $p->die( return_code => $ERRORS{UNKNOWN}, message => "Could not retrieve page"); + # Output: PAGESIZE UNKNOWN Could not retrieve page + # Return code: 3 + } + + $p->add_perfdata( label => "size", + value => $value, + uom => "kB", + threshold => $threshold, + ); + $p->add_perfdata( label => "time", ... ); + + $p->die( return_code => $threshold->get_status($value), message => "page size at http://... was ${value}kB" ); + # Output: PAGESIZE OK: page size at http://... was 36kB | size=36kB;10:25;25: time=... + # Return code: 0 + +=head1 DESCRIPTION + +This is the place for common routines when writing Nagios plugins. The idea is to make it as +easy as possible for developers to conform to the plugin guidelines +(http://nagiosplug.sourceforge.net/developer-guidelines.html). + +=head1 DESIGN + +To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data +(ie, thresholds, ranges, performance). However, a plugin developer does not need to know about the +different types - a "use Nagios::Plugin" should be sufficient. + +There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables +when use'ing Nagios::Plugin + + use Nagios::Plugin qw(%ERRORS) + print $ERRORS{WARNING} # prints 1 + +=head1 VERSIONING + +Only methods listed in the documentation for each module is public. + +These modules are experimental and so the interfaces may change up until Nagios::Plugin +hits version 1.0, but every attempt will be made to make backwards compatible. + +=over 4 + +=head1 STARTING + +=item use Nagios::Plugin qw(%ERRORS) + +Imports the %ERRORS hash. This is currently the only symbol that can be imported. + +=head1 CLASS METHODS + +=item Nagios::Plugin->new( shortname => $$ ) + +Initializes a new Nagios::Plugin object. Can specify the shortname here. + +=head1 OBJECT METHODS + +=item set_thresholds( warning => "10:25", critical => "25:" ) + +Sets the thresholds, based on the range specification at +http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. +Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold +is crossed. + +=item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold ) + +Adds to an array a Nagios::Plugin::Performance object with the specified values. + +This needs to be done separately to allow multiple perfdata output. + +=item die( return_code => $ERRORS{CRITICAL}, message => "Output" ) + +Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified +return code. Also prints performance data if any have been added in. + +=back + +=head1 SEE ALSO + +http://nagiosplug.sourceforge.net + +=head1 AUTHOR + +Ton Voon, Eton.voon@altinity.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2006 by Nagios Plugin Development Team + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.4 or, +at your option, any later version of Perl 5 you may have available. + + +=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 @@ +# This module holds all exported variables +# and base functions +package Nagios::Plugin::Base; + +use strict; +use warnings; + +use Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(%ERRORS); + +our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); + +our %STATUS_TEXT = reverse %ERRORS; + +my $exit_on_die = 1; +sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; +my $print_on_die = 1; +sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; + +sub die { + my ($class, $args, $plugin) = @_; + my $return_code = $args->{return_code} || 3; + my $message = $args->{message} || "Internal error"; + my $output = join(" ", $STATUS_TEXT{$return_code}, $message); + if ($plugin) { + $output = $plugin->shortname." $output" if $plugin->shortname; + $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; + } + if ($print_on_die) { + print $output, $/; + } + if ($exit_on_die) { + exit $return_code; + } else { + return $output; + } +} + +1; +__END__ + +=head1 NAME + +Nagios::Plugin::Base - Base functions for Nagios::Plugins + +=head1 DESCRIPTION + +See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate +common backend functionality. + +=head1 AUTHOR + +Ton Voon, Eton.voon@altinity.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2006 by Nagios Plugin Development Team + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.4 or, +at your option, any later version of Perl 5 you may have available. + + +=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 @@ +package Nagios::Plugin::Performance; + +use 5.008004; + +use strict; +use warnings; + +use Carp; +use Nagios::Plugin::Threshold; +use Class::Struct; +struct "Nagios::Plugin::Performance" => { + label => '$', + value => '$', + uom => '$', + threshold => 'Nagios::Plugin::Threshold', + min => '$', + max => '$', + }; + +sub perfoutput { + my $self = shift; + my $output = $self->label."=".$self->value.$self->uom.";".$self->threshold->warning.";".$self->threshold->critical; + return $output; +} + +sub _parse { + my $class = shift; + my $string = shift; + my $p = $class->new; + $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)? *//; + return undef unless ($1 && $2); + $p->label($1); + $p->value($2+0); + $p->uom($3); + $p->threshold(Nagios::Plugin::Threshold->set_thresholds(warning => $4, critical => $5)); + $p->min($6); + $p->max($7); + return ($p, $string); +} + +sub parse_perfstring { + my ($class, $perfstring) = @_; + my @perfs; + my $obj; + while ($perfstring) { + ($obj, $perfstring) = $class->_parse($perfstring); + return undef unless $obj; + push @perfs, $obj; + } + return undef unless @perfs; + return @perfs; +} + +1; +__END__ + +=head1 NAME + +Nagios::Plugin::Performance - Performance information in a perl object + +=head1 SYNOPSIS + + use Nagios::Plugin::Performance; + + @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); + print "1st label = ", $p[0]->label, $/; + print "1st uom = ", $p[0]->uom, $/; + print "2nd crit = ", $p[1]->threshold->critical, $/; + +=head1 DESCRIPTION + +Handles common Nagios Plugin performance data. This has a public interface because it could be +used by performance graphing routines, such as nagiostat (http://nagiostat.sourceforge.net), +perfparse (http://perfparse.sourceforge.net), nagiosgraph (http://nagiosgraph.sourceforge.net) or +NagiosGrapher (http://www.nagiosexchange.org/NagiosGrapher.84.0.html). + +Once the performance string has been parsed, you can query the label, value, uom, or thresholds. + +=head1 CLASS METHODS + +=over 4 + +=item Nagios::Plugin::Performance->parse_perfstring($string) + +Returns an array of Nagios::Plugin::Performance objects based on the string entered. +If there is an error parsing the string, undef is returned. + +=head1 OBJECT METHODS + +=item label, value, uom, min, max + +These all return scalars. min and max are not well supported yet. + +=item threshold + +This returns a Nagios::Plugin::Threshold object. + +=back + +=head1 SEE ALSO + +Nagios::Plugin for information about versioning. + +http://nagiosplug.sourceforge.net + +=head1 AUTHOR + +Ton Voon, Eton.voon@altinity.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2006 by Altinity Limited + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.4 or, +at your option, any later version of Perl 5 you may have available. + + +=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 @@ +package Nagios::Plugin::Range; + +use 5.008004; + +use strict; +use warnings; + +use overload + '""' => sub { shift->stringify }; + +use Class::Struct; +struct "Nagios::Plugin::Range" => { + start => '$', + end => '$', + start_infinity => '$', # TRUE / FALSE + end_infinity => '$', # TRUE / FALSE + alert_on => '$', # OUTSIDE 0, INSIDE 1 + }; + +my $outside = 0; +my $inside = 1; + +sub stringify { + my $self = shift; + return (($self->alert_on) ? "@" : "") . + (($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) . + (($self->end_infinity == 1) ? "" : $self->end); +} + +sub set_range_start { + my ($self, $value) = @_; + $self->start($value+0); # Force scalar into number + $self->start_infinity(0); +} + +sub set_range_end { + my ($self, $value) = @_; + $self->end($value+0); # Force scalar into number + $self->end_infinity(0); +} + +# Returns a N::P::Range object if the string is a conforms to a Nagios Plugin range string, otherwise null +sub parse_range_string { + my ($class, $string) = @_; + my $valid = 0; + my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside); + + if ($string =~ s/^\@//) { + $range->alert_on($inside); + } + if ($string =~ s/^~//) { + $range->start_infinity(1); + } + if (($_) = $string =~ /^([-\d\.]+)?:/) { + $range->set_range_start($_) if defined $_; + $string =~ s/^([-\d\.]+)?://; + $valid++ + } + if ($string =~ /^([-\d\.]+)$/) { + $range->set_range_end($string); + $valid++; + } + + if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { + return $range; + } + return undef; +} + +# Returns 1 if an alert should be raised, otherwise 0 +sub check_range { + my ($self, $value) = @_; + my $false = 0; + my $true = 1; + if ($self->alert_on == $inside) { + $false = 1; + $true = 0; + } + if ($self->end_infinity == 0 && $self->start_infinity == 0) { + if ($self->start <= $value && $value <= $self->end) { + return $false; + } else { + return $true; + } + } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { + if ($self->start <= $value) { + return $false; + } else { + return $true; + } + } elsif ($self->start_infinity == 1 && $self->end_infinity == 0) { + if ($value <= $self->end) { + return $false; + } else { + return $true; + } + } else { + return $false; + } +} + +1; +__END__ + +=head1 NAME + +Nagios::Plugin::Range - Common range functions for Nagios::Plugin + +=head1 DESCRIPTION + +Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces. + +=head1 AUTHOR + +Ton Voon, Eton.voon@altinity.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2006 by Altinity Limited + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.4 or, +at your option, any later version of Perl 5 you may have available. + + +=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 @@ +package Nagios::Plugin::Threshold; + +use 5.008004; + +use strict; +use warnings; + +use Nagios::Plugin::Range; +use Nagios::Plugin::Base; + +use Class::Struct; +struct "Nagios::Plugin::Threshold" => { + warning => 'Nagios::Plugin::Range', + critical => 'Nagios::Plugin::Range', + }; + +sub set_thresholds { + my ($class, %args) = @_; + my $t = $class->new; + if (defined $args{warning}) { + my $r = Nagios::Plugin::Range->parse_range_string($args{warning}); + if (defined $r) { + $t->warning($r); + } else { + Nagios::Plugin::Base->die( { + return_code => $ERRORS{UNKNOWN}, + message => "Warning range incorrect: '$args{warning}'" + } ); + } + } + if (defined $args{critical}) { + my $r = Nagios::Plugin::Range->parse_range_string($args{critical}); + if (defined $r) { + $t->critical($r); + } else { + Nagios::Plugin::Base->die( { + return_code => $ERRORS{UNKNOWN}, + message => "Critical range incorrect: '$args{critical}'" + } ); + } + } + return $t; +} + +sub get_status { + my ($self, $value) = @_; + if ($self->critical) { + if ($self->critical->check_range($value) == 1) { + return $ERRORS{CRITICAL}; + } + } + if ($self->warning) { + if ($self->warning->check_range($value) == 1) { + return $ERRORS{WARNING}; + } + } +} + +1; +__END__ + +=head1 NAME + +Nagios::Plugin::Threshold - Threshold information in a perl object + +=head1 DESCRIPTION + +Handles common Nagios Plugin threshold data. See Nagios::Plugin or Nagios::Plugin::Performance for +creation of this object. + +=head1 OBJECT METHODS + +=over 4 + +=item warning, critical + +Returns the warning or critical range as a Nagios::Plugin::Range object. + +=item get_status($value) + +Given a value, will see if the value breeches the critical or the warning range. Returns the status code. + +=head1 AUTHOR + +Ton Voon, Eton.voon@altinity.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2006 by Altinity Limited + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.8.4 or, +at your option, any later version of Perl 5 you may have available. + + +=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 @@ + +use strict; +use Test::More tests => 42; +BEGIN { use_ok('Nagios::Plugin::Performance') }; + +use Nagios::Plugin::Base; +Nagios::Plugin::Base->exit_on_die(0); + +my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); +cmp_ok( $p[0]->label, 'eq', "/", "label okay"); +cmp_ok( $p[0]->value, '==', 382, "value okay"); +cmp_ok( $p[0]->uom, 'eq', "MB", "uom okay"); +cmp_ok( $p[0]->threshold->warning->end, "==", 15264, "warn okay"); +cmp_ok( $p[0]->threshold->critical->end, "==", 15269, "crit okay"); +ok( ! defined $p[0]->min, "min okay"); +ok( ! defined $p[0]->max, "max okay"); + +cmp_ok( $p[1]->label, 'eq', "/var", "label okay"); +cmp_ok( $p[1]->value, '==', 218, "value okay"); +cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay"); +cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); +cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); + +ok( ! defined Nagios::Plugin::Performance->parse_perfstring("rubbish"), "Errors correctly"); +ok( ! defined Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); + +@p = Nagios::Plugin::Performance->parse_perfstring( + "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); +cmp_ok( $p[0]->label, "eq", "time", "label okay"); +cmp_ok( $p[0]->value, "==", 0.001229, "value okay"); +cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); +cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay"); +cmp_ok( $p[0]->threshold->critical, "eq", "0", "crit okay"); + +@p = Nagios::Plugin::Performance->parse_perfstring( + "load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;"); +cmp_ok( $p[0]->label, "eq", "load1", "label okay"); +cmp_ok( $p[0]->value, "eq", "0", "value okay with 0 as string"); +cmp_ok( $p[0]->uom, "eq", "", "uom empty"); +cmp_ok( $p[0]->threshold->warning, "eq", "5", "warn okay"); +cmp_ok( $p[0]->threshold->critical, "eq", "9", "crit okay"); +cmp_ok( $p[1]->label, "eq", "load5", "label okay"); +cmp_ok( $p[2]->label, "eq", "load15", "label okay"); + +@p = Nagios::Plugin::Performance->parse_perfstring( "users=4;20;50;0" ); +cmp_ok( $p[0]->label, "eq", "users", "label okay"); +cmp_ok( $p[0]->value, "==", 4, "value okay"); +cmp_ok( $p[0]->uom, "eq", "", "uom empty"); +cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay"); +cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay"); + +@p = Nagios::Plugin::Performance->parse_perfstring( + "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" ); +cmp_ok( $p[0]->label, "eq", "time", "label okay"); +cmp_ok( $p[0]->value, "eq", "0.2153", "value okay"); +cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); +cmp_ok( $p[0]->threshold->warning, 'eq', "5", "warn okay"); +cmp_ok( $p[0]->threshold->critical, 'eq', "10", "crit okay"); +cmp_ok( $p[1]->label, "eq", "size", "label okay"); +cmp_ok( $p[1]->value, "==", 426, "value okay"); +cmp_ok( $p[1]->uom, "eq", "B", "uom okay"); + ok( ! defined $p[1]->threshold->warning, "warn okay"); + 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 @@ + +use strict; +use Test::More tests => 60; +BEGIN { use_ok('Nagios::Plugin::Range') }; + + +my $r = Nagios::Plugin::Range->parse_range_string("6"); +isa_ok( $r, "Nagios::Plugin::Range"); +ok( defined $r, "'6' is valid range"); +cmp_ok( $r->start, '==', 0, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 6, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "6", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string("-7:23"); +ok( defined $r, "'-7:23' is valid range"); +cmp_ok( $r->start, '==', -7, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 23, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string(":5.75"); +ok( defined $r, "':5.75' is valid range"); +cmp_ok( $r->start, '==', 0, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 5.75, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); + +$r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); +ok( defined $r, "'~:-95.99' is valid range"); +cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); +cmp_ok( $r->end, '==', -95.99, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); +ok( defined $r, "'123456789012345:' is valid range"); +cmp_ok( $r->start, '==', 123456789012345, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); +cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string("~:0"); +ok( defined $r, "'~:0' is valid range"); +cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); +cmp_ok( $r->end, '==', 0, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r->alert_on, '==', 0, "Will alert on outside of range"); +cmp_ok( $r, 'eq', "~:0", "Stringification back to original"); +ok( $r->check_range(0.5) == 1, "0.5 - alert"); +ok( $r->check_range(-10) == 0, "-10 - no alert"); +ok( $r->check_range(0) == 0, "0 - no alert"); + +$r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); +ok( defined $r, '"@0:657.8210567" is a valid range'); +cmp_ok( $r->start, '==', 0, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 657.8210567, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r->alert_on, '==', 1, "Will alert on inside of range"); +cmp_ok( $r, 'eq', '@657.8210567', "Stringification to simplified version"); +ok( $r->check_range(32.88) == 1, "32.88 - alert"); +ok( $r->check_range(-2) == 0, "-2 - no alert"); +ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); +ok( $r->check_range(0) == 1, "0 - alert"); + +$r = Nagios::Plugin::Range->parse_range_string('1:1'); +ok( defined $r, '"1:1" is a valid range'); +cmp_ok( $r->start, '==', 1, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 1, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version"); +ok( $r->check_range(0.5) == 1, "0.5 - alert"); +ok( $r->check_range(1) == 0, "1 - no alert"); +ok( $r->check_range(5.2) == 1, "5.2 - alert"); + +$r = Nagios::Plugin::Range->parse_range_string('2:1'); +ok( ! defined $r, '"2:1" is rejected'); + +# 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 @@ + +use strict; +use Test::More tests => 18; +BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; + +Nagios::Plugin::Base->exit_on_die(0); +Nagios::Plugin::Base->print_on_die(0); + +my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); +ok( defined $t, "Threshold ('', '80') set"); +ok( ! defined $t->warning, "Warning not set"); +cmp_ok( $t->critical->end, '==', 80, "Critical set correctly"); + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); +ok( defined $t, "Threshold ('5:33', '') set"); +cmp_ok( $t->warning->start, '==', 5, "Warning start set"); +cmp_ok( $t->warning->end, '==', 33, "Warning end set"); +ok( ! defined $t->critical, "Critical not set"); + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60"); +ok( defined $t, "Threshold ('30', '60') set"); +cmp_ok( $t->warning->end, '==', 30, "Warning end set"); +cmp_ok( $t->critical->end, '==',60, "Critical end set"); +cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok"); +cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning"); +cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical"); + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish"); +ok( defined $t, "Threshold object created although ..."); +ok( ! defined $t->warning, "Warning not set"); +ok( ! defined $t->critical, "Critical not set"); + 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 @@ + +use strict; +use Test::More tests => 5; +BEGIN { use_ok('Nagios::Plugin') }; + +use Nagios::Plugin::Base; +Nagios::Plugin::Base->exit_on_die(0); +Nagios::Plugin::Base->print_on_die(0); + +my $p = Nagios::Plugin->new; +isa_ok( $p, "Nagios::Plugin"); + +$p->shortname("PAGESIZE"); + +my $t = $p->set_thresholds( warning => "10:25", critical => "25:" ); + +$p->add_perfdata( + label => "size", + value => 1, + uom => "kB", + threshold => $t, + ); + +cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct"); + +my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" ); +cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay"); + +cmp_ok( $p->die( return_code => $t->get_status(30), message => "page size at http://... was 30kB" ), + "eq", 'PAGESIZE WARNING page size at http://... was 30kB | size=1kB;10:25;25:', "Output okay"); + + -- cgit v0.10-9-g596f