summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin.pm
blob: 3c6e8d75e79b490819034a53b4dd659fdb616f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# This is only because Class::Struct doesn't allow subclasses
# Trick stolen from Class::DBI
package Nagios::__::Plugin;

use 5.008004;
use Class::Struct;
struct "Nagios::__::Plugin" => {
	perfdata => '@',
	};

package Nagios::Plugin;

use Nagios::Plugin::Base;
use Nagios::Plugin::Performance;
use Nagios::Plugin::Threshold;

use strict;
use warnings;

use Carp;

use Exporter;
our @ISA = qw(Exporter Nagios::__::Plugin);
our @EXPORT_OK = qw(%ERRORS);

our $VERSION = '0.10';

sub add_perfdata {
	my ($self, %args) = @_;
	my $perf = Nagios::Plugin::Performance->new(%args);
	push @{$self->perfdata}, $perf;
}

sub all_perfoutput {
	my $self = shift;
	return join(" ", map {$_->perfoutput} (@{$self->perfdata}));
}

sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); }

my $shortname;
sub shortname { shift; @_ ? $shortname = shift : $shortname }

sub die {
	my $self = shift;
	my %args = @_;
	Nagios::Plugin::Base->die(\%args, $self);
}

1;
__END__

=head1 NAME

Nagios::Plugin - Object oriented helper routines for your Nagios plugin

=head1 SYNOPSIS

  use Nagios::Plugin qw(%ERRORS);
  $p = Nagios::Plugin->new( shortname => "PAGESIZE" );

  $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" );

  # ... collect current metric into $value
  if ($trouble_getting_metric) {
	$p->die( return_code => $ERRORS{UNKNOWN}, message => "Could not retrieve page");
	# Output: PAGESIZE UNKNOWN Could not retrieve page
	# Return code: 3
  }

  $p->add_perfdata( label => "size",
    value => $value,
    uom => "kB",
    threshold => $threshold,
    );
  $p->add_perfdata( label => "time", ... );

  $p->die( return_code => $threshold->get_status($value), message => "page size at http://... was ${value}kB" );
  # Output: PAGESIZE OK: page size at http://... was 36kB | size=36kB;10:25;25: time=...
  # Return code: 0

=head1 DESCRIPTION

This is the place for common routines when writing Nagios plugins. The idea is to make it as 
easy as possible for developers to conform to the plugin guidelines 
(http://nagiosplug.sourceforge.net/developer-guidelines.html).

=head1 DESIGN

To facilitate object oriented classes, there are multiple perl modules, each reflecting a type of data
(ie, thresholds, ranges, performance). However, a plugin developer does not need to know about the
different types - a "use Nagios::Plugin" should be sufficient.

There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables
when use'ing Nagios::Plugin

  use Nagios::Plugin qw(%ERRORS)
  print $ERRORS{WARNING} # prints 1

=head1 VERSIONING

Only methods listed in the documentation for each module is public.

These modules are experimental and so the interfaces may change up until Nagios::Plugin
hits version 1.0, but every attempt will be made to make backwards compatible.

=over 4

=head1 STARTING

=item use Nagios::Plugin qw(%ERRORS)

Imports the %ERRORS hash. This is currently the only symbol that can be imported.

=head1 CLASS METHODS

=item Nagios::Plugin->new( shortname => $$ )

Initializes a new Nagios::Plugin object. Can specify the shortname here.

=head1 OBJECT METHODS

=item set_thresholds( warning => "10:25", critical => "25:" )

Sets the thresholds, based on the range specification at 
http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. 
Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold
is crossed.

=item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold )

Adds to an array a Nagios::Plugin::Performance object with the specified values.

This needs to be done separately to allow multiple perfdata output.

=item die( return_code => $ERRORS{CRITICAL}, message => "Output" )

Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified
return code. Also prints performance data if any have been added in.

=back

=head1 SEE ALSO

http://nagiosplug.sourceforge.net

=head1 AUTHOR

Ton Voon, E<lt>ton.voon@altinity.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Nagios Plugin Development Team

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.4 or,
at your option, any later version of Perl 5 you may have available.


=cut