summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2006-06-08 12:27:44 +0000
committerTon Voon <tonvoon@users.sourceforge.net>2006-06-08 12:27:44 +0000
commit2c6651034f76e2bccb549a867485f8fabbf07cb1 (patch)
tree8de53d7efe4564017e1ebce73353c7e3a3c8b548 /lib
parent83e29d795dddf34a652cda8665179b77372ddcfe (diff)
downloadmonitoring-plugin-perl-2c6651034f76e2bccb549a867485f8fabbf07cb1.tar.gz
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1419 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib')
-rw-r--r--lib/Nagios/Plugin.pm160
-rw-r--r--lib/Nagios/Plugin/Base.pm65
-rw-r--r--lib/Nagios/Plugin/Performance.pm119
-rw-r--r--lib/Nagios/Plugin/Range.pm126
-rw-r--r--lib/Nagios/Plugin/Threshold.pm96
5 files changed, 566 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
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm
new file mode 100644
index 0000000..f3a7f7c
--- /dev/null
+++ b/lib/Nagios/Plugin/Base.pm
@@ -0,0 +1,65 @@
1# This module holds all exported variables
2# and base functions
3package Nagios::Plugin::Base;
4
5use strict;
6use warnings;
7
8use Exporter;
9our @ISA = qw(Exporter);
10our @EXPORT = qw(%ERRORS);
11
12our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
13
14our %STATUS_TEXT = reverse %ERRORS;
15
16my $exit_on_die = 1;
17sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die };
18my $print_on_die = 1;
19sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die };
20
21sub die {
22 my ($class, $args, $plugin) = @_;
23 my $return_code = $args->{return_code} || 3;
24 my $message = $args->{message} || "Internal error";
25 my $output = join(" ", $STATUS_TEXT{$return_code}, $message);
26 if ($plugin) {
27 $output = $plugin->shortname." $output" if $plugin->shortname;
28 $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata;
29 }
30 if ($print_on_die) {
31 print $output, $/;
32 }
33 if ($exit_on_die) {
34 exit $return_code;
35 } else {
36 return $output;
37 }
38}
39
401;
41__END__
42
43=head1 NAME
44
45Nagios::Plugin::Base - Base functions for Nagios::Plugins
46
47=head1 DESCRIPTION
48
49See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate
50common backend functionality.
51
52=head1 AUTHOR
53
54Ton Voon, E<lt>ton.voon@altinity.comE<gt>
55
56=head1 COPYRIGHT AND LICENSE
57
58Copyright (C) 2006 by Nagios Plugin Development Team
59
60This library is free software; you can redistribute it and/or modify
61it under the same terms as Perl itself, either Perl version 5.8.4 or,
62at your option, any later version of Perl 5 you may have available.
63
64
65=cut
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm
new file mode 100644
index 0000000..eee1bee
--- /dev/null
+++ b/lib/Nagios/Plugin/Performance.pm
@@ -0,0 +1,119 @@
1package Nagios::Plugin::Performance;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use Carp;
9use Nagios::Plugin::Threshold;
10use Class::Struct;
11struct "Nagios::Plugin::Performance" => {
12 label => '$',
13 value => '$',
14 uom => '$',
15 threshold => 'Nagios::Plugin::Threshold',
16 min => '$',
17 max => '$',
18 };
19
20sub perfoutput {
21 my $self = shift;
22 my $output = $self->label."=".$self->value.$self->uom.";".$self->threshold->warning.";".$self->threshold->critical;
23 return $output;
24}
25
26sub _parse {
27 my $class = shift;
28 my $string = shift;
29 my $p = $class->new;
30 $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)? *//;
31 return undef unless ($1 && $2);
32 $p->label($1);
33 $p->value($2+0);
34 $p->uom($3);
35 $p->threshold(Nagios::Plugin::Threshold->set_thresholds(warning => $4, critical => $5));
36 $p->min($6);
37 $p->max($7);
38 return ($p, $string);
39}
40
41sub parse_perfstring {
42 my ($class, $perfstring) = @_;
43 my @perfs;
44 my $obj;
45 while ($perfstring) {
46 ($obj, $perfstring) = $class->_parse($perfstring);
47 return undef unless $obj;
48 push @perfs, $obj;
49 }
50 return undef unless @perfs;
51 return @perfs;
52}
53
541;
55__END__
56
57=head1 NAME
58
59Nagios::Plugin::Performance - Performance information in a perl object
60
61=head1 SYNOPSIS
62
63 use Nagios::Plugin::Performance;
64
65 @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448");
66 print "1st label = ", $p[0]->label, $/;
67 print "1st uom = ", $p[0]->uom, $/;
68 print "2nd crit = ", $p[1]->threshold->critical, $/;
69
70=head1 DESCRIPTION
71
72Handles common Nagios Plugin performance data. This has a public interface because it could be
73used by performance graphing routines, such as nagiostat (http://nagiostat.sourceforge.net),
74perfparse (http://perfparse.sourceforge.net), nagiosgraph (http://nagiosgraph.sourceforge.net) or
75NagiosGrapher (http://www.nagiosexchange.org/NagiosGrapher.84.0.html).
76
77Once the performance string has been parsed, you can query the label, value, uom, or thresholds.
78
79=head1 CLASS METHODS
80
81=over 4
82
83=item Nagios::Plugin::Performance->parse_perfstring($string)
84
85Returns an array of Nagios::Plugin::Performance objects based on the string entered.
86If there is an error parsing the string, undef is returned.
87
88=head1 OBJECT METHODS
89
90=item label, value, uom, min, max
91
92These all return scalars. min and max are not well supported yet.
93
94=item threshold
95
96This returns a Nagios::Plugin::Threshold object.
97
98=back
99
100=head1 SEE ALSO
101
102Nagios::Plugin for information about versioning.
103
104http://nagiosplug.sourceforge.net
105
106=head1 AUTHOR
107
108Ton Voon, E<lt>ton.voon@altinity.comE<gt>
109
110=head1 COPYRIGHT AND LICENSE
111
112Copyright (C) 2006 by Altinity Limited
113
114This library is free software; you can redistribute it and/or modify
115it under the same terms as Perl itself, either Perl version 5.8.4 or,
116at your option, any later version of Perl 5 you may have available.
117
118
119=cut
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm
new file mode 100644
index 0000000..3d6f613
--- /dev/null
+++ b/lib/Nagios/Plugin/Range.pm
@@ -0,0 +1,126 @@
1package Nagios::Plugin::Range;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use overload
9 '""' => sub { shift->stringify };
10
11use Class::Struct;
12struct "Nagios::Plugin::Range" => {
13 start => '$',
14 end => '$',
15 start_infinity => '$', # TRUE / FALSE
16 end_infinity => '$', # TRUE / FALSE
17 alert_on => '$', # OUTSIDE 0, INSIDE 1
18 };
19
20my $outside = 0;
21my $inside = 1;
22
23sub stringify {
24 my $self = shift;
25 return (($self->alert_on) ? "@" : "") .
26 (($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) .
27 (($self->end_infinity == 1) ? "" : $self->end);
28}
29
30sub set_range_start {
31 my ($self, $value) = @_;
32 $self->start($value+0); # Force scalar into number
33 $self->start_infinity(0);
34}
35
36sub set_range_end {
37 my ($self, $value) = @_;
38 $self->end($value+0); # Force scalar into number
39 $self->end_infinity(0);
40}
41
42# Returns a N::P::Range object if the string is a conforms to a Nagios Plugin range string, otherwise null
43sub parse_range_string {
44 my ($class, $string) = @_;
45 my $valid = 0;
46 my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside);
47
48 if ($string =~ s/^\@//) {
49 $range->alert_on($inside);
50 }
51 if ($string =~ s/^~//) {
52 $range->start_infinity(1);
53 }
54 if (($_) = $string =~ /^([-\d\.]+)?:/) {
55 $range->set_range_start($_) if defined $_;
56 $string =~ s/^([-\d\.]+)?://;
57 $valid++
58 }
59 if ($string =~ /^([-\d\.]+)$/) {
60 $range->set_range_end($string);
61 $valid++;
62 }
63
64 if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) {
65 return $range;
66 }
67 return undef;
68}
69
70# Returns 1 if an alert should be raised, otherwise 0
71sub check_range {
72 my ($self, $value) = @_;
73 my $false = 0;
74 my $true = 1;
75 if ($self->alert_on == $inside) {
76 $false = 1;
77 $true = 0;
78 }
79 if ($self->end_infinity == 0 && $self->start_infinity == 0) {
80 if ($self->start <= $value && $value <= $self->end) {
81 return $false;
82 } else {
83 return $true;
84 }
85 } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) {
86 if ($self->start <= $value) {
87 return $false;
88 } else {
89 return $true;
90 }
91 } elsif ($self->start_infinity == 1 && $self->end_infinity == 0) {
92 if ($value <= $self->end) {
93 return $false;
94 } else {
95 return $true;
96 }
97 } else {
98 return $false;
99 }
100}
101
1021;
103__END__
104
105=head1 NAME
106
107Nagios::Plugin::Range - Common range functions for Nagios::Plugin
108
109=head1 DESCRIPTION
110
111Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces.
112
113=head1 AUTHOR
114
115Ton Voon, E<lt>ton.voon@altinity.comE<gt>
116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright (C) 2006 by Altinity Limited
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself, either Perl version 5.8.4 or,
123at your option, any later version of Perl 5 you may have available.
124
125
126=cut
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm
new file mode 100644
index 0000000..9c5d042
--- /dev/null
+++ b/lib/Nagios/Plugin/Threshold.pm
@@ -0,0 +1,96 @@
1package Nagios::Plugin::Threshold;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use Nagios::Plugin::Range;
9use Nagios::Plugin::Base;
10
11use Class::Struct;
12struct "Nagios::Plugin::Threshold" => {
13 warning => 'Nagios::Plugin::Range',
14 critical => 'Nagios::Plugin::Range',
15 };
16
17sub set_thresholds {
18 my ($class, %args) = @_;
19 my $t = $class->new;
20 if (defined $args{warning}) {
21 my $r = Nagios::Plugin::Range->parse_range_string($args{warning});
22 if (defined $r) {
23 $t->warning($r);
24 } else {
25 Nagios::Plugin::Base->die( {
26 return_code => $ERRORS{UNKNOWN},
27 message => "Warning range incorrect: '$args{warning}'"
28 } );
29 }
30 }
31 if (defined $args{critical}) {
32 my $r = Nagios::Plugin::Range->parse_range_string($args{critical});
33 if (defined $r) {
34 $t->critical($r);
35 } else {
36 Nagios::Plugin::Base->die( {
37 return_code => $ERRORS{UNKNOWN},
38 message => "Critical range incorrect: '$args{critical}'"
39 } );
40 }
41 }
42 return $t;
43}
44
45sub get_status {
46 my ($self, $value) = @_;
47 if ($self->critical) {
48 if ($self->critical->check_range($value) == 1) {
49 return $ERRORS{CRITICAL};
50 }
51 }
52 if ($self->warning) {
53 if ($self->warning->check_range($value) == 1) {
54 return $ERRORS{WARNING};
55 }
56 }
57}
58
591;
60__END__
61
62=head1 NAME
63
64Nagios::Plugin::Threshold - Threshold information in a perl object
65
66=head1 DESCRIPTION
67
68Handles common Nagios Plugin threshold data. See Nagios::Plugin or Nagios::Plugin::Performance for
69creation of this object.
70
71=head1 OBJECT METHODS
72
73=over 4
74
75=item warning, critical
76
77Returns the warning or critical range as a Nagios::Plugin::Range object.
78
79=item get_status($value)
80
81Given a value, will see if the value breeches the critical or the warning range. Returns the status code.
82
83=head1 AUTHOR
84
85Ton Voon, E<lt>ton.voon@altinity.comE<gt>
86
87=head1 COPYRIGHT AND LICENSE
88
89Copyright (C) 2006 by Altinity Limited
90
91This library is free software; you can redistribute it and/or modify
92it under the same terms as Perl itself, either Perl version 5.8.4 or,
93at your option, any later version of Perl 5 you may have available.
94
95
96=cut