summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Nagios/Plugin.pm')
-rw-r--r--lib/Nagios/Plugin.pm160
1 files changed, 160 insertions, 0 deletions
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
new file mode 100644
index 0000000..3c6e8d7
--- /dev/null
+++ b/lib/Nagios/Plugin.pm
@@ -0,0 +1,160 @@
1# This is only because Class::Struct doesn't allow subclasses
2# Trick stolen from Class::DBI
3package Nagios::__::Plugin;
4
5use 5.008004;
6use Class::Struct;
7struct "Nagios::__::Plugin" => {
8 perfdata => '@',
9 };
10
11package Nagios::Plugin;
12
13use Nagios::Plugin::Base;
14use Nagios::Plugin::Performance;
15use Nagios::Plugin::Threshold;
16
17use strict;
18use warnings;
19
20use Carp;
21
22use Exporter;
23our @ISA = qw(Exporter Nagios::__::Plugin);
24our @EXPORT_OK = qw(%ERRORS);
25
26our $VERSION = '0.10';
27
28sub add_perfdata {
29 my ($self, %args) = @_;
30 my $perf = Nagios::Plugin::Performance->new(%args);
31 push @{$self->perfdata}, $perf;
32}
33
34sub all_perfoutput {
35 my $self = shift;
36 return join(" ", map {$_->perfoutput} (@{$self->perfdata}));
37}
38
39sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); }
40
41my $shortname;
42sub shortname { shift; @_ ? $shortname = shift : $shortname }
43
44sub die {
45 my $self = shift;
46 my %args = @_;
47 Nagios::Plugin::Base->die(\%args, $self);
48}
49
501;
51__END__
52
53=head1 NAME
54
55Nagios::Plugin - Object oriented helper routines for your Nagios plugin
56
57=head1 SYNOPSIS
58
59 use Nagios::Plugin qw(%ERRORS);
60 $p = Nagios::Plugin->new( shortname => "PAGESIZE" );
61
62 $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" );
63
64 # ... collect current metric into $value
65 if ($trouble_getting_metric) {
66 $p->die( return_code => $ERRORS{UNKNOWN}, message => "Could not retrieve page");
67 # Output: PAGESIZE UNKNOWN Could not retrieve page
68 # Return code: 3
69 }
70
71 $p->add_perfdata( label => "size",
72 value => $value,
73 uom => "kB",
74 threshold => $threshold,
75 );
76 $p->add_perfdata( label => "time", ... );
77
78 $p->die( return_code => $threshold->get_status($value), message => "page size at http://... was ${value}kB" );
79 # Output: PAGESIZE OK: page size at http://... was 36kB | size=36kB;10:25;25: time=...
80 # Return code: 0
81
82=head1 DESCRIPTION
83
84This is the place for common routines when writing Nagios plugins. The idea is to make it as
85easy as possible for developers to conform to the plugin guidelines
86(http://nagiosplug.sourceforge.net/developer-guidelines.html).
87
88=head1 DESIGN
89
90To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data
91(ie, thresholds, ranges, performance). However, a plugin developer does not need to know about the
92different types - a "use Nagios::Plugin" should be sufficient.
93
94There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables
95when use'ing Nagios::Plugin
96
97 use Nagios::Plugin qw(%ERRORS)
98 print $ERRORS{WARNING} # prints 1
99
100=head1 VERSIONING
101
102Only methods listed in the documentation for each module is public.
103
104These modules are experimental and so the interfaces may change up until Nagios::Plugin
105hits version 1.0, but every attempt will be made to make backwards compatible.
106
107=over 4
108
109=head1 STARTING
110
111=item use Nagios::Plugin qw(%ERRORS)
112
113Imports the %ERRORS hash. This is currently the only symbol that can be imported.
114
115=head1 CLASS METHODS
116
117=item Nagios::Plugin->new( shortname => $$ )
118
119Initializes a new Nagios::Plugin object. Can specify the shortname here.
120
121=head1 OBJECT METHODS
122
123=item set_thresholds( warning => "10:25", critical => "25:" )
124
125Sets the thresholds, based on the range specification at
126http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT.
127Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold
128is crossed.
129
130=item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold )
131
132Adds to an array a Nagios::Plugin::Performance object with the specified values.
133
134This needs to be done separately to allow multiple perfdata output.
135
136=item die( return_code => $ERRORS{CRITICAL}, message => "Output" )
137
138Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified
139return code. Also prints performance data if any have been added in.
140
141=back
142
143=head1 SEE ALSO
144
145http://nagiosplug.sourceforge.net
146
147=head1 AUTHOR
148
149Ton Voon, E<lt>ton.voon@altinity.comE<gt>
150
151=head1 COPYRIGHT AND LICENSE
152
153Copyright (C) 2006 by Nagios Plugin Development Team
154
155This library is free software; you can redistribute it and/or modify
156it under the same terms as Perl itself, either Perl version 5.8.4 or,
157at your option, any later version of Perl 5 you may have available.
158
159
160=cut