diff options
Diffstat (limited to 'lib/Nagios/Plugin.pm')
| -rw-r--r-- | lib/Nagios/Plugin.pm | 160 |
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 | ||
| 3 | package Nagios::__::Plugin; | ||
| 4 | |||
| 5 | use 5.008004; | ||
| 6 | use Class::Struct; | ||
| 7 | struct "Nagios::__::Plugin" => { | ||
| 8 | perfdata => '@', | ||
| 9 | }; | ||
| 10 | |||
| 11 | package Nagios::Plugin; | ||
| 12 | |||
| 13 | use Nagios::Plugin::Base; | ||
| 14 | use Nagios::Plugin::Performance; | ||
| 15 | use Nagios::Plugin::Threshold; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | |||
| 20 | use Carp; | ||
| 21 | |||
| 22 | use Exporter; | ||
| 23 | our @ISA = qw(Exporter Nagios::__::Plugin); | ||
| 24 | our @EXPORT_OK = qw(%ERRORS); | ||
| 25 | |||
| 26 | our $VERSION = '0.10'; | ||
| 27 | |||
| 28 | sub add_perfdata { | ||
| 29 | my ($self, %args) = @_; | ||
| 30 | my $perf = Nagios::Plugin::Performance->new(%args); | ||
| 31 | push @{$self->perfdata}, $perf; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub all_perfoutput { | ||
| 35 | my $self = shift; | ||
| 36 | return join(" ", map {$_->perfoutput} (@{$self->perfdata})); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); } | ||
| 40 | |||
| 41 | my $shortname; | ||
| 42 | sub shortname { shift; @_ ? $shortname = shift : $shortname } | ||
| 43 | |||
| 44 | sub die { | ||
| 45 | my $self = shift; | ||
| 46 | my %args = @_; | ||
| 47 | Nagios::Plugin::Base->die(\%args, $self); | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | __END__ | ||
| 52 | |||
| 53 | =head1 NAME | ||
| 54 | |||
| 55 | Nagios::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 | |||
| 84 | This is the place for common routines when writing Nagios plugins. The idea is to make it as | ||
| 85 | easy as possible for developers to conform to the plugin guidelines | ||
| 86 | (http://nagiosplug.sourceforge.net/developer-guidelines.html). | ||
| 87 | |||
| 88 | =head1 DESIGN | ||
| 89 | |||
| 90 | To 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 | ||
| 92 | different types - a "use Nagios::Plugin" should be sufficient. | ||
| 93 | |||
| 94 | There is a Nagios::Plugin::Export. This holds all internals variables. You can specify these variables | ||
| 95 | when use'ing Nagios::Plugin | ||
| 96 | |||
| 97 | use Nagios::Plugin qw(%ERRORS) | ||
| 98 | print $ERRORS{WARNING} # prints 1 | ||
| 99 | |||
| 100 | =head1 VERSIONING | ||
| 101 | |||
| 102 | Only methods listed in the documentation for each module is public. | ||
| 103 | |||
| 104 | These modules are experimental and so the interfaces may change up until Nagios::Plugin | ||
| 105 | hits 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 | |||
| 113 | Imports 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 | |||
| 119 | Initializes 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 | |||
| 125 | Sets the thresholds, based on the range specification at | ||
| 126 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. | ||
| 127 | Returns a Nagios::Plugin::Threshold object, which can be used to input a value to see which threshold | ||
| 128 | is crossed. | ||
| 129 | |||
| 130 | =item add_perfdata( label => "size", value => $value, uom => "kB", threshold => $threshold ) | ||
| 131 | |||
| 132 | Adds to an array a Nagios::Plugin::Performance object with the specified values. | ||
| 133 | |||
| 134 | This needs to be done separately to allow multiple perfdata output. | ||
| 135 | |||
| 136 | =item die( return_code => $ERRORS{CRITICAL}, message => "Output" ) | ||
| 137 | |||
| 138 | Exits the plugin and prints to STDOUT a Nagios plugin compatible output. Exits with the specified | ||
| 139 | return code. Also prints performance data if any have been added in. | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =head1 SEE ALSO | ||
| 144 | |||
| 145 | http://nagiosplug.sourceforge.net | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
| 150 | |||
| 151 | =head1 COPYRIGHT AND LICENSE | ||
| 152 | |||
| 153 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
| 154 | |||
| 155 | This library is free software; you can redistribute it and/or modify | ||
| 156 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
| 157 | at your option, any later version of Perl 5 you may have available. | ||
| 158 | |||
| 159 | |||
| 160 | =cut | ||
