diff options
Diffstat (limited to 'lib/Nagios/Plugin/Performance.pm')
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 185 |
1 files changed, 130 insertions, 55 deletions
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index fc1c0bc..1f036f2 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm | |||
@@ -6,41 +6,49 @@ use strict; | |||
6 | use warnings; | 6 | use warnings; |
7 | 7 | ||
8 | use Carp; | 8 | use Carp; |
9 | use Nagios::Plugin::Threshold; | 9 | use base qw(Class::Accessor::Fast); |
10 | Nagios::Plugin::Performance->mk_ro_accessors( | ||
11 | qw(label value uom warning critical min max) | ||
12 | ); | ||
13 | |||
10 | use Nagios::Plugin::Functions; | 14 | use Nagios::Plugin::Functions; |
15 | use Nagios::Plugin::Threshold; | ||
16 | use Nagios::Plugin::Range; | ||
11 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 17 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
12 | 18 | ||
13 | use Class::Struct; | ||
14 | struct "Nagios::Plugin::Performance" => { | ||
15 | label => '$', | ||
16 | value => '$', | ||
17 | uom => '$', | ||
18 | threshold => 'Nagios::Plugin::Threshold', | ||
19 | min => '$', | ||
20 | max => '$', | ||
21 | }; | ||
22 | |||
23 | sub perfoutput { | ||
24 | my $self = shift; | ||
25 | my $output = $self->label."=".$self->value. ($self->uom || "") .";".$self->threshold->warning.";".$self->threshold->critical; | ||
26 | return $output; | ||
27 | } | ||
28 | |||
29 | sub _parse { | 19 | sub _parse { |
30 | my $class = shift; | 20 | my $class = shift; |
31 | my $string = shift; | 21 | my $string = shift; |
32 | my $p = $class->new; | ||
33 | $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?\s*//; | 22 | $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?\s*//; |
34 | return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); | 23 | return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); |
35 | $p->label($1); | 24 | my $p = $class->new( |
36 | $p->value($2+0); | 25 | label => $1, value => $2+0, uom => $3, warning => $4, critical => $5, |
37 | $p->uom($3); | 26 | min => $6, max => $7 |
38 | $p->threshold(Nagios::Plugin::Threshold->set_thresholds(warning => $4, critical => $5)); | 27 | ); |
39 | $p->min($6); | ||
40 | $p->max($7); | ||
41 | return ($p, $string); | 28 | return ($p, $string); |
42 | } | 29 | } |
43 | 30 | ||
31 | # Map undef to '' | ||
32 | sub _nvl { | ||
33 | my ($self, $value) = @_; | ||
34 | defined $value ? $value : '' | ||
35 | } | ||
36 | |||
37 | sub perfoutput { | ||
38 | my $self = shift; | ||
39 | my $out = sprintf "%s=%s%s;%s;%s;%s;%s", | ||
40 | $self->label, | ||
41 | $self->value, | ||
42 | $self->_nvl($self->uom), | ||
43 | $self->_nvl($self->warning), | ||
44 | $self->_nvl($self->critical), | ||
45 | $self->_nvl($self->min), | ||
46 | $self->_nvl($self->max); | ||
47 | # Previous implementation omitted trailing ;; - do we need this? | ||
48 | $out =~ s/;;$//; | ||
49 | return $out; | ||
50 | } | ||
51 | |||
44 | sub parse_perfstring { | 52 | sub parse_perfstring { |
45 | my ($class, $perfstring) = @_; | 53 | my ($class, $perfstring) = @_; |
46 | my @perfs; | 54 | my @perfs; |
@@ -58,8 +66,9 @@ sub rrdlabel { | |||
58 | my $name = $self->label; | 66 | my $name = $self->label; |
59 | if ($name eq "/") { | 67 | if ($name eq "/") { |
60 | $name = "root"; | 68 | $name = "root"; |
69 | } | ||
61 | # If filesystem name, remove initial / and convert subsequent "/" to "_" | 70 | # If filesystem name, remove initial / and convert subsequent "/" to "_" |
62 | } elsif ($name =~ s/^\///) { | 71 | elsif ($name =~ s/^\///) { |
63 | $name =~ s/\//_/g; | 72 | $name =~ s/\//_/g; |
64 | } | 73 | } |
65 | # Convert bad chars | 74 | # Convert bad chars |
@@ -68,84 +77,150 @@ sub rrdlabel { | |||
68 | return substr( $name, 0, 19 ); | 77 | return substr( $name, 0, 19 ); |
69 | } | 78 | } |
70 | 79 | ||
80 | # Backward compatibility: create a threshold object on the fly as requested | ||
81 | sub threshold | ||
82 | { | ||
83 | my $self = shift; | ||
84 | return Nagios::Plugin::Threshold->set_thresholds( | ||
85 | warning => $self->warning, critical => $self->critical | ||
86 | ); | ||
87 | } | ||
88 | |||
89 | # Constructor - unpack thresholds, map args to hashref | ||
90 | sub new | ||
91 | { | ||
92 | my $class = shift; | ||
93 | my %arg = @_; | ||
94 | |||
95 | # Convert thresholds | ||
96 | if (my $threshold = delete $arg{threshold}) { | ||
97 | $arg{warning} ||= $threshold->warning . ""; | ||
98 | $arg{critical} ||= $threshold->critical . ""; | ||
99 | } | ||
100 | |||
101 | $class->SUPER::new(\%arg); | ||
102 | } | ||
103 | |||
71 | 1; | 104 | 1; |
105 | |||
72 | __END__ | 106 | __END__ |
73 | 107 | ||
74 | =head1 NAME | 108 | =head1 NAME |
75 | 109 | ||
76 | Nagios::Plugin::Performance - Performance information in a perl object | 110 | Nagios::Plugin::Performance - class for handling Nagios::Plugin |
111 | performance data. | ||
77 | 112 | ||
78 | =head1 SYNOPSIS | 113 | =head1 SYNOPSIS |
79 | 114 | ||
80 | use Nagios::Plugin::Performance; | 115 | use Nagios::Plugin::Performance; |
81 | 116 | ||
82 | @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); | 117 | # Constructor (also accepts a 'threshold' obj instead of warning/critical) |
83 | if (@p) { | 118 | $p = Nagios::Plugin::Performance->new( |
84 | print "1st label = ", $p[0]->label, $/; | 119 | label => 'size', |
85 | print "1st uom = ", $p[0]->uom, $/; | 120 | value => $value, |
86 | print "2nd crit = ", $p[1]->threshold->critical, $/; | 121 | uom => "kB", |
87 | } else { | 122 | warning => $warning, |
88 | print "Cannot parse",$/; | 123 | critical => $critical, |
124 | min => $min, | ||
125 | max => $max, | ||
126 | ); | ||
127 | |||
128 | # Parser | ||
129 | @perf = Nagios::Plugin::Performance->parse_perfstring( | ||
130 | "/=382MB;15264;15269;; /var=218MB;9443;9448" | ||
131 | ) | ||
132 | or warn("Failed to parse perfstring"); | ||
133 | |||
134 | # Accessors | ||
135 | for $p (@perf) { | ||
136 | printf "label: %s\n", $p->label; | ||
137 | printf "value: %s\n", $p->value; | ||
138 | printf "uom: %s\n", $p->uom; | ||
139 | printf "warning: %s\n", $p->warning; | ||
140 | printf "critical: %s\n", $p->critical; | ||
141 | printf "min: %s\n", $p->min; | ||
142 | printf "max: %s\n", $p->max; | ||
143 | # Special accessor returning a threshold obj containing warning/critical | ||
144 | $threshold = $p->threshold; | ||
89 | } | 145 | } |
90 | 146 | ||
147 | # Perfdata output format i.e. label=value[uom];[warn];[crit];[min];[max] | ||
148 | print $p->perfoutput; | ||
149 | |||
150 | |||
91 | =head1 DESCRIPTION | 151 | =head1 DESCRIPTION |
92 | 152 | ||
93 | Handles common Nagios Plugin performance data. This has a public interface because it could be | 153 | Nagios::Plugin class for handling performance data. This is a public |
94 | used by performance graphing routines, such as nagiostat (http://nagiostat.sourceforge.net), | 154 | interface because it could be used by performance graphing routines, |
95 | perfparse (http://perfparse.sourceforge.net), nagiosgraph (http://nagiosgraph.sourceforge.net) or | 155 | such as nagiostat (http://nagiostat.sourceforge.net), perfparse |
96 | NagiosGrapher (http://www.nagiosexchange.org/NagiosGrapher.84.0.html). | 156 | (http://perfparse.sourceforge.net), nagiosgraph |
157 | (http://nagiosgraph.sourceforge.net) or NagiosGrapher | ||
158 | (http://www.nagiosexchange.org/NagiosGrapher.84.0.html). | ||
97 | 159 | ||
98 | Once the performance string has been parsed, you can query the label, value, uom, or thresholds. | 160 | Nagios::Plugin::Performance offers both a parsing interface (via |
161 | parse_perfstring), for turning nagios performance output strings into | ||
162 | their components, and a composition interface (via new), for turning | ||
163 | components into perfdata strings. | ||
99 | 164 | ||
100 | =head1 CLASS METHODS | 165 | =head1 CLASS METHODS |
101 | 166 | ||
102 | =over 4 | 167 | =over 4 |
103 | 168 | ||
169 | =item Nagios::Plugin::Performance->new(%attributes) | ||
170 | |||
171 | Instantiates a new Nagios::Plugin::Performance object with the given | ||
172 | attributes. | ||
173 | |||
104 | =item Nagios::Plugin::Performance->parse_perfstring($string) | 174 | =item Nagios::Plugin::Performance->parse_perfstring($string) |
105 | 175 | ||
106 | Returns an array of Nagios::Plugin::Performance objects based on the string entered. | 176 | Returns an array of Nagios::Plugin::Performance objects based on the string |
107 | If there is an error parsing the string, an empty array is returned. | 177 | entered. If there is an error parsing the string, an empty array is returned. |
108 | 178 | ||
109 | =back | 179 | =back |
110 | 180 | ||
111 | =head1 OBJECT METHODS | 181 | =head1 OBJECT METHODS (ACCESSORS) |
112 | 182 | ||
113 | =over 4 | 183 | =over 4 |
114 | 184 | ||
115 | =item label, value, uom, min, max | 185 | =item label, value, uom, warning, critical, min, max |
116 | 186 | ||
117 | These all return scalars. min and max are not well supported yet. | 187 | These all return scalars. min and max are not well supported yet. |
118 | 188 | ||
189 | =item threshold | ||
190 | |||
191 | Returns a Nagios::Plugin::Threshold object holding the warning and critical | ||
192 | ranges for this performance data (if any). | ||
193 | |||
119 | =item rrdlabel | 194 | =item rrdlabel |
120 | 195 | ||
121 | Returns a label that can be used for the dataset name of an RRD, ie, between 1-19 | 196 | Returns a string based on 'label' that is suitable for use as dataset name of |
122 | characters long with characters [a-zA-Z0-9_]. | 197 | an RRD i.e. munges label to be 1-19 characters long with only characters |
198 | [a-zA-Z0-9_]. | ||
123 | 199 | ||
124 | There is no guarantee that multiple N:P:Performance objects will have unique rrdlabels. | 200 | There is no guarantee that multiple N:P:Performance objects will have unique |
201 | rrdlabels. | ||
125 | 202 | ||
126 | =item threshold | 203 | =item perfoutput |
127 | 204 | ||
128 | This returns a Nagios::Plugin::Threshold object. | 205 | Outputs the data in Nagios::Plugin perfdata format i.e. |
206 | label=value[uom];[warn];[crit];[min];[max]. | ||
129 | 207 | ||
130 | =back | 208 | =back |
131 | 209 | ||
132 | =head1 SEE ALSO | 210 | =head1 SEE ALSO |
133 | 211 | ||
134 | Nagios::Plugin for information about versioning. | 212 | Nagios::Plugin, Nagios::Plugin::Threshold, http://nagiosplug.sourceforge.net. |
135 | |||
136 | http://nagiosplug.sourceforge.net | ||
137 | 213 | ||
138 | =head1 AUTHOR | 214 | =head1 AUTHOR |
139 | 215 | ||
140 | This code is maintained by the Nagios Plugin Development Team: http://nagiosplug.sourceforge.net | 216 | This code is maintained by the Nagios Plugin Development Team: see |
217 | http://nagiosplug.sourceforge.net. | ||
141 | 218 | ||
142 | =head1 COPYRIGHT AND LICENSE | 219 | =head1 COPYRIGHT AND LICENSE |
143 | 220 | ||
144 | Copyright (C) 2006 Nagios Plugin Development Team | 221 | Copyright (C) 2006-2007 Nagios Plugin Development Team |
145 | 222 | ||
146 | This library is free software; you can redistribute it and/or modify | 223 | This library is free software; you can redistribute it and/or modify |
147 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | 224 | it under the same terms as Perl itself. |
148 | at your option, any later version of Perl 5 you may have available. | ||
149 | |||
150 | 225 | ||
151 | =cut | 226 | =cut |