summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorNathan Vonnahme <n8v@users.sourceforge.net>2006-09-07 00:53:51 (GMT)
committerNathan Vonnahme <n8v@users.sourceforge.net>2006-09-07 00:53:51 (GMT)
commitbc239b3bd5023ed2da77ab03c581e56a4772f1d4 (patch)
tree655747da3db99a6e8ed0aab03f3036827c221748 /t
parent31336ec620bec84d5d4db1ba69deac2157611c12 (diff)
downloadmonitoring-plugin-perl-bc239b3bd5023ed2da77ab03c581e56a4772f1d4.tar.gz
adding example script and test for it, and fixing POD according to warnings from POD::Checker
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1476 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 't')
-rwxr-xr-xt/check_stuff.pl168
-rwxr-xr-xt/check_stuff.t76
2 files changed, 244 insertions, 0 deletions
diff --git a/t/check_stuff.pl b/t/check_stuff.pl
new file mode 100755
index 0000000..51f551f
--- /dev/null
+++ b/t/check_stuff.pl
@@ -0,0 +1,168 @@
1#!/usr/local/bin/perl
2
3### check_stuff.pl
4
5# an example Nagios plugin using the Nagios::Plugin modules.
6
7# Originally by Nathan Vonnahme, n8v at users dot sourceforge
8# dot net, July 19 2006
9
10# Please modify to your heart's content and use as the basis for all
11# the really cool Nagios monitoring scripts you're going to create.
12# You rock.
13
14# $Id$
15
16##############################################################################
17# prologue
18use strict;
19use warnings;
20
21use Nagios::Plugin qw(%ERRORS);
22
23use Nagios::Plugin::Getopt;
24
25
26use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result);
27'$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion
28$VERSION = $1;
29$0 =~ m!^.*/([^/]+)$!;
30$PROGNAME = $1;
31
32# shortname is the identifier this script will give to Nagios.
33# it's set here to the uppercase program name with file extension removed,
34# e.g. check_stuff.pl -> CHECK_STUFF
35my $short_name = uc $PROGNAME;
36$short_name =~ s/\.\w+$//;
37
38
39##############################################################################
40# define and get the command line options.
41# see the command line option guidelines at
42#
43
44
45# Instantiate Nagios::Plugin::Getopt object (usage and version are mandatory)
46my $nagopts = Nagios::Plugin::Getopt->new(
47 usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>]
48 [ -c|--critical=<critical threshold> ]
49 [ -w|--warning=<warning threshold> ]
50 [ -r|--result = <INTEGER> ]",
51 version => $VERSION,
52 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.',
53
54 extra => qq{
55
56THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max'
57(or 'max'). If specified '\@min:max', a warning status will be generated
58if the count *is* inside the specified range.
59
60See more threshold examples at
61 http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT
62
63Examples:
64
65 $PROGNAME -w 10 -c 18
66 Returns a warning if the resulting number is greater than 10, or a
67 critical error if it is greater than 18.
68
69 $PROGNAME -w 10: -c 4:
70 Returns a warning if the resulting number is less than 10, or a
71 critical error if it is less than 4.
72
73
74}
75
76);
77
78
79# Define and document the valid command line options
80# usage, help, version, timeout and verbose are defined by default.
81
82$nagopts->arg(
83 spec => 'warning|w=s',
84
85 help =>
86qq{-w, --warning=INTEGER:INTEGER
87 Minimum and maximum number of allowable result, outside of which a
88 warning will be generated. If omitted, no warning is generated.},
89
90# required => 1,
91# default => 10,
92);
93
94$nagopts->arg(
95 spec => 'critical|c=s',
96 help =>
97qq{-c, --critical=INTEGER:INTEGER
98 Minimum and maximum number of the generated result, outside of
99 which a critical will be generated. If omitted, a critical is
100 generated if no processes are running.},
101
102);
103
104$nagopts->arg(
105 spec => 'result|r=f',
106 help =>
107qq{-r, --result=INTEGER
108 Specify the result on the command line rather than generating a
109 random number. For testing.},
110);
111
112# Parse arguments and process standard ones (e.g. usage, help, version)
113$nagopts->getopts;
114
115
116my $p = Nagios::Plugin->new;
117
118$p->shortname($short_name);
119
120
121# sanity checking on command line options
122if ( (defined $nagopts->result) && ($nagopts->result < 0 || $nagopts->result > 20) ) {
123 $p->die(
124 return_code => $ERRORS{UNKNOWN},
125 message => 'invalid number supplied for the -r option'
126 );
127}
128
129unless ( defined $nagopts->warning || defined $nagopts->critical ) {
130 $p->die(
131 return_code => $ERRORS{UNKNOWN},
132 message => "you didn't supply a threshold argument"
133 );
134}
135
136##############################################################################
137# define a Nagios::Threshold object based on the command line options
138my $t = $p->set_thresholds( warning => $nagopts->warning, critical => $nagopts->critical );
139
140
141##############################################################################
142# check stuff.
143
144# THIS is where you'd do your actual checking to get a real value for $result
145# don't forget to timeout after $nagopts->timeout seconds, if applicable.
146my $result;
147if (defined $nagopts->result) {
148 $result = $nagopts->result;
149 print "using supplied result $result from command line\n" if $nagopts->verbose;
150}
151else {
152 $result = int rand(20)+1;
153 print "generated random result $result\n" if $nagopts->verbose;
154}
155
156print "status of result ($result) is ", $t->get_status($result), "\n"
157 if $nagopts->verbose;
158
159
160
161
162##############################################################################
163# output the result and exit
164$p->die(
165 return_code => $t->get_status($result),
166 message => "sample result was $result"
167);
168
diff --git a/t/check_stuff.t b/t/check_stuff.t
new file mode 100755
index 0000000..a748605
--- /dev/null
+++ b/t/check_stuff.t
@@ -0,0 +1,76 @@
1#!/usr/local/bin/perl
2#
3use strict; use warnings;
4#use Test::More qw(no_plan);
5use Test::More tests => 16;
6
7my ($r,$args);
8my $s = 't/check_stuff.pl';
9$s = 'perl -Ilib '.$s;
10
11my $n = 'CHECK_STUFF';
12
13# Nagios status strings and exit codes
14my %e = qw(
15 OK 0
16 WARNING 1
17 CRITICAL 2
18 UNKNOWN 3
19 );
20
21$r = `$s`;
22is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with no args";
23like $r, qr/^$n UNKNOWN/, "UNKNOWN with no args";
24
25
26#TODO:
27SKIP: {
28 local $TODO = q~d'oh! we'll have to redirect STDERR and check it with like() here instead of checking `` which only gets STDIN. Maybe use IPC::Open3?~;
29 skip "too noisy, see TODO here", 6;
30
31 $r = `$s -V`;
32 is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -V arg";
33 like $r, qr/\d+\.\d/i, "looks like there's a version"; # broken
34 is $r, '', "prints nothing to STDOUT";
35
36 $r = `$s -h`;
37 is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -h arg";
38 like $r, qr/usage/i, "looks like there's something helpful"; # broken
39 is $r, '', "prints nothing to STDOUT";
40}
41
42
43$args = " -r 99 ";
44diag "running `$s $args`" if $ENV{TEST_VERBOSE};
45$r = `$s $args`;
46diag "output: '$r'" if $ENV{TEST_VERBOSE};
47is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with $args";
48like $r, qr/UNKNOWN.+invalid/i, "UNKNOWN (warning: invalid -r) with $args";
49
50
51my $expected = {
52 " -w 10:15 -c~:15 -r 0" => 'WARNING',
53 " -w 10:15 -c~:15 -r 11" => 'OK',
54 " -w 10:15 -c~:15 -r 15.8" => 'CRITICAL',
55};
56
57test_expected( $s, $expected );
58
59
60sub test_expected {
61 my $s = shift;
62 my $expected = shift;
63 foreach ( keys %$expected ) {
64 diag "running `$s $_`" if $ENV{TEST_VERBOSE};
65 $r = `$s $_`;
66 diag "output: '$r'" if $ENV{TEST_VERBOSE};
67 is $?>>8 , $e{$expected->{$_}}, "exits($e{$expected->{$_}}) with $_";
68 like $r, qr/^$n $expected->{$_}/i, "looks $expected->{$_} with $_";
69 }
70}
71
72
73
74
75
76