From b418181dfe80dd75169b6e8a619ac1932155dea2 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Mon, 20 Jan 2014 00:54:34 +0100 Subject: renamed module into Monitoring::Plugin since the complete monitoring team has been renamed, we also rename this module. Signed-off-by: Sven Nierlein --- t/Monitoring-Plugin-01.t | 71 +++++++ t/Monitoring-Plugin-02.t | 160 ++++++++++++++++ t/Monitoring-Plugin-03.t | 262 +++++++++++++++++++++++++ t/Monitoring-Plugin-04.t | 99 ++++++++++ t/Monitoring-Plugin-05.t | 15 ++ t/Monitoring-Plugin-Functions-01.t | 161 ++++++++++++++++ t/Monitoring-Plugin-Functions-02.t | 177 +++++++++++++++++ t/Monitoring-Plugin-Functions-03.t | 21 +++ t/Monitoring-Plugin-Functions-04.t | 21 +++ t/Monitoring-Plugin-Getopt-01.t | 149 +++++++++++++++ t/Monitoring-Plugin-Getopt-02.t | 63 +++++++ t/Monitoring-Plugin-Getopt-03.t | 107 +++++++++++ t/Monitoring-Plugin-Getopt-04.t | 97 ++++++++++ t/Monitoring-Plugin-Performance-02.t | 13 ++ t/Monitoring-Plugin-Performance.t | 357 +++++++++++++++++++++++++++++++++++ t/Monitoring-Plugin-Range.t | 243 ++++++++++++++++++++++++ t/Monitoring-Plugin-Threshold.t | 221 ++++++++++++++++++++++ t/Nagios-Plugin-01.t | 72 ------- t/Nagios-Plugin-02.t | 161 ---------------- t/Nagios-Plugin-03.t | 263 -------------------------- t/Nagios-Plugin-04.t | 99 ---------- t/Nagios-Plugin-05.t | 16 -- t/Nagios-Plugin-Functions-01.t | 161 ---------------- t/Nagios-Plugin-Functions-02.t | 178 ----------------- t/Nagios-Plugin-Functions-03.t | 21 --- t/Nagios-Plugin-Functions-04.t | 21 --- t/Nagios-Plugin-Getopt-01.t | 150 --------------- t/Nagios-Plugin-Getopt-02.t | 64 ------- t/Nagios-Plugin-Getopt-03.t | 108 ----------- t/Nagios-Plugin-Getopt-04.t | 98 ---------- t/Nagios-Plugin-Performance-02.t | 13 -- t/Nagios-Plugin-Performance.t | 357 ----------------------------------- t/Nagios-Plugin-Range.t | 243 ------------------------ t/Nagios-Plugin-Threshold.t | 221 ---------------------- t/check_stuff.pl | 45 ++--- t/check_stuff.t | 10 +- t/npg03/README | 16 +- 37 files changed, 2267 insertions(+), 2287 deletions(-) create mode 100644 t/Monitoring-Plugin-01.t create mode 100644 t/Monitoring-Plugin-02.t create mode 100644 t/Monitoring-Plugin-03.t create mode 100644 t/Monitoring-Plugin-04.t create mode 100644 t/Monitoring-Plugin-05.t create mode 100644 t/Monitoring-Plugin-Functions-01.t create mode 100644 t/Monitoring-Plugin-Functions-02.t create mode 100644 t/Monitoring-Plugin-Functions-03.t create mode 100644 t/Monitoring-Plugin-Functions-04.t create mode 100644 t/Monitoring-Plugin-Getopt-01.t create mode 100644 t/Monitoring-Plugin-Getopt-02.t create mode 100644 t/Monitoring-Plugin-Getopt-03.t create mode 100644 t/Monitoring-Plugin-Getopt-04.t create mode 100644 t/Monitoring-Plugin-Performance-02.t create mode 100644 t/Monitoring-Plugin-Performance.t create mode 100644 t/Monitoring-Plugin-Range.t create mode 100644 t/Monitoring-Plugin-Threshold.t delete mode 100644 t/Nagios-Plugin-01.t delete mode 100644 t/Nagios-Plugin-02.t delete mode 100644 t/Nagios-Plugin-03.t delete mode 100644 t/Nagios-Plugin-04.t delete mode 100644 t/Nagios-Plugin-05.t delete mode 100644 t/Nagios-Plugin-Functions-01.t delete mode 100644 t/Nagios-Plugin-Functions-02.t delete mode 100644 t/Nagios-Plugin-Functions-03.t delete mode 100644 t/Nagios-Plugin-Functions-04.t delete mode 100644 t/Nagios-Plugin-Getopt-01.t delete mode 100644 t/Nagios-Plugin-Getopt-02.t delete mode 100644 t/Nagios-Plugin-Getopt-03.t delete mode 100644 t/Nagios-Plugin-Getopt-04.t delete mode 100644 t/Nagios-Plugin-Performance-02.t delete mode 100644 t/Nagios-Plugin-Performance.t delete mode 100644 t/Nagios-Plugin-Range.t delete mode 100644 t/Nagios-Plugin-Threshold.t (limited to 't') diff --git a/t/Monitoring-Plugin-01.t b/t/Monitoring-Plugin-01.t new file mode 100644 index 0000000..f5882a5 --- /dev/null +++ b/t/Monitoring-Plugin-01.t @@ -0,0 +1,71 @@ +# Monitoring::Plugin original test cases + +use strict; +use Test::More tests => 15; + +BEGIN { use_ok('Monitoring::Plugin') }; + +use Monitoring::Plugin::Functions; +Monitoring::Plugin::Functions::_fake_exit(1); + +diag "\nusing Monitoring::Plugin revision ". $Monitoring::Plugin::VERSION . "\n" + if $ENV{TEST_VERBOSE}; + +my $p = Monitoring::Plugin->new(); +isa_ok( $p, "Monitoring::Plugin"); + +$p->shortname("PAGESIZE"); +is($p->shortname, "PAGESIZE", "shortname explicitly set correctly"); + +$p = Monitoring::Plugin->new(); +is($p->shortname, "MONITORING-PLUGIN-01", "shortname should default on new"); + +$p = Monitoring::Plugin->new( shortname => "SIZE", () ); +is($p->shortname, "SIZE", "shortname set correctly on new"); + +$p = Monitoring::Plugin->new( plugin => "check_stuff", () ); +is($p->shortname, "STUFF", "shortname uses plugin name as default"); + +$p = Monitoring::Plugin->new( shortname => "SIZE", plugin => "check_stuff", () ); +is($p->shortname, "SIZE", "shortname is not overriden by default"); + +diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; +my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); + +use Data::Dumper; +#diag "dumping p: ". Dumper $p; +#diag "dumping perfdata: ". Dumper $p->perfdata; + + +$p->add_perfdata( + label => "size", + value => 1, + uom => "kB", + threshold => $t, + ); + +cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); +#diag "dumping perfdata: ". Dumper ($p->perfdata); + +$p->add_perfdata( + label => "time", + value => "3.52", + threshold => $t, + ); + +is( $p->all_perfoutput, "size=1kB;10:25;~:25 time=3.52;10:25;~:25", "Perfdata correct when no uom specified"); + +my $expected = {qw( + -1 WARNING + 1 WARNING + 20 OK + 25 OK + 26 CRITICAL + 30 CRITICAL + )}; + +foreach (sort {$a<=>$b} keys %$expected) { + like $p->die( return_code => $t->get_status($_), message => "page size at http://... was ${_}kB" ), + qr/$expected->{$_}/, + "Output okay. $_ = $expected->{$_}" ; +} diff --git a/t/Monitoring-Plugin-02.t b/t/Monitoring-Plugin-02.t new file mode 100644 index 0000000..6cc834d --- /dev/null +++ b/t/Monitoring-Plugin-02.t @@ -0,0 +1,160 @@ +# Monitoring::Plugin test set 2, testing MP::Functions wrapping + +use strict; +use Test::More tests => 103; + +BEGIN { use_ok("Monitoring::Plugin") } +require Monitoring::Plugin::Functions; +Monitoring::Plugin::Functions::_fake_exit(1); + +# Hardcoded checks of constants +my %ERRORS = %Monitoring::Plugin::Functions::ERRORS; +is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); +is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); +is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); +is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); +is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); + +my $plugin = 'TEST_PLUGIN'; +my $np = Monitoring::Plugin->new( shortname => $plugin ); +is($np->shortname, $plugin, "shortname() is $plugin"); + +# Test plugin_exit( CONSTANT, $msg ), plugin_exit( $string, $msg ) +my $r; +my @ok = ( + [ OK, "OK", 'test the first', ], + [ WARNING, "WARNING", 'test the second', ], + [ CRITICAL, "CRITICAL", 'test the third', ], + [ UNKNOWN, "UNKNOWN", 'test the fourth', ], + [ DEPENDENT, "DEPENDENT", 'test the fifth', ], +); +for (@ok) { + # CONSTANT + $r = $np->plugin_exit($_->[0], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_exit(%s, $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_exit(%s, $msg) output matched "%s"', $_->[1], + $plugin . ' ' . $_->[1] . '.*' . $_->[2])); + + # $string + $r = $np->plugin_exit($_->[1], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_exit("%s", $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_exit("%s", $msg) output matched "%s"', $_->[1], + $plugin . ' ' . $_->[1] . '.*' . $_->[2])); + like($r, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_exit("%s", $msg) stringified matched "%s"', $_->[1], + $plugin . ' ' . $_->[1] . '.*' . $_->[2])); +} + +# plugin_exit code corner cases +my @ugly1 = ( + [ -1, 'testing code -1' ], + [ 7, 'testing code 7' ], + [ undef, 'testing code undef' ], + [ '', qq(testing code '') ], + [ 'string', qq(testing code 'string') ], +); +for (@ugly1) { + $r = $np->plugin_exit($_->[0], $_->[1]); + my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; + is($r->return_code, UNKNOWN, "plugin_exit($display, \$msg) returned ". UNKNOWN); + like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, + sprintf('plugin_exit(%s, $msg) output matched "%s"', + $display, 'UNKNOWN.*' . $_->[1])); +} + +# plugin_exit message corner cases +my @ugly2 = ( + [ '' ], + [ undef ], + [ UNKNOWN ], +); +for (@ugly2) { + $r = $np->plugin_exit(CRITICAL, $_->[0]); + my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; + my $display2 = defined $_->[0] ? $_->[0] : ''; + like($r->message, qr/CRITICAL\b.*\b$display2$/, + sprintf('plugin_exit(%s, $msg) output matched "%s"', + $display1, "CRITICAL.*$display2")); +} + +# Test plugin_die( $msg ) +my @msg = ( + [ 'die you dog' ], + [ '' ], + [ undef ], +); +for (@msg) { + $r = $np->plugin_die($_->[0]); + my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; + my $display2 = defined $_->[0] ? $_->[0] : ''; + is($r->return_code, UNKNOWN, + sprintf('plugin_die(%s) returned UNKNOWN', $display1)); + like($r->message, qr/UNKNOWN\b.*\b$display2$/, + sprintf('plugin_die(%s) output matched "%s"', $display1, + "UNKNOWN.*$display2")); +} + +# Test plugin_die( CONSTANT, $msg ), plugin_die( $msg, CONSTANT ), +# plugin_die( $string, $msg ), and plugin_die( $msg, $string ) +@ok = ( + [ OK, "OK", 'test the first', ], + [ WARNING, "WARNING", 'test the second', ], + [ CRITICAL, "CRITICAL", 'test the third', ], + [ UNKNOWN, "UNKNOWN", 'test the fourth', ], + [ DEPENDENT, "DEPENDENT", 'test the fifth', ], +); +for (@ok) { + # CONSTANT, $msg + $r = $np->plugin_die($_->[0], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_die(%s, $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die(%s, $msg) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $msg, CONSTANT + $r = $np->plugin_die($_->[2], $_->[0]); + is($r->return_code, $_->[0], + sprintf('plugin_die($msg, %s) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die($msg, %s) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $string, $msg + $r = $np->plugin_die($_->[1], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_die("%s", $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die("%s", $msg) output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die("%s", $msg) stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + + # $string, $msg + $r = $np->plugin_die($_->[2], $_->[1]); + is($r->return_code, $_->[0], + sprintf('plugin_die($msg, "%s") returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die($msg, "%s") output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die($msg, "%s") stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); +} + + +# shortname testing +SKIP: { + skip "requires File::Basename", 2 unless eval { require File::Basename }; + $np = Monitoring::Plugin->new( version => "1"); + $plugin = uc File::Basename::basename($0); + $plugin =~ s/\..*$//; + is($np->shortname, $plugin, "shortname() is '$plugin'"); + $r = $np->plugin_exit(OK, "foobar"); + like($r->message, qr/^$plugin OK/, "message begins with '$plugin OK'"); +} diff --git a/t/Monitoring-Plugin-03.t b/t/Monitoring-Plugin-03.t new file mode 100644 index 0000000..d419342 --- /dev/null +++ b/t/Monitoring-Plugin-03.t @@ -0,0 +1,262 @@ +# $np->check_messages tests + +use strict; +use Test::More tests => 61; + +BEGIN { + use_ok("Monitoring::Plugin"); + use_ok("Monitoring::Plugin::Functions", ":all"); +} +Monitoring::Plugin::Functions::_fake_exit(1); + +my $plugin = 'MP_CHECK_MESSAGES_03'; +my $np = Monitoring::Plugin->new( shortname => $plugin, () ); +is($np->shortname, $plugin, "shortname() is $plugin"); + +my ($code, $message); + +# ------------------------------------------------------------------------- +# Check codes +my @codes = ( + [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ], + [ [], [ qw(Warning) ], WARNING ], + [ [], [], OK ], +); +my $i = 0; +for (@codes) { + $i++; + $code = $np->check_messages( critical => $_->[0], warning => $_->[1] ); + is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}"); +} + +# ------------------------------------------------------------------------- +# Check messages +my %arrays = ( + critical => [ qw(A B C) ], + warning => [ qw(D E F) ], + ok => [ qw(G H I) ], +); +my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays; + +# critical, warning +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, +); +is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning) message is $message"); + +# critical, warning, ok +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + ok => $arrays{ok}, +); +is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning, ok) message is $message"); + +# critical, warning, $ok +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + ok => 'G H I', +); +is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning, \$ok) message is $message"); + +# warning +($code, $message) = $np->check_messages( + critical => [], warning => $arrays{warning}, +); +is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(warning) message is $message"); + +# warning, ok +($code, $message) = $np->check_messages( + critical => [], warning => $arrays{warning}, ok => $arrays{ok}, +); +is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(warning, ok) message is $message"); + +# ok +($code, $message) = $np->check_messages( + critical => [], warning => [], ok => $arrays{ok}, +); +is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{ok}, "(ok) message is $message"); + +# $ok +($code, $message) = $np->check_messages( + critical => [], warning => [], ok => 'G H I', +); +is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{ok}, "(\$ok) message is $message"); + +# ------------------------------------------------------------------------- +# explicit join +my $join = '+'; +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + join => $join, +); +is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message"); +$join = ''; +($code, $message) = $np->check_messages( + critical => [], warning => $arrays{warning}, + join => $join, +); +is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message"); +$join = undef; +($code, $message) = $np->check_messages( + critical => [], warning => [], ok => $arrays{ok}, + join => $join, +); +is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message"); + +# ------------------------------------------------------------------------- +# join_all messages +my $join_all = ' :: '; +my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } + qw(critical warning ok)); +my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } + qw(critical warning)); +my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) } + qw(warning ok)); + +# critical, warning, ok +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok}, + join_all => $join_all, +); +is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message"); + +# critical, warning, $ok +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I', + join_all => $join_all, +); +is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); + +# critical, warning +($code, $message) = $np->check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + join_all => $join_all, +); +is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message"); + +# warning, ok +($code, $message) = $np->check_messages( + critical => [], warning => $arrays{warning}, ok => $arrays{ok}, + join_all => $join_all, +); +is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message"); + +# warning, $ok +($code, $message) = $np->check_messages( + critical => [], warning => $arrays{warning}, ok => 'G H I', + join_all => $join_all, +); +is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); + +# warning +($code, $message) = $np->check_messages( + critical => [], warning => $arrays{warning}, + join_all => $join_all, +); +is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); +is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message"); + +# ------------------------------------------------------------------------- +# add_messages + +# Constant codes +$np = Monitoring::Plugin->new(); +$np->add_message( CRITICAL, "A B C" ); +$np->add_message( WARNING, "D E F" ); +($code, $message) = $np->check_messages(); +is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( CRITICAL, "A B C" ); +($code, $message) = $np->check_messages(); +is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(CRITICAL) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( WARNING, "D E F" ); +($code, $message) = $np->check_messages(); +is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(WARNING) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( WARNING, "D E F" ); +$np->add_message( OK, "G H I" ); +($code, $message) = $np->check_messages(); +is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(WARNING, OK) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( OK, "G H I" ); +($code, $message) = $np->check_messages(); +is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); +is($message, $messages{ok}, "(OK) message is $message"); + + +# String codes +$np = Monitoring::Plugin->new(); +$np->add_message( critical => "A B C" ); +$np->add_message( warning => "D E F" ); +($code, $message) = $np->check_messages(); +is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( critical => "A B C" ); +($code, $message) = $np->check_messages(); +is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( warning => "D E F" ); +($code, $message) = $np->check_messages(); +is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(warning) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( warning => "D E F" ); +$np->add_message( ok => "G H I" ); +($code, $message) = $np->check_messages(); +is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(warning, ok) message is $message"); + +$np = Monitoring::Plugin->new(); +$np->add_message( ok => "G H I" ); +($code, $message) = $np->check_messages(); +is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{ok}, "(ok) message is $message"); + + +# No add_message +$np = Monitoring::Plugin->new(); +($code, $message) = $np->check_messages(); +is($code, OK, "() code is $STATUS_TEXT{$code}"); +is($message, '', "() message is ''"); + + +# ------------------------------------------------------------------------- +# Error conditions + +# add_message errors +$np = Monitoring::Plugin->new(); +ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, + 'add_message dies on invalid code'); +ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, + 'add_message dies on invalid code'); +# UNKNOWN and DEPENDENT error codes +ok(! defined eval { $np->add_message( unknown => 'hi mum' ) }, + 'add_message dies on UNKNOWN code'); +ok(! defined eval { $np->add_message( dependent => 'hi mum' ) }, + 'add_message dies on DEPENDENT code'); diff --git a/t/Monitoring-Plugin-04.t b/t/Monitoring-Plugin-04.t new file mode 100644 index 0000000..41027f7 --- /dev/null +++ b/t/Monitoring-Plugin-04.t @@ -0,0 +1,99 @@ + +# tests for toplevel access to Threshold and GetOpts stuff + +use strict; +#use Test::More 'no_plan'; +use Test::More tests=>30; + +BEGIN { use_ok('Monitoring::Plugin') }; +use Monitoring::Plugin::Functions; +Monitoring::Plugin::Functions::_fake_exit(1); + + +eval { Monitoring::Plugin->new(); }; +ok(! $@, "constructor DOESN'T die without usage"); + +my $p = Monitoring::Plugin->new(); +eval { $p->add_arg('warning', 'warning') }; +ok($@, "add_arg() dies if you haven't instantiated with usage"); +eval { $p->getopts }; +ok($@, "getopts() dies if you haven't instantiated with usage"); + +$p = Monitoring::Plugin->new( usage => "dummy usage statement" ); + +# option accessors work +can_ok $p, 'opts'; +isa_ok $p->opts, 'Monitoring::Plugin::Getopt', "Getopt object is defined"; + +$p->add_arg('warning|w=s', "warning"); +$p->add_arg('critical|c=s', "critical"); + +@ARGV = qw(-w 5 -c 10); +$p->getopts; +is $p->opts->warning, "5", "warning opt is accessible"; +is $p->opts->critical, "10", "critical opt is accessible"; + + +can_ok $p, 'perfdata'; +#isa_ok $p->perfdata, 'Monitoring::Plugin::Performance', "perfdata object is defined"; + + +can_ok $p, 'threshold'; +#isa_ok $p->threshold, 'Monitoring::Plugin::Threshold', "threshold object is defined"; + + +eval { $p->check_threshold() }; +ok($@, "check_threshold dies if called with no args"); + + +# thresholds set implicitly +is $p->check_threshold(2), OK, "check_threshold OK when called implicitly"; +is $p->check_threshold(6), WARNING, "check_threshold WARNING"; +is $p->check_threshold(11), CRITICAL, "check_threshold CRITICAL"; +is $p->check_threshold(check=>11), CRITICAL, "check_threshold CRITICAL with hash param"; + +# Check that arrays allowed +is $p->check_threshold([2,1]), OK, "check_threshold OK when called implicitly"; +is $p->check_threshold([6,2]), WARNING, "check_threshold WARNING"; +is $p->check_threshold([1,2,6,11]), CRITICAL, "check_threshold CRITICAL"; +is $p->check_threshold(check=>[1,2,6,11]), CRITICAL, "check_threshold CRITICAL with hash param"; + +# thresholds set explicitly +is $p->check_threshold( + check => 2, + warning => 50, + critical => 100 +), OK, "check_threshold explicit OK"; + +is $p->check_threshold( + check => 66, + warning => 50, + critical => 100 +), WARNING, "check_threshold explicit WARNING"; + + +is $p->check_threshold( + check => -1, + warning => 5, + critical => '0:5', +), CRITICAL, "check_threshold explicit CRITICAL"; + + + +# what happens if you forget to define warning or critical thresholds? +$p = undef; +$p = Monitoring::Plugin->new(); + +is $p->check_threshold(2), UNKNOWN, "everything is now UNKNOWN"; +is $p->check_threshold(-200), UNKNOWN, "everything is now UNKNOWN"; +is $p->check_threshold(134098.3124), UNKNOWN, "everything is now UNKNOWN"; +is $p->check_threshold("foo bar baz"), UNKNOWN, "everything is now UNKNOWN"; + + +# how about when you define just one? + +$p->set_thresholds(warning => "10:25"); +is $p->check_threshold(2), WARNING, "check_threshold works (WARNING) after explicit set_thresholds"; +is $p->check_threshold(-200), WARNING, "and again"; +is $p->check_threshold(25.5), WARNING, "and again"; +is $p->check_threshold(11), OK, "now OK"; diff --git a/t/Monitoring-Plugin-05.t b/t/Monitoring-Plugin-05.t new file mode 100644 index 0000000..d17464f --- /dev/null +++ b/t/Monitoring-Plugin-05.t @@ -0,0 +1,15 @@ +# Check for exported vars +# Can't include Monitoring::Plugin::Functions because it also exports %STATUS_TEXT + +use strict; +use Test::More tests=>4; + +BEGIN { use_ok('Monitoring::Plugin') }; + +eval ' $_ = $STATUS_TEXT{0} '; +like( $@, '/Global symbol "%STATUS_TEXT" requires explicit package name/' ); + +use_ok("Monitoring::Plugin", qw(%STATUS_TEXT)); + +eval ' $_ = $STATUS_TEXT{0} '; +is( $@, '' ); diff --git a/t/Monitoring-Plugin-Functions-01.t b/t/Monitoring-Plugin-Functions-01.t new file mode 100644 index 0000000..084ad28 --- /dev/null +++ b/t/Monitoring-Plugin-Functions-01.t @@ -0,0 +1,161 @@ + +use strict; +use Test::More tests => 113; + +BEGIN { use_ok("Monitoring::Plugin::Functions", ":all"); } +Monitoring::Plugin::Functions::_fake_exit(1); + +my $this_version=$Monitoring::Plugin::Functions::VERSION; +foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { + my $mod = "Monitoring::Plugin$m"; + use_ok($mod); + # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing + my $v = "$mod"."::VERSION"; + my $a = eval "\$$v"; + is($a, $this_version, "Version number for $mod the same as Functions: $this_version"); +} + +# check get_shortname +is(get_shortname, "MONITORING-PLUGIN-FUNCTIONS-01", "get_shortname ok"); + +# Hardcoded checks of constants +ok(%ERRORS, '%ERRORS defined'); +is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); +is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); +is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); +is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); +is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); + +# Test plugin_exit( CONSTANT, $msg ), plugin_exit( $string, $msg ) +my $r; +my @ok = ( + [ OK, "OK", 'test the first', ], + [ WARNING, "WARNING", 'test the second', ], + [ CRITICAL, "CRITICAL", 'test the third', ], + [ UNKNOWN, "UNKNOWN", 'test the fourth', ], + [ DEPENDENT, "DEPENDENT", 'test the fifth', ], +); +for (@ok) { + # CONSTANT + $r = plugin_exit($_->[0], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_exit(%s, $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_exit(%s, $msg) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $string + $r = plugin_exit($_->[1], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_exit("%s", $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_exit("%s", $msg) output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_exit("%s", $msg) stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); +} + +# plugin_exit code corner cases +my @ugly1 = ( + [ -1, 'testing code -1' ], + [ 7, 'testing code 7' ], + [ undef, 'testing code undef' ], + [ '', qq(testing code '') ], + [ 'string', qq(testing code 'string') ], +); +for (@ugly1) { + $r = plugin_exit($_->[0], $_->[1]); + my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; + is($r->return_code, UNKNOWN, "plugin_exit($display, \$msg) returned ". UNKNOWN); + like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, + sprintf('plugin_exit(%s, $msg) output matched "%s"', + $display, 'UNKNOWN.*' . $_->[1])); +} + +# plugin_exit message corner cases +my @ugly2 = ( + [ '' ], + [ undef ], + [ UNKNOWN ], +); +for (@ugly2) { + $r = plugin_exit(CRITICAL, $_->[0]); + my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; + my $display2 = defined $_->[0] ? $_->[0] : ''; + like($r->message, qr/CRITICAL\b.*\b$display2$/, + sprintf('plugin_exit(%s, $msg) output matched "%s"', + $display1, "CRITICAL.*$display2")); +} + +# Test plugin_die( $msg ) +my @msg = ( + [ 'die you dog' ], + [ '' ], + [ undef ], +); +for (@msg) { + $r = plugin_die($_->[0]); + my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; + my $display2 = defined $_->[0] ? $_->[0] : ''; + is($r->return_code, UNKNOWN, + sprintf('plugin_die(%s) returned UNKNOWN', $display1)); + like($r->message, qr/UNKNOWN\b.*\b$display2$/, + sprintf('plugin_die(%s) output matched "%s"', $display1, + "UNKNOWN.*$display2")); +} + +# Test plugin_die( CONSTANT, $msg ), plugin_die( $msg, CONSTANT ), +# plugin_die( $string, $msg ), and plugin_die( $msg, $string ) +@ok = ( + [ OK, "OK", 'test the first', ], + [ WARNING, "WARNING", 'test the second', ], + [ CRITICAL, "CRITICAL", 'test the third', ], + [ UNKNOWN, "UNKNOWN", 'test the fourth', ], + [ DEPENDENT, "DEPENDENT", 'test the fifth', ], +); +for (@ok) { + # CONSTANT, $msg + $r = plugin_die($_->[0], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_die(%s, $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die(%s, $msg) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $msg, CONSTANT + $r = plugin_die($_->[2], $_->[0]); + is($r->return_code, $_->[0], + sprintf('plugin_die($msg, %s) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die($msg, %s) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $string, $msg + $r = plugin_die($_->[1], $_->[2]); + is($r->return_code, $_->[0], + sprintf('plugin_die("%s", $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die("%s", $msg) output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die("%s", $msg) stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + + # $string, $msg + $r = plugin_die($_->[2], $_->[1]); + is($r->return_code, $_->[0], + sprintf('plugin_die($msg, "%s") returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die($msg, "%s") output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('plugin_die($msg, "%s") stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); +} + +# Check that _use_die set to 1 will catch exceptions correctly +Monitoring::Plugin::Functions::_fake_exit(0); +Monitoring::Plugin::Functions::_use_die(1); +eval { plugin_die("Using die") }; +is( $@, "MONITORING-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception"); diff --git a/t/Monitoring-Plugin-Functions-02.t b/t/Monitoring-Plugin-Functions-02.t new file mode 100644 index 0000000..3796210 --- /dev/null +++ b/t/Monitoring-Plugin-Functions-02.t @@ -0,0 +1,177 @@ +# check_messages tests + +use strict; +use Test::More tests => 37; + +BEGIN { use_ok("Monitoring::Plugin::Functions", ":all") } + +my ($code, $message); + +# ------------------------------------------------------------------------- +# Check codes +my @codes = ( + [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ], + [ [], [ qw(Warning) ], WARNING ], + [ [], [], OK ], +); +my $i = 0; +for (@codes) { + $i++; + $code = check_messages( critical => $_->[0], warning => $_->[1] ); + is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}"); +} + +# ------------------------------------------------------------------------- +# Check messages +my %arrays = ( + critical => [ qw(A B C) ], + warning => [ qw(D E F) ], + ok => [ qw(G H I) ], +); +my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays; + +# critical, warning +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, +); +is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning) message is $message"); + +# critical, warning, ok +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + ok => $arrays{ok}, +); +is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning, ok) message is $message"); + +# critical, warning, $ok +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + ok => 'G H I', +); +is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{critical}, "(critical, warning, \$ok) message is $message"); + +# warning +($code, $message) = check_messages( + critical => [], warning => $arrays{warning}, +); +is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(warning) message is $message"); + +# warning, ok +($code, $message) = check_messages( + critical => [], warning => $arrays{warning}, ok => $arrays{ok}, +); +is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{warning}, "(warning, ok) message is $message"); + +# ok +($code, $message) = check_messages( + critical => [], warning => [], ok => $arrays{ok}, +); +is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{ok}, "(ok) message is $message"); + +# $ok +($code, $message) = check_messages( + critical => [], warning => [], ok => 'G H I', +); +is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}"); +is($message, $messages{ok}, "(\$ok) message is $message"); + +# ------------------------------------------------------------------------- +# explicit join +my $join = '+'; +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + join => $join, +); +is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message"); +$join = ''; +($code, $message) = check_messages( + critical => [], warning => $arrays{warning}, + join => $join, +); +is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message"); +$join = undef; +($code, $message) = check_messages( + critical => [], warning => [], ok => $arrays{ok}, + join => $join, +); +is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message"); + +# ------------------------------------------------------------------------- +# join_all messages +my $join_all = ' :: '; +my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } + qw(critical warning ok)); +my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } + qw(critical warning)); +my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) } + qw(warning ok)); + +# critical, warning, ok +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok}, + join_all => $join_all, +); +is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message"); + +# critical, warning, $ok +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I', + join_all => $join_all, +); +is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); + +# critical, warning +($code, $message) = check_messages( + critical => $arrays{critical}, warning => $arrays{warning}, + join_all => $join_all, +); +is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message"); + +# warning, ok +($code, $message) = check_messages( + critical => [], warning => $arrays{warning}, ok => $arrays{ok}, + join_all => $join_all, +); +is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message"); + +# warning, $ok +($code, $message) = check_messages( + critical => [], warning => $arrays{warning}, ok => 'G H I', + join_all => $join_all, +); +is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}"); +is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); + +# warning +($code, $message) = check_messages( + critical => [], warning => $arrays{warning}, + join_all => $join_all, +); +is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); +is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message"); + +# ------------------------------------------------------------------------- +# Error cases + +# Test failures without required fields +ok(! defined eval { ($code, $message) = check_messages() }, + "check_messages dies without message args"); + +ok(! defined eval { ($code, $message) = check_messages(warning => $arrays{warning}) }, + "check_messages dies without 'critical' message"); + +ok(! defined eval { ($code, $message) = check_messages(critical => $arrays{critical}) }, + "check_messages dies without 'warning' message"); + +ok(defined eval { ($code, $message) = check_messages(critical => $arrays{critical}, warning => $arrays{warning}) }, + "check_messages ok with 'critical' and 'warning' messages"); diff --git a/t/Monitoring-Plugin-Functions-03.t b/t/Monitoring-Plugin-Functions-03.t new file mode 100644 index 0000000..9f0a62c --- /dev/null +++ b/t/Monitoring-Plugin-Functions-03.t @@ -0,0 +1,21 @@ +# max_state tests + +use strict; +use Test::More tests => 8; + +BEGIN { use_ok("Monitoring::Plugin::Functions", ":all") } + +my $new_state = max_state( OK, WARNING ); + +is( $new_state, WARNING, "Moved up to WARNING" ); +is( max_state( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); + +$new_state = max_state( $new_state, CRITICAL ); +is( $new_state, CRITICAL, "Now at CRITICAL" ); +is( max_state( OK, OK ), OK, "This is OK" ); + +is( max_state( OK, UNKNOWN ), OK, "This is still OK, not UNKNOWN" ); + +is( max_state( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); + +is( max_state(), UNKNOWN, "Return UNKNOWN if no parameters" ); diff --git a/t/Monitoring-Plugin-Functions-04.t b/t/Monitoring-Plugin-Functions-04.t new file mode 100644 index 0000000..0a7c4e4 --- /dev/null +++ b/t/Monitoring-Plugin-Functions-04.t @@ -0,0 +1,21 @@ +# max_state_alt tests + +use strict; +use Test::More tests => 8; + +BEGIN { use_ok("Monitoring::Plugin::Functions", ":all") } + +my $new_state = max_state_alt( OK, WARNING ); + +is( $new_state, WARNING, "Moved up to WARNING" ); +is( max_state_alt( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); + +$new_state = max_state_alt( $new_state, CRITICAL ); +is( $new_state, CRITICAL, "Now at CRITICAL" ); +is( max_state_alt( OK, OK ), OK, "This is OK" ); + +is( max_state_alt( OK, UNKNOWN ), UNKNOWN, "This is UNKNOWN" ); + +is( max_state_alt( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); + +is( max_state_alt(), UNKNOWN, "Return UNKNOWN if no parameters" ); diff --git a/t/Monitoring-Plugin-Getopt-01.t b/t/Monitoring-Plugin-Getopt-01.t new file mode 100644 index 0000000..36f1f55 --- /dev/null +++ b/t/Monitoring-Plugin-Getopt-01.t @@ -0,0 +1,149 @@ +# Monitoring::Plugin::Getopt basic tests + +use strict; + +use Test::More tests => 76; +BEGIN { use_ok('Monitoring::Plugin::Getopt') }; + +# Needed to get evals to work in testing +Monitoring::Plugin::Functions::_use_die(1); + +my %PARAM = ( + version => '0.01', + url => 'http://www.openfusion.com.au/labs/nagios/', + blurb => 'This plugin tests various stuff.', + usage => "Usage: %s -H -w + -c ", + plugin => 'test_plugin', +); + +sub setup +{ + # Instantiate object + my $ng = Monitoring::Plugin::Getopt->new(%PARAM); + ok($ng, 'constructor ok'); + + # Add argument - short form - arg spec, help text, default, required? + $ng->arg('warning|w=s' => + qq(-w, --warning=INTEGER\n Exit with WARNING status if less than INTEGER foobars are free), + 5); + + # Add argument - named version + $ng->arg( + spec => 'critical|c=i', + help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), + required => 1, + ); + + return $ng; +} + +my $ng; + +# Simple usage (short and long args) +@ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose); +$ng = setup; +$ng->getopts; +is($ng->warning, 3, 'warning set to 3'); +is($ng->critical, 10, 'critical set to 10'); +is($ng->timeout, 12, 'timeout set to 12'); + +# Check multiple verbose flags +@ARGV = qw(-w 3 --critical 10 -v -v -v); +$ng = setup; +$ng->getopts; +is ($ng->verbose, 3, "Verbose set to level 3"); + +@ARGV = qw(-w 3 --critical 10 --verbose --verbose --verbose); +$ng = setup; +$ng->getopts; +is ($ng->verbose, 3, "Verbose set to level 3 (longhand)"); + +# Missing args +@ARGV = qw(); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); +like($@, qr/Usage:/, 'usage message'); +like($@, qr/Missing arg/, 'missing arguments'); +is($ng->verbose, 0, 'verbose set to 0'); +# Missing critical +@ARGV = qw(-w0 -v); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); +like($@, qr/Usage:/, 'usage message'); +like($@, qr/Missing argument: critical/, 'missing argument: critical'); +unlike($@, qr/Missing argument: warning/, 'no missing argument: warning'); +is($ng->warning, 0, 'warning set to 0'); +is($ng->critical, undef, 'critical undef'); +is($ng->timeout, 15, 'timeout set to default'); +is($ng->verbose, 1, 'verbose set to true'); +# Missing warning +@ARGV = qw(--critical=27 --timeout 17 --verbose); +$ng = setup; +$ng->getopts; +is($ng->warning, 5, 'warning 5 (default)'); +is($ng->critical, 27, 'critical set to 27'); +is($ng->timeout, 17, 'timeout set to 17'); +is($ng->verbose, 1, 'verbose set to true'); + +# -? --usage +@ARGV = ( '-?' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on usage'); +like($@, qr/Usage:/, 'usage message'); +unlike($@, qr/Missing arg/, 'no missing arguments'); +@ARGV = ( '--usage' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on usage'); +like($@, qr/Usage:/, 'usage message'); +unlike($@, qr/Missing arg/, 'no missing arguments'); + +# -V --version +@ARGV = ( '-V' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on version'); +like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); +like($@, qr/$PARAM{version}/, 'version info includes version'); +like($@, qr/$PARAM{url}/, 'version info includes url'); +unlike($@, qr/Usage:/, 'no usage message'); +unlike($@, qr/Missing arg/, 'no missing arguments'); + +@ARGV = ( '--version' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on version'); +like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); +like($@, qr/$PARAM{version}/, 'version info includes version'); +like($@, qr/$PARAM{url}/, 'version info includes url'); +unlike($@, qr/Usage:/, 'no usage message'); +unlike($@, qr/Missing arg/, 'no missing arguments'); + +# -h --help +@ARGV = ( '-h' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on help'); +like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); +like($@, qr/$PARAM{version}/, 'help includes version'); +like($@, qr/$PARAM{url}/, 'help includes url'); +like($@, qr/General Public Licence/, 'help includes licence'); +like($@, qr/$PARAM{blurb}/, 'help includes blurb'); +like($@, qr/Usage:/, 'help includes usage message'); +like($@, qr/--version/, 'help includes default options 1'); +like($@, qr/--verbose/, 'help includes default options 2'); +like($@, qr/--warning/, 'help includes custom option 1'); +like($@, qr/--critical/, 'help includes custom option 2'); +unlike($@, qr/Missing arg/, 'no missing arguments'); + +@ARGV = ( '--help' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on help'); +like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); +like($@, qr/$PARAM{version}/, 'help includes version'); +like($@, qr/$PARAM{url}/, 'help includes url'); +like($@, qr/General Public Licence/, 'help includes licence'); +like($@, qr/$PARAM{blurb}/, 'help includes blurb'); +like($@, qr/Usage:/, 'help includes usage message'); +like($@, qr/--version/, 'help includes default options 1'); +like($@, qr/--verbose/, 'help includes default options 2'); +like($@, qr/--warning/, 'help includes custom option 1'); +like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); +unlike($@, qr/Missing arg/, 'no missing arguments'); diff --git a/t/Monitoring-Plugin-Getopt-02.t b/t/Monitoring-Plugin-Getopt-02.t new file mode 100644 index 0000000..c6c563e --- /dev/null +++ b/t/Monitoring-Plugin-Getopt-02.t @@ -0,0 +1,63 @@ +# Monitoring::Plugin::Getopt timeout tests + +use strict; + +use Test::More tests => 14; +BEGIN { use_ok('Monitoring::Plugin::Getopt') }; + +# Needed to get evals to work in testing +Monitoring::Plugin::Functions::_use_die(1); + +my %PARAM = ( + version => '0.01', + url => 'http://www.openfusion.com.au/labs/nagios/', + blurb => 'This plugin tests various stuff.', + usage => "Usage: %s -H -w + -c ", + plugin => 'test_plugin', + timeout => 18, +); + +sub setup +{ + # Instantiate object + my $ng = Monitoring::Plugin::Getopt->new(%PARAM); + ok($ng, 'constructor ok'); + return $ng; +} + +my $ng; + +# No args +@ARGV = qw(); +$ng = setup(); +$ng->getopts; +is($ng->timeout, 18, 'default timeout set to 18'); + +# Check help message +@ARGV = ( '-h' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on help'); +like($@, qr/times out.*default: 18\b/i, 'help timeout changed to 18'); + +# Explicit timeout +@ARGV = qw(--timeout=25 --verbose); +$ng = setup(); +$ng->getopts; +is($ng->timeout, 25, 'timeout changed to 25'); + +# Explicit timeout +@ARGV = qw(-t10 --verbose); +$ng = setup(); +$ng->getopts; +is($ng->timeout, 10, 'timeout changed to 10'); + +# Short timeout, test default timeout handler +@ARGV = qw(-t2 --verbose); +$ng = setup(); +$ng->getopts; +is($ng->timeout, 2, 'timeout changed to 2'); +alarm($ng->timeout); +# Loop +ok(! defined eval { 1 while 1 }, 'loop timed out'); +like($@, qr/UNKNOWN\b.*\btimed out/, 'default timeout handler ok'); diff --git a/t/Monitoring-Plugin-Getopt-03.t b/t/Monitoring-Plugin-Getopt-03.t new file mode 100644 index 0000000..6490145 --- /dev/null +++ b/t/Monitoring-Plugin-Getopt-03.t @@ -0,0 +1,107 @@ +# Monitoring::Plugin::Getopt --extra-opts tests + +use strict; +use File::Spec; +use File::Basename; +use IO::File; + +use Test::More qw(no_plan); +BEGIN { use_ok('Monitoring::Plugin::Getopt') }; + +# Needed to get evals to work in testing +Monitoring::Plugin::Functions::_use_die(1); + +my $tdir = 'npg03'; +if (! -d $tdir) { + my $ttdir = File::Spec->catdir('t', $tdir); + die "missing '$tdir' directory\n" unless -d $ttdir; + $tdir = $ttdir; +} + +# Load expected files +my %EXPECTED = (); +for my $efile (glob File::Spec->catfile($tdir, 'expected', '*')) { + my $fh = IO::File->new($efile, 'r') or die "Cannot open input file '$efile': $!"; + if (my $cmd = $fh->getline()) { # First line only! + chomp $cmd; + $cmd =~ s/^\s+//; + $cmd =~ s/\s+$//; + $EXPECTED{ basename($efile) } = $cmd; + } +} + +# Override MONITORING_CONFIG_PATH to use our test plugins.ini file +$ENV{MONITORING_CONFIG_PATH} = "/random/bogus/path:$tdir"; + +my %PARAM = ( + version => '0.01', + blurb => 'This plugin tests various stuff.', + usage => "Usage: %s -H -w + -c ", +); + +sub ng_setup +{ + my $arg = shift; + + # Instantiate object + my $ng = Monitoring::Plugin::Getopt->new(%PARAM); + + if (ref $arg eq 'ARRAY' && @$arg) { + $ng->arg(%$_) foreach @$arg; + } + + return $ng; +} + +# Setup our Monitoring::Plugin::Getopt object +my $ng; +my $arg = [ + { spec => 'S', help => '-S' }, + { spec => 'H=s', help => '-H' }, + { spec => 'p=s@', help => '-p' }, + { spec => 'path=s@', help => '--path' }, + { spec => 'username|u=s', help => '--username' }, + { spec => 'password=s', help => '--password' }, + { spec => 'critical=s', help => '--critical' }, + { spec => 'warning=s', help => '--warning' }, + { spec => 'expect=s', help => '--expect' }, + { spec => 'units=s', help => '--units' }, +]; + +#my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); +#my %SKIP = map { $_ => 1 } qw(06_singlechar2); +my %SKIP = (); + +# Process all test cases in $tdir/input +my $glob = $ARGV[0] || '*'; +for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) { + $ng = ng_setup($arg); + + my $fh = IO::File->new($infile, 'r') or die "Cannot open input file '$infile': $!"; + $infile = basename($infile); + + if (my $cmd = $fh->getline()) { # First line only! + $cmd =~ s/^\s+//; + my ($plugin, @args) = split /\s+/, $cmd; + + # Fake out the plugin name + $ng->{_attr}->{plugin} = $plugin; + + # Parse the options + SKIP: { + skip "Skipping ..." if $SKIP{$infile}; + + @ARGV = @args; + eval { $ng->getopts }; + if ($@) { + chomp $@; + ok($infile =~ m/_(dies?|catch)$/, "$infile ($@)"); + is($@, $EXPECTED{$infile}, $infile) if ($infile =~ m/_catch$/); + } + else { + is($plugin . ' ' . $ng->_cmdline, $EXPECTED{$infile}, $infile); + } + } + } +} diff --git a/t/Monitoring-Plugin-Getopt-04.t b/t/Monitoring-Plugin-Getopt-04.t new file mode 100644 index 0000000..b6345d0 --- /dev/null +++ b/t/Monitoring-Plugin-Getopt-04.t @@ -0,0 +1,97 @@ +# Monitoring::Plugin::Getopt spec-to-help generation tests + +use strict; + +use Test::More tests => 11; +BEGIN { use_ok('Monitoring::Plugin::Getopt') }; + +# Needed to get evals to work in testing +Monitoring::Plugin::Functions::_use_die(1); + +my %PARAM = ( + version => '0.01', + usage => "Don't use this plugin!", +); + +sub setup +{ + # Instantiate object + my $ng = Monitoring::Plugin::Getopt->new(%PARAM); + ok($ng, 'constructor ok'); + + # Positional args, no short arguments, INTEGER + $ng->arg('warning=i' => + qq(Exit with WARNING status if less than INTEGER foobars are free), + 5); + + # Named args, long + short arguments, INTEGER + $ng->arg( + spec => 'critical|c=i', + help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), + required => 1, + ); + + # Named args, multiple short arguments, STRING, default expansion + $ng->arg( + spec => 'x|y|z=s', + help => qq(Foobar. Default: %s), + default => "XYZ", + ); + + # Named args, multiple mixed, no label + $ng->arg( + spec => 'long|longer|longest|l', + help => qq(Long format), + ); + + # Named args, long + short, explicit label + $ng->arg( + spec => 'hostname|H=s', + label => 'ADDRESS', + help => qq(Hostname), + ); + + # Positional args, long only, explicit label + $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR'); + + # Multiline help test, named args + $ng->arg( + spec => 'disk=s', + label => [ qw(BYTES PERCENT%), undef ], + help => [ + qq(Disk limit in BYTES), + qq(Disk limit in PERCENT), + qq(Disk limit in FOOBARS (Default: %s)), + ], + default => 1024, + ); + + # Multiline help test, positional args + $ng->arg( + 'limit=s', + [ + qq(Limit in BYTES), + qq(Limit in PERCENT), + ], + undef, + undef, + [ undef, 'PERCENT%' ], + ); + + return $ng; +} + +my $ng; + +@ARGV = ( '--help' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on help'); +like($@, qr/\n --warning=INTEGER/, 'warning ok'); +like($@, qr/\n -c, --critical=INTEGER/, 'critical ok'); +like($@, qr/\n -x, -y, -z=STRING\n Foobar. Default: XYZ\n/, 'x|y|z ok'); +like($@, qr/\n -l, --long, --longer, --longest\n Long format\n/, 'long ok'); +like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); +like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); +like($@, qr/\n --disk=BYTES\n Disk limit in BYTES\n --disk=PERCENT%\n Disk limit in PERCENT\n --disk=STRING\n Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok'); +like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); +#print $@; diff --git a/t/Monitoring-Plugin-Performance-02.t b/t/Monitoring-Plugin-Performance-02.t new file mode 100644 index 0000000..c024685 --- /dev/null +++ b/t/Monitoring-Plugin-Performance-02.t @@ -0,0 +1,13 @@ + +use strict; +use Test::More tests => 3; +use_ok("Monitoring::Plugin::Performance", use_die => 1); + +eval { Monitoring::Plugin::Functions::plugin_die("Testing") }; +is( $@, "MONITORING-PLUGIN-PERFORMANCE-02 UNKNOWN - Testing\n", "use_die correctly set on import"); + + +use_ok("Monitoring::Plugin::Performance"); +eval { Monitoring::Plugin::Functions::plugin_die("Test OK exit", 0) }; + +fail("Should not get here if code works correctly because prior plugin_die should have exited"); diff --git a/t/Monitoring-Plugin-Performance.t b/t/Monitoring-Plugin-Performance.t new file mode 100644 index 0000000..bedb2e2 --- /dev/null +++ b/t/Monitoring-Plugin-Performance.t @@ -0,0 +1,357 @@ + +use warnings; +use strict; +use Test::More; +use Monitoring::Plugin::Functions; +Monitoring::Plugin::Functions::_fake_exit(1); + + +my (@p, $p); +my @test = ( + { + perfoutput => "/=382MB;15264;15269;0;32768", label => '/', rrdlabel => 'root', value => 382, uom => 'MB', warning => 15264, critical => 15269, min => 0, max => 32768, clean_label => "root", + }, { + perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var", + }, { + perfoutput => '/var/long@:-/filesystem/name/and/bad/chars=218MB;9443;9448', label => '/var/long@:-/filesystem/name/and/bad/chars', rrdlabel => 'var_long____filesys', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => 'var_long____filesystem_name_and_bad_chars', + }, { + perfoutput => "'page file'=36%;80;90;", + expected_perfoutput => "'page file'=36%;80;90", + label => 'page file', + rrdlabel => 'page_file', + value => '36', + uom => '%', + warning => 80, + critical => 90, + min => undef, + max => undef, + clean_label => 'page_file', + }, { + perfoutput => "'data'=5;;;;", + expected_perfoutput => "data=5;;", + label => 'data', + rrdlabel => 'data', + value => 5, + uom => "", + warning => undef, + critical => undef, + min => undef, + max => undef, + clean_label => 'data', + }, +); + +plan tests => (11 * scalar @test) + 176; + +use_ok('Monitoring::Plugin::Performance'); +diag "\nusing Monitoring::Plugin::Performance revision ". $Monitoring::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; + +# Round-trip tests +for my $t (@test) { + # Parse to components + ($p) = Monitoring::Plugin::Performance->parse_perfstring($t->{perfoutput}); + is ($p->value, $t->{value}, "value okay $t->{value}"); + is ($p->label, $t->{label}, "label okay $t->{label}"); + is ($p->uom, $t->{uom}, "uom okay $t->{uom}"); + + # Construct from components + my @construct = qw(label value uom warning critical min max); + $p = Monitoring::Plugin::Performance->new(map { $_ => $t->{$_} } @construct); + my $expected_perfoutput = $t->{perfoutput}; + if (exists $t->{expected_perfoutput}) { + $expected_perfoutput = $t->{expected_perfoutput}; + }; + is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)"); + # Check threshold accessor + foreach my $type (qw(warning critical)) { + if (! defined $t->{$type}) { + isnt( $p->threshold->$type->is_set, "threshold $type not set"); + } else { + is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})"); + } + } + is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay"); + is($p->clean_label, $t->{clean_label}, "clean_label okay" ); + + # Construct using threshold + @construct = qw(label value uom min max); + $p = Monitoring::Plugin::Performance->new( + map({ $_ => $t->{$_} } @construct), + threshold => Monitoring::Plugin::Threshold->set_thresholds(warning => $t->{warning}, critical => $t->{critical}), + ); + is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)"); + # Check warning/critical accessors + foreach my $type (qw(warning critical)) { + if (! defined $t->{$type}) { + isnt( $p->threshold->$type->is_set, "threshold $type not set"); + } else { + is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})"); + } + } +} + + +# Test multiple parse_perfstrings +@p = Monitoring::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); +cmp_ok( $p[0]->label, 'eq', "/", "label okay"); +cmp_ok( $p[0]->rrdlabel, 'eq', "root", "rrd 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 undef"); +ok(! defined $p[0]->max, "max undef"); + +cmp_ok( $p[1]->label, 'eq', "/var", "label okay"); +cmp_ok( $p[1]->rrdlabel, 'eq', "var", "rrd 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"); + +@p = Monitoring::Plugin::Performance->parse_perfstring("rubbish"); +ok( ! @p, "Errors correctly"); +ok( ! Monitoring::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); + + + +# Check 1 bad with 1 good format output +@p = Monitoring::Plugin::Performance->parse_perfstring("rta=&391ms;100,200;500,034;0; pl=0%;20;60 "); +is( scalar @p, 1, "One bad piece of data - only one returned" ); +is( $p[0]->label, "pl", "label okay for different numeric"); +is( $p[0]->value, 0, "value okay"); +is( $p[0]->uom, "%", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "20", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "60", "warn okay"); + +# Same as above, but order swapped +@p = Monitoring::Plugin::Performance->parse_perfstring(" pl=0%;20;60 rta=&391ms;100,200;500,034;0; "); +is( scalar @p, 1, "One bad piece of data - only one returned" ); +is( $p[0]->label, "pl", "label okay for different numeric"); +is( $p[0]->value, 0, "value okay"); +is( $p[0]->uom, "%", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "20", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "60", "warn okay"); + + + + +@p = Monitoring::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"); + ok( $p[0]->threshold->warning->is_set, "warn okay"); + ok( $p[0]->threshold->critical->is_set, "crit okay"); + + + +@p = Monitoring::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 = Monitoring::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 = Monitoring::Plugin::Performance->parse_perfstring( "users=4;20;50;0\n" ); + ok( @p, "parse correctly with linefeed at end (nagiosgraph)"); + +@p = Monitoring::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( ! $p[1]->threshold->warning->is_set, "warn okay"); + ok( ! $p[1]->threshold->critical->is_set, "crit okay"); + +# Edge cases +@p = Monitoring::Plugin::Performance->parse_perfstring("/home/a-m=0;0;0 shared-folder:big=20 12345678901234567890=20"); +cmp_ok( $p[0]->rrdlabel, "eq", "home_a_m", "changing / to _"); + ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); +cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); + +# turn off fake_exit and enable use_die so we pick up on errors via plugin_die +Monitoring::Plugin::Functions::_use_die(1); +Monitoring::Plugin::Functions::_fake_exit(0); + +@p = Monitoring::Plugin::Performance->parse_perfstring("time=0.002722s;0.000000;0.000000;0.000000;10.000000"); +cmp_ok( $p[0]->label, "eq", "time", "label okay"); +cmp_ok( $p[0]->value, "eq", "0.002722", "value okay"); +cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); + ok( defined $p[0]->threshold->warning->is_set, "Warning range has been set"); + ok( defined $p[0]->threshold->critical->is_set, "Critical range has been set"); +# The two below used to be cmp_ok, but Test::More 0.86 appears to have a problem with a stringification +# of 0. See http://rt.cpan.org/Ticket/Display.html?id=41109 +# We need to force stringification for test. See RT 57709 +is( $p[0]->threshold->warning."", "0", "warn okay"); +is( $p[0]->threshold->critical."", "0", "crit okay"); + +@p = Monitoring::Plugin::Performance->parse_perfstring("pct_used=73.7%;90;95"); +cmp_ok( $p[0]->label, "eq", "pct_used", "label okay"); +cmp_ok( $p[0]->value, "eq", "73.7", "value okay"); +cmp_ok( $p[0]->uom, "eq", "%", "uom okay"); + ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); + ok( defined eval { $p[0]->threshold->critical->is_set }, "Critical range has been set"); +cmp_ok( $p[0]->threshold->warning, 'eq', "90", "warn okay"); +cmp_ok( $p[0]->threshold->critical, 'eq', "95", "crit okay"); + +# Check ranges are parsed correctly +@p = Monitoring::Plugin::Performance->parse_perfstring("availability=93.8%;90:99;"); +is( $p[0]->label, "availability", "label okay"); +is( $p[0]->value, "93.8", "value okay"); +is( $p[0]->uom, "%", "uom okay"); +ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); +is( $p[0]->threshold->critical->is_set, 0, "Critical range has not been set"); +is( $p[0]->threshold->warning, "90:99", "warn okay"); + +# Check that negative values are parsed correctly in value and ranges +@p = Monitoring::Plugin::Performance->parse_perfstring("offset=-0.004476s;-60.000000:-5;-120.000000:-3;"); +is( $p[0]->label, "offset", "label okay"); +is( $p[0]->value, "-0.004476", "value okay"); +is( $p[0]->uom, "s", "uom okay"); +ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); +ok( defined eval { $p[0]->threshold->critical->is_set }, "Critical range has been set"); +is( $p[0]->threshold->warning, "-60:-5", "warn okay"); +is( $p[0]->threshold->critical, "-120:-3", "crit okay"); + +# Check infinity values are okay +@p = Monitoring::Plugin::Performance->parse_perfstring("salary=52GBP;~:23.5;45.2:"); +is( $p[0]->label, "salary", "label okay"); +is( $p[0]->value, "52", "value okay"); +is( $p[0]->uom, "GBP", "uom okay"); +ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->warning, "~:23.5", "warn okay"); +is( $p[0]->threshold->critical, "45.2:", "warn okay"); + +# Check scientific notation +@p = Monitoring::Plugin::Performance->parse_perfstring("offset=1.120567322e-05"); +is( $p[0]->label, "offset", "label okay for scientific notation"); +is( $p[0]->value, 1.120567322e-05, "value okay"); +is( $p[0]->uom, "", "uom okay"); +ok( ! $p[0]->threshold->warning->is_set, "Warning range has not been set"); +ok( ! $p[0]->threshold->critical->is_set, "Critical range has not been set"); + + +# Check scientific notation with warnings and criticals +@p = Monitoring::Plugin::Performance->parse_perfstring("offset=-1.120567322e-05unit;-1.1e-05:1.0e-03;4.3e+02:4.3e+25"); +is( $p[0]->label, "offset", "label okay for scientific notation in warnings and criticals"); +is( $p[0]->value, -1.120567322e-05, "value okay"); +is( $p[0]->uom, "unit", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay"); + + + +# Check different collation with commas instead of periods +@p = Monitoring::Plugin::Performance->parse_perfstring("rta=1,391ms;100,200;500,034;0; pl=0%;20;60;;"); +is( $p[0]->label, "rta", "label okay for numeric with commas instead of periods"); +is( $p[0]->value, 1.391, "value okay"); +is( $p[0]->uom, "ms", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "100.2", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "500.034", "warn okay"); +is( $p[1]->label, "pl", "label okay for different numeric"); +is( $p[1]->value, 0, "value okay"); +is( $p[1]->uom, "%", "uom okay"); +ok( $p[1]->threshold->warning->is_set, "Warning range has been set"); +is( $p[1]->threshold->warning, "20", "warn okay"); +is( $p[1]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[1]->threshold->critical, "60", "warn okay"); + + +# Another set of comma separated stuff +@p = Monitoring::Plugin::Performance->parse_perfstring("offset=-0,023545s;60,000000;120,000000;"); +is( $p[0]->label, "offset", "label okay for numeric with commas instead of periods"); +is( $p[0]->value, -0.023545, "value okay"); +is( $p[0]->uom, "s", "uom okay"); +is( $p[0]->threshold->warning->is_set, 1, "Warning range has been set"); +is( $p[0]->threshold->warning, 60, "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, 120, "warn okay"); + +# Some values with funny commas +@p = Monitoring::Plugin::Performance->parse_perfstring("time=1800,600,300,0,3600 other=45.6"); +is( $p[0]->label, "other", "Ignored time=1800,600,300,0,3600, but allowed other=45.6"); +is( $p[0]->value, 45.6, "value okay"); +is( $p[0]->uom, "", "uom okay"); + + +# Test labels with spaces (returned by nsclient++) +@p = Monitoring::Plugin::Performance->parse_perfstring("'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 'D:\ Label: Serial Number XA22aa2e'=8015MB;16388;18436;1;2048"); +is( $p[0]->label, "C:\ Label: Serial Number bc22aa2e"); +is( $p[0]->rrdlabel, "C__Label___Serial_N"); +is( $p[0]->value, 8015, "value okay"); +is( $p[0]->uom, "MB", "uom okay"); +is( $p[0]->threshold->warning->end, 16387, "warn okay"); +is( $p[0]->threshold->critical->end, 18435, "crit okay"); +is( $p[0]->min, 0, "min ok"); +is( $p[0]->max, 20484, "max ok"); + +is( $p[1]->label, "D:\ Label: Serial Number XA22aa2e", "label okay"); +is( $p[1]->rrdlabel, "D__Label__Serial_Nu", "rrd label okay"); +is( $p[1]->value, 8015, "value okay"); +is( $p[1]->uom, "MB", "uom okay"); +is( $p[1]->threshold->warning->end, 16388, "warn okay"); +is( $p[1]->threshold->critical->end, 18436, "crit okay"); +is( $p[1]->min, 1, "min ok"); +is( $p[1]->max, 2048, "max ok"); + + +# Mix labels with and without quotes +@p = Monitoring::Plugin::Performance->parse_perfstring(" short=4 'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 end=5 "); +is( $p[0]->label, "short" ); +is( $p[0]->rrdlabel, "short"); +is( $p[0]->value, 4, "value okay"); +is( $p[0]->uom, "", "uom okay"); +isnt( $p[0]->threshold->warning->is_set, "warn okay"); +isnt( $p[0]->threshold->critical->is_set, "crit okay"); +is( $p[0]->min, undef, "min ok"); +is( $p[0]->max, undef, "max ok"); + +is( $p[1]->label, "C:\ Label: Serial Number bc22aa2e", "label okay"); +is( $p[1]->rrdlabel, "C__Label___Serial_N", "rrd label okay"); +is( $p[1]->value, 8015, "value okay"); +is( $p[1]->uom, "MB", "uom okay"); +is( $p[1]->threshold->warning->end, 16387, "warn okay"); +is( $p[1]->threshold->critical->end, 18435, "crit okay"); +is( $p[1]->min, 0, "min ok"); +is( $p[1]->max, 20484, "max ok"); + +is( $p[2]->label, "end" ); +is( $p[2]->rrdlabel, "end" ); +is( $p[2]->value, 5, "value okay"); +is( $p[2]->uom, "", "uom okay"); +isnt( $p[2]->threshold->warning->is_set, "warn okay"); +isnt( $p[2]->threshold->critical->is_set, 18436, "crit okay"); +is( $p[2]->min, undef, "min ok"); +is( $p[2]->max, undef, "max ok"); + + +@p = Monitoring::Plugin::Performance->parse_perfstring("processes=9;WKFLSV32.exe;9="); +is_deeply( \@p, [], "Fails parsing correctly"); + +# add_perfdata tests in t/Monitoring-Plugin-01.t diff --git a/t/Monitoring-Plugin-Range.t b/t/Monitoring-Plugin-Range.t new file mode 100644 index 0000000..9a6e826 --- /dev/null +++ b/t/Monitoring-Plugin-Range.t @@ -0,0 +1,243 @@ + +use strict; +#use Test::More qw(no_plan); +use Test::More tests => 151; + +BEGIN { + use_ok('Monitoring::Plugin::Range'); + # Silence warnings unless TEST_VERBOSE is set + $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; +}; + +diag "\nusing Monitoring::Plugin::Range revision ". $Monitoring::Plugin::Range::VERSION . "\n" if $ENV{TEST_VERBOSE}; + +my $r; + +diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:" if $ENV{TEST_VERBOSE}; + +foreach (qw( + : + 1:~ + foo + 1-10 + 10:~ + 1-10:2.4 + +), '1,10' # avoid warning about using , inside qw() +) { + $r =Monitoring::Plugin::Range->parse_range_string($_); + is $r, undef, "'$_' should not be a valid range" ; +} + + +diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE}; +$r = Monitoring::Plugin::Range->parse_range_string("6"); +isa_ok( $r, "Monitoring::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"); + +my $expected = { + -1 => 1, # 1 means it raises an alert because it's OUTSIDE the range + 0 => 0, # 0 means it's inside the range (no alert) + 4 => 0, + 6 => 0, + 6.1 => 1, + 79.999999 => 1, +}; + +sub test_expected { + my $r = shift; + my $expected = shift; + foreach (sort {$a<=>$b} keys %$expected) { + is $r->check_range($_), $expected->{$_}, + " $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")"; + } +} + +test_expected( $r, $expected ); + +diag "range : -7..23, inclusive" if $ENV{TEST_VERBOSE}; +$r = Monitoring::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"); + +$expected = { + -23 => 1, + -7 => 0, + -1 => 0, + 0 => 0, + 4 => 0, + 23 => 0, + 23.1 => 1, + 79.999999 => 1, +}; +test_expected( $r, $expected ); + + +diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE}; +$r = Monitoring::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"); +$expected = { + -1 => 1, + 0 => 0, + 4 => 0, + 5.75 => 0, + 5.7501 => 1, + 6 => 1, + 6.1 => 1, + 79.999999 => 1, +}; +test_expected( $r, $expected ); + + + +diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE}; +$r = Monitoring::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"); +$expected = { + -1001341 => 0, + -96 => 0, + -95.999 => 0, + -95.99 => 0, + -95.989 => 1, + -95 => 1, + 0 => 1, + 5.7501 => 1, + 79.999999 => 1, +}; +test_expected( $r, $expected ); + +diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE}; +test_expected( $r, $expected ); +$r = Monitoring::Plugin::Range->parse_range_string("10:"); +ok( defined $r, "'10:' is valid range"); +cmp_ok( $r->start, '==', 10, "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', "10:", "Stringification back to original"); +$expected = { + -95.999 => 1, + -1 => 1, + 0 => 1, + 9.91 => 1, + 10 => 0, + 11.1 => 0, + 123456789012346 => 0, +}; +test_expected( $r, $expected ); + + + +diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE}; +test_expected( $r, $expected ); +$r = Monitoring::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"); +$expected = { + -95.999 => 1, + -1 => 1, + 0 => 1, + # The fractional values needs to be quoted, otherwise the hash rounds it up to ..345 + # and there is one less test run. + # I think some newer versions of perl use a higher precision value for the hash key. + # This doesn't appear to affect the actual plugin though + "123456789012344.91" => 1, + 123456789012345 => 0, + "123456789012345.61" => 0, + 123456789012346 => 0, +}; +test_expected( $r, $expected ); + + +diag "range: <= zero " if $ENV{TEST_VERBOSE}; +$r = Monitoring::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"); +$expected = { + -123456789012344.91 => 0, + -1 => 0, + 0 => 0, + .001 => 1, + 123456789012345 => 1, +}; +test_expected( $r, $expected ); + + +diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE}; +$r = Monitoring::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"); +$expected = { + -134151 => 0, + -1 => 0, + 0 => 1, + .001 => 1, + 657.8210567 => 1, + 657.9 => 0, + 123456789012345 => 0, +}; +test_expected( $r, $expected ); + + +diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE}; +$r = Monitoring::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"); +$expected = { + -1 => 1, + 0 => 1, + .5 => 1, + 1 => 0, + 1.001 => 1, + 5.2 => 1, +}; +test_expected( $r, $expected ); + + +$r = Monitoring::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/Monitoring-Plugin-Threshold.t b/t/Monitoring-Plugin-Threshold.t new file mode 100644 index 0000000..8849464 --- /dev/null +++ b/t/Monitoring-Plugin-Threshold.t @@ -0,0 +1,221 @@ + +use strict; +use Test::More tests => 93; +BEGIN { + use_ok('Monitoring::Plugin::Threshold'); + use_ok('Monitoring::Plugin::Functions', ':all' ); + # Silence warnings unless TEST_VERBOSE is set + $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; +} + +diag "\nusing Monitoring::Plugin::Threshold revision ". $Monitoring::Plugin::Threshold::VERSION . "\n" + if $ENV{TEST_VERBOSE}; + +Monitoring::Plugin::Functions::_fake_exit(1); + +my $t; + +$t = Monitoring::Plugin::Threshold->set_thresholds(warning => undef, critical => undef); +ok( defined $t, "two undefs" ); +ok( ! $t->warning->is_set, "warning not set" ); +ok( ! $t->critical->is_set, "critical not set" ); + +$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "", critical => ""); +ok( defined $t, "two empty strings" ); +ok( ! $t->warning->is_set, "warning not set" ); +ok( ! $t->critical->is_set, "critical not set" ); + +diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; +my $t = Monitoring::Plugin::Threshold->set_thresholds(critical => "80"); +ok( defined $t, "Threshold ('', '80') set"); +ok( ! $t->warning->is_set, "Warning not set"); +cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly"); +cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly"); +ok ! $t->critical->end_infinity, "not forever"; + +my $expected = { qw( + -1 CRITICAL + 4 OK + 79.999999 OK + 80 OK + 80.1 CRITICAL + 102321 CRITICAL +) }; + +sub test_expected_statuses { + my $t = shift; + my $expected = shift; + my $debug = shift; + + foreach (sort {$a<=>$b} keys %$expected) { + is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; + if ($debug) { + diag "val = $_; critical check = ".$t->critical->check_range($_). + "; warning check = ".$t->warning->check_range($_); + } + } + use Data::Dumper; + diag "thresh dump: ". Dumper $t if $debug; +} +test_expected_statuses( $t, $expected ); + +# GMC: this test seems bogus to me - either we've died, in which case internal +# state is undefined (and untestable!), or we should be returning a non-fatal error +if (0) { + diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; + eval { $t = Monitoring::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( ! $t->critical->is_set, "Critical not set"); +} + +# GC: same as previous test, except critical is undef instead of '' +diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; +$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef); +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( ! $t->critical->is_set, "Critical not set"); + +$expected = { qw( + -1 WARNING + 4 WARNING + 4.999999 WARNING + 5 OK + 14.21 OK + 33 OK + 33.01 WARNING + 10231 WARNING +) }; +test_expected_statuses( $t, $expected ); + +diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE}; +$t = Monitoring::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"); +ok $t->critical->start_infinity, "Critical starts at negative infinity"; + +$expected = { qw( + -1 OK + 4 OK + 29.999999 OK + 30 OK + 30.1 WARNING + 50.90 WARNING + 59.9 WARNING + 60 WARNING + 60.00001 CRITICAL + 10231 CRITICAL +) }; +test_expected_statuses( $t, $expected ); + +# "I'm going to die homeless, penniless, and 30 pounds overweight." +# "...and that's...okay." + +# TODO: figure out why this doesn't work and fix the test. +goto SKIP_DEATH; +diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; +diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; +Monitoring::Plugin::Functions->print_on_die(1); +Monitoring::Plugin::Functions->exit_on_die(1); + +dies_ok( sub { + $t = Monitoring::Plugin::Threshold->set_thresholds( + warning => "total", + critical => "rubbish" + ) + }, "bad thresholds cause death" +); +Monitoring::Plugin::Functions->print_on_die(0); +Monitoring::Plugin::Functions->exit_on_die(0); +SKIP_DEATH: + + +diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE}; +$t = Monitoring::Plugin::Threshold->set_thresholds( critical => "~:25" ); +ok( defined $t, "Threshold ('', '~:25') set (".$t->critical.")" ); +ok( ! $t->warning->is_set, "Warning not set"); +cmp_ok( $t->critical->end, '==',25, "Critical end set"); +ok $t->critical->start_infinity, "Critical starts at negative infinity"; + +$expected = { qw( + -1 OK + 4 OK + 10 OK + 14.21 OK + 25 OK + 25.01 CRITICAL + 31001 CRITICAL +) }; +test_expected_statuses( $t, $expected); + +diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE}; +$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25"); +ok( defined $t, "Threshold ('10:25', '~:25') set"); +cmp_ok( $t->warning->start, '==', 10, "Warning start set"); +cmp_ok( $t->warning->end, '==', 25, "Warning end set"); +cmp_ok( $t->critical->end, '==', 25, "Critical end set"); + +$expected = { qw( + -1 WARNING + 4 WARNING + 9.999999 WARNING + 10 OK + 14.21 OK + 25 OK + 25.01 CRITICAL + 31001 CRITICAL +) }; +test_expected_statuses( $t, $expected ); + + +diag "warn if INSIDE {10..25} , critical if < 10 " if $ENV{TEST_VERBOSE}; +$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:"); +$expected = { qw( + -1 CRITICAL + 4 CRITICAL + 9.999999 CRITICAL + 10 WARNING + 14.21 WARNING + 25 WARNING + 25.01 OK + 31001 OK +) }; +test_expected_statuses( $t, $expected ); + + +# GMC: as of 0.16, set_thresholds can also be called as a mutator +diag "threshold mutator: warn if more than 30; critical if > 60" + if $ENV{TEST_VERBOSE}; +my $t1 = $t; +$t->set_thresholds(warning => "0:45", critical => "0:90"); +is($t1, $t, "same threshold object after \$t->set_thresholds"); +ok( defined $t, "Threshold ('0:45', '0:90') set"); +is( $t->warning->start, 0, "Warning start ok"); +is( $t->warning->end, 45, "Warning end ok"); +is( $t->critical->start, 0, "Critical start ok"); +is( $t->critical->end, 90, "Critical end ok"); + + +# Also as of 0.16, accepts N::P::Range objects as arguments +my $warning = Monitoring::Plugin::Range->parse_range_string("50"); +my $critical = Monitoring::Plugin::Range->parse_range_string("70:90"); +$t = Monitoring::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical); +ok( defined $t, "Threshold from ranges ('50', '70:90') set"); +is( $t->warning->start, 0, "Warning start ok"); +is( $t->warning->end, 50, "Warning end ok"); +is( $t->critical->start, 70, "Critical start ok"); +is( $t->critical->end, 90, "Critical end ok"); + +$critical = Monitoring::Plugin::Range->parse_range_string("90:"); +$t->set_thresholds(warning => "~:20", critical => $critical); +ok( defined $t, "Threshold from string + range ('~:20', '90:') set"); +ok( $t->warning->start_infinity, "Warning start ok (infinity)"); +is( $t->warning->end, 20, "Warning end ok"); +is( $t->critical->start, 90, "Critical start ok"); +ok( $t->critical->end_infinity, "Critical end ok (infinity)"); + + +ok 1, "sweet, made it to the end."; diff --git a/t/Nagios-Plugin-01.t b/t/Nagios-Plugin-01.t deleted file mode 100644 index 947a704..0000000 --- a/t/Nagios-Plugin-01.t +++ /dev/null @@ -1,72 +0,0 @@ -# Nagios::Plugin original test cases - -use strict; -use Test::More tests => 15; - -BEGIN { use_ok('Nagios::Plugin') }; - -use Nagios::Plugin::Functions; -Nagios::Plugin::Functions::_fake_exit(1); - -diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" - if $ENV{TEST_VERBOSE}; - -my $p = Nagios::Plugin->new(); -isa_ok( $p, "Nagios::Plugin"); - -$p->shortname("PAGESIZE"); -is($p->shortname, "PAGESIZE", "shortname explicitly set correctly"); - -$p = Nagios::Plugin->new(); -is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); - -$p = Nagios::Plugin->new( shortname => "SIZE", () ); -is($p->shortname, "SIZE", "shortname set correctly on new"); - -$p = Nagios::Plugin->new( plugin => "check_stuff", () ); -is($p->shortname, "STUFF", "shortname uses plugin name as default"); - -$p = Nagios::Plugin->new( shortname => "SIZE", plugin => "check_stuff", () ); -is($p->shortname, "SIZE", "shortname is not overriden by default"); - -diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; -my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); - -use Data::Dumper; -#diag "dumping p: ". Dumper $p; -#diag "dumping perfdata: ". Dumper $p->perfdata; - - -$p->add_perfdata( - label => "size", - value => 1, - uom => "kB", - threshold => $t, - ); - -cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); -#diag "dumping perfdata: ". Dumper ($p->perfdata); - -$p->add_perfdata( - label => "time", - value => "3.52", - threshold => $t, - ); - -is( $p->all_perfoutput, "size=1kB;10:25;~:25 time=3.52;10:25;~:25", "Perfdata correct when no uom specified"); - -my $expected = {qw( - -1 WARNING - 1 WARNING - 20 OK - 25 OK - 26 CRITICAL - 30 CRITICAL - )}; - -foreach (sort {$a<=>$b} keys %$expected) { - like $p->die( return_code => $t->get_status($_), message => "page size at http://... was ${_}kB" ), - qr/$expected->{$_}/, - "Output okay. $_ = $expected->{$_}" ; -} - diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t deleted file mode 100644 index f64fb6f..0000000 --- a/t/Nagios-Plugin-02.t +++ /dev/null @@ -1,161 +0,0 @@ -# Nagios::Plugin test set 2, testing NP::Functions wrapping - -use strict; -use Test::More tests => 103; - -BEGIN { use_ok("Nagios::Plugin") } -require Nagios::Plugin::Functions; -Nagios::Plugin::Functions::_fake_exit(1); - -# Hardcoded checks of constants -my %ERRORS = %Nagios::Plugin::Functions::ERRORS; -is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); -is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); -is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); -is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); -is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); - -my $plugin = 'TEST_PLUGIN'; -my $np = Nagios::Plugin->new( shortname => $plugin ); -is($np->shortname, $plugin, "shortname() is $plugin"); - -# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) -my $r; -my @ok = ( - [ OK, "OK", 'test the first', ], - [ WARNING, "WARNING", 'test the second', ], - [ CRITICAL, "CRITICAL", 'test the third', ], - [ UNKNOWN, "UNKNOWN", 'test the fourth', ], - [ DEPENDENT, "DEPENDENT", 'test the fifth', ], -); -for (@ok) { - # CONSTANT - $r = $np->nagios_exit($_->[0], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_exit(%s, $msg) output matched "%s"', $_->[1], - $plugin . ' ' . $_->[1] . '.*' . $_->[2])); - - # $string - $r = $np->nagios_exit($_->[1], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], - $plugin . ' ' . $_->[1] . '.*' . $_->[2])); - like($r, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], - $plugin . ' ' . $_->[1] . '.*' . $_->[2])); -} - -# nagios_exit code corner cases -my @ugly1 = ( - [ -1, 'testing code -1' ], - [ 7, 'testing code 7' ], - [ undef, 'testing code undef' ], - [ '', qq(testing code '') ], - [ 'string', qq(testing code 'string') ], -); -for (@ugly1) { - $r = $np->nagios_exit($_->[0], $_->[1]); - my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; - is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN); - like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, - sprintf('nagios_exit(%s, $msg) output matched "%s"', - $display, 'UNKNOWN.*' . $_->[1])); -} - -# nagios_exit message corner cases -my @ugly2 = ( - [ '' ], - [ undef ], - [ UNKNOWN ], -); -for (@ugly2) { - $r = $np->nagios_exit(CRITICAL, $_->[0]); - my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; - my $display2 = defined $_->[0] ? $_->[0] : ''; - like($r->message, qr/CRITICAL\b.*\b$display2$/, - sprintf('nagios_exit(%s, $msg) output matched "%s"', - $display1, "CRITICAL.*$display2")); -} - -# Test nagios_die( $msg ) -my @msg = ( - [ 'die you dog' ], - [ '' ], - [ undef ], -); -for (@msg) { - $r = $np->nagios_die($_->[0]); - my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; - my $display2 = defined $_->[0] ? $_->[0] : ''; - is($r->return_code, UNKNOWN, - sprintf('nagios_die(%s) returned UNKNOWN', $display1)); - like($r->message, qr/UNKNOWN\b.*\b$display2$/, - sprintf('nagios_die(%s) output matched "%s"', $display1, - "UNKNOWN.*$display2")); -} - -# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), -# nagios_die( $string, $msg ), and nagios_die( $msg, $string ) -@ok = ( - [ OK, "OK", 'test the first', ], - [ WARNING, "WARNING", 'test the second', ], - [ CRITICAL, "CRITICAL", 'test the third', ], - [ UNKNOWN, "UNKNOWN", 'test the fourth', ], - [ DEPENDENT, "DEPENDENT", 'test the fifth', ], -); -for (@ok) { - # CONSTANT, $msg - $r = $np->nagios_die($_->[0], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die(%s, $msg) output matched "%s"', - $_->[1], $_->[1] . '.*' . $_->[2])); - - # $msg, CONSTANT - $r = $np->nagios_die($_->[2], $_->[0]); - is($r->return_code, $_->[0], - sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die($msg, %s) output matched "%s"', - $_->[1], $_->[1] . '.*' . $_->[2])); - - # $string, $msg - $r = $np->nagios_die($_->[1], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - like($r, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - - # $string, $msg - $r = $np->nagios_die($_->[2], $_->[1]); - is($r->return_code, $_->[0], - sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - like($r, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); -} - - -# shortname testing -SKIP: { - skip "requires File::Basename", 2 unless eval { require File::Basename }; - $np = Nagios::Plugin->new( version => "1"); - $plugin = uc File::Basename::basename($0); - $plugin =~ s/\..*$//; - is($np->shortname, $plugin, "shortname() is '$plugin'"); - $r = $np->nagios_exit(OK, "foobar"); - like($r->message, qr/^$plugin OK/, "message begins with '$plugin OK'"); -} - diff --git a/t/Nagios-Plugin-03.t b/t/Nagios-Plugin-03.t deleted file mode 100644 index bc4f5e3..0000000 --- a/t/Nagios-Plugin-03.t +++ /dev/null @@ -1,263 +0,0 @@ -# $np->check_messages tests - -use strict; -use Test::More tests => 61; - -BEGIN { - use_ok("Nagios::Plugin"); - use_ok("Nagios::Plugin::Functions", ":all"); -} -Nagios::Plugin::Functions::_fake_exit(1); - -my $plugin = 'NP_CHECK_MESSAGES_03'; -my $np = Nagios::Plugin->new( shortname => $plugin, () ); -is($np->shortname, $plugin, "shortname() is $plugin"); - -my ($code, $message); - -# ------------------------------------------------------------------------- -# Check codes -my @codes = ( - [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ], - [ [], [ qw(Warning) ], WARNING ], - [ [], [], OK ], -); -my $i = 0; -for (@codes) { - $i++; - $code = $np->check_messages( critical => $_->[0], warning => $_->[1] ); - is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}"); -} - -# ------------------------------------------------------------------------- -# Check messages -my %arrays = ( - critical => [ qw(A B C) ], - warning => [ qw(D E F) ], - ok => [ qw(G H I) ], -); -my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays; - -# critical, warning -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, -); -is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning) message is $message"); - -# critical, warning, ok -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - ok => $arrays{ok}, -); -is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning, ok) message is $message"); - -# critical, warning, $ok -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - ok => 'G H I', -); -is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning, \$ok) message is $message"); - -# warning -($code, $message) = $np->check_messages( - critical => [], warning => $arrays{warning}, -); -is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(warning) message is $message"); - -# warning, ok -($code, $message) = $np->check_messages( - critical => [], warning => $arrays{warning}, ok => $arrays{ok}, -); -is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(warning, ok) message is $message"); - -# ok -($code, $message) = $np->check_messages( - critical => [], warning => [], ok => $arrays{ok}, -); -is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{ok}, "(ok) message is $message"); - -# $ok -($code, $message) = $np->check_messages( - critical => [], warning => [], ok => 'G H I', -); -is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{ok}, "(\$ok) message is $message"); - -# ------------------------------------------------------------------------- -# explicit join -my $join = '+'; -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - join => $join, -); -is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message"); -$join = ''; -($code, $message) = $np->check_messages( - critical => [], warning => $arrays{warning}, - join => $join, -); -is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message"); -$join = undef; -($code, $message) = $np->check_messages( - critical => [], warning => [], ok => $arrays{ok}, - join => $join, -); -is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message"); - -# ------------------------------------------------------------------------- -# join_all messages -my $join_all = ' :: '; -my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } - qw(critical warning ok)); -my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } - qw(critical warning)); -my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) } - qw(warning ok)); - -# critical, warning, ok -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok}, - join_all => $join_all, -); -is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message"); - -# critical, warning, $ok -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I', - join_all => $join_all, -); -is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); - -# critical, warning -($code, $message) = $np->check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - join_all => $join_all, -); -is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message"); - -# warning, ok -($code, $message) = $np->check_messages( - critical => [], warning => $arrays{warning}, ok => $arrays{ok}, - join_all => $join_all, -); -is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message"); - -# warning, $ok -($code, $message) = $np->check_messages( - critical => [], warning => $arrays{warning}, ok => 'G H I', - join_all => $join_all, -); -is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); - -# warning -($code, $message) = $np->check_messages( - critical => [], warning => $arrays{warning}, - join_all => $join_all, -); -is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); -is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message"); - -# ------------------------------------------------------------------------- -# add_messages - -# Constant codes -$np = Nagios::Plugin->new(); -$np->add_message( CRITICAL, "A B C" ); -$np->add_message( WARNING, "D E F" ); -($code, $message) = $np->check_messages(); -is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( CRITICAL, "A B C" ); -($code, $message) = $np->check_messages(); -is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(CRITICAL) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( WARNING, "D E F" ); -($code, $message) = $np->check_messages(); -is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(WARNING) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( WARNING, "D E F" ); -$np->add_message( OK, "G H I" ); -($code, $message) = $np->check_messages(); -is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(WARNING, OK) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( OK, "G H I" ); -($code, $message) = $np->check_messages(); -is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); -is($message, $messages{ok}, "(OK) message is $message"); - - -# String codes -$np = Nagios::Plugin->new(); -$np->add_message( critical => "A B C" ); -$np->add_message( warning => "D E F" ); -($code, $message) = $np->check_messages(); -is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( critical => "A B C" ); -($code, $message) = $np->check_messages(); -is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( warning => "D E F" ); -($code, $message) = $np->check_messages(); -is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(warning) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( warning => "D E F" ); -$np->add_message( ok => "G H I" ); -($code, $message) = $np->check_messages(); -is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(warning, ok) message is $message"); - -$np = Nagios::Plugin->new(); -$np->add_message( ok => "G H I" ); -($code, $message) = $np->check_messages(); -is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{ok}, "(ok) message is $message"); - - -# No add_message -$np = Nagios::Plugin->new(); -($code, $message) = $np->check_messages(); -is($code, OK, "() code is $STATUS_TEXT{$code}"); -is($message, '', "() message is ''"); - - -# ------------------------------------------------------------------------- -# Error conditions - -# add_message errors -$np = Nagios::Plugin->new(); -ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, - 'add_message dies on invalid code'); -ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, - 'add_message dies on invalid code'); -# UNKNOWN and DEPENDENT error codes -ok(! defined eval { $np->add_message( unknown => 'hi mum' ) }, - 'add_message dies on UNKNOWN code'); -ok(! defined eval { $np->add_message( dependent => 'hi mum' ) }, - 'add_message dies on DEPENDENT code'); - diff --git a/t/Nagios-Plugin-04.t b/t/Nagios-Plugin-04.t deleted file mode 100644 index e5eb3ab..0000000 --- a/t/Nagios-Plugin-04.t +++ /dev/null @@ -1,99 +0,0 @@ - -# tests for toplevel access to Threshold and GetOpts stuff - -use strict; -#use Test::More 'no_plan'; -use Test::More tests=>30; - -BEGIN { use_ok('Nagios::Plugin') }; -use Nagios::Plugin::Functions; -Nagios::Plugin::Functions::_fake_exit(1); - - -eval { Nagios::Plugin->new(); }; -ok(! $@, "constructor DOESN'T die without usage"); - -my $p = Nagios::Plugin->new(); -eval { $p->add_arg('warning', 'warning') }; -ok($@, "add_arg() dies if you haven't instantiated with usage"); -eval { $p->getopts }; -ok($@, "getopts() dies if you haven't instantiated with usage"); - -$p = Nagios::Plugin->new( usage => "dummy usage statement" ); - -# option accessors work -can_ok $p, 'opts'; -isa_ok $p->opts, 'Nagios::Plugin::Getopt', "Getopt object is defined"; - -$p->add_arg('warning|w=s', "warning"); -$p->add_arg('critical|c=s', "critical"); - -@ARGV = qw(-w 5 -c 10); -$p->getopts; -is $p->opts->warning, "5", "warning opt is accessible"; -is $p->opts->critical, "10", "critical opt is accessible"; - - -can_ok $p, 'perfdata'; -#isa_ok $p->perfdata, 'Nagios::Plugin::Performance', "perfdata object is defined"; - - -can_ok $p, 'threshold'; -#isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined"; - - -eval { $p->check_threshold() }; -ok($@, "check_threshold dies if called with no args"); - - -# thresholds set implicitly -is $p->check_threshold(2), OK, "check_threshold OK when called implicitly"; -is $p->check_threshold(6), WARNING, "check_threshold WARNING"; -is $p->check_threshold(11), CRITICAL, "check_threshold CRITICAL"; -is $p->check_threshold(check=>11), CRITICAL, "check_threshold CRITICAL with hash param"; - -# Check that arrays allowed -is $p->check_threshold([2,1]), OK, "check_threshold OK when called implicitly"; -is $p->check_threshold([6,2]), WARNING, "check_threshold WARNING"; -is $p->check_threshold([1,2,6,11]), CRITICAL, "check_threshold CRITICAL"; -is $p->check_threshold(check=>[1,2,6,11]), CRITICAL, "check_threshold CRITICAL with hash param"; - -# thresholds set explicitly -is $p->check_threshold( - check => 2, - warning => 50, - critical => 100 -), OK, "check_threshold explicit OK"; - -is $p->check_threshold( - check => 66, - warning => 50, - critical => 100 -), WARNING, "check_threshold explicit WARNING"; - - -is $p->check_threshold( - check => -1, - warning => 5, - critical => '0:5', -), CRITICAL, "check_threshold explicit CRITICAL"; - - - -# what happens if you forget to define warning or critical thresholds? -$p = undef; -$p = Nagios::Plugin->new(); - -is $p->check_threshold(2), UNKNOWN, "everything is now UNKNOWN"; -is $p->check_threshold(-200), UNKNOWN, "everything is now UNKNOWN"; -is $p->check_threshold(134098.3124), UNKNOWN, "everything is now UNKNOWN"; -is $p->check_threshold("foo bar baz"), UNKNOWN, "everything is now UNKNOWN"; - - -# how about when you define just one? - -$p->set_thresholds(warning => "10:25"); -is $p->check_threshold(2), WARNING, "check_threshold works (WARNING) after explicit set_thresholds"; -is $p->check_threshold(-200), WARNING, "and again"; -is $p->check_threshold(25.5), WARNING, "and again"; -is $p->check_threshold(11), OK, "now OK"; diff --git a/t/Nagios-Plugin-05.t b/t/Nagios-Plugin-05.t deleted file mode 100644 index cc602cc..0000000 --- a/t/Nagios-Plugin-05.t +++ /dev/null @@ -1,16 +0,0 @@ -# Check for exported vars -# Can't include Nagios::Plugin::Functions because it also exports %STATUS_TEXT - -use strict; -use Test::More tests=>4; - -BEGIN { use_ok('Nagios::Plugin') }; - -eval ' $_ = $STATUS_TEXT{0} '; -like( $@, '/Global symbol "%STATUS_TEXT" requires explicit package name/' ); - -use_ok("Nagios::Plugin", qw(%STATUS_TEXT)); - -eval ' $_ = $STATUS_TEXT{0} '; -is( $@, '' ); - diff --git a/t/Nagios-Plugin-Functions-01.t b/t/Nagios-Plugin-Functions-01.t deleted file mode 100644 index 5268255..0000000 --- a/t/Nagios-Plugin-Functions-01.t +++ /dev/null @@ -1,161 +0,0 @@ - -use strict; -use Test::More tests => 113; - -BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); } -Nagios::Plugin::Functions::_fake_exit(1); - -my $this_version=$Nagios::Plugin::Functions::VERSION; -foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { - my $mod = "Nagios::Plugin$m"; - use_ok($mod); - # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing - my $v = "$mod"."::VERSION"; - my $a = eval "\$$v"; - is($a, $this_version, "Version number for $mod the same as Functions: $this_version"); -} - -# check get_shortname -is(get_shortname, "NAGIOS-PLUGIN-FUNCTIONS-01", "get_shortname ok"); - -# Hardcoded checks of constants -ok(defined %ERRORS, '%ERRORS defined'); -is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); -is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); -is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); -is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); -is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); - -# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) -my $r; -my @ok = ( - [ OK, "OK", 'test the first', ], - [ WARNING, "WARNING", 'test the second', ], - [ CRITICAL, "CRITICAL", 'test the third', ], - [ UNKNOWN, "UNKNOWN", 'test the fourth', ], - [ DEPENDENT, "DEPENDENT", 'test the fifth', ], -); -for (@ok) { - # CONSTANT - $r = nagios_exit($_->[0], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_exit(%s, $msg) output matched "%s"', - $_->[1], $_->[1] . '.*' . $_->[2])); - - # $string - $r = nagios_exit($_->[1], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - like($r, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); -} - -# nagios_exit code corner cases -my @ugly1 = ( - [ -1, 'testing code -1' ], - [ 7, 'testing code 7' ], - [ undef, 'testing code undef' ], - [ '', qq(testing code '') ], - [ 'string', qq(testing code 'string') ], -); -for (@ugly1) { - $r = nagios_exit($_->[0], $_->[1]); - my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; - is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN); - like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, - sprintf('nagios_exit(%s, $msg) output matched "%s"', - $display, 'UNKNOWN.*' . $_->[1])); -} - -# nagios_exit message corner cases -my @ugly2 = ( - [ '' ], - [ undef ], - [ UNKNOWN ], -); -for (@ugly2) { - $r = nagios_exit(CRITICAL, $_->[0]); - my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; - my $display2 = defined $_->[0] ? $_->[0] : ''; - like($r->message, qr/CRITICAL\b.*\b$display2$/, - sprintf('nagios_exit(%s, $msg) output matched "%s"', - $display1, "CRITICAL.*$display2")); -} - -# Test nagios_die( $msg ) -my @msg = ( - [ 'die you dog' ], - [ '' ], - [ undef ], -); -for (@msg) { - $r = nagios_die($_->[0]); - my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; - my $display2 = defined $_->[0] ? $_->[0] : ''; - is($r->return_code, UNKNOWN, - sprintf('nagios_die(%s) returned UNKNOWN', $display1)); - like($r->message, qr/UNKNOWN\b.*\b$display2$/, - sprintf('nagios_die(%s) output matched "%s"', $display1, - "UNKNOWN.*$display2")); -} - -# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), -# nagios_die( $string, $msg ), and nagios_die( $msg, $string ) -@ok = ( - [ OK, "OK", 'test the first', ], - [ WARNING, "WARNING", 'test the second', ], - [ CRITICAL, "CRITICAL", 'test the third', ], - [ UNKNOWN, "UNKNOWN", 'test the fourth', ], - [ DEPENDENT, "DEPENDENT", 'test the fifth', ], -); -for (@ok) { - # CONSTANT, $msg - $r = nagios_die($_->[0], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die(%s, $msg) output matched "%s"', - $_->[1], $_->[1] . '.*' . $_->[2])); - - # $msg, CONSTANT - $r = nagios_die($_->[2], $_->[0]); - is($r->return_code, $_->[0], - sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die($msg, %s) output matched "%s"', - $_->[1], $_->[1] . '.*' . $_->[2])); - - # $string, $msg - $r = nagios_die($_->[1], $_->[2]); - is($r->return_code, $_->[0], - sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - like($r, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - - # $string, $msg - $r = nagios_die($_->[2], $_->[1]); - is($r->return_code, $_->[0], - sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0])); - like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); - like($r, qr/$_->[1]\b.*\b$_->[2]$/, - sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], - $_->[1] . '.*' . $_->[2])); -} - -# Check that _use_die set to 1 will catch exceptions correctly -Nagios::Plugin::Functions::_fake_exit(0); -Nagios::Plugin::Functions::_use_die(1); -eval { nagios_die("Using die") }; -is( $@, "NAGIOS-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception"); diff --git a/t/Nagios-Plugin-Functions-02.t b/t/Nagios-Plugin-Functions-02.t deleted file mode 100644 index 1a9351b..0000000 --- a/t/Nagios-Plugin-Functions-02.t +++ /dev/null @@ -1,178 +0,0 @@ -# check_messages tests - -use strict; -use Test::More tests => 37; - -BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } - -my ($code, $message); - -# ------------------------------------------------------------------------- -# Check codes -my @codes = ( - [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ], - [ [], [ qw(Warning) ], WARNING ], - [ [], [], OK ], -); -my $i = 0; -for (@codes) { - $i++; - $code = check_messages( critical => $_->[0], warning => $_->[1] ); - is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}"); -} - -# ------------------------------------------------------------------------- -# Check messages -my %arrays = ( - critical => [ qw(A B C) ], - warning => [ qw(D E F) ], - ok => [ qw(G H I) ], -); -my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays; - -# critical, warning -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, -); -is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning) message is $message"); - -# critical, warning, ok -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - ok => $arrays{ok}, -); -is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning, ok) message is $message"); - -# critical, warning, $ok -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - ok => 'G H I', -); -is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{critical}, "(critical, warning, \$ok) message is $message"); - -# warning -($code, $message) = check_messages( - critical => [], warning => $arrays{warning}, -); -is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(warning) message is $message"); - -# warning, ok -($code, $message) = check_messages( - critical => [], warning => $arrays{warning}, ok => $arrays{ok}, -); -is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{warning}, "(warning, ok) message is $message"); - -# ok -($code, $message) = check_messages( - critical => [], warning => [], ok => $arrays{ok}, -); -is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{ok}, "(ok) message is $message"); - -# $ok -($code, $message) = check_messages( - critical => [], warning => [], ok => 'G H I', -); -is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}"); -is($message, $messages{ok}, "(\$ok) message is $message"); - -# ------------------------------------------------------------------------- -# explicit join -my $join = '+'; -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - join => $join, -); -is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message"); -$join = ''; -($code, $message) = check_messages( - critical => [], warning => $arrays{warning}, - join => $join, -); -is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message"); -$join = undef; -($code, $message) = check_messages( - critical => [], warning => [], ok => $arrays{ok}, - join => $join, -); -is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message"); - -# ------------------------------------------------------------------------- -# join_all messages -my $join_all = ' :: '; -my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } - qw(critical warning ok)); -my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } - qw(critical warning)); -my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) } - qw(warning ok)); - -# critical, warning, ok -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok}, - join_all => $join_all, -); -is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message"); - -# critical, warning, $ok -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I', - join_all => $join_all, -); -is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); - -# critical, warning -($code, $message) = check_messages( - critical => $arrays{critical}, warning => $arrays{warning}, - join_all => $join_all, -); -is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message"); - -# warning, ok -($code, $message) = check_messages( - critical => [], warning => $arrays{warning}, ok => $arrays{ok}, - join_all => $join_all, -); -is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message"); - -# warning, $ok -($code, $message) = check_messages( - critical => [], warning => $arrays{warning}, ok => 'G H I', - join_all => $join_all, -); -is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}"); -is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); - -# warning -($code, $message) = check_messages( - critical => [], warning => $arrays{warning}, - join_all => $join_all, -); -is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); -is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message"); - -# ------------------------------------------------------------------------- -# Error cases - -# Test failures without required fields -ok(! defined eval { ($code, $message) = check_messages() }, - "check_messages dies without message args"); - -ok(! defined eval { ($code, $message) = check_messages(warning => $arrays{warning}) }, - "check_messages dies without 'critical' message"); - -ok(! defined eval { ($code, $message) = check_messages(critical => $arrays{critical}) }, - "check_messages dies without 'warning' message"); - -ok(defined eval { ($code, $message) = check_messages(critical => $arrays{critical}, warning => $arrays{warning}) }, - "check_messages ok with 'critical' and 'warning' messages"); - diff --git a/t/Nagios-Plugin-Functions-03.t b/t/Nagios-Plugin-Functions-03.t deleted file mode 100644 index 3706e4c..0000000 --- a/t/Nagios-Plugin-Functions-03.t +++ /dev/null @@ -1,21 +0,0 @@ -# max_state tests - -use strict; -use Test::More tests => 8; - -BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } - -my $new_state = max_state( OK, WARNING ); - -is( $new_state, WARNING, "Moved up to WARNING" ); -is( max_state( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); - -$new_state = max_state( $new_state, CRITICAL ); -is( $new_state, CRITICAL, "Now at CRITICAL" ); -is( max_state( OK, OK ), OK, "This is OK" ); - -is( max_state( OK, UNKNOWN ), OK, "This is still OK, not UNKNOWN" ); - -is( max_state( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); - -is( max_state(), UNKNOWN, "Return UNKNOWN if no parameters" ); diff --git a/t/Nagios-Plugin-Functions-04.t b/t/Nagios-Plugin-Functions-04.t deleted file mode 100644 index d3ff05c..0000000 --- a/t/Nagios-Plugin-Functions-04.t +++ /dev/null @@ -1,21 +0,0 @@ -# max_state_alt tests - -use strict; -use Test::More tests => 8; - -BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } - -my $new_state = max_state_alt( OK, WARNING ); - -is( $new_state, WARNING, "Moved up to WARNING" ); -is( max_state_alt( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); - -$new_state = max_state_alt( $new_state, CRITICAL ); -is( $new_state, CRITICAL, "Now at CRITICAL" ); -is( max_state_alt( OK, OK ), OK, "This is OK" ); - -is( max_state_alt( OK, UNKNOWN ), UNKNOWN, "This is UNKNOWN" ); - -is( max_state_alt( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); - -is( max_state_alt(), UNKNOWN, "Return UNKNOWN if no parameters" ); diff --git a/t/Nagios-Plugin-Getopt-01.t b/t/Nagios-Plugin-Getopt-01.t deleted file mode 100644 index 7076d75..0000000 --- a/t/Nagios-Plugin-Getopt-01.t +++ /dev/null @@ -1,150 +0,0 @@ -# Nagios::Plugin::Getopt basic tests - -use strict; - -use Test::More tests => 76; -BEGIN { use_ok('Nagios::Plugin::Getopt') }; - -# Needed to get evals to work in testing -Nagios::Plugin::Functions::_use_die(1); - -my %PARAM = ( - version => '0.01', - url => 'http://www.openfusion.com.au/labs/nagios/', - blurb => 'This plugin tests various stuff.', - usage => "Usage: %s -H -w - -c ", - plugin => 'test_plugin', -); - -sub setup -{ - # Instantiate object - my $ng = Nagios::Plugin::Getopt->new(%PARAM); - ok($ng, 'constructor ok'); - - # Add argument - short form - arg spec, help text, default, required? - $ng->arg('warning|w=s' => - qq(-w, --warning=INTEGER\n Exit with WARNING status if less than INTEGER foobars are free), - 5); - - # Add argument - named version - $ng->arg( - spec => 'critical|c=i', - help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), - required => 1, - ); - - return $ng; -} - -my $ng; - -# Simple usage (short and long args) -@ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose); -$ng = setup; -$ng->getopts; -is($ng->warning, 3, 'warning set to 3'); -is($ng->critical, 10, 'critical set to 10'); -is($ng->timeout, 12, 'timeout set to 12'); - -# Check multiple verbose flags -@ARGV = qw(-w 3 --critical 10 -v -v -v); -$ng = setup; -$ng->getopts; -is ($ng->verbose, 3, "Verbose set to level 3"); - -@ARGV = qw(-w 3 --critical 10 --verbose --verbose --verbose); -$ng = setup; -$ng->getopts; -is ($ng->verbose, 3, "Verbose set to level 3 (longhand)"); - -# Missing args -@ARGV = qw(); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); -like($@, qr/Usage:/, 'usage message'); -like($@, qr/Missing arg/, 'missing arguments'); -is($ng->verbose, 0, 'verbose set to 0'); -# Missing critical -@ARGV = qw(-w0 -v); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); -like($@, qr/Usage:/, 'usage message'); -like($@, qr/Missing argument: critical/, 'missing argument: critical'); -unlike($@, qr/Missing argument: warning/, 'no missing argument: warning'); -is($ng->warning, 0, 'warning set to 0'); -is($ng->critical, undef, 'critical undef'); -is($ng->timeout, 15, 'timeout set to default'); -is($ng->verbose, 1, 'verbose set to true'); -# Missing warning -@ARGV = qw(--critical=27 --timeout 17 --verbose); -$ng = setup; -$ng->getopts; -is($ng->warning, 5, 'warning 5 (default)'); -is($ng->critical, 27, 'critical set to 27'); -is($ng->timeout, 17, 'timeout set to 17'); -is($ng->verbose, 1, 'verbose set to true'); - -# -? --usage -@ARGV = ( '-?' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on usage'); -like($@, qr/Usage:/, 'usage message'); -unlike($@, qr/Missing arg/, 'no missing arguments'); -@ARGV = ( '--usage' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on usage'); -like($@, qr/Usage:/, 'usage message'); -unlike($@, qr/Missing arg/, 'no missing arguments'); - -# -V --version -@ARGV = ( '-V' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on version'); -like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); -like($@, qr/$PARAM{version}/, 'version info includes version'); -like($@, qr/$PARAM{url}/, 'version info includes url'); -unlike($@, qr/Usage:/, 'no usage message'); -unlike($@, qr/Missing arg/, 'no missing arguments'); - -@ARGV = ( '--version' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on version'); -like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); -like($@, qr/$PARAM{version}/, 'version info includes version'); -like($@, qr/$PARAM{url}/, 'version info includes url'); -unlike($@, qr/Usage:/, 'no usage message'); -unlike($@, qr/Missing arg/, 'no missing arguments'); - -# -h --help -@ARGV = ( '-h' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on help'); -like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); -like($@, qr/$PARAM{version}/, 'help includes version'); -like($@, qr/$PARAM{url}/, 'help includes url'); -like($@, qr/General Public Licence/, 'help includes licence'); -like($@, qr/$PARAM{blurb}/, 'help includes blurb'); -like($@, qr/Usage:/, 'help includes usage message'); -like($@, qr/--version/, 'help includes default options 1'); -like($@, qr/--verbose/, 'help includes default options 2'); -like($@, qr/--warning/, 'help includes custom option 1'); -like($@, qr/--critical/, 'help includes custom option 2'); -unlike($@, qr/Missing arg/, 'no missing arguments'); - -@ARGV = ( '--help' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on help'); -like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); -like($@, qr/$PARAM{version}/, 'help includes version'); -like($@, qr/$PARAM{url}/, 'help includes url'); -like($@, qr/General Public Licence/, 'help includes licence'); -like($@, qr/$PARAM{blurb}/, 'help includes blurb'); -like($@, qr/Usage:/, 'help includes usage message'); -like($@, qr/--version/, 'help includes default options 1'); -like($@, qr/--verbose/, 'help includes default options 2'); -like($@, qr/--warning/, 'help includes custom option 1'); -like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); -unlike($@, qr/Missing arg/, 'no missing arguments'); - diff --git a/t/Nagios-Plugin-Getopt-02.t b/t/Nagios-Plugin-Getopt-02.t deleted file mode 100644 index f83b180..0000000 --- a/t/Nagios-Plugin-Getopt-02.t +++ /dev/null @@ -1,64 +0,0 @@ -# Nagios::Plugin::Getopt timeout tests - -use strict; - -use Test::More tests => 14; -BEGIN { use_ok('Nagios::Plugin::Getopt') }; - -# Needed to get evals to work in testing -Nagios::Plugin::Functions::_use_die(1); - -my %PARAM = ( - version => '0.01', - url => 'http://www.openfusion.com.au/labs/nagios/', - blurb => 'This plugin tests various stuff.', - usage => "Usage: %s -H -w - -c ", - plugin => 'test_plugin', - timeout => 18, -); - -sub setup -{ - # Instantiate object - my $ng = Nagios::Plugin::Getopt->new(%PARAM); - ok($ng, 'constructor ok'); - return $ng; -} - -my $ng; - -# No args -@ARGV = qw(); -$ng = setup(); -$ng->getopts; -is($ng->timeout, 18, 'default timeout set to 18'); - -# Check help message -@ARGV = ( '-h' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on help'); -like($@, qr/times out.*default: 18\b/i, 'help timeout changed to 18'); - -# Explicit timeout -@ARGV = qw(--timeout=25 --verbose); -$ng = setup(); -$ng->getopts; -is($ng->timeout, 25, 'timeout changed to 25'); - -# Explicit timeout -@ARGV = qw(-t10 --verbose); -$ng = setup(); -$ng->getopts; -is($ng->timeout, 10, 'timeout changed to 10'); - -# Short timeout, test default timeout handler -@ARGV = qw(-t2 --verbose); -$ng = setup(); -$ng->getopts; -is($ng->timeout, 2, 'timeout changed to 2'); -alarm($ng->timeout); -# Loop -ok(! defined eval { 1 while 1 }, 'loop timed out'); -like($@, qr/UNKNOWN\b.*\btimed out/, 'default timeout handler ok'); - diff --git a/t/Nagios-Plugin-Getopt-03.t b/t/Nagios-Plugin-Getopt-03.t deleted file mode 100644 index 560b782..0000000 --- a/t/Nagios-Plugin-Getopt-03.t +++ /dev/null @@ -1,108 +0,0 @@ -# Nagios::Plugin::Getopt --extra-opts tests - -use strict; -use File::Spec; -use File::Basename; -use IO::File; - -use Test::More qw(no_plan); -BEGIN { use_ok('Nagios::Plugin::Getopt') }; - -# Needed to get evals to work in testing -Nagios::Plugin::Functions::_use_die(1); - -my $tdir = 'npg03'; -if (! -d $tdir) { - my $ttdir = File::Spec->catdir('t', $tdir); - die "missing '$tdir' directory\n" unless -d $ttdir; - $tdir = $ttdir; -} - -# Load expected files -my %EXPECTED = (); -for my $efile (glob File::Spec->catfile($tdir, 'expected', '*')) { - my $fh = IO::File->new($efile, 'r') or die "Cannot open input file '$efile': $!"; - if (my $cmd = $fh->getline()) { # First line only! - chomp $cmd; - $cmd =~ s/^\s+//; - $cmd =~ s/\s+$//; - $EXPECTED{ basename($efile) } = $cmd; - } -} - -# Override NAGIOS_CONFIG_PATH to use our test plugins.ini file -$ENV{NAGIOS_CONFIG_PATH} = "/random/bogus/path:$tdir"; - -my %PARAM = ( - version => '0.01', - blurb => 'This plugin tests various stuff.', - usage => "Usage: %s -H -w - -c ", -); - -sub ng_setup -{ - my $arg = shift; - - # Instantiate object - my $ng = Nagios::Plugin::Getopt->new(%PARAM); - - if (ref $arg eq 'ARRAY' && @$arg) { - $ng->arg(%$_) foreach @$arg; - } - - return $ng; -} - -# Setup our Nagios::Plugin::Getopt object -my $ng; -my $arg = [ - { spec => 'S', help => '-S' }, - { spec => 'H=s', help => '-H' }, - { spec => 'p=s@', help => '-p' }, - { spec => 'path=s@', help => '--path' }, - { spec => 'username|u=s', help => '--username' }, - { spec => 'password=s', help => '--password' }, - { spec => 'critical=s', help => '--critical' }, - { spec => 'warning=s', help => '--warning' }, - { spec => 'expect=s', help => '--expect' }, - { spec => 'units=s', help => '--units' }, -]; - -#my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); -#my %SKIP = map { $_ => 1 } qw(06_singlechar2); -my %SKIP = (); - -# Process all test cases in $tdir/input -my $glob = $ARGV[0] || '*'; -for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) { - $ng = ng_setup($arg); - - my $fh = IO::File->new($infile, 'r') or die "Cannot open input file '$infile': $!"; - $infile = basename($infile); - - if (my $cmd = $fh->getline()) { # First line only! - $cmd =~ s/^\s+//; - my ($plugin, @args) = split /\s+/, $cmd; - - # Fake out the plugin name - $ng->{_attr}->{plugin} = $plugin; - - # Parse the options - SKIP: { - skip "Skipping ..." if $SKIP{$infile}; - - @ARGV = @args; - eval { $ng->getopts }; - if ($@) { - chomp $@; - ok($infile =~ m/_(dies?|catch)$/, "$infile ($@)"); - is($@, $EXPECTED{$infile}, $infile) if ($infile =~ m/_catch$/); - } - else { - is($plugin . ' ' . $ng->_cmdline, $EXPECTED{$infile}, $infile); - } - } - } -} - diff --git a/t/Nagios-Plugin-Getopt-04.t b/t/Nagios-Plugin-Getopt-04.t deleted file mode 100644 index 115f2a2..0000000 --- a/t/Nagios-Plugin-Getopt-04.t +++ /dev/null @@ -1,98 +0,0 @@ -# Nagios::Plugin::Getopt spec-to-help generation tests - -use strict; - -use Test::More tests => 11; -BEGIN { use_ok('Nagios::Plugin::Getopt') }; - -# Needed to get evals to work in testing -Nagios::Plugin::Functions::_use_die(1); - -my %PARAM = ( - version => '0.01', - usage => "Don't use this plugin!", -); - -sub setup -{ - # Instantiate object - my $ng = Nagios::Plugin::Getopt->new(%PARAM); - ok($ng, 'constructor ok'); - - # Positional args, no short arguments, INTEGER - $ng->arg('warning=i' => - qq(Exit with WARNING status if less than INTEGER foobars are free), - 5); - - # Named args, long + short arguments, INTEGER - $ng->arg( - spec => 'critical|c=i', - help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), - required => 1, - ); - - # Named args, multiple short arguments, STRING, default expansion - $ng->arg( - spec => 'x|y|z=s', - help => qq(Foobar. Default: %s), - default => "XYZ", - ); - - # Named args, multiple mixed, no label - $ng->arg( - spec => 'long|longer|longest|l', - help => qq(Long format), - ); - - # Named args, long + short, explicit label - $ng->arg( - spec => 'hostname|H=s', - label => 'ADDRESS', - help => qq(Hostname), - ); - - # Positional args, long only, explicit label - $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR'); - - # Multiline help test, named args - $ng->arg( - spec => 'disk=s', - label => [ qw(BYTES PERCENT%), undef ], - help => [ - qq(Disk limit in BYTES), - qq(Disk limit in PERCENT), - qq(Disk limit in FOOBARS (Default: %s)), - ], - default => 1024, - ); - - # Multiline help test, positional args - $ng->arg( - 'limit=s', - [ - qq(Limit in BYTES), - qq(Limit in PERCENT), - ], - undef, - undef, - [ undef, 'PERCENT%' ], - ); - - return $ng; -} - -my $ng; - -@ARGV = ( '--help' ); -$ng = setup; -ok(! defined eval { $ng->getopts }, 'getopts died on help'); -like($@, qr/\n --warning=INTEGER/, 'warning ok'); -like($@, qr/\n -c, --critical=INTEGER/, 'critical ok'); -like($@, qr/\n -x, -y, -z=STRING\n Foobar. Default: XYZ\n/, 'x|y|z ok'); -like($@, qr/\n -l, --long, --longer, --longest\n Long format\n/, 'long ok'); -like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); -like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); -like($@, qr/\n --disk=BYTES\n Disk limit in BYTES\n --disk=PERCENT%\n Disk limit in PERCENT\n --disk=STRING\n Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok'); -like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); -#print $@; - diff --git a/t/Nagios-Plugin-Performance-02.t b/t/Nagios-Plugin-Performance-02.t deleted file mode 100644 index c0c5a71..0000000 --- a/t/Nagios-Plugin-Performance-02.t +++ /dev/null @@ -1,13 +0,0 @@ - -use strict; -use Test::More tests => 3; -use_ok("Nagios::Plugin::Performance", use_die => 1); - -eval { Nagios::Plugin::Functions::nagios_die("Testing") }; -is( $@, "NAGIOS-PLUGIN-PERFORMANCE-02 UNKNOWN - Testing\n", "use_die correctly set on import"); - - -use_ok("Nagios::Plugin::Performance"); -eval { Nagios::Plugin::Functions::nagios_die("Test OK exit", 0) }; - -fail("Should not get here if code works correctly because prior nagios_die should have exited"); diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t deleted file mode 100644 index 8dce336..0000000 --- a/t/Nagios-Plugin-Performance.t +++ /dev/null @@ -1,357 +0,0 @@ - -use warnings; -use strict; -use Test::More; -use Nagios::Plugin::Functions; -Nagios::Plugin::Functions::_fake_exit(1); - - -my (@p, $p); -my @test = ( - { - perfoutput => "/=382MB;15264;15269;0;32768", label => '/', rrdlabel => 'root', value => 382, uom => 'MB', warning => 15264, critical => 15269, min => 0, max => 32768, clean_label => "root", - }, { - perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var", - }, { - perfoutput => '/var/long@:-/filesystem/name/and/bad/chars=218MB;9443;9448', label => '/var/long@:-/filesystem/name/and/bad/chars', rrdlabel => 'var_long____filesys', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => 'var_long____filesystem_name_and_bad_chars', - }, { - perfoutput => "'page file'=36%;80;90;", - expected_perfoutput => "'page file'=36%;80;90", - label => 'page file', - rrdlabel => 'page_file', - value => '36', - uom => '%', - warning => 80, - critical => 90, - min => undef, - max => undef, - clean_label => 'page_file', - }, { - perfoutput => "'data'=5;;;;", - expected_perfoutput => "data=5;;", - label => 'data', - rrdlabel => 'data', - value => 5, - uom => "", - warning => undef, - critical => undef, - min => undef, - max => undef, - clean_label => 'data', - }, -); - -plan tests => (11 * scalar @test) + 176; - -use_ok('Nagios::Plugin::Performance'); -diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; - -# Round-trip tests -for my $t (@test) { - # Parse to components - ($p) = Nagios::Plugin::Performance->parse_perfstring($t->{perfoutput}); - is ($p->value, $t->{value}, "value okay $t->{value}"); - is ($p->label, $t->{label}, "label okay $t->{label}"); - is ($p->uom, $t->{uom}, "uom okay $t->{uom}"); - - # Construct from components - my @construct = qw(label value uom warning critical min max); - $p = Nagios::Plugin::Performance->new(map { $_ => $t->{$_} } @construct); - my $expected_perfoutput = $t->{perfoutput}; - if (exists $t->{expected_perfoutput}) { - $expected_perfoutput = $t->{expected_perfoutput}; - }; - is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)"); - # Check threshold accessor - foreach my $type (qw(warning critical)) { - if (! defined $t->{$type}) { - isnt( $p->threshold->$type->is_set, "threshold $type not set"); - } else { - is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})"); - } - } - is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay"); - is($p->clean_label, $t->{clean_label}, "clean_label okay" ); - - # Construct using threshold - @construct = qw(label value uom min max); - $p = Nagios::Plugin::Performance->new( - map({ $_ => $t->{$_} } @construct), - threshold => Nagios::Plugin::Threshold->set_thresholds(warning => $t->{warning}, critical => $t->{critical}), - ); - is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)"); - # Check warning/critical accessors - foreach my $type (qw(warning critical)) { - if (! defined $t->{$type}) { - isnt( $p->threshold->$type->is_set, "threshold $type not set"); - } else { - is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})"); - } - } -} - - -# Test multiple parse_perfstrings -@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]->rrdlabel, 'eq', "root", "rrd 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 undef"); -ok(! defined $p[0]->max, "max undef"); - -cmp_ok( $p[1]->label, 'eq', "/var", "label okay"); -cmp_ok( $p[1]->rrdlabel, 'eq', "var", "rrd 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"); - -@p = Nagios::Plugin::Performance->parse_perfstring("rubbish"); -ok( ! @p, "Errors correctly"); -ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); - - - -# Check 1 bad with 1 good format output -@p = Nagios::Plugin::Performance->parse_perfstring("rta=&391ms;100,200;500,034;0; pl=0%;20;60 "); -is( scalar @p, 1, "One bad piece of data - only one returned" ); -is( $p[0]->label, "pl", "label okay for different numeric"); -is( $p[0]->value, 0, "value okay"); -is( $p[0]->uom, "%", "uom okay"); -ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); -is( $p[0]->threshold->warning, "20", "warn okay"); -is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[0]->threshold->critical, "60", "warn okay"); - -# Same as above, but order swapped -@p = Nagios::Plugin::Performance->parse_perfstring(" pl=0%;20;60 rta=&391ms;100,200;500,034;0; "); -is( scalar @p, 1, "One bad piece of data - only one returned" ); -is( $p[0]->label, "pl", "label okay for different numeric"); -is( $p[0]->value, 0, "value okay"); -is( $p[0]->uom, "%", "uom okay"); -ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); -is( $p[0]->threshold->warning, "20", "warn okay"); -is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[0]->threshold->critical, "60", "warn okay"); - - - - -@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"); - ok( $p[0]->threshold->warning->is_set, "warn okay"); - ok( $p[0]->threshold->critical->is_set, "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( "users=4;20;50;0\n" ); - ok( @p, "parse correctly with linefeed at end (nagiosgraph)"); - -@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( ! $p[1]->threshold->warning->is_set, "warn okay"); - ok( ! $p[1]->threshold->critical->is_set, "crit okay"); - -# Edge cases -@p = Nagios::Plugin::Performance->parse_perfstring("/home/a-m=0;0;0 shared-folder:big=20 12345678901234567890=20"); -cmp_ok( $p[0]->rrdlabel, "eq", "home_a_m", "changing / to _"); - ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); -cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); -cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); - -# turn off fake_exit and enable use_die so we pick up on errors via nagios_die -Nagios::Plugin::Functions::_use_die(1); -Nagios::Plugin::Functions::_fake_exit(0); - -@p = Nagios::Plugin::Performance->parse_perfstring("time=0.002722s;0.000000;0.000000;0.000000;10.000000"); -cmp_ok( $p[0]->label, "eq", "time", "label okay"); -cmp_ok( $p[0]->value, "eq", "0.002722", "value okay"); -cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); - ok( defined $p[0]->threshold->warning->is_set, "Warning range has been set"); - ok( defined $p[0]->threshold->critical->is_set, "Critical range has been set"); -# The two below used to be cmp_ok, but Test::More 0.86 appears to have a problem with a stringification -# of 0. See http://rt.cpan.org/Ticket/Display.html?id=41109 -# We need to force stringification for test. See RT 57709 -is( $p[0]->threshold->warning."", "0", "warn okay"); -is( $p[0]->threshold->critical."", "0", "crit okay"); - -@p = Nagios::Plugin::Performance->parse_perfstring("pct_used=73.7%;90;95"); -cmp_ok( $p[0]->label, "eq", "pct_used", "label okay"); -cmp_ok( $p[0]->value, "eq", "73.7", "value okay"); -cmp_ok( $p[0]->uom, "eq", "%", "uom okay"); - ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); - ok( defined eval { $p[0]->threshold->critical->is_set }, "Critical range has been set"); -cmp_ok( $p[0]->threshold->warning, 'eq', "90", "warn okay"); -cmp_ok( $p[0]->threshold->critical, 'eq', "95", "crit okay"); - -# Check ranges are parsed correctly -@p = Nagios::Plugin::Performance->parse_perfstring("availability=93.8%;90:99;"); -is( $p[0]->label, "availability", "label okay"); -is( $p[0]->value, "93.8", "value okay"); -is( $p[0]->uom, "%", "uom okay"); -ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); -is( $p[0]->threshold->critical->is_set, 0, "Critical range has not been set"); -is( $p[0]->threshold->warning, "90:99", "warn okay"); - -# Check that negative values are parsed correctly in value and ranges -@p = Nagios::Plugin::Performance->parse_perfstring("offset=-0.004476s;-60.000000:-5;-120.000000:-3;"); -is( $p[0]->label, "offset", "label okay"); -is( $p[0]->value, "-0.004476", "value okay"); -is( $p[0]->uom, "s", "uom okay"); -ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); -ok( defined eval { $p[0]->threshold->critical->is_set }, "Critical range has been set"); -is( $p[0]->threshold->warning, "-60:-5", "warn okay"); -is( $p[0]->threshold->critical, "-120:-3", "crit okay"); - -# Check infinity values are okay -@p = Nagios::Plugin::Performance->parse_perfstring("salary=52GBP;~:23.5;45.2:"); -is( $p[0]->label, "salary", "label okay"); -is( $p[0]->value, "52", "value okay"); -is( $p[0]->uom, "GBP", "uom okay"); -ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); -is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[0]->threshold->warning, "~:23.5", "warn okay"); -is( $p[0]->threshold->critical, "45.2:", "warn okay"); - -# Check scientific notation -@p = Nagios::Plugin::Performance->parse_perfstring("offset=1.120567322e-05"); -is( $p[0]->label, "offset", "label okay for scientific notation"); -is( $p[0]->value, 1.120567322e-05, "value okay"); -is( $p[0]->uom, "", "uom okay"); -ok( ! $p[0]->threshold->warning->is_set, "Warning range has not been set"); -ok( ! $p[0]->threshold->critical->is_set, "Critical range has not been set"); - - -# Check scientific notation with warnings and criticals -@p = Nagios::Plugin::Performance->parse_perfstring("offset=-1.120567322e-05unit;-1.1e-05:1.0e-03;4.3e+02:4.3e+25"); -is( $p[0]->label, "offset", "label okay for scientific notation in warnings and criticals"); -is( $p[0]->value, -1.120567322e-05, "value okay"); -is( $p[0]->uom, "unit", "uom okay"); -ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); -is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay"); -is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay"); - - - -# Check different collation with commas instead of periods -@p = Nagios::Plugin::Performance->parse_perfstring("rta=1,391ms;100,200;500,034;0; pl=0%;20;60;;"); -is( $p[0]->label, "rta", "label okay for numeric with commas instead of periods"); -is( $p[0]->value, 1.391, "value okay"); -is( $p[0]->uom, "ms", "uom okay"); -ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); -is( $p[0]->threshold->warning, "100.2", "warn okay"); -is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[0]->threshold->critical, "500.034", "warn okay"); -is( $p[1]->label, "pl", "label okay for different numeric"); -is( $p[1]->value, 0, "value okay"); -is( $p[1]->uom, "%", "uom okay"); -ok( $p[1]->threshold->warning->is_set, "Warning range has been set"); -is( $p[1]->threshold->warning, "20", "warn okay"); -is( $p[1]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[1]->threshold->critical, "60", "warn okay"); - - -# Another set of comma separated stuff -@p = Nagios::Plugin::Performance->parse_perfstring("offset=-0,023545s;60,000000;120,000000;"); -is( $p[0]->label, "offset", "label okay for numeric with commas instead of periods"); -is( $p[0]->value, -0.023545, "value okay"); -is( $p[0]->uom, "s", "uom okay"); -is( $p[0]->threshold->warning->is_set, 1, "Warning range has been set"); -is( $p[0]->threshold->warning, 60, "warn okay"); -is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); -is( $p[0]->threshold->critical, 120, "warn okay"); - -# Some values with funny commas -@p = Nagios::Plugin::Performance->parse_perfstring("time=1800,600,300,0,3600 other=45.6"); -is( $p[0]->label, "other", "Ignored time=1800,600,300,0,3600, but allowed other=45.6"); -is( $p[0]->value, 45.6, "value okay"); -is( $p[0]->uom, "", "uom okay"); - - -# Test labels with spaces (returned by nsclient++) -@p = Nagios::Plugin::Performance->parse_perfstring("'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 'D:\ Label: Serial Number XA22aa2e'=8015MB;16388;18436;1;2048"); -is( $p[0]->label, "C:\ Label: Serial Number bc22aa2e"); -is( $p[0]->rrdlabel, "C__Label___Serial_N"); -is( $p[0]->value, 8015, "value okay"); -is( $p[0]->uom, "MB", "uom okay"); -is( $p[0]->threshold->warning->end, 16387, "warn okay"); -is( $p[0]->threshold->critical->end, 18435, "crit okay"); -is( $p[0]->min, 0, "min ok"); -is( $p[0]->max, 20484, "max ok"); - -is( $p[1]->label, "D:\ Label: Serial Number XA22aa2e", "label okay"); -is( $p[1]->rrdlabel, "D__Label__Serial_Nu", "rrd label okay"); -is( $p[1]->value, 8015, "value okay"); -is( $p[1]->uom, "MB", "uom okay"); -is( $p[1]->threshold->warning->end, 16388, "warn okay"); -is( $p[1]->threshold->critical->end, 18436, "crit okay"); -is( $p[1]->min, 1, "min ok"); -is( $p[1]->max, 2048, "max ok"); - - -# Mix labels with and without quotes -@p = Nagios::Plugin::Performance->parse_perfstring(" short=4 'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 end=5 "); -is( $p[0]->label, "short" ); -is( $p[0]->rrdlabel, "short"); -is( $p[0]->value, 4, "value okay"); -is( $p[0]->uom, "", "uom okay"); -isnt( $p[0]->threshold->warning->is_set, "warn okay"); -isnt( $p[0]->threshold->critical->is_set, "crit okay"); -is( $p[0]->min, undef, "min ok"); -is( $p[0]->max, undef, "max ok"); - -is( $p[1]->label, "C:\ Label: Serial Number bc22aa2e", "label okay"); -is( $p[1]->rrdlabel, "C__Label___Serial_N", "rrd label okay"); -is( $p[1]->value, 8015, "value okay"); -is( $p[1]->uom, "MB", "uom okay"); -is( $p[1]->threshold->warning->end, 16387, "warn okay"); -is( $p[1]->threshold->critical->end, 18435, "crit okay"); -is( $p[1]->min, 0, "min ok"); -is( $p[1]->max, 20484, "max ok"); - -is( $p[2]->label, "end" ); -is( $p[2]->rrdlabel, "end" ); -is( $p[2]->value, 5, "value okay"); -is( $p[2]->uom, "", "uom okay"); -isnt( $p[2]->threshold->warning->is_set, "warn okay"); -isnt( $p[2]->threshold->critical->is_set, 18436, "crit okay"); -is( $p[2]->min, undef, "min ok"); -is( $p[2]->max, undef, "max ok"); - - -@p = Nagios::Plugin::Performance->parse_perfstring("processes=9;WKFLSV32.exe;9="); -is_deeply( \@p, [], "Fails parsing correctly"); - -# add_perfdata tests in t/Nagios-Plugin-01.t diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t deleted file mode 100644 index 6fe080c..0000000 --- a/t/Nagios-Plugin-Range.t +++ /dev/null @@ -1,243 +0,0 @@ - -use strict; -#use Test::More qw(no_plan); -use Test::More tests => 151; - -BEGIN { - use_ok('Nagios::Plugin::Range'); - # Silence warnings unless TEST_VERBOSE is set - $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; -}; - -diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n" if $ENV{TEST_VERBOSE}; - -my $r; - -diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:" if $ENV{TEST_VERBOSE}; - -foreach (qw( - : - 1:~ - foo - 1-10 - 10:~ - 1-10:2.4 - -), '1,10' # avoid warning about using , inside qw() -) { - $r =Nagios::Plugin::Range->parse_range_string($_); - is $r, undef, "'$_' should not be a valid range" ; -} - - -diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE}; -$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"); - -my $expected = { - -1 => 1, # 1 means it raises an alert because it's OUTSIDE the range - 0 => 0, # 0 means it's inside the range (no alert) - 4 => 0, - 6 => 0, - 6.1 => 1, - 79.999999 => 1, -}; - -sub test_expected { - my $r = shift; - my $expected = shift; - foreach (sort {$a<=>$b} keys %$expected) { - is $r->check_range($_), $expected->{$_}, - " $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")"; - } -} - -test_expected( $r, $expected ); - -diag "range : -7..23, inclusive" if $ENV{TEST_VERBOSE}; -$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"); - -$expected = { - -23 => 1, - -7 => 0, - -1 => 0, - 0 => 0, - 4 => 0, - 23 => 0, - 23.1 => 1, - 79.999999 => 1, -}; -test_expected( $r, $expected ); - - -diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE}; -$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"); -$expected = { - -1 => 1, - 0 => 0, - 4 => 0, - 5.75 => 0, - 5.7501 => 1, - 6 => 1, - 6.1 => 1, - 79.999999 => 1, -}; -test_expected( $r, $expected ); - - - -diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE}; -$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"); -$expected = { - -1001341 => 0, - -96 => 0, - -95.999 => 0, - -95.99 => 0, - -95.989 => 1, - -95 => 1, - 0 => 1, - 5.7501 => 1, - 79.999999 => 1, -}; -test_expected( $r, $expected ); - -diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE}; -test_expected( $r, $expected ); -$r = Nagios::Plugin::Range->parse_range_string("10:"); -ok( defined $r, "'10:' is valid range"); -cmp_ok( $r->start, '==', 10, "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', "10:", "Stringification back to original"); -$expected = { - -95.999 => 1, - -1 => 1, - 0 => 1, - 9.91 => 1, - 10 => 0, - 11.1 => 0, - 123456789012346 => 0, -}; -test_expected( $r, $expected ); - - - -diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE}; -test_expected( $r, $expected ); -$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"); -$expected = { - -95.999 => 1, - -1 => 1, - 0 => 1, - # The fractional values needs to be quoted, otherwise the hash rounds it up to ..345 - # and there is one less test run. - # I think some newer versions of perl use a higher precision value for the hash key. - # This doesn't appear to affect the actual plugin though - "123456789012344.91" => 1, - 123456789012345 => 0, - "123456789012345.61" => 0, - 123456789012346 => 0, -}; -test_expected( $r, $expected ); - - -diag "range: <= zero " if $ENV{TEST_VERBOSE}; -$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"); -$expected = { - -123456789012344.91 => 0, - -1 => 0, - 0 => 0, - .001 => 1, - 123456789012345 => 1, -}; -test_expected( $r, $expected ); - - -diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE}; -$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"); -$expected = { - -134151 => 0, - -1 => 0, - 0 => 1, - .001 => 1, - 657.8210567 => 1, - 657.9 => 0, - 123456789012345 => 0, -}; -test_expected( $r, $expected ); - - -diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE}; -$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"); -$expected = { - -1 => 1, - 0 => 1, - .5 => 1, - 1 => 0, - 1.001 => 1, - 5.2 => 1, -}; -test_expected( $r, $expected ); - - -$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 deleted file mode 100644 index 78d2189..0000000 --- a/t/Nagios-Plugin-Threshold.t +++ /dev/null @@ -1,221 +0,0 @@ - -use strict; -use Test::More tests => 93; -BEGIN { - use_ok('Nagios::Plugin::Threshold'); - use_ok('Nagios::Plugin::Functions', ':all' ); - # Silence warnings unless TEST_VERBOSE is set - $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; -} - -diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n" - if $ENV{TEST_VERBOSE}; - -Nagios::Plugin::Functions::_fake_exit(1); - -my $t; - -$t = Nagios::Plugin::Threshold->set_thresholds(warning => undef, critical => undef); -ok( defined $t, "two undefs" ); -ok( ! $t->warning->is_set, "warning not set" ); -ok( ! $t->critical->is_set, "critical not set" ); - -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "", critical => ""); -ok( defined $t, "two empty strings" ); -ok( ! $t->warning->is_set, "warning not set" ); -ok( ! $t->critical->is_set, "critical not set" ); - -diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; -my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); -ok( defined $t, "Threshold ('', '80') set"); -ok( ! $t->warning->is_set, "Warning not set"); -cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly"); -cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly"); -ok ! $t->critical->end_infinity, "not forever"; - -my $expected = { qw( - -1 CRITICAL - 4 OK - 79.999999 OK - 80 OK - 80.1 CRITICAL - 102321 CRITICAL -) }; - -sub test_expected_statuses { - my $t = shift; - my $expected = shift; - my $debug = shift; - - foreach (sort {$a<=>$b} keys %$expected) { - is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; - if ($debug) { - diag "val = $_; critical check = ".$t->critical->check_range($_). - "; warning check = ".$t->warning->check_range($_); - } - } - use Data::Dumper; - diag "thresh dump: ". Dumper $t if $debug; -} -test_expected_statuses( $t, $expected ); - -# GMC: this test seems bogus to me - either we've died, in which case internal -# state is undefined (and untestable!), or we should be returning a non-fatal error -if (0) { - diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; - eval { $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( ! $t->critical->is_set, "Critical not set"); -} - -# GC: same as previous test, except critical is undef instead of '' -diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef); -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( ! $t->critical->is_set, "Critical not set"); - -$expected = { qw( - -1 WARNING - 4 WARNING - 4.999999 WARNING - 5 OK - 14.21 OK - 33 OK - 33.01 WARNING - 10231 WARNING -) }; -test_expected_statuses( $t, $expected ); - -diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE}; -$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"); -ok $t->critical->start_infinity, "Critical starts at negative infinity"; - -$expected = { qw( - -1 OK - 4 OK - 29.999999 OK - 30 OK - 30.1 WARNING - 50.90 WARNING - 59.9 WARNING - 60 WARNING - 60.00001 CRITICAL - 10231 CRITICAL -) }; -test_expected_statuses( $t, $expected ); - -# "I'm going to die homeless, penniless, and 30 pounds overweight." -# "...and that's...okay." - -# TODO: figure out why this doesn't work and fix the test. -goto SKIP_DEATH; -diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; -diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; -Nagios::Plugin::Functions->print_on_die(1); -Nagios::Plugin::Functions->exit_on_die(1); - -dies_ok( sub { - $t = Nagios::Plugin::Threshold->set_thresholds( - warning => "total", - critical => "rubbish" - ) - }, "bad thresholds cause death" -); -Nagios::Plugin::Functions->print_on_die(0); -Nagios::Plugin::Functions->exit_on_die(0); -SKIP_DEATH: - - -diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE}; -$t = Nagios::Plugin::Threshold->set_thresholds( critical => "~:25" ); -ok( defined $t, "Threshold ('', '~:25') set (".$t->critical.")" ); -ok( ! $t->warning->is_set, "Warning not set"); -cmp_ok( $t->critical->end, '==',25, "Critical end set"); -ok $t->critical->start_infinity, "Critical starts at negative infinity"; - -$expected = { qw( - -1 OK - 4 OK - 10 OK - 14.21 OK - 25 OK - 25.01 CRITICAL - 31001 CRITICAL -) }; -test_expected_statuses( $t, $expected); - -diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE}; -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25"); -ok( defined $t, "Threshold ('10:25', '~:25') set"); -cmp_ok( $t->warning->start, '==', 10, "Warning start set"); -cmp_ok( $t->warning->end, '==', 25, "Warning end set"); -cmp_ok( $t->critical->end, '==', 25, "Critical end set"); - -$expected = { qw( - -1 WARNING - 4 WARNING - 9.999999 WARNING - 10 OK - 14.21 OK - 25 OK - 25.01 CRITICAL - 31001 CRITICAL -) }; -test_expected_statuses( $t, $expected ); - - -diag "warn if INSIDE {10..25} , critical if < 10 " if $ENV{TEST_VERBOSE}; -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:"); -$expected = { qw( - -1 CRITICAL - 4 CRITICAL - 9.999999 CRITICAL - 10 WARNING - 14.21 WARNING - 25 WARNING - 25.01 OK - 31001 OK -) }; -test_expected_statuses( $t, $expected ); - - -# GMC: as of 0.16, set_thresholds can also be called as a mutator -diag "threshold mutator: warn if more than 30; critical if > 60" - if $ENV{TEST_VERBOSE}; -my $t1 = $t; -$t->set_thresholds(warning => "0:45", critical => "0:90"); -is($t1, $t, "same threshold object after \$t->set_thresholds"); -ok( defined $t, "Threshold ('0:45', '0:90') set"); -is( $t->warning->start, 0, "Warning start ok"); -is( $t->warning->end, 45, "Warning end ok"); -is( $t->critical->start, 0, "Critical start ok"); -is( $t->critical->end, 90, "Critical end ok"); - - -# Also as of 0.16, accepts N::P::Range objects as arguments -my $warning = Nagios::Plugin::Range->parse_range_string("50"); -my $critical = Nagios::Plugin::Range->parse_range_string("70:90"); -$t = Nagios::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical); -ok( defined $t, "Threshold from ranges ('50', '70:90') set"); -is( $t->warning->start, 0, "Warning start ok"); -is( $t->warning->end, 50, "Warning end ok"); -is( $t->critical->start, 70, "Critical start ok"); -is( $t->critical->end, 90, "Critical end ok"); - -$critical = Nagios::Plugin::Range->parse_range_string("90:"); -$t->set_thresholds(warning => "~:20", critical => $critical); -ok( defined $t, "Threshold from string + range ('~:20', '90:') set"); -ok( $t->warning->start_infinity, "Warning start ok (infinity)"); -is( $t->warning->end, 20, "Warning end ok"); -is( $t->critical->start, 90, "Critical start ok"); -ok( $t->critical->end_infinity, "Critical end ok (infinity)"); - - -ok 1, "sweet, made it to the end."; diff --git a/t/check_stuff.pl b/t/check_stuff.pl index 112a18a..4fc125c 100755 --- a/t/check_stuff.pl +++ b/t/check_stuff.pl @@ -2,21 +2,21 @@ ### check_stuff.pl -# an example Nagios plugin using the Nagios::Plugin modules. +# an example Monitoring plugin using the Monitoring::Plugin modules. # Originally by Nathan Vonnahme, n8v at users dot sourceforge # dot net, July 19 2006 # Please modify to your heart's content and use as the basis for all -# the really cool Nagios monitoring scripts you're going to create. -# You rock. +# the really cool monitoring scripts you're going to create. +# You rock. ############################################################################## # prologue use strict; use warnings; -use Nagios::Plugin ; +use Monitoring::Plugin; use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); $VERSION = '1.0'; @@ -28,18 +28,18 @@ $PROGNAME = basename($0); ############################################################################## # define and get the command line options. -# see the command line option guidelines at -# http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS +# see the command line option guidelines at +# https://www.monitoring-plugins.org/doc/guidelines.html#PLUGOPTIONS -# Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory) -my $p = Nagios::Plugin->new( +# Instantiate Monitoring::Plugin object (the 'usage' parameter is mandatory) +my $p = Monitoring::Plugin->new( usage => "Usage: %s [ -v|--verbose ] [-H ] [-t ] - [ -c|--critical= ] - [ -w|--warning= ] + [ -c|--critical= ] + [ -w|--warning= ] [ -r|--result = ]", version => $VERSION, - blurb => 'This plugin is an example of a Nagios plugin written in Perl using the Nagios::Plugin modules. It will generate a random integer between 1 and 20 (though you can specify the number with the -n option for testing), and will output OK, WARNING or CRITICAL if the resulting number is outside the specified thresholds.', + blurb => 'This plugin is an example of a monitoring plugin written in Perl using the Monitoring::Plugin modules. It will generate a random integer between 1 and 20 (though you can specify the number with the -n option for testing), and will output OK, WARNING or CRITICAL if the resulting number is outside the specified thresholds.', extra => " @@ -47,11 +47,7 @@ THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max' (or 'max'). If specified '\@min:max', a warning status will be generated if the count *is* inside the specified range. -See more threshold examples at http - : // nagiosplug - . sourceforge - . net / developer-guidelines - . html #THRESHOLDFORMAT +See more threshold examples at https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT Examples: @@ -75,7 +71,7 @@ See more threshold examples at http $p->add_arg( spec => 'warning|w=s', - help => + help => qq{-w, --warning=INTEGER:INTEGER Minimum and maximum number of allowable result, outside of which a warning will be generated. If omitted, no warning is generated.}, @@ -86,7 +82,7 @@ qq{-w, --warning=INTEGER:INTEGER $p->add_arg( spec => 'critical|c=s', - help => + help => qq{-c, --critical=INTEGER:INTEGER Minimum and maximum number of the generated result, outside of which a critical will be generated. }, @@ -94,7 +90,7 @@ qq{-c, --critical=INTEGER:INTEGER $p->add_arg( spec => 'result|r=f', - help => + help => qq{-r, --result=INTEGER Specify the result on the command line rather than generating a random number. For testing.}, @@ -106,11 +102,11 @@ $p->getopts; # perform sanity checking on command line options if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) { - $p->nagios_die( " invalid number supplied for the -r option " ); + $p->plugin_die( " invalid number supplied for the -r option " ); } unless ( defined $p->opts->warning || defined $p->opts->critical ) { - $p->nagios_die( " you didn't supply a threshold argument " ); + $p->plugin_die( " you didn't supply a threshold argument " ); } @@ -135,8 +131,7 @@ else { ############################################################################## # check the result against the defined warning and critical thresholds, # output the result and exit -$p->nagios_exit( - return_code => $p->check_threshold($result), - message => " sample result was $result" +$p->plugin_exit( + return_code => $p->check_threshold($result), + message => " sample result was $result" ); - diff --git a/t/check_stuff.t b/t/check_stuff.t index 2815b76..6a1d845 100755 --- a/t/check_stuff.t +++ b/t/check_stuff.t @@ -1,5 +1,5 @@ #!/usr/local/bin/perl -# +# use strict; use warnings; #use Test::More qw(no_plan); use Test::More tests => 14; @@ -10,7 +10,7 @@ $s = "$^X -Ilib $s"; my $n = 'STUFF'; -# Nagios status strings and exit codes +# Monitoring status strings and exit codes my %e = qw( OK 0 WARNING 1 @@ -58,9 +58,3 @@ sub test_expected { like $r, qr/^$n $expected->{$_}/i, "looks $expected->{$_} with $_"; } } - - - - - - diff --git a/t/npg03/README b/t/npg03/README index 3dbdaf8..541ece7 100644 --- a/t/npg03/README +++ b/t/npg03/README @@ -1,15 +1,15 @@ -Nagios-Plugin-Getopt-03.t automatically tests all cases defined in -the 'input' directory and expects the output to match the -corresponding file in the 'expected' directory. To define a new test -case, just create a new file in the 'input' directory containing the -input command line, and a corresponding file in the 'expected' -directory containing what you think the expanded command line should +Monitoring-Plugin-Getopt-03.t automatically tests all cases defined in +the 'input' directory and expects the output to match the +corresponding file in the 'expected' directory. To define a new test +case, just create a new file in the 'input' directory containing the +input command line, and a corresponding file in the 'expected' +directory containing what you think the expanded command line should be. Note that this expansion is normalised as follows: - command line arguments are reported in alphabetical order - extraneous white space is removed -Also, if you use a completely new argument than those currently -defined in Nagios-Plugin-Getopt-03.t you will need to define it +Also, if you use a completely new argument than those currently +defined in Monitoring-Plugin-Getopt-03.t you will need to define it there as well. -- cgit v1.2.3-74-g34f1