diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-06-08 12:27:44 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-06-08 12:27:44 +0000 |
commit | 2c6651034f76e2bccb549a867485f8fabbf07cb1 (patch) | |
tree | 8de53d7efe4564017e1ebce73353c7e3a3c8b548 /lib/Nagios/Plugin | |
parent | 83e29d795dddf34a652cda8665179b77372ddcfe (diff) | |
download | monitoring-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/Nagios/Plugin')
-rw-r--r-- | lib/Nagios/Plugin/Base.pm | 65 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 119 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Range.pm | 126 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 96 |
4 files changed, 406 insertions, 0 deletions
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 | ||
3 | package Nagios::Plugin::Base; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Exporter; | ||
9 | our @ISA = qw(Exporter); | ||
10 | our @EXPORT = qw(%ERRORS); | ||
11 | |||
12 | our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); | ||
13 | |||
14 | our %STATUS_TEXT = reverse %ERRORS; | ||
15 | |||
16 | my $exit_on_die = 1; | ||
17 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | ||
18 | my $print_on_die = 1; | ||
19 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | ||
20 | |||
21 | sub 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 | |||
40 | 1; | ||
41 | __END__ | ||
42 | |||
43 | =head1 NAME | ||
44 | |||
45 | Nagios::Plugin::Base - Base functions for Nagios::Plugins | ||
46 | |||
47 | =head1 DESCRIPTION | ||
48 | |||
49 | See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate | ||
50 | common backend functionality. | ||
51 | |||
52 | =head1 AUTHOR | ||
53 | |||
54 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
55 | |||
56 | =head1 COPYRIGHT AND LICENSE | ||
57 | |||
58 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
59 | |||
60 | This library is free software; you can redistribute it and/or modify | ||
61 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
62 | at 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 @@ | |||
1 | package Nagios::Plugin::Performance; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Carp; | ||
9 | use Nagios::Plugin::Threshold; | ||
10 | use Class::Struct; | ||
11 | struct "Nagios::Plugin::Performance" => { | ||
12 | label => '$', | ||
13 | value => '$', | ||
14 | uom => '$', | ||
15 | threshold => 'Nagios::Plugin::Threshold', | ||
16 | min => '$', | ||
17 | max => '$', | ||
18 | }; | ||
19 | |||
20 | sub 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 | |||
26 | sub _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 | |||
41 | sub 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 | |||
54 | 1; | ||
55 | __END__ | ||
56 | |||
57 | =head1 NAME | ||
58 | |||
59 | Nagios::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 | |||
72 | Handles common Nagios Plugin performance data. This has a public interface because it could be | ||
73 | used by performance graphing routines, such as nagiostat (http://nagiostat.sourceforge.net), | ||
74 | perfparse (http://perfparse.sourceforge.net), nagiosgraph (http://nagiosgraph.sourceforge.net) or | ||
75 | NagiosGrapher (http://www.nagiosexchange.org/NagiosGrapher.84.0.html). | ||
76 | |||
77 | Once 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 | |||
85 | Returns an array of Nagios::Plugin::Performance objects based on the string entered. | ||
86 | If there is an error parsing the string, undef is returned. | ||
87 | |||
88 | =head1 OBJECT METHODS | ||
89 | |||
90 | =item label, value, uom, min, max | ||
91 | |||
92 | These all return scalars. min and max are not well supported yet. | ||
93 | |||
94 | =item threshold | ||
95 | |||
96 | This returns a Nagios::Plugin::Threshold object. | ||
97 | |||
98 | =back | ||
99 | |||
100 | =head1 SEE ALSO | ||
101 | |||
102 | Nagios::Plugin for information about versioning. | ||
103 | |||
104 | http://nagiosplug.sourceforge.net | ||
105 | |||
106 | =head1 AUTHOR | ||
107 | |||
108 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
109 | |||
110 | =head1 COPYRIGHT AND LICENSE | ||
111 | |||
112 | Copyright (C) 2006 by Altinity Limited | ||
113 | |||
114 | This library is free software; you can redistribute it and/or modify | ||
115 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
116 | at 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 @@ | |||
1 | package Nagios::Plugin::Range; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use overload | ||
9 | '""' => sub { shift->stringify }; | ||
10 | |||
11 | use Class::Struct; | ||
12 | struct "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 | |||
20 | my $outside = 0; | ||
21 | my $inside = 1; | ||
22 | |||
23 | sub 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 | |||
30 | sub set_range_start { | ||
31 | my ($self, $value) = @_; | ||
32 | $self->start($value+0); # Force scalar into number | ||
33 | $self->start_infinity(0); | ||
34 | } | ||
35 | |||
36 | sub 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 | ||
43 | sub 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 | ||
71 | sub 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 | |||
102 | 1; | ||
103 | __END__ | ||
104 | |||
105 | =head1 NAME | ||
106 | |||
107 | Nagios::Plugin::Range - Common range functions for Nagios::Plugin | ||
108 | |||
109 | =head1 DESCRIPTION | ||
110 | |||
111 | Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces. | ||
112 | |||
113 | =head1 AUTHOR | ||
114 | |||
115 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
116 | |||
117 | =head1 COPYRIGHT AND LICENSE | ||
118 | |||
119 | Copyright (C) 2006 by Altinity Limited | ||
120 | |||
121 | This library is free software; you can redistribute it and/or modify | ||
122 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
123 | at 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 @@ | |||
1 | package Nagios::Plugin::Threshold; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Nagios::Plugin::Range; | ||
9 | use Nagios::Plugin::Base; | ||
10 | |||
11 | use Class::Struct; | ||
12 | struct "Nagios::Plugin::Threshold" => { | ||
13 | warning => 'Nagios::Plugin::Range', | ||
14 | critical => 'Nagios::Plugin::Range', | ||
15 | }; | ||
16 | |||
17 | sub 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 | |||
45 | sub 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 | |||
59 | 1; | ||
60 | __END__ | ||
61 | |||
62 | =head1 NAME | ||
63 | |||
64 | Nagios::Plugin::Threshold - Threshold information in a perl object | ||
65 | |||
66 | =head1 DESCRIPTION | ||
67 | |||
68 | Handles common Nagios Plugin threshold data. See Nagios::Plugin or Nagios::Plugin::Performance for | ||
69 | creation of this object. | ||
70 | |||
71 | =head1 OBJECT METHODS | ||
72 | |||
73 | =over 4 | ||
74 | |||
75 | =item warning, critical | ||
76 | |||
77 | Returns the warning or critical range as a Nagios::Plugin::Range object. | ||
78 | |||
79 | =item get_status($value) | ||
80 | |||
81 | Given a value, will see if the value breeches the critical or the warning range. Returns the status code. | ||
82 | |||
83 | =head1 AUTHOR | ||
84 | |||
85 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
86 | |||
87 | =head1 COPYRIGHT AND LICENSE | ||
88 | |||
89 | Copyright (C) 2006 by Altinity Limited | ||
90 | |||
91 | This library is free software; you can redistribute it and/or modify | ||
92 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
93 | at your option, any later version of Perl 5 you may have available. | ||
94 | |||
95 | |||
96 | =cut | ||