summaryrefslogtreecommitdiffstats
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
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
-rw-r--r--MANIFEST2
-rw-r--r--lib/Nagios/Plugin.pm26
-rw-r--r--lib/Nagios/Plugin/Getopt.pm4
-rw-r--r--lib/Nagios/Plugin/Performance.pm4
-rw-r--r--lib/Nagios/Plugin/Threshold.pm2
-rwxr-xr-xt/check_stuff.pl168
-rwxr-xr-xt/check_stuff.t76
7 files changed, 275 insertions, 7 deletions
diff --git a/MANIFEST b/MANIFEST
index bbee644..61f396b 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -2,6 +2,8 @@ Changes
2Makefile.PL 2Makefile.PL
3MANIFEST 3MANIFEST
4README 4README
5t/check_stuff.pl
6t/check_stuff.t
5t/Nagios-Plugin.t 7t/Nagios-Plugin.t
6t/Nagios-Plugin-Performance.t 8t/Nagios-Plugin-Performance.t
7t/Nagios-Plugin-Range.t 9t/Nagios-Plugin-Range.t
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index c14dfa6..0915571 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -86,6 +86,12 @@ This is the place for common routines when writing Nagios plugins. The idea is t
86easy as possible for developers to conform to the plugin guidelines 86easy as possible for developers to conform to the plugin guidelines
87(http://nagiosplug.sourceforge.net/developer-guidelines.html). 87(http://nagiosplug.sourceforge.net/developer-guidelines.html).
88 88
89=head1 EXAMPLE SCRIPT
90
91"Enough talk! Show me where to start!"
92
93See the file 'check_stuff.pl' in the 't' directory for a complete working example of a plugin script.
94
89=head1 DESIGN 95=head1 DESIGN
90 96
91To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data 97To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data
@@ -105,22 +111,30 @@ Only methods listed in the documentation for each module is public.
105These modules are experimental and so the interfaces may change up until Nagios::Plugin 111These modules are experimental and so the interfaces may change up until Nagios::Plugin
106hits version 1.0, but every attempt will be made to make backwards compatible. 112hits version 1.0, but every attempt will be made to make backwards compatible.
107 113
108=over 4
109
110=head1 STARTING 114=head1 STARTING
111 115
116=over 4
117
112=item use Nagios::Plugin qw(%ERRORS) 118=item use Nagios::Plugin qw(%ERRORS)
113 119
114Imports the %ERRORS hash. This is currently the only symbol that can be imported. 120Imports the %ERRORS hash. This is currently the only symbol that can be imported.
115 121
122=back
123
116=head1 CLASS METHODS 124=head1 CLASS METHODS
117 125
126=over 4
127
118=item Nagios::Plugin->new( shortname => $$ ) 128=item Nagios::Plugin->new( shortname => $$ )
119 129
120Initializes a new Nagios::Plugin object. Can specify the shortname here. 130Initializes a new Nagios::Plugin object. Can specify the shortname here.
121 131
132=back
133
122=head1 OBJECT METHODS 134=head1 OBJECT METHODS
123 135
136=over 4
137
124=item set_thresholds( warning => "10:25", critical => "~:25" ) 138=item set_thresholds( warning => "10:25", critical => "~:25" )
125 139
126Sets the thresholds, based on the range specification at 140Sets the thresholds, based on the range specification at
@@ -147,9 +161,13 @@ http://nagiosplug.sourceforge.net
147 161
148=head1 AUTHOR 162=head1 AUTHOR
149 163
150Ton Voon, E<lt>ton.voon@altinity.comE<gt> 164Maintained by the Nagios Plugin development team - http://nagiosplug.sourceforge.net
165
166Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt>
167
168Nathan Vonnahme added extra tests and subsequent fixes.
151 169
152Thanks to Nathan Vonnahme for loads of extra tests and subsequent fixes. 170Gavin Carr contributed the Nagios::Plugin::GetOpt module.
153 171
154=head1 COPYRIGHT AND LICENSE 172=head1 COPYRIGHT AND LICENSE
155 173
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm
index 1903e8c..d38dced 100644
--- a/lib/Nagios/Plugin/Getopt.pm
+++ b/lib/Nagios/Plugin/Getopt.pm
@@ -296,9 +296,7 @@ __END__
296 296
297=head1 NAME 297=head1 NAME
298 298
299Nagios::Plugin::Getopt - OO perl module providing standardised argument 299Nagios::Plugin::Getopt - OO perl module providing standardised argument processing for Nagios plugins
300processing for Nagios plugins
301
302 300
303=head1 VERSION 301=head1 VERSION
304 302
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm
index 83c92fb..9e99f54 100644
--- a/lib/Nagios/Plugin/Performance.pm
+++ b/lib/Nagios/Plugin/Performance.pm
@@ -106,8 +106,12 @@ Once the performance string has been parsed, you can query the label, value, uom
106Returns an array of Nagios::Plugin::Performance objects based on the string entered. 106Returns an array of Nagios::Plugin::Performance objects based on the string entered.
107If there is an error parsing the string, an empty array is returned. 107If there is an error parsing the string, an empty array is returned.
108 108
109=back
110
109=head1 OBJECT METHODS 111=head1 OBJECT METHODS
110 112
113=over 4
114
111=item label, value, uom, min, max 115=item label, value, uom, min, max
112 116
113These all return scalars. min and max are not well supported yet. 117These all return scalars. min and max are not well supported yet.
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm
index f3410e8..9e7b938 100644
--- a/lib/Nagios/Plugin/Threshold.pm
+++ b/lib/Nagios/Plugin/Threshold.pm
@@ -83,6 +83,8 @@ Returns the warning or critical range as a Nagios::Plugin::Range object.
83 83
84Given a value, will see if the value breeches the critical or the warning range. Returns the status code. 84Given a value, will see if the value breeches the critical or the warning range. Returns the status code.
85 85
86=back
87
86=head1 AUTHOR 88=head1 AUTHOR
87 89
88Ton Voon, E<lt>ton.voon@altinity.comE<gt> 90Ton Voon, E<lt>ton.voon@altinity.comE<gt>
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