Debian Codegraph @

check_hpasm/check_hpasm-4.9/plugins-scripts/Nagios/MiniPlugin.pm

raw
1 package Nagios::MiniPlugin;
2
3 use strict;
4 use Getopt::Long qw(:config no_ignore_case bundling);
5
6 our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
7
8 require Exporter;
9 our @ISA = qw(Exporter);
10 our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
11 our @EXPORT_OK = qw(%ERRORS);
12
13 use constant OK => 0;
14 use constant WARNING => 1;
15 use constant CRITICAL => 2;
16 use constant UNKNOWN => 3;
17 use constant DEPENDENT => 4;
18
19 our %ERRORS = (
20 'OK' => OK,
21 'WARNING' => WARNING,
22 'CRITICAL' => CRITICAL,
23 'UNKNOWN' => UNKNOWN,
24 'DEPENDENT' => DEPENDENT,
25 );
26
27 our %STATUS_TEXT = reverse %ERRORS;
28
29 sub new {
30 my $class = shift;
31 my %params = @_;
32 my $self = {
33 perfdata => [],
34 messages => {
35 ok => [],
36 warning => [],
37 critical => [],
38 unknown => [],
39 },
40 args => [],
41 opts => Nagios::MiniPlugin::Getopt->new(%params),
42 };
43 foreach (qw(shortname usage version url plugin blurb extra
44 license timeout)) {
45 $self->{$_} = $params{$_};
46 }
47 bless $self, $class;
48 }
49
50 sub add_arg {
51 my $self = shift;
52 $self->{opts}->add_arg(@_);
53 }
54
55 sub getopts {
56 my $self = shift;
57 $self->{opts}->getopts();
58 }
59
60 sub opts {
61 my $self = shift;
62 return $self->{opts};
63 }
64
65 sub add_message {
66 my $self = shift;
67 my ($code, @messages) = @_;
68 $code = (qw(ok warning critical unknown))[$code] if $code =~ /^\d+$/;
69 $code = lc $code;
70 push @{$self->{messages}->{$code}}, @messages;
71 }
72
73 sub remove_message {
74 my $self = shift;
75 my ($code, @messages) = @_;
76 $code = (qw(ok warning critical unknown))[$code] if $code =~ /^\d+$/;
77 $code = lc $code;
78 pop @{$self->{messages}->{$code}};
79 }
80
81 sub add_perfdata {
82 my ($self, %args) = @_;
83 my $str = $args{label}.'='.$args{value};
84 if ($args{uom}) {
85 $str .= $args{uom};
86 }
87 if ($args{warning}) {
88 $str .= ';'.$args{warning};
89 }
90 if ($args{critical}) {
91 $str .= ';'.$args{critical};
92 }
93 push @{$self->{perfdata}}, $str;
94 }
95
96
97 sub check_messages {
98 my $self = shift;
99 my %args = @_;
100
101 # Add object messages to any passed in as args
102 for my $code (qw(critical warning unknown ok)) {
103 my $messages = $self->{messages}->{$code} || [];
104 if ($args{$code}) {
105 unless (ref $args{$code} eq 'ARRAY') {
106 if ($code eq 'ok') {
107 $args{$code} = [ $args{$code} ];
108 }
109 }
110 push @{$args{$code}}, @$messages;
111 } else {
112 $args{$code} = $messages;
113 }
114 }
115 my %arg = %args;
116 $arg{join} = ' ' unless defined $arg{join};
117
118 # Decide $code
119 my $code = OK;
120 $code ||= CRITICAL if @{$arg{critical}};
121 $code ||= WARNING if @{$arg{warning}};
122 $code ||= UNKNOWN if @{$arg{unknown}};
123 return $code unless wantarray;
124
125 # Compose message
126 my $message = '';
127 if ($arg{join_all}) {
128 $message = join( $arg{join_all},
129 map { @$_ ? join( $arg{'join'}, @$_) : () }
130 $arg{critical},
131 $arg{warning},
132 $arg{unknown},
133 $arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : []
134 );
135 }
136
137 else {
138 $message ||= join( $arg{'join'}, @{$arg{critical}} )
139 if $code == CRITICAL;
140 $message ||= join( $arg{'join'}, @{$arg{warning}} )
141 if $code == WARNING;
142 $message ||= join( $arg{'join'}, @{$arg{unknown}} )
143 if $code == UNKNOWN;
144 $message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok}
145 if $arg{ok};
146 }
147
148 return ($code, $message);
149 }
150
151 sub nagios_exit {
152 my $self = shift;
153 my ($code, $message, $arg) = @_;
154 $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
155 $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
156 $message = '' unless defined $message;
157 if (ref $message && ref $message eq 'ARRAY') {
158 $message = join(' ', map { chomp; $_ } @$message);
159 } else {
160 chomp $message;
161 }
162 my $output = "$STATUS_TEXT{$code}";
163 $output .= " - $message" if defined $message && $message ne '';
164 if (scalar (@{$self->{perfdata}})) {
165 $output .= " | ".join(" ", @{$self->{perfdata}});
166 }
167 $output .= "\n";
168 print $output;
169 exit $code;
170 }
171
172 package Nagios::MiniPlugin::Getopt;
173
174 use strict;
175 use File::Basename;
176 use Data::Dumper;
177 use Getopt::Long qw(:config no_ignore_case bundling);
178
179 # Standard defaults
180 my %DEFAULT = (
181 timeout => 60,
182 verbose => 0,
183 license =>
184 "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
185 It may be used, redistributed and/or modified under the terms of the GNU
186 General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).",
187 );
188 # Standard arguments
189 my @ARGS = ({
190 spec => 'usage|?',
191 help => "-?, --usage\n Print usage information",
192 }, {
193 spec => 'help|h',
194 help => "-h, --help\n Print detailed help screen",
195 }, {
196 spec => 'version|V',
197 help => "-V, --version\n Print version information",
198 }, {
199 #spec => 'extra-opts:s@',
200 #help => "--extra-opts=[<section>[@<config_file>]]\n Section and/or config_file from which to load extra options (may repeat)",
201 }, {
202 spec => 'timeout|t=i',
203 help => "-t, --timeout=INTEGER\n Seconds before plugin times out (default: %s)",
204 default => $DEFAULT{timeout},
205 }, {
206 spec => 'verbose|v+',
207 help => "-v, --verbose\n Show details for command-line debugging (can repeat up to 3 times)",
208 default => $DEFAULT{verbose},
209 },
210 );
211 # Standard arguments we traditionally display last in the help output
212 my %DEFER_ARGS = map { $_ => 1 } qw(timeout verbose);
213
214 sub _init
215 {
216 my $self = shift;
217 my %params = @_;
218 # Check params
219 my $plugin = basename($ENV{NAGIOS_PLUGIN} || $0);
220 #my %attr = validate( @_, {
221 my %attr = (
222 usage => 1,
223 version => 0,
224 url => 0,
225 plugin => { default => $plugin },
226 blurb => 0,
227 extra => 0,
228 'extra-opts' => 0,
229 license => { default => $DEFAULT{license} },
230 timeout => { default => $DEFAULT{timeout} },
231 );
232
233 # Add attr to private _attr hash (except timeout)
234 $self->{timeout} = delete $attr{timeout};
235 $self->{_attr} = { %attr };
236 foreach (keys %{$self->{_attr}}) {
237 if (exists $params{$_}) {
238 $self->{_attr}->{$_} = $params{$_};
239 } else {
240 $self->{_attr}->{$_} = $self->{_attr}->{$_}->{default}
241 if ref ($self->{_attr}->{$_}) eq 'HASH' &&
242 exists $self->{_attr}->{$_}->{default};
243 }
244 }
245 # Chomp _attr values
246 chomp foreach values %{$self->{_attr}};
247
248 # Setup initial args list
249 $self->{_args} = [ grep { exists $_->{spec} } @ARGS ];
250
251 $self
252 }
253
254 sub new
255 {
256 my $class = shift;
257 my $self = bless {}, $class;
258 $self->_init(@_);
259 }
260
261 sub add_arg {
262 my $self = shift;
263 my %arg = @_;
264 push (@{$self->{_args}}, \%arg);
265 }
266
267 sub getopts {
268 my $self = shift;
269 my %commandline = ();
270 my @params = map { $_->{spec} } @{$self->{_args}};
271 if (! GetOptions(\%commandline, @params)) {
272 $self->print_help();
273 exit 0;
274 } else {
275 no strict 'refs';
276 do { $self->print_help(); exit 0; } if $commandline{help};
277 do { $self->print_version(); exit 0 } if $commandline{version};
278 do { $self->print_usage(); exit 0 } if $commandline{usage};
279 foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } @{$self->{_args}}) {
280 my $field = $_;
281 *{"$field"} = sub {
282 return $self->{opts}->{$field};
283 };
284 }
285 foreach (grep { exists $_->{default} } @{$self->{_args}}) {
286 $_->{spec} =~ /^([\w\-]+)/;
287 my $spec = $1;
288 $self->{opts}->{$spec} = $_->{default};
289 }
290 foreach (keys %commandline) {
291 $self->{opts}->{$_} = $commandline{$_};
292 }
293 }
294 }
295
296 sub get {
297 my $self = shift;
298 my $opt = shift;
299 return $self->{opts}->{$opt};
300 }
301
302 sub print_help {
303 my $self = shift;
304 $self->print_version();
305 printf "\n%s\n", $self->{_attr}->{license};
306 printf "\n%s\n\n", $self->{_attr}->{blurb};
307 $self->print_usage();
308 foreach (@{$self->{_args}}) {
309 printf " %s\n", $_->{help};
310 }
311 exit 0;
312 }
313
314 sub print_usage {
315 my $self = shift;
316 printf $self->{_attr}->{usage}, $self->{_attr}->{plugin};
317 print "\n";
318 }
319
320 sub print_version {
321 my $self = shift;
322 printf "%s %s", $self->{_attr}->{plugin}, $self->{_attr}->{version};
323 printf " [%s]", $self->{_attr}->{url} if $self->{_attr}->{url};
324 print "\n";
325 }
326
327 sub print_license {
328 my $self = shift;
329 printf "%s\n", $self->{_attr}->{license};
330 print "\n";
331 }
332
333 1;