| 1 |
package Monitoring::GLPlugin; |
| 2 |
|
| 3 |
=head1 Monitoring::GLPlugin |
| 4 |
|
| 5 |
Monitoring::GLPlugin - infrastructure functions to build a monitoring plugin |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
use strict; |
| 10 |
use IO::File; |
| 11 |
use File::Basename; |
| 12 |
use Digest::MD5 qw(md5_hex); |
| 13 |
use Errno; |
| 14 |
use JSON; |
| 15 |
use File::Slurp qw(read_file); |
| 16 |
use Data::Dumper; |
| 17 |
$Data::Dumper::Indent = 1; |
| 18 |
eval { |
| 19 |
# avoid "used only once" because older Data::Dumper don't have this |
| 20 |
# use OMD please because OMD has everything! |
| 21 |
no warnings 'all'; |
| 22 |
$Data::Dumper::Sparseseen = 1; |
| 23 |
}; |
| 24 |
our $AUTOLOAD; |
| 25 |
*VERSION = \'5.5'; |
| 26 |
|
| 27 |
use constant { OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 }; |
| 28 |
|
| 29 |
{ |
| 30 |
our $mode = undef; |
| 31 |
our $plugin = undef; |
| 32 |
our $pluginname = basename($ENV{'NAGIOS_PLUGIN'} || $0); |
| 33 |
our $blacklist = undef; |
| 34 |
our $info = []; |
| 35 |
our $extendedinfo = []; |
| 36 |
our $summary = []; |
| 37 |
our $variables = {}; |
| 38 |
our $survive_sudo_env = ["LD_LIBRARY_PATH", "SHLIB_PATH"]; |
| 39 |
} |
| 40 |
|
| 41 |
sub new { |
| 42 |
my ($class, %params) = @_; |
| 43 |
my $self = {}; |
| 44 |
bless $self, $class; |
| 45 |
require Monitoring::GLPlugin::Commandline |
| 46 |
if ! grep /BEGIN/, keys %Monitoring::GLPlugin::Commandline::; |
| 47 |
require Monitoring::GLPlugin::Item |
| 48 |
if ! grep /BEGIN/, keys %Monitoring::GLPlugin::Item::; |
| 49 |
require Monitoring::GLPlugin::TableItem |
| 50 |
if ! grep /BEGIN/, keys %Monitoring::GLPlugin::TableItem::; |
| 51 |
$Monitoring::GLPlugin::plugin = Monitoring::GLPlugin::Commandline->new(%params); |
| 52 |
return $self; |
| 53 |
} |
| 54 |
|
| 55 |
sub rebless { |
| 56 |
my ($self, $class) = @_; |
| 57 |
bless $self, $class; |
| 58 |
$self->debug('using '.$class); |
| 59 |
# gilt nur fuer "echte" Fabrikate mit "Classes::" vorndran |
| 60 |
$self->{classified_as} = ref($self) if $class !~ /^Monitoring::GLPlugin/; |
| 61 |
} |
| 62 |
|
| 63 |
sub init { |
| 64 |
my ($self) = @_; |
| 65 |
if ($self->opts->can("blacklist") && $self->opts->blacklist && |
| 66 |
-f $self->opts->blacklist) { |
| 67 |
$self->opts->blacklist = do { |
| 68 |
local (@ARGV, $/) = $self->opts->blacklist; <> }; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
sub dumper { |
| 73 |
my ($self, $object) = @_; |
| 74 |
my $run = $object->{runtime}; |
| 75 |
delete $object->{runtime}; |
| 76 |
printf STDERR "%s\n", Data::Dumper::Dumper($object); |
| 77 |
$object->{runtime} = $run; |
| 78 |
} |
| 79 |
|
| 80 |
sub no_such_mode { |
| 81 |
my ($self) = @_; |
| 82 |
$self->nagios_exit(3, |
| 83 |
sprintf "Mode %s is not implemented for this type of device", |
| 84 |
$self->opts->mode |
| 85 |
); |
| 86 |
exit 3; |
| 87 |
} |
| 88 |
|
| 89 |
######################################################### |
| 90 |
# framework-related. setup, options |
| 91 |
# |
| 92 |
sub add_default_args { |
| 93 |
my ($self) = @_; |
| 94 |
$self->add_arg( |
| 95 |
spec => 'mode=s', |
| 96 |
help => "--mode |
| 97 |
A keyword which tells the plugin what to do", |
| 98 |
required => 1, |
| 99 |
); |
| 100 |
$self->add_arg( |
| 101 |
spec => 'regexp', |
| 102 |
help => "--regexp |
| 103 |
Parameter name/name2/name3 will be interpreted as (perl) regular expression", |
| 104 |
required => 0,); |
| 105 |
$self->add_arg( |
| 106 |
spec => 'warning=s', |
| 107 |
help => "--warning |
| 108 |
The warning threshold", |
| 109 |
required => 0,); |
| 110 |
$self->add_arg( |
| 111 |
spec => 'critical=s', |
| 112 |
help => "--critical |
| 113 |
The critical threshold", |
| 114 |
required => 0,); |
| 115 |
$self->add_arg( |
| 116 |
spec => 'warningx=s%', |
| 117 |
help => '--warningx |
| 118 |
The extended warning thresholds |
| 119 |
e.g. --warningx db_msdb_free_pct=6: to override the threshold for a |
| 120 |
specific item ', |
| 121 |
required => 0, |
| 122 |
); |
| 123 |
$self->add_arg( |
| 124 |
spec => 'criticalx=s%', |
| 125 |
help => '--criticalx |
| 126 |
The extended critical thresholds', |
| 127 |
required => 0, |
| 128 |
); |
| 129 |
$self->add_arg( |
| 130 |
spec => 'units=s', |
| 131 |
help => "--units |
| 132 |
One of %, B, KB, MB, GB, Bit, KBi, MBi, GBi. (used for e.g. mode interface-usage)", |
| 133 |
required => 0, |
| 134 |
); |
| 135 |
$self->add_arg( |
| 136 |
spec => 'name=s', |
| 137 |
help => "--name |
| 138 |
The name of a specific component to check", |
| 139 |
required => 0, |
| 140 |
decode => "rfc3986", |
| 141 |
); |
| 142 |
$self->add_arg( |
| 143 |
spec => 'name2=s', |
| 144 |
help => "--name2 |
| 145 |
The secondary name of a component", |
| 146 |
required => 0, |
| 147 |
decode => "rfc3986", |
| 148 |
); |
| 149 |
$self->add_arg( |
| 150 |
spec => 'name3=s', |
| 151 |
help => "--name3 |
| 152 |
The tertiary name of a component", |
| 153 |
required => 0, |
| 154 |
decode => "rfc3986", |
| 155 |
); |
| 156 |
$self->add_arg( |
| 157 |
spec => 'extra-opts=s', |
| 158 |
help => "--extra-opts |
| 159 |
read command line arguments from an external file", |
| 160 |
required => 0, |
| 161 |
); |
| 162 |
$self->add_arg( |
| 163 |
spec => 'blacklist|b=s', |
| 164 |
help => '--blacklist |
| 165 |
Blacklist some (missing/failed) components', |
| 166 |
required => 0, |
| 167 |
default => '', |
| 168 |
); |
| 169 |
$self->add_arg( |
| 170 |
spec => 'mitigation=s', |
| 171 |
help => "--mitigation |
| 172 |
The parameter allows you to change a critical error to a warning. |
| 173 |
It works only for specific checks. Which ones? Try it out or look in the code. |
| 174 |
--mitigation warning ranks an error as warning which by default would be critical.", |
| 175 |
required => 0, |
| 176 |
); |
| 177 |
$self->add_arg( |
| 178 |
spec => 'lookback=s', |
| 179 |
help => "--lookback |
| 180 |
The amount of time you want to look back when calculating average rates. |
| 181 |
Use it for mode interface-errors or interface-usage. Without --lookback |
| 182 |
the time between two runs of check_nwc_health is the base for calculations. |
| 183 |
If you want your checkresult to be based for example on the past hour, |
| 184 |
use --lookback 3600. ", |
| 185 |
required => 0, |
| 186 |
); |
| 187 |
$self->add_arg( |
| 188 |
spec => 'environment|e=s%', |
| 189 |
help => "--environment |
| 190 |
Add a variable to the plugin's environment", |
| 191 |
required => 0, |
| 192 |
); |
| 193 |
$self->add_arg( |
| 194 |
spec => 'negate=s%', |
| 195 |
help => "--negate |
| 196 |
Emulate the negate plugin. --negate warning=critical --negate unknown=critical", |
| 197 |
required => 0, |
| 198 |
); |
| 199 |
$self->add_arg( |
| 200 |
spec => 'morphmessage=s%', |
| 201 |
help => '--morphmessage |
| 202 |
Modify the final output message', |
| 203 |
required => 0, |
| 204 |
decode => "rfc3986", |
| 205 |
); |
| 206 |
$self->add_arg( |
| 207 |
spec => 'morphperfdata=s%', |
| 208 |
help => "--morphperfdata |
| 209 |
The parameter allows you to change performance data labels. |
| 210 |
It's a perl regexp and a substitution. |
| 211 |
Example: --morphperfdata '(.*)ISATAP(.*)'='\$1patasi\$2'", |
| 212 |
required => 0, |
| 213 |
decode => "rfc3986", |
| 214 |
); |
| 215 |
$self->add_arg( |
| 216 |
spec => 'selectedperfdata=s', |
| 217 |
help => "--selectedperfdata |
| 218 |
The parameter allows you to limit the list of performance data. It's a perl regexp. |
| 219 |
Only matching perfdata show up in the output", |
| 220 |
required => 0, |
| 221 |
); |
| 222 |
$self->add_arg( |
| 223 |
spec => 'report=s', |
| 224 |
help => "--report |
| 225 |
Can be used to shorten the output", |
| 226 |
required => 0, |
| 227 |
default => 'long', |
| 228 |
); |
| 229 |
$self->add_arg( |
| 230 |
spec => 'multiline', |
| 231 |
help => '--multiline |
| 232 |
Multiline output', |
| 233 |
required => 0, |
| 234 |
); |
| 235 |
$self->add_arg( |
| 236 |
spec => 'with-mymodules-dyn-dir=s', |
| 237 |
help => "--with-mymodules-dyn-dir |
| 238 |
Add-on modules for the my-modes will be searched in this directory", |
| 239 |
required => 0, |
| 240 |
); |
| 241 |
$self->add_arg( |
| 242 |
spec => 'statefilesdir=s', |
| 243 |
help => '--statefilesdir |
| 244 |
An alternate directory where the plugin can save files', |
| 245 |
required => 0, |
| 246 |
env => 'STATEFILESDIR', |
| 247 |
); |
| 248 |
$self->add_arg( |
| 249 |
spec => 'isvalidtime=i', |
| 250 |
help => '--isvalidtime |
| 251 |
Signals the plugin to return OK if now is not a valid check time', |
| 252 |
required => 0, |
| 253 |
default => 1, |
| 254 |
); |
| 255 |
$self->add_arg( |
| 256 |
spec => 'reset', |
| 257 |
help => "--reset |
| 258 |
remove the state file", |
| 259 |
required => 0, |
| 260 |
hidden => 1, |
| 261 |
); |
| 262 |
$self->add_arg( |
| 263 |
spec => 'runas=s', |
| 264 |
help => "--runas |
| 265 |
run as a different user", |
| 266 |
required => 0, |
| 267 |
hidden => 1, |
| 268 |
); |
| 269 |
$self->add_arg( |
| 270 |
spec => 'shell', |
| 271 |
help => "--shell |
| 272 |
forget what you see", |
| 273 |
required => 0, |
| 274 |
hidden => 1, |
| 275 |
); |
| 276 |
$self->add_arg( |
| 277 |
spec => 'drecksptkdb=s', |
| 278 |
help => "--drecksptkdb |
| 279 |
This parameter must be used instead of --name, because Devel::ptkdb is stealing the latter from the command line", |
| 280 |
aliasfor => "name", |
| 281 |
required => 0, |
| 282 |
hidden => 1, |
| 283 |
); |
| 284 |
$self->add_arg( |
| 285 |
spec => 'tracefile=s', |
| 286 |
help => "--tracefile |
| 287 |
Write debugging-info to this file (if it exists)", |
| 288 |
required => 0, |
| 289 |
hidden => 1, |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
sub add_default_modes { |
| 294 |
my ($self) = @_; |
| 295 |
$self->add_mode( |
| 296 |
internal => 'encode', |
| 297 |
spec => 'encode', |
| 298 |
alias => undef, |
| 299 |
help => 'encode stdin', |
| 300 |
hidden => 1, |
| 301 |
); |
| 302 |
$self->add_mode( |
| 303 |
internal => 'decode', |
| 304 |
spec => 'decode', |
| 305 |
alias => undef, |
| 306 |
help => 'decode stdin or --name', |
| 307 |
hidden => 1, |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
sub add_modes { |
| 312 |
my ($self, $modes) = @_; |
| 313 |
my $modestring = ""; |
| 314 |
my @modes = @{$modes}; |
| 315 |
my $longest = length ((reverse sort {length $a <=> length $b} map { $_->[1] } @modes)[0]); |
| 316 |
my $format = " %-". |
| 317 |
(length ((reverse sort {length $a <=> length $b} map { $_->[1] } @modes)[0])). |
| 318 |
"s\t(%s)\n"; |
| 319 |
foreach (@modes) { |
| 320 |
$modestring .= sprintf $format, $_->[1], $_->[3]; |
| 321 |
} |
| 322 |
$modestring .= sprintf "\n"; |
| 323 |
$Monitoring::GLPlugin::plugin->{modestring} = $modestring; |
| 324 |
} |
| 325 |
|
| 326 |
sub add_arg { |
| 327 |
my ($self, %args) = @_; |
| 328 |
if ($args{help} =~ /^--mode/) { |
| 329 |
$args{help} .= "\n".$Monitoring::GLPlugin::plugin->{modestring}; |
| 330 |
} |
| 331 |
$Monitoring::GLPlugin::plugin->{opts}->add_arg(%args); |
| 332 |
} |
| 333 |
|
| 334 |
sub mod_arg { |
| 335 |
my ($self, @arg) = @_; |
| 336 |
$Monitoring::GLPlugin::plugin->{opts}->mod_arg(@arg); |
| 337 |
} |
| 338 |
|
| 339 |
sub add_mode { |
| 340 |
my ($self, %args) = @_; |
| 341 |
push(@{$Monitoring::GLPlugin::plugin->{modes}}, \%args); |
| 342 |
my $longest = length ((reverse sort {length $a <=> length $b} map { $_->{spec} } @{$Monitoring::GLPlugin::plugin->{modes}})[0]); |
| 343 |
my $format = " %-". |
| 344 |
(length ((reverse sort {length $a <=> length $b} map { $_->{spec} } @{$Monitoring::GLPlugin::plugin->{modes}})[0])). |
| 345 |
"s\t(%s)\n"; |
| 346 |
$Monitoring::GLPlugin::plugin->{modestring} = ""; |
| 347 |
foreach (@{$Monitoring::GLPlugin::plugin->{modes}}) { |
| 348 |
$Monitoring::GLPlugin::plugin->{modestring} .= sprintf $format, $_->{spec}, $_->{help}; |
| 349 |
} |
| 350 |
$Monitoring::GLPlugin::plugin->{modestring} .= "\n"; |
| 351 |
} |
| 352 |
|
| 353 |
sub validate_args { |
| 354 |
my ($self) = @_; |
| 355 |
if ($self->opts->mode =~ /^my-([^\-.]+)/) { |
| 356 |
my $param = $self->opts->mode; |
| 357 |
$param =~ s/\-/::/g; |
| 358 |
$self->add_mode( |
| 359 |
internal => $param, |
| 360 |
spec => $self->opts->mode, |
| 361 |
alias => undef, |
| 362 |
help => 'my extension', |
| 363 |
); |
| 364 |
} elsif ($self->opts->mode eq 'encode') { |
| 365 |
my $input = <>; |
| 366 |
chomp $input; |
| 367 |
$input =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; |
| 368 |
printf "%s\n", $input; |
| 369 |
exit 0; |
| 370 |
} elsif ($self->opts->mode eq 'decode') { |
| 371 |
if (! -t STDIN) { |
| 372 |
my $input = <>; |
| 373 |
chomp $input; |
| 374 |
$input =~ s/%([A-Za-z0-9]{2})/chr(hex($1))/seg; |
| 375 |
printf "%s\n", $input; |
| 376 |
exit OK; |
| 377 |
} else { |
| 378 |
if ($self->opts->name) { |
| 379 |
my $input = $self->opts->name; |
| 380 |
$input =~ s/%([A-Za-z0-9]{2})/chr(hex($1))/seg; |
| 381 |
printf "%s\n", $input; |
| 382 |
exit OK; |
| 383 |
} else { |
| 384 |
printf "i can't find your encoded statement. use --name or pipe it in my stdin\n"; |
| 385 |
exit UNKNOWN; |
| 386 |
} |
| 387 |
} |
| 388 |
} elsif ((! grep { $self->opts->mode eq $_ } map { $_->{spec} } @{$Monitoring::GLPlugin::plugin->{modes}}) && |
| 389 |
(! grep { $self->opts->mode eq $_ } map { defined $_->{alias} ? @{$_->{alias}} : () } @{$Monitoring::GLPlugin::plugin->{modes}})) { |
| 390 |
printf "UNKNOWN - mode %s\n", $self->opts->mode; |
| 391 |
$self->opts->print_help(); |
| 392 |
exit 3; |
| 393 |
} |
| 394 |
if ($self->opts->name && $self->opts->name =~ /(%22)|(%27)/) { |
| 395 |
my $name = $self->opts->name; |
| 396 |
$name =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; |
| 397 |
$self->override_opt('name', $name); |
| 398 |
} |
| 399 |
$Monitoring::GLPlugin::mode = ( |
| 400 |
map { $_->{internal} } |
| 401 |
grep { |
| 402 |
($self->opts->mode eq $_->{spec}) || |
| 403 |
( defined $_->{alias} && grep { $self->opts->mode eq $_ } @{$_->{alias}}) |
| 404 |
} @{$Monitoring::GLPlugin::plugin->{modes}} |
| 405 |
)[0]; |
| 406 |
if ($self->opts->multiline) { |
| 407 |
$ENV{NRPE_MULTILINESUPPORT} = 1; |
| 408 |
} else { |
| 409 |
$ENV{NRPE_MULTILINESUPPORT} = 0; |
| 410 |
} |
| 411 |
if ($self->opts->can("statefilesdir") && ! $self->opts->statefilesdir) { |
| 412 |
if ($^O =~ /MSWin/) { |
| 413 |
if (defined $ENV{TEMP}) { |
| 414 |
$self->override_opt('statefilesdir', $ENV{TEMP}."/".$Monitoring::GLPlugin::plugin->{name}); |
| 415 |
} elsif (defined $ENV{TMP}) { |
| 416 |
$self->override_opt('statefilesdir', $ENV{TMP}."/".$Monitoring::GLPlugin::plugin->{name}); |
| 417 |
} elsif (defined $ENV{windir}) { |
| 418 |
$self->override_opt('statefilesdir', File::Spec->catfile($ENV{windir}, 'Temp')."/".$Monitoring::GLPlugin::plugin->{name}); |
| 419 |
} else { |
| 420 |
$self->override_opt('statefilesdir', "C:/".$Monitoring::GLPlugin::plugin->{name}); |
| 421 |
} |
| 422 |
} elsif (exists $ENV{OMD_ROOT}) { |
| 423 |
$self->override_opt('statefilesdir', $ENV{OMD_ROOT}."/var/tmp/".$Monitoring::GLPlugin::plugin->{name}); |
| 424 |
} else { |
| 425 |
$self->override_opt('statefilesdir', "/var/tmp/".$Monitoring::GLPlugin::plugin->{name}); |
| 426 |
} |
| 427 |
} |
| 428 |
$Monitoring::GLPlugin::plugin->{statefilesdir} = $self->opts->statefilesdir |
| 429 |
if $self->opts->can("statefilesdir"); |
| 430 |
if ($self->opts->can("warningx") && $self->opts->warningx) { |
| 431 |
foreach my $key (keys %{$self->opts->warningx}) { |
| 432 |
$self->set_thresholds(metric => $key, |
| 433 |
warning => $self->opts->warningx->{$key}); |
| 434 |
} |
| 435 |
} |
| 436 |
if ($self->opts->can("criticalx") && $self->opts->criticalx) { |
| 437 |
foreach my $key (keys %{$self->opts->criticalx}) { |
| 438 |
$self->set_thresholds(metric => $key, |
| 439 |
critical => $self->opts->criticalx->{$key}); |
| 440 |
} |
| 441 |
} |
| 442 |
$self->set_timeout_alarm() if ! $SIG{'ALRM'}; |
| 443 |
} |
| 444 |
|
| 445 |
sub set_timeout_alarm { |
| 446 |
my ($self, $timeout, $handler) = @_; |
| 447 |
$timeout ||= $self->opts->timeout; |
| 448 |
$handler ||= sub { |
| 449 |
$self->nagios_exit(UNKNOWN, |
| 450 |
sprintf("%s timed out after %d seconds\n", |
| 451 |
$Monitoring::GLPlugin::plugin->{name}, $self->opts->timeout) |
| 452 |
); |
| 453 |
}; |
| 454 |
use POSIX ':signal_h'; |
| 455 |
if ($^O =~ /MSWin/) { |
| 456 |
local $SIG{'ALRM'} = $handler; |
| 457 |
} else { |
| 458 |
my $mask = POSIX::SigSet->new( SIGALRM ); |
| 459 |
my $action = POSIX::SigAction->new( |
| 460 |
$handler, $mask |
| 461 |
); |
| 462 |
my $oldaction = POSIX::SigAction->new(); |
| 463 |
sigaction(SIGALRM ,$action ,$oldaction ); |
| 464 |
} |
| 465 |
alarm(int($timeout)); # 1 second before the global unknown timeout |
| 466 |
} |
| 467 |
|
| 468 |
######################################################### |
| 469 |
# global helpers |
| 470 |
# |
| 471 |
sub set_variable { |
| 472 |
my ($self, $key, $value) = @_; |
| 473 |
$Monitoring::GLPlugin::variables->{$key} = $value; |
| 474 |
} |
| 475 |
|
| 476 |
sub get_variable { |
| 477 |
my ($self, $key, $fallback) = @_; |
| 478 |
return exists $Monitoring::GLPlugin::variables->{$key} ? |
| 479 |
$Monitoring::GLPlugin::variables->{$key} : $fallback; |
| 480 |
} |
| 481 |
|
| 482 |
sub debug { |
| 483 |
my ($self, $format, @message) = @_; |
| 484 |
if ($self->get_variable("verbose") && |
| 485 |
$self->get_variable("verbose") > $self->get_variable("verbosity", 10)) { |
| 486 |
printf("%s: ", scalar localtime); |
| 487 |
printf($format, @message); |
| 488 |
printf "\n"; |
| 489 |
} |
| 490 |
if ($Monitoring::GLPlugin::tracefile) { |
| 491 |
my $logfh = IO::File->new(); |
| 492 |
$logfh->autoflush(1); |
| 493 |
if ($logfh->open($Monitoring::GLPlugin::tracefile, "a")) { |
| 494 |
$logfh->printf("%s: ", scalar localtime); |
| 495 |
$logfh->printf($format, @message); |
| 496 |
$logfh->printf("\n"); |
| 497 |
$logfh->close(); |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
sub filter_namex { |
| 503 |
my ($self, $opt, $name) = @_; |
| 504 |
if ($opt) { |
| 505 |
if ($self->opts->regexp) { |
| 506 |
if ($name =~ /$opt/i) { |
| 507 |
return 1; |
| 508 |
} |
| 509 |
} else { |
| 510 |
if (lc $opt eq lc $name) { |
| 511 |
return 1; |
| 512 |
} |
| 513 |
} |
| 514 |
} else { |
| 515 |
return 1; |
| 516 |
} |
| 517 |
return 0; |
| 518 |
} |
| 519 |
|
| 520 |
sub filter_name { |
| 521 |
my ($self, $name) = @_; |
| 522 |
return $self->filter_namex($self->opts->name, $name); |
| 523 |
} |
| 524 |
|
| 525 |
sub filter_name2 { |
| 526 |
my ($self, $name) = @_; |
| 527 |
return $self->filter_namex($self->opts->name2, $name); |
| 528 |
} |
| 529 |
|
| 530 |
sub filter_name3 { |
| 531 |
my ($self, $name) = @_; |
| 532 |
return $self->filter_namex($self->opts->name3, $name); |
| 533 |
} |
| 534 |
|
| 535 |
sub version_is_minimum { |
| 536 |
my ($self, $version) = @_; |
| 537 |
my $installed_version; |
| 538 |
my $newer = 1; |
| 539 |
if ($self->get_variable("version")) { |
| 540 |
$installed_version = $self->get_variable("version"); |
| 541 |
} elsif (exists $self->{version}) { |
| 542 |
$installed_version = $self->{version}; |
| 543 |
} else { |
| 544 |
return 0; |
| 545 |
} |
| 546 |
my @v1 = map { $_ eq "x" ? 0 : $_ } split(/\./, $version); |
| 547 |
my @v2 = split(/\./, $installed_version); |
| 548 |
if (scalar(@v1) > scalar(@v2)) { |
| 549 |
push(@v2, (0) x (scalar(@v1) - scalar(@v2))); |
| 550 |
} elsif (scalar(@v2) > scalar(@v1)) { |
| 551 |
push(@v1, (0) x (scalar(@v2) - scalar(@v1))); |
| 552 |
} |
| 553 |
foreach my $pos (0..$#v1) { |
| 554 |
if ($v2[$pos] > $v1[$pos]) { |
| 555 |
$newer = 1; |
| 556 |
last; |
| 557 |
} elsif ($v2[$pos] < $v1[$pos]) { |
| 558 |
$newer = 0; |
| 559 |
last; |
| 560 |
} |
| 561 |
} |
| 562 |
return $newer; |
| 563 |
} |
| 564 |
|
| 565 |
sub accentfree { |
| 566 |
my ($self, $text) = @_; |
| 567 |
# thanks mycoyne who posted this accent-remove-algorithm |
| 568 |
# http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_23275533.html#a21234612 |
| 569 |
my @transformed; |
| 570 |
my %replace = ( |
| 571 |
'9a' => 's', '9c' => 'oe', '9e' => 'z', '9f' => 'Y', 'c0' => 'A', 'c1' => 'A', |
| 572 |
'c2' => 'A', 'c3' => 'A', 'c4' => 'A', 'c5' => 'A', 'c6' => 'AE', 'c7' => 'C', |
| 573 |
'c8' => 'E', 'c9' => 'E', 'ca' => 'E', 'cb' => 'E', 'cc' => 'I', 'cd' => 'I', |
| 574 |
'ce' => 'I', 'cf' => 'I', 'd0' => 'D', 'd1' => 'N', 'd2' => 'O', 'd3' => 'O', |
| 575 |
'd4' => 'O', 'd5' => 'O', 'd6' => 'O', 'd8' => 'O', 'd9' => 'U', 'da' => 'U', |
| 576 |
'db' => 'U', 'dc' => 'U', 'dd' => 'Y', 'e0' => 'a', 'e1' => 'a', 'e2' => 'a', |
| 577 |
'e3' => 'a', 'e4' => 'a', 'e5' => 'a', 'e6' => 'ae', 'e7' => 'c', 'e8' => 'e', |
| 578 |
'e9' => 'e', 'ea' => 'e', 'eb' => 'e', 'ec' => 'i', 'ed' => 'i', 'ee' => 'i', |
| 579 |
'ef' => 'i', 'f0' => 'o', 'f1' => 'n', 'f2' => 'o', 'f3' => 'o', 'f4' => 'o', |
| 580 |
'f5' => 'o', 'f6' => 'o', 'f8' => 'o', 'f9' => 'u', 'fa' => 'u', 'fb' => 'u', |
| 581 |
'fc' => 'u', 'fd' => 'y', 'ff' => 'y', |
| 582 |
'8a' => 'S', '8c' => 'CE', '9a' => 's', '9c' => 'oe', '9f' => 'Y', 'a2' => 'o', 'aa' => 'a', |
| 583 |
'b2' => '2', 'b3' => '3', 'b9' => '1', 'bc' => '1/4', 'bd' => '1/2', 'be' => '3/4', |
| 584 |
'c0' => 'A', 'c1' => 'A', 'c2' => 'A', 'c3' => 'A', 'c4' => 'A', 'c5' => 'A', 'c6' => 'AE', |
| 585 |
'c7' => 'C', 'c8' => 'E', 'c9' => 'E', 'ca' => 'E', 'cb' => 'E', |
| 586 |
'cc' => 'I', 'cd' => 'I', 'ce' => 'I', 'cf' => 'I', 'd0' => 'D', 'd1' => 'N', |
| 587 |
'd2' => 'O', 'd3' => 'O', 'd4' => 'O', 'd5' => 'O', 'd6' => 'O', |
| 588 |
'd8' => 'O', 'd9' => 'U', 'da' => 'U', 'db' => 'U', 'dc' => 'U', 'dd' => 'Y', |
| 589 |
'df' => 'ss', 'e0' => 'a', 'e1' => 'a', 'e2' => 'a', 'e3' => 'a', 'e4' => 'a', 'e5' => 'a', |
| 590 |
'e6' => 'ae', 'e7' => 'c', 'e8' => 'e', 'e9' => 'e', 'ea' => 'e', 'eb' => 'e', |
| 591 |
'ec' => 'i', 'ed' => 'i', 'ee' => 'i', 'ef' => 'i', 'f1' => 'n', |
| 592 |
'f2' => 'o', 'f3' => 'o', 'f4' => 'o', 'f5' => 'o', 'f6' => 'o', 'f8' => 'o', |
| 593 |
'f9' => 'u', 'fa' => 'u', 'fb' => 'u', 'fc' => 'u', 'fd' => 'y', 'ff' => 'yy', |
| 594 |
); |
| 595 |
my @letters = split //, $text;; |
| 596 |
for (my $i = 0; $i <= $#letters; $i++) { |
| 597 |
my $hex = sprintf "%x", ord($letters[$i]); |
| 598 |
$letters[$i] = $replace{$hex} if (exists $replace{$hex}); |
| 599 |
} |
| 600 |
push @transformed, @letters; |
| 601 |
$text = join '', @transformed; |
| 602 |
$text =~ s/[[:^ascii:]]//g; |
| 603 |
return $text; |
| 604 |
} |
| 605 |
|
| 606 |
sub dump { |
| 607 |
my ($self, $indent) = @_; |
| 608 |
$indent = $indent ? " " x $indent : ""; |
| 609 |
if ($self->can("internal_name")) { |
| 610 |
printf "%s[%s]\n", $indent, $self->internal_name(); |
| 611 |
} else { |
| 612 |
my $class = ref($self); |
| 613 |
$class =~ s/^.*:://; |
| 614 |
printf "%s[%s]\n", $indent, uc $class; |
| 615 |
} |
| 616 |
foreach (grep !/^(info|trace|warning|critical|blacklisted|extendedinfo|flat_indices|indices)$/, sort keys %{$self}) { |
| 617 |
printf "%s%s: %s\n", $indent, $_, $self->{$_} if defined $self->{$_} && ref($self->{$_}) ne "ARRAY"; |
| 618 |
} |
| 619 |
if ($self->{info}) { |
| 620 |
printf "%sinfo: %s\n", $indent, $self->{info}; |
| 621 |
} |
| 622 |
foreach (grep !/^(info|trace|warning|critical|blacklisted|extendedinfo|flat_indices|indices)$/, sort keys %{$self}) { |
| 623 |
if (defined $self->{$_} && ref($self->{$_}) eq "ARRAY") { |
| 624 |
my $have_flat_indices = 1; |
| 625 |
foreach my $obj (@{$self->{$_}}) { |
| 626 |
$have_flat_indices = 0 if (ref($obj) ne "HASH" || ! exists $obj->{flat_indices}); |
| 627 |
} |
| 628 |
if ($have_flat_indices) { |
| 629 |
foreach my $obj (sort { |
| 630 |
join('', map { sprintf("%30d",$_) } split( /\./, $a->{flat_indices})) cmp |
| 631 |
join('', map { sprintf("%30d",$_) } split( /\./, $b->{flat_indices})) |
| 632 |
} @{$self->{$_}}) { |
| 633 |
$obj->dump(); |
| 634 |
} |
| 635 |
} else { |
| 636 |
foreach my $obj (@{$self->{$_}}) { |
| 637 |
$obj->dump() if UNIVERSAL::can($obj, "isa") && $obj->can("dump"); |
| 638 |
} |
| 639 |
} |
| 640 |
} elsif (defined $self->{$_} && ref($self->{$_}) =~ /^Classes::/) { |
| 641 |
$self->{$_}->dump(2) if UNIVERSAL::can($self->{$_}, "isa") && $self->{$_}->can("dump"); |
| 642 |
} |
| 643 |
} |
| 644 |
printf "\n"; |
| 645 |
} |
| 646 |
|
| 647 |
sub table_ascii { |
| 648 |
my ($self, $table, $titles) = @_; |
| 649 |
my $text = ""; |
| 650 |
my $column_length = {}; |
| 651 |
my $column = 0; |
| 652 |
foreach (@{$titles}) { |
| 653 |
$column_length->{$column++} = length($_); |
| 654 |
} |
| 655 |
foreach my $tr (@{$table}) { |
| 656 |
@{$tr} = map { ref($_) eq "ARRAY" ? $_->[0] : $_; } @{$tr}; |
| 657 |
$column = 0; |
| 658 |
foreach my $td (@{$tr}) { |
| 659 |
if (length($td) > $column_length->{$column}) { |
| 660 |
$column_length->{$column} = length($td); |
| 661 |
} |
| 662 |
$column++; |
| 663 |
} |
| 664 |
} |
| 665 |
$column = 0; |
| 666 |
foreach (@{$titles}) { |
| 667 |
$column_length->{$column} = "%".($column_length->{$column} + 3)."s"; |
| 668 |
$column++; |
| 669 |
} |
| 670 |
$column = 0; |
| 671 |
foreach (@{$titles}) { |
| 672 |
$text .= sprintf $column_length->{$column++}, $_; |
| 673 |
} |
| 674 |
$text .= "\n"; |
| 675 |
foreach my $tr (@{$table}) { |
| 676 |
$column = 0; |
| 677 |
foreach my $td (@{$tr}) { |
| 678 |
$text .= sprintf $column_length->{$column++}, $td; |
| 679 |
} |
| 680 |
$text .= "\n"; |
| 681 |
} |
| 682 |
return $text; |
| 683 |
} |
| 684 |
|
| 685 |
sub table_html { |
| 686 |
my ($self, $table, $titles) = @_; |
| 687 |
my $text = ""; |
| 688 |
$text .= "<table style=\"border-collapse:collapse; border: 1px solid black;\">"; |
| 689 |
$text .= "<tr>"; |
| 690 |
foreach (@{$titles}) { |
| 691 |
$text .= sprintf "<th style=\"text-align: left; padding-left: 4px; padding-right: 6px;\">%s</th>", $_; |
| 692 |
} |
| 693 |
$text .= "</tr>"; |
| 694 |
foreach my $tr (@{$table}) { |
| 695 |
$text .= "<tr>"; |
| 696 |
foreach my $td (@{$tr}) { |
| 697 |
my $class = "statusOK"; |
| 698 |
if (ref($td) eq "ARRAY") { |
| 699 |
$class = { |
| 700 |
0 => "statusOK", |
| 701 |
1 => "statusWARNING", |
| 702 |
2 => "statusCRITICAL", |
| 703 |
3 => "statusUNKNOWN", |
| 704 |
}->{$td->[1]}; |
| 705 |
$td = $td->[0]; |
| 706 |
} |
| 707 |
$text .= sprintf "<td style=\"text-align: left; padding-left: 4px; padding-right: 6px;\" class=\"%s\">%s</td>", $class, $td; |
| 708 |
} |
| 709 |
$text .= "</tr>"; |
| 710 |
} |
| 711 |
$text .= "</table>"; |
| 712 |
return $text; |
| 713 |
} |
| 714 |
|
| 715 |
sub load_my_extension { |
| 716 |
my ($self) = @_; |
| 717 |
if ($self->opts->mode =~ /^my-([^-.]+)/) { |
| 718 |
my $class = $1; |
| 719 |
my $loaderror = undef; |
| 720 |
substr($class, 0, 1) = uc substr($class, 0, 1); |
| 721 |
if (! $self->opts->get("with-mymodules-dyn-dir")) { |
| 722 |
$self->override_opt("with-mymodules-dyn-dir", ""); |
| 723 |
} |
| 724 |
my $plugin_name = $Monitoring::GLPlugin::pluginname; |
| 725 |
$plugin_name =~ /check_(.*?)_health/; |
| 726 |
my $deprecated_class = "DBD::".(uc $1)."::Server"; |
| 727 |
$plugin_name = "Check".uc(substr($1, 0, 1)).substr($1, 1)."Health"; |
| 728 |
foreach my $libpath (split(":", $self->opts->get("with-mymodules-dyn-dir"))) { |
| 729 |
foreach my $extmod (glob $libpath."/".$plugin_name."*.pm") { |
| 730 |
my $stderrvar; |
| 731 |
*SAVEERR = *STDERR; |
| 732 |
open OUT ,'>',\$stderrvar; |
| 733 |
*STDERR = *OUT; |
| 734 |
eval { |
| 735 |
$self->debug(sprintf "loading module %s", $extmod); |
| 736 |
require $extmod; |
| 737 |
}; |
| 738 |
*STDERR = *SAVEERR; |
| 739 |
if ($@) { |
| 740 |
$loaderror = $extmod; |
| 741 |
$self->debug(sprintf "failed loading module %s: %s", $extmod, $@); |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
my $original_class = ref($self); |
| 746 |
my $original_init = $self->can("init"); |
| 747 |
$self->compatibility_class() if $self->can('compatibility_class'); |
| 748 |
bless $self, "My$class"; |
| 749 |
$self->compatibility_methods() if $self->can('compatibility_methods') && |
| 750 |
$self->isa($deprecated_class); |
| 751 |
if ($self->isa("Monitoring::GLPlugin")) { |
| 752 |
my $new_init = $self->can("init"); |
| 753 |
if ($new_init == $original_init) { |
| 754 |
$self->add_unknown( |
| 755 |
sprintf "Class %s needs an init() method", ref($self)); |
| 756 |
} else { |
| 757 |
# now go back to check_*_health.pl where init() will be called |
| 758 |
} |
| 759 |
} else { |
| 760 |
bless $self, $original_class; |
| 761 |
$self->add_unknown( |
| 762 |
sprintf "Class %s is not a subclass of Monitoring::GLPlugin%s", |
| 763 |
"My$class", |
| 764 |
$loaderror ? sprintf " (syntax error in %s?)", $loaderror : "" ); |
| 765 |
my ($code, $message) = $self->check_messages(join => ', ', join_all => ', '); |
| 766 |
$self->nagios_exit($code, $message); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
sub number_of_bits { |
| 772 |
my ($self, $unit) = @_; |
| 773 |
# https://en.wikipedia.org/wiki/Data_rate_units |
| 774 |
my $bits = { |
| 775 |
'bit' => 1, # Bit per second |
| 776 |
'B' => 8, # Byte per second, 8 bits per second |
| 777 |
'kbit' => 1000, # Kilobit per second, 1,000 bits per second |
| 778 |
'kb' => 1000, # Kilobit per second, 1,000 bits per second |
| 779 |
'Kibit' => 1024, # Kibibit per second, 1,024 bits per second |
| 780 |
'kB' => 8000, # Kilobyte per second, 8,000 bits per second |
| 781 |
'KiB' => 8192, # Kibibyte per second, 1,024 bytes per second |
| 782 |
'Mbit' => 1000000, # Megabit per second, 1,000,000 bits per second |
| 783 |
'Mb' => 1000000, # Megabit per second, 1,000,000 bits per second |
| 784 |
'Mibit' => 1048576, # Mebibit per second, 1,024 kibibits per second |
| 785 |
'MB' => 8000000, # Megabyte per second, 1,000 kilobytes per second |
| 786 |
'MiB' => 8388608, # Mebibyte per second, 1,024 kibibytes per second |
| 787 |
'Gbit' => 1000000000, # Gigabit per second, 1,000 megabits per second |
| 788 |
'Gb' => 1000000000, # Gigabit per second, 1,000 megabits per second |
| 789 |
'Gibit' => 1073741824, # Gibibit per second, 1,024 mebibits per second |
| 790 |
'GB' => 8000000000, # Gigabyte per second, 1,000 megabytes per second |
| 791 |
'GiB' => 8589934592, # Gibibyte per second, 8192 mebibits per second |
| 792 |
'Tbit' => 1000000000000, # Terabit per second, 1,000 gigabits per second |
| 793 |
'Tb' => 1000000000000, # Terabit per second, 1,000 gigabits per second |
| 794 |
'Tibit' => 1099511627776, # Tebibit per second, 1,024 gibibits per second |
| 795 |
'TB' => 8000000000000, # Terabyte per second, 1,000 gigabytes per second |
| 796 |
# eigene kreationen |
| 797 |
'Bits' => 1, |
| 798 |
'Bit' => 1, # Bit per second |
| 799 |
'KB' => 1024, # Kilobyte (like disk kilobyte) |
| 800 |
'KBi' => 1024, # -"- |
| 801 |
'MBi' => 1024 * 1024, # Megabyte (like disk megabyte) |
| 802 |
'GBi' => 1024 * 1024 * 1024, # Gigybate (like disk gigybyte) |
| 803 |
}; |
| 804 |
if (exists $bits->{$unit}) { |
| 805 |
return $bits->{$unit}; |
| 806 |
} else { |
| 807 |
return 0; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
######################################################### |
| 813 |
# runtime methods |
| 814 |
# |
| 815 |
sub mode : lvalue { |
| 816 |
my ($self) = @_; |
| 817 |
$Monitoring::GLPlugin::mode; |
| 818 |
} |
| 819 |
|
| 820 |
sub statefilesdir { |
| 821 |
my ($self) = @_; |
| 822 |
return $Monitoring::GLPlugin::plugin->{statefilesdir}; |
| 823 |
} |
| 824 |
|
| 825 |
sub opts { # die beiden _nicht_ in AUTOLOAD schieben, das kracht! |
| 826 |
my ($self) = @_; |
| 827 |
return $Monitoring::GLPlugin::plugin->opts(); |
| 828 |
} |
| 829 |
|
| 830 |
sub getopts { |
| 831 |
my ($self, $envparams) = @_; |
| 832 |
$envparams ||= []; |
| 833 |
my $needs_restart = 0; |
| 834 |
my @restart_opts = (); |
| 835 |
$Monitoring::GLPlugin::plugin->getopts(); |
| 836 |
# es kann sein, dass beim aufraeumen zum schluss als erstes objekt |
| 837 |
# das $Monitoring::GLPlugin::plugin geloescht wird. in anderen destruktoren |
| 838 |
# (insb. fuer dbi disconnect) steht dann $self->opts->verbose |
| 839 |
# nicht mehr zur verfuegung bzw. $Monitoring::GLPlugin::plugin->opts ist undef. |
| 840 |
$self->set_variable("verbose", $self->opts->verbose); |
| 841 |
$Monitoring::GLPlugin::tracefile = $self->opts->tracefile ? |
| 842 |
$self->opts->tracefile : |
| 843 |
$self->system_tmpdir()."/".$Monitoring::GLPlugin::pluginname.".trace"; |
| 844 |
if (! -f $Monitoring::GLPlugin::tracefile) { |
| 845 |
$Monitoring::GLPlugin::tracefile = undef; |
| 846 |
} |
| 847 |
# |
| 848 |
# die gueltigkeit von modes wird bereits hier geprueft und nicht danach |
| 849 |
# in validate_args. (zwischen getopts und validate_args wird |
| 850 |
# normalerweise classify aufgerufen, welches bereits eine verbindung |
| 851 |
# zum endgeraet herstellt. bei falschem mode waere das eine verschwendung |
| 852 |
# bzw. durch den exit3 ein evt. unsauberes beenden der verbindung. |
| 853 |
if ((! grep { $self->opts->mode eq $_ } map { $_->{spec} } @{$Monitoring::GLPlugin::plugin->{modes}}) && |
| 854 |
(! grep { $self->opts->mode eq $_ } map { defined $_->{alias} ? @{$_->{alias}} : () } @{$Monitoring::GLPlugin::plugin->{modes}})) { |
| 855 |
if ($self->opts->mode !~ /^my-/) { |
| 856 |
printf "UNKNOWN - mode %s\n", $self->opts->mode; |
| 857 |
$self->opts->print_help(); |
| 858 |
exit 3; |
| 859 |
} |
| 860 |
} |
| 861 |
if ($self->opts->environment) { |
| 862 |
# wenn die gewuenschten Environmentvariablen sich von den derzeit |
| 863 |
# gesetzten unterscheiden, dann restart. Denn $ENV aendert |
| 864 |
# _nicht_ das Environment des laufenden Prozesses. |
| 865 |
# $ENV{ZEUGS} = 1 bedeutet lediglich, dass $ENV{ZEUGS} bei weiterer |
| 866 |
# Verwendung 1 ist, bedeutet aber _nicht_, dass diese Variable |
| 867 |
# im Environment des laufenden Prozesses existiert. |
| 868 |
foreach (keys %{$self->opts->environment}) { |
| 869 |
if ((! $ENV{$_}) || ($ENV{$_} ne $self->opts->environment->{$_})) { |
| 870 |
$needs_restart = 1; |
| 871 |
$ENV{$_} = $self->opts->environment->{$_}; |
| 872 |
$self->debug(sprintf "new %s=%s forces restart\n", $_, $ENV{$_}); |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
if ($self->opts->runas) { |
| 877 |
# exec sudo $0 ... und dann ohne --runas |
| 878 |
$needs_restart = 1; |
| 879 |
# wenn wir environmentvariablen haben, die laut survive_sudo_env als |
| 880 |
# wichtig erachtet werden, dann muessen wir die ueber einen moeglichen |
| 881 |
# sudo-aufruf rueberretten, also in zusaetzliche --environment umwandenln. |
| 882 |
# sudo putzt das Environment naemlich aus. |
| 883 |
foreach my $survive_env (@{$Monitoring::GLPlugin::survive_sudo_env}) { |
| 884 |
if ($ENV{$survive_env} && ! scalar(grep { /^$survive_env=/ } |
| 885 |
keys %{$self->opts->environment})) { |
| 886 |
$self->opts->environment->{$survive_env} = $ENV{$survive_env}; |
| 887 |
printf STDERR "add important --environment %s=%s\n", |
| 888 |
$survive_env, $ENV{$survive_env} if $self->opts->verbose >= 2; |
| 889 |
push(@restart_opts, '--environment'); |
| 890 |
push(@restart_opts, sprintf '%s=%s', |
| 891 |
$survive_env, $ENV{$survive_env}); |
| 892 |
} |
| 893 |
} |
| 894 |
} |
| 895 |
if ($needs_restart) { |
| 896 |
foreach my $option (keys %{$self->opts->all_my_opts}) { |
| 897 |
# der fliegt raus, sonst gehts gleich wieder in needs_restart rein |
| 898 |
next if $option eq "runas"; |
| 899 |
foreach my $spec (map { $_->{spec} } @{$Monitoring::GLPlugin::plugin->opts->{_args}}) { |
| 900 |
if ($spec =~ /^([\-\w]+)[\?\+:\|\w+]*=(.*)/) { |
| 901 |
if ($1 eq $option && $2 =~ /s%/) { |
| 902 |
foreach (keys %{$self->opts->$option()}) { |
| 903 |
push(@restart_opts, sprintf "--%s", $option); |
| 904 |
push(@restart_opts, sprintf "%s=%s", $_, $self->opts->$option()->{$_}); |
| 905 |
} |
| 906 |
} elsif ($1 eq $option) { |
| 907 |
push(@restart_opts, sprintf "--%s", $option); |
| 908 |
push(@restart_opts, sprintf "%s", $self->opts->$option()); |
| 909 |
} |
| 910 |
} elsif ($spec eq $option) { |
| 911 |
push(@restart_opts, sprintf "--%s", $option); |
| 912 |
} |
| 913 |
} |
| 914 |
} |
| 915 |
if ($self->opts->runas && ($> == 0)) { |
| 916 |
# Ja, es gibt so Narrische, die gehen mit check_by_ssh als root |
| 917 |
# auf Datenbankmaschinen drauf und lassen dann dort check_oracle_health |
| 918 |
# laufen. Damit OPS$-Anmeldung dann funktioniert, wird mit --runas |
| 919 |
# auf eine andere Kennung umgeschwenkt. Diese Kennung gleich fuer |
| 920 |
# ssh zu verwenden geht aus Sicherheitsgruenden nicht. Narrische halt. |
| 921 |
exec "su", "-c", sprintf("%s %s", $0, join(" ", @restart_opts)), "-", $self->opts->runas; |
| 922 |
} elsif ($self->opts->runas) { |
| 923 |
exec "sudo", "-S", "-u", $self->opts->runas, $0, @restart_opts; |
| 924 |
} else { |
| 925 |
exec $0, @restart_opts; |
| 926 |
# dadurch werden SHLIB oder LD_LIBRARY_PATH sauber gesetzt, damit beim |
| 927 |
# erneuten Start libclntsh.so etc. gefunden werden. |
| 928 |
} |
| 929 |
exit; |
| 930 |
} |
| 931 |
if ($self->opts->shell) { |
| 932 |
# So komme ich bei den Narrischen zu einer root-Shell. |
| 933 |
system("/bin/sh"); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
sub add_ok { |
| 939 |
my ($self, $message) = @_; |
| 940 |
$message ||= $self->{info}; |
| 941 |
$self->add_message(OK, $message); |
| 942 |
} |
| 943 |
|
| 944 |
sub add_warning { |
| 945 |
my ($self, $message) = @_; |
| 946 |
$message ||= $self->{info}; |
| 947 |
$self->add_message(WARNING, $message); |
| 948 |
} |
| 949 |
|
| 950 |
sub add_critical { |
| 951 |
my ($self, $message) = @_; |
| 952 |
$message ||= $self->{info}; |
| 953 |
$self->add_message(CRITICAL, $message); |
| 954 |
} |
| 955 |
|
| 956 |
sub add_unknown { |
| 957 |
my ($self, $message) = @_; |
| 958 |
$message ||= $self->{info}; |
| 959 |
$self->add_message(UNKNOWN, $message); |
| 960 |
} |
| 961 |
|
| 962 |
sub add_ok_mitigation { |
| 963 |
my ($self, $message) = @_; |
| 964 |
if (defined $self->opts->mitigation()) { |
| 965 |
$self->add_message($self->opts->mitigation(), $message); |
| 966 |
} else { |
| 967 |
$self->add_ok($message); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
sub add_warning_mitigation { |
| 972 |
my ($self, $message) = @_; |
| 973 |
if (defined $self->opts->mitigation()) { |
| 974 |
$self->add_message($self->opts->mitigation(), $message); |
| 975 |
} else { |
| 976 |
$self->add_warning($message); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
sub add_critical_mitigation { |
| 981 |
my ($self, $message) = @_; |
| 982 |
if (defined $self->opts->mitigation()) { |
| 983 |
$self->add_message($self->opts->mitigation(), $message); |
| 984 |
} else { |
| 985 |
$self->add_critical($message); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
sub add_unknown_mitigation { |
| 990 |
my ($self, $message) = @_; |
| 991 |
if (defined $self->opts->mitigation()) { |
| 992 |
$self->add_message($self->opts->mitigation(), $message); |
| 993 |
} else { |
| 994 |
$self->add_unknown($message); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
sub add_message { |
| 999 |
my ($self, $level, $message) = @_; |
| 1000 |
$message ||= $self->{info}; |
| 1001 |
$Monitoring::GLPlugin::plugin->add_message($level, $message) |
| 1002 |
unless $self->is_blacklisted(); |
| 1003 |
if (exists $self->{failed}) { |
| 1004 |
if ($level == UNKNOWN && $self->{failed} == OK) { |
| 1005 |
$self->{failed} = $level; |
| 1006 |
} elsif ($level > $self->{failed}) { |
| 1007 |
$self->{failed} = $level; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
sub clear_ok { |
| 1013 |
my ($self) = @_; |
| 1014 |
$self->clear_messages(OK); |
| 1015 |
} |
| 1016 |
|
| 1017 |
sub clear_warning { |
| 1018 |
my ($self) = @_; |
| 1019 |
$self->clear_messages(WARNING); |
| 1020 |
} |
| 1021 |
|
| 1022 |
sub clear_critical { |
| 1023 |
my ($self) = @_; |
| 1024 |
$self->clear_messages(CRITICAL); |
| 1025 |
} |
| 1026 |
|
| 1027 |
sub clear_unknown { |
| 1028 |
my ($self) = @_; |
| 1029 |
$self->clear_messages(UNKNOWN); |
| 1030 |
} |
| 1031 |
|
| 1032 |
sub clear_all { # deprecated, use clear_messages |
| 1033 |
my ($self) = @_; |
| 1034 |
$self->clear_ok(); |
| 1035 |
$self->clear_warning(); |
| 1036 |
$self->clear_critical(); |
| 1037 |
$self->clear_unknown(); |
| 1038 |
} |
| 1039 |
|
| 1040 |
sub set_level { |
| 1041 |
my ($self, $code) = @_; |
| 1042 |
$code = (qw(ok warning critical unknown))[$code] if $code =~ /^\d+$/; |
| 1043 |
$code = lc $code; |
| 1044 |
if (! exists $self->{tmp_level}) { |
| 1045 |
$self->{tmp_level} = { |
| 1046 |
ok => 0, |
| 1047 |
warning => 0, |
| 1048 |
critical => 0, |
| 1049 |
unknown => 0, |
| 1050 |
}; |
| 1051 |
} |
| 1052 |
$self->{tmp_level}->{$code}++; |
| 1053 |
} |
| 1054 |
|
| 1055 |
sub get_level { |
| 1056 |
my ($self) = @_; |
| 1057 |
return OK if ! exists $self->{tmp_level}; |
| 1058 |
my $code = OK; |
| 1059 |
return CRITICAL if $self->{tmp_level}->{critical}; |
| 1060 |
return WARNING if $self->{tmp_level}->{warning}; |
| 1061 |
return UNKNOWN if $self->{tmp_level}->{unknown}; |
| 1062 |
return $code; |
| 1063 |
} |
| 1064 |
|
| 1065 |
######################################################### |
| 1066 |
# blacklisting |
| 1067 |
# |
| 1068 |
sub blacklist { |
| 1069 |
my ($self) = @_; |
| 1070 |
$self->{blacklisted} = 1; |
| 1071 |
} |
| 1072 |
|
| 1073 |
sub add_blacklist { |
| 1074 |
my ($self, $list) = @_; |
| 1075 |
$Monitoring::GLPlugin::blacklist = join('/', |
| 1076 |
(split('/', $self->opts->blacklist), $list)); |
| 1077 |
} |
| 1078 |
|
| 1079 |
sub is_blacklisted { |
| 1080 |
my ($self) = @_; |
| 1081 |
if (! $self->opts->can("blacklist")) { |
| 1082 |
return 0; |
| 1083 |
} |
| 1084 |
if (! exists $self->{blacklisted}) { |
| 1085 |
$self->{blacklisted} = 0; |
| 1086 |
} |
| 1087 |
if (exists $self->{blacklisted} && $self->{blacklisted}) { |
| 1088 |
return $self->{blacklisted}; |
| 1089 |
} |
| 1090 |
# FAN:459,203/TEMP:102229/ENVSUBSYSTEM |
| 1091 |
# FAN_459,FAN_203,TEMP_102229,ENVSUBSYSTEM |
| 1092 |
# ALERT:(The Storage Center is not able to access Tiebreaker)/TEMP:102229 |
| 1093 |
if ($self->opts->blacklist =~ /_/) { |
| 1094 |
foreach my $bl_item (split(/,/, $self->opts->blacklist)) { |
| 1095 |
if ($bl_item eq $self->internal_name()) { |
| 1096 |
$self->{blacklisted} = 1; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
} else { |
| 1100 |
foreach my $bl_items (split(/\//, $self->opts->blacklist)) { |
| 1101 |
if ($bl_items =~ /^(\w+):([\:\d\-\.,]+)$/) { |
| 1102 |
my $bl_type = $1; |
| 1103 |
my $bl_names = $2; |
| 1104 |
foreach my $bl_name (split(/,/, $bl_names)) { |
| 1105 |
if ($bl_type."_".$bl_name eq $self->internal_name()) { |
| 1106 |
$self->{blacklisted} = 1; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} elsif ($bl_items =~ /^(\w+):\((.*)\)$/ and $self->can("internal_content")) { |
| 1110 |
my $bl_type = $1; |
| 1111 |
my $bl_pattern = qr/$2/; |
| 1112 |
if ($self->internal_name() =~ /^${bl_type}_/) { |
| 1113 |
if ($self->internal_content() =~ /$bl_pattern/) { |
| 1114 |
$self->{blacklisted} = 1; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
} elsif ($bl_items =~ /^(\w+)$/) { |
| 1118 |
if ($bl_items eq $self->internal_name()) { |
| 1119 |
$self->{blacklisted} = 1; |
| 1120 |
} |
| 1121 |
} |
| 1122 |
} |
| 1123 |
} |
| 1124 |
return $self->{blacklisted}; |
| 1125 |
} |
| 1126 |
|
| 1127 |
######################################################### |
| 1128 |
# additional info |
| 1129 |
# |
| 1130 |
sub add_info { |
| 1131 |
my ($self, $info) = @_; |
| 1132 |
$info = $self->is_blacklisted() ? $info.' (blacklisted)' : $info; |
| 1133 |
$self->{info} = $info; |
| 1134 |
push(@{$Monitoring::GLPlugin::info}, $info); |
| 1135 |
} |
| 1136 |
|
| 1137 |
sub annotate_info { |
| 1138 |
my ($self, $annotation) = @_; |
| 1139 |
my $lastinfo = pop(@{$Monitoring::GLPlugin::info}); |
| 1140 |
$lastinfo .= sprintf ' (%s)', $annotation; |
| 1141 |
$self->{info} = $lastinfo; |
| 1142 |
push(@{$Monitoring::GLPlugin::info}, $lastinfo); |
| 1143 |
} |
| 1144 |
|
| 1145 |
sub add_extendedinfo { # deprecated |
| 1146 |
my ($self, $info) = @_; |
| 1147 |
$self->{extendedinfo} = $info; |
| 1148 |
return if ! $self->opts->extendedinfo; |
| 1149 |
push(@{$Monitoring::GLPlugin::extendedinfo}, $info); |
| 1150 |
} |
| 1151 |
|
| 1152 |
sub get_info { |
| 1153 |
my ($self, $separator) = @_; |
| 1154 |
$separator ||= ' '; |
| 1155 |
return join($separator , @{$Monitoring::GLPlugin::info}); |
| 1156 |
} |
| 1157 |
|
| 1158 |
sub get_last_info { |
| 1159 |
my ($self) = @_; |
| 1160 |
return pop(@{$Monitoring::GLPlugin::info}); |
| 1161 |
} |
| 1162 |
|
| 1163 |
sub get_extendedinfo { |
| 1164 |
my ($self, $separator) = @_; |
| 1165 |
$separator ||= ' '; |
| 1166 |
return join($separator, @{$Monitoring::GLPlugin::extendedinfo}); |
| 1167 |
} |
| 1168 |
|
| 1169 |
sub add_summary { # deprecated |
| 1170 |
my ($self, $summary) = @_; |
| 1171 |
push(@{$Monitoring::GLPlugin::summary}, $summary); |
| 1172 |
} |
| 1173 |
|
| 1174 |
sub get_summary { |
| 1175 |
my ($self) = @_; |
| 1176 |
return join(', ', @{$Monitoring::GLPlugin::summary}); |
| 1177 |
} |
| 1178 |
|
| 1179 |
######################################################### |
| 1180 |
# persistency |
| 1181 |
# |
| 1182 |
sub valdiff { |
| 1183 |
my ($self, $pparams, @keys) = @_; |
| 1184 |
my %params = %{$pparams}; |
| 1185 |
my $now = time; |
| 1186 |
my $newest_history_set = {}; |
| 1187 |
$params{freeze} = 0 if ! $params{freeze}; |
| 1188 |
my $mode = "normal"; |
| 1189 |
if ($self->opts->lookback && $self->opts->lookback == 99999 && $params{freeze} == 0) { |
| 1190 |
$mode = "lookback_freeze_chill"; |
| 1191 |
} elsif ($self->opts->lookback && $self->opts->lookback == 99999 && $params{freeze} == 1) { |
| 1192 |
$mode = "lookback_freeze_shockfrost"; |
| 1193 |
} elsif ($self->opts->lookback && $self->opts->lookback == 99999 && $params{freeze} == 2) { |
| 1194 |
$mode = "lookback_freeze_defrost"; |
| 1195 |
} elsif ($self->opts->lookback) { |
| 1196 |
$mode = "lookback"; |
| 1197 |
} |
| 1198 |
# lookback=99999, freeze=0(default) |
| 1199 |
# nimm den letzten lauf und schreib ihn nach {cold} |
| 1200 |
# vergleich dann |
| 1201 |
# wenn es frozen gibt, vergleich frozen und den letzten lauf |
| 1202 |
# sonst den letzten lauf und den aktuellen lauf |
| 1203 |
# lookback=99999, freeze=1 |
| 1204 |
# wird dann aufgerufen,wenn nach dem freeze=0 ein problem festgestellt wurde |
| 1205 |
# (also als 2.valdiff hinterher) |
| 1206 |
# schreib cold nach frozen |
| 1207 |
# lookback=99999, freeze=2 |
| 1208 |
# wird dann aufgerufen,wenn nach dem freeze=0 wieder alles ok ist |
| 1209 |
# (also als 2.valdiff hinterher) |
| 1210 |
# loescht frozen |
| 1211 |
# |
| 1212 |
my $last_values = $self->load_state(%params) || eval { |
| 1213 |
my $empty_events = {}; |
| 1214 |
foreach (@keys) { |
| 1215 |
if (ref($self->{$_}) eq "ARRAY") { |
| 1216 |
$empty_events->{$_} = []; |
| 1217 |
} else { |
| 1218 |
$empty_events->{$_} = 0; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
$empty_events->{timestamp} = 0; |
| 1222 |
if ($mode eq "lookback") { |
| 1223 |
$empty_events->{lookback_history} = {}; |
| 1224 |
} elsif ($mode eq "lookback_freeze_chill") { |
| 1225 |
$empty_events->{cold} = {}; |
| 1226 |
$empty_events->{frozen} = {}; |
| 1227 |
} |
| 1228 |
$empty_events; |
| 1229 |
}; |
| 1230 |
$self->{'delta_timestamp'} = $now - $last_values->{timestamp}; |
| 1231 |
foreach (@keys) { |
| 1232 |
if ($mode eq "lookback_freeze_chill") { |
| 1233 |
# die werte vom letzten lauf wegsichern. |
| 1234 |
# vielleicht gibts gleich einen freeze=1, dann muessen die eingefroren werden |
| 1235 |
if (exists $last_values->{$_}) { |
| 1236 |
if (ref($self->{$_}) eq "ARRAY") { |
| 1237 |
$last_values->{cold}->{$_} = []; |
| 1238 |
foreach my $value (@{$last_values->{$_}}) { |
| 1239 |
push(@{$last_values->{cold}->{$_}}, $value); |
| 1240 |
} |
| 1241 |
} else { |
| 1242 |
$last_values->{cold}->{$_} = $last_values->{$_}; |
| 1243 |
} |
| 1244 |
} else { |
| 1245 |
if (ref($self->{$_}) eq "ARRAY") { |
| 1246 |
$last_values->{cold}->{$_} = []; |
| 1247 |
} else { |
| 1248 |
$last_values->{cold}->{$_} = 0; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
# es wird so getan, als sei der frozen wert vom letzten lauf |
| 1252 |
if (exists $last_values->{frozen}->{$_}) { |
| 1253 |
if (ref($self->{$_}) eq "ARRAY") { |
| 1254 |
$last_values->{$_} = []; |
| 1255 |
foreach my $value (@{$last_values->{frozen}->{$_}}) { |
| 1256 |
push(@{$last_values->{$_}}, $value); |
| 1257 |
} |
| 1258 |
} else { |
| 1259 |
$last_values->{$_} = $last_values->{frozen}->{$_}; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
} elsif ($mode eq "lookback") { |
| 1263 |
# find a last_value in the history which fits lookback best |
| 1264 |
# and overwrite $last_values->{$_} with historic data |
| 1265 |
if (exists $last_values->{lookback_history}->{$_}) { |
| 1266 |
foreach my $date (sort {$a <=> $b} keys %{$last_values->{lookback_history}->{$_}}) { |
| 1267 |
$newest_history_set->{$_} = $last_values->{lookback_history}->{$_}->{$date}; |
| 1268 |
$newest_history_set->{timestamp} = $date; |
| 1269 |
} |
| 1270 |
foreach my $date (sort {$a <=> $b} keys %{$last_values->{lookback_history}->{$_}}) { |
| 1271 |
if ($date >= ($now - $self->opts->lookback)) { |
| 1272 |
$last_values->{$_} = $last_values->{lookback_history}->{$_}->{$date}; |
| 1273 |
$last_values->{timestamp} = $date; |
| 1274 |
$self->{'delta_timestamp'} = $now - $last_values->{timestamp}; |
| 1275 |
if (ref($last_values->{$_}) eq "ARRAY") { |
| 1276 |
$self->debug(sprintf "oldest value of %s within lookback is size %s (age %d)", |
| 1277 |
$_, scalar(@{$last_values->{$_}}), $now - $date); |
| 1278 |
} else { |
| 1279 |
$self->debug(sprintf "oldest value of %s within lookback is %s (age %d)", |
| 1280 |
$_, $last_values->{$_}, $now - $date); |
| 1281 |
} |
| 1282 |
last; |
| 1283 |
} else { |
| 1284 |
$self->debug(sprintf "deprecate %s of age %d", $_, time - $date); |
| 1285 |
delete $last_values->{lookback_history}->{$_}->{$date}; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
if ($mode eq "normal" || $mode eq "lookback" || $mode eq "lookback_freeze_chill") { |
| 1291 |
if (exists $self->{$_} && defined $self->{$_} && $self->{$_} =~ /^\d+\.*\d*$/) { |
| 1292 |
# $VAR1 = { 'sysStatTmSleepCycles' => '', |
| 1293 |
# no idea why this happens, but we can repair it. |
| 1294 |
$last_values->{$_} = $self->{$_} if ! (exists $last_values->{$_} && defined $last_values->{$_} && $last_values->{$_} ne ""); |
| 1295 |
if ($self->{$_} >= $last_values->{$_}) { |
| 1296 |
$self->{'delta_'.$_} = $self->{$_} - $last_values->{$_}; |
| 1297 |
} elsif ($self->{$_} eq $last_values->{$_}) { |
| 1298 |
# dawischt! in einem fall wurde 131071.999023438 >= 131071.999023438 da oben nicht erkannt |
| 1299 |
# subtrahieren ging auch daneben, weil ein winziger negativer wert rauskam. |
| 1300 |
$self->{'delta_'.$_} = 0; |
| 1301 |
} else { |
| 1302 |
if ($mode =~ /lookback_freeze/) { |
| 1303 |
# hier koennen delta-werte auch negativ sein, wenn z.b. peers verschwinden |
| 1304 |
$self->{'delta_'.$_} = $self->{$_} - $last_values->{$_}; |
| 1305 |
} elsif (exists $params{lastarray}) { |
| 1306 |
$self->{'delta_'.$_} = $self->{$_} - $last_values->{$_}; |
| 1307 |
} else { |
| 1308 |
# vermutlich db restart und zaehler alle auf null |
| 1309 |
$self->{'delta_'.$_} = $self->{$_}; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
$self->debug(sprintf "delta_%s %f", $_, $self->{'delta_'.$_}); |
| 1313 |
$self->{$_.'_per_sec'} = $self->{'delta_timestamp'} ? |
| 1314 |
$self->{'delta_'.$_} / $self->{'delta_timestamp'} : 0; |
| 1315 |
} elsif (ref($self->{$_}) eq "ARRAY") { |
| 1316 |
if ((! exists $last_values->{$_} || ! defined $last_values->{$_}) && exists $params{lastarray}) { |
| 1317 |
# innerhalb der lookback-zeit wurde nichts in der lookback_history |
| 1318 |
# gefunden. allenfalls irgendwas aelteres. normalerweise |
| 1319 |
# wuerde jetzt das array als [] initialisiert. |
| 1320 |
# d.h. es wuerde ein delta geben, @found s.u. |
| 1321 |
# wenn man das nicht will, sondern einfach aktuelles array mit |
| 1322 |
# dem array des letzten laufs vergleichen will, setzt man lastarray |
| 1323 |
$last_values->{$_} = %{$newest_history_set} ? |
| 1324 |
$newest_history_set->{$_} : [] |
| 1325 |
} elsif ((! exists $last_values->{$_} || ! defined $last_values->{$_}) && ! exists $params{lastarray}) { |
| 1326 |
$last_values->{$_} = [] if ! exists $last_values->{$_}; |
| 1327 |
} elsif (exists $last_values->{$_} && ! defined $last_values->{$_}) { |
| 1328 |
# $_ kann es auch ausserhalb des lookback_history-keys als normalen |
| 1329 |
# key geben. der zeigt normalerweise auf den entspr. letzten |
| 1330 |
# lookback_history eintrag. wurde der wegen ueberalterung abgeschnitten |
| 1331 |
# ist der hier auch undef. |
| 1332 |
$last_values->{$_} = %{$newest_history_set} ? |
| 1333 |
$newest_history_set->{$_} : [] |
| 1334 |
} |
| 1335 |
my %saved = map { $_ => 1 } @{$last_values->{$_}}; |
| 1336 |
my %current = map { $_ => 1 } @{$self->{$_}}; |
| 1337 |
my @found = grep(!defined $saved{$_}, @{$self->{$_}}); |
| 1338 |
my @lost = grep(!defined $current{$_}, @{$last_values->{$_}}); |
| 1339 |
$self->{'delta_found_'.$_} = \@found; |
| 1340 |
$self->{'delta_lost_'.$_} = \@lost; |
| 1341 |
} else { |
| 1342 |
# nicht ganz sauber, aber das artet aus, wenn man jedem uninitialized hinterherstochert. |
| 1343 |
# wem das nicht passt, der kann gerne ein paar tage debugging beauftragen. |
| 1344 |
# das kostet aber mehr als drei kugeln eis. |
| 1345 |
$last_values->{$_} = 0 if ! (exists $last_values->{$_} && defined $last_values->{$_} && $last_values->{$_} ne ""); |
| 1346 |
$self->{$_} = 0 if ! (exists $self->{$_} && defined $self->{$_} && $self->{$_} ne ""); |
| 1347 |
$self->{'delta_'.$_} = 0; |
| 1348 |
} |
| 1349 |
} |
| 1350 |
} |
| 1351 |
$params{save} = eval { |
| 1352 |
my $empty_events = {}; |
| 1353 |
foreach (@keys) { |
| 1354 |
$empty_events->{$_} = $self->{$_}; |
| 1355 |
if ($mode =~ /lookback_freeze/) { |
| 1356 |
if (exists $last_values->{frozen}->{$_}) { |
| 1357 |
if (ref($last_values->{frozen}->{$_}) eq "ARRAY") { |
| 1358 |
@{$empty_events->{cold}->{$_}} = @{$last_values->{frozen}->{$_}}; |
| 1359 |
} else { |
| 1360 |
$empty_events->{cold}->{$_} = $last_values->{frozen}->{$_}; |
| 1361 |
} |
| 1362 |
} else { |
| 1363 |
if (ref($last_values->{cold}->{$_}) eq "ARRAY") { |
| 1364 |
@{$empty_events->{cold}->{$_}} = @{$last_values->{cold}->{$_}}; |
| 1365 |
} else { |
| 1366 |
$empty_events->{cold}->{$_} = $last_values->{cold}->{$_}; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
$empty_events->{cold}->{timestamp} = $last_values->{cold}->{timestamp}; |
| 1370 |
} |
| 1371 |
if ($mode eq "lookback_freeze_shockfrost") { |
| 1372 |
if (ref($empty_events->{cold}->{$_}) eq "ARRAY") { |
| 1373 |
@{$empty_events->{frozen}->{$_}} = @{$empty_events->{cold}->{$_}}; |
| 1374 |
} else { |
| 1375 |
$empty_events->{frozen}->{$_} = $empty_events->{cold}->{$_}; |
| 1376 |
} |
| 1377 |
$empty_events->{frozen}->{timestamp} = $now; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
$empty_events->{timestamp} = $now; |
| 1381 |
if ($mode eq "lookback") { |
| 1382 |
$empty_events->{lookback_history} = $last_values->{lookback_history}; |
| 1383 |
foreach (@keys) { |
| 1384 |
if (ref($self->{$_}) eq "ARRAY") { |
| 1385 |
@{$empty_events->{lookback_history}->{$_}->{$now}} = @{$self->{$_}}; |
| 1386 |
} else { |
| 1387 |
$empty_events->{lookback_history}->{$_}->{$now} = $self->{$_}; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
} |
| 1391 |
if ($mode eq "lookback_freeze_defrost") { |
| 1392 |
delete $empty_events->{freeze}; |
| 1393 |
} |
| 1394 |
$empty_events; |
| 1395 |
}; |
| 1396 |
$self->save_state(%params); |
| 1397 |
} |
| 1398 |
|
| 1399 |
sub create_statefilesdir { |
| 1400 |
my ($self) = @_; |
| 1401 |
if (! -d $self->statefilesdir()) { |
| 1402 |
eval { |
| 1403 |
use File::Path; |
| 1404 |
mkpath $self->statefilesdir(); |
| 1405 |
}; |
| 1406 |
if ($@ || ! -w $self->statefilesdir()) { |
| 1407 |
$self->add_message(UNKNOWN, |
| 1408 |
sprintf "cannot create status dir %s! check your filesystem (permissions/usage/integrity) and disk devices", $self->statefilesdir()); |
| 1409 |
} |
| 1410 |
} elsif (! -w $self->statefilesdir()) { |
| 1411 |
$self->add_message(UNKNOWN, |
| 1412 |
sprintf "cannot write status dir %s! check your filesystem (permissions/usage/integrity) and disk devices", $self->statefilesdir()); |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
sub create_statefile { |
| 1417 |
my ($self, %params) = @_; |
| 1418 |
my $extension = ""; |
| 1419 |
$extension .= $params{name} ? '_'.$params{name} : ''; |
| 1420 |
$extension =~ s/\//_/g; |
| 1421 |
$extension =~ s/\(/_/g; |
| 1422 |
$extension =~ s/\)/_/g; |
| 1423 |
$extension =~ s/\*/_/g; |
| 1424 |
$extension =~ s/\s/_/g; |
| 1425 |
return sprintf "%s/%s%s", $self->statefilesdir(), |
| 1426 |
$self->clean_path($self->mode), $self->clean_path(lc $extension); |
| 1427 |
} |
| 1428 |
|
| 1429 |
sub clean_path { |
| 1430 |
my ($self, $path) = @_; |
| 1431 |
if ($^O =~ /MSWin/) { |
| 1432 |
$path =~ s/:/_/g; |
| 1433 |
} |
| 1434 |
return $path; |
| 1435 |
} |
| 1436 |
|
| 1437 |
sub schimpf { |
| 1438 |
my ($self) = @_; |
| 1439 |
printf "statefilesdir %s is not writable.\nYou didn't run this plugin as root, didn't you?\n", $self->statefilesdir(); |
| 1440 |
} |
| 1441 |
|
| 1442 |
# $self->protect_value('1.1-flat_index', 'cpu_busy', 'percent'); |
| 1443 |
sub protect_value { |
| 1444 |
my ($self, $ident, $key, $validfunc) = @_; |
| 1445 |
if (ref($validfunc) ne "CODE" && $validfunc eq "percent") { |
| 1446 |
$validfunc = sub { |
| 1447 |
my $value = shift; |
| 1448 |
return 0 if ! defined $value; |
| 1449 |
return 0 if $value !~ /^[-+]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$/; |
| 1450 |
return ($value < 0 || $value > 100) ? 0 : 1; |
| 1451 |
}; |
| 1452 |
} elsif (ref($validfunc) ne "CODE" && $validfunc eq "positive") { |
| 1453 |
$validfunc = sub { |
| 1454 |
my $value = shift; |
| 1455 |
return 0 if ! defined $value; |
| 1456 |
return 0 if $value !~ /^[-+]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$/; |
| 1457 |
return ($value < 0) ? 0 : 1; |
| 1458 |
}; |
| 1459 |
} |
| 1460 |
if (&$validfunc($self->{$key})) { |
| 1461 |
$self->save_state(name => 'protect_'.$ident.'_'.$key, save => { |
| 1462 |
$key => $self->{$key}, |
| 1463 |
exception => 0, |
| 1464 |
}); |
| 1465 |
} else { |
| 1466 |
# if the device gives us an clearly wrong value, simply use the last value. |
| 1467 |
my $laststate = $self->load_state(name => 'protect_'.$ident.'_'.$key) || { |
| 1468 |
exception => 0, |
| 1469 |
}; |
| 1470 |
$self->debug(sprintf "self->{%s} is %s and invalid for the %dth time", |
| 1471 |
$key, defined $self->{$key} ? $self->{$key} : "<undef>", |
| 1472 |
$laststate->{exception} + 1); |
| 1473 |
if ($laststate->{exception} <= 5) { |
| 1474 |
# but only 5 times. |
| 1475 |
# if the error persists, somebody has to check the device. |
| 1476 |
$self->{$key} = $laststate->{$key}; |
| 1477 |
} |
| 1478 |
$self->save_state(name => 'protect_'.$ident.'_'.$key, save => { |
| 1479 |
$key => $laststate->{$key}, |
| 1480 |
exception => ++$laststate->{exception}, |
| 1481 |
}); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
|
| 1485 |
sub save_state { |
| 1486 |
my ($self, %params) = @_; |
| 1487 |
$self->create_statefilesdir(); |
| 1488 |
my $statefile = $self->create_statefile(%params); |
| 1489 |
my $tmpfile = $statefile.$$.rand(); |
| 1490 |
if ((ref($params{save}) eq "HASH") && exists $params{save}->{timestamp}) { |
| 1491 |
$params{save}->{localtime} = scalar localtime $params{save}->{timestamp}; |
| 1492 |
} |
| 1493 |
my $seekfh = IO::File->new(); |
| 1494 |
if ($seekfh->open($tmpfile, "w")) { |
| 1495 |
my $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
| 1496 |
my $jsonscalar = $coder->encode($params{save}); |
| 1497 |
$seekfh->print($jsonscalar); |
| 1498 |
# the very time-consuming old way. |
| 1499 |
# $seekfh->printf("%s", Data::Dumper::Dumper($params{save})); |
| 1500 |
$seekfh->flush(); |
| 1501 |
$seekfh->close(); |
| 1502 |
$self->debug(sprintf "saved %s to %s", |
| 1503 |
Data::Dumper::Dumper($params{save}), $statefile); |
| 1504 |
} |
| 1505 |
if (! rename $tmpfile, $statefile) { |
| 1506 |
$self->add_message(UNKNOWN, |
| 1507 |
sprintf "cannot write status file %s! check your filesystem (permissions/usage/integrity) and disk devices", $statefile); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
sub load_state { |
| 1512 |
my ($self, %params) = @_; |
| 1513 |
my $statefile = $self->create_statefile(%params); |
| 1514 |
if ( -f $statefile) { |
| 1515 |
our $VAR1; |
| 1516 |
eval { |
| 1517 |
delete $INC{$statefile} if exists $INC{$statefile}; # else unit tests fail |
| 1518 |
my $jsonscalar = read_file($statefile); |
| 1519 |
my $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
| 1520 |
$VAR1 = $coder->decode($jsonscalar); |
| 1521 |
}; |
| 1522 |
if($@) { |
| 1523 |
$self->debug(sprintf "json load from %s failed. fallback", $statefile); |
| 1524 |
eval { |
| 1525 |
require $statefile; |
| 1526 |
}; |
| 1527 |
if($@) { |
| 1528 |
printf "FATAL: Could not load old state in perl format!\n"; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
$self->debug(sprintf "load %s from %s", Data::Dumper::Dumper($VAR1), $statefile); |
| 1532 |
return $VAR1; |
| 1533 |
} else { |
| 1534 |
return undef; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
######################################################### |
| 1539 |
# daemon mode |
| 1540 |
# |
| 1541 |
sub check_pidfile { |
| 1542 |
my ($self) = @_; |
| 1543 |
my $fh = IO::File->new(); |
| 1544 |
if ($fh->open($self->{pidfile}, "r")) { |
| 1545 |
my $pid = $fh->getline(); |
| 1546 |
$fh->close(); |
| 1547 |
if (! $pid) { |
| 1548 |
$self->debug("Found pidfile %s with no valid pid. Exiting.", |
| 1549 |
$self->{pidfile}); |
| 1550 |
return 0; |
| 1551 |
} else { |
| 1552 |
$self->debug("Found pidfile %s with pid %d", $self->{pidfile}, $pid); |
| 1553 |
kill 0, $pid; |
| 1554 |
if ($! == Errno::ESRCH) { |
| 1555 |
$self->debug("This pidfile is stale. Writing a new one"); |
| 1556 |
$self->write_pidfile(); |
| 1557 |
return 1; |
| 1558 |
} else { |
| 1559 |
$self->debug("This pidfile is held by a running process. Exiting"); |
| 1560 |
return 0; |
| 1561 |
} |
| 1562 |
} |
| 1563 |
} else { |
| 1564 |
$self->debug("Found no pidfile. Writing a new one"); |
| 1565 |
$self->write_pidfile(); |
| 1566 |
return 1; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
sub write_pidfile { |
| 1571 |
my ($self) = @_; |
| 1572 |
if (! -d dirname($self->{pidfile})) { |
| 1573 |
eval "require File::Path;"; |
| 1574 |
if (defined(&File::Path::mkpath)) { |
| 1575 |
import File::Path; |
| 1576 |
eval { mkpath(dirname($self->{pidfile})); }; |
| 1577 |
} else { |
| 1578 |
my @dirs = (); |
| 1579 |
map { |
| 1580 |
push @dirs, $_; |
| 1581 |
mkdir(join('/', @dirs)) |
| 1582 |
if join('/', @dirs) && ! -d join('/', @dirs); |
| 1583 |
} split(/\//, dirname($self->{pidfile})); |
| 1584 |
} |
| 1585 |
} |
| 1586 |
my $fh = IO::File->new(); |
| 1587 |
$fh->autoflush(1); |
| 1588 |
if ($fh->open($self->{pidfile}, "w")) { |
| 1589 |
$fh->printf("%s", $$); |
| 1590 |
$fh->close(); |
| 1591 |
} else { |
| 1592 |
$self->debug("Could not write pidfile %s", $self->{pidfile}); |
| 1593 |
die "pid file could not be written"; |
| 1594 |
} |
| 1595 |
} |
| 1596 |
|
| 1597 |
sub system_vartmpdir { |
| 1598 |
my ($self) = @_; |
| 1599 |
if ($^O =~ /MSWin/) { |
| 1600 |
return $self->system_tmpdir(); |
| 1601 |
} else { |
| 1602 |
return "/var/tmp/".$Monitoring::GLPlugin::pluginname; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
sub system_tmpdir { |
| 1607 |
my ($self) = @_; |
| 1608 |
if ($^O =~ /MSWin/) { |
| 1609 |
return $ENV{TEMP} if defined $ENV{TEMP}; |
| 1610 |
return $ENV{TMP} if defined $ENV{TMP}; |
| 1611 |
return File::Spec->catfile($ENV{windir}, 'Temp') |
| 1612 |
if defined $ENV{windir}; |
| 1613 |
return 'C:\Temp'; |
| 1614 |
} else { |
| 1615 |
return "/tmp"; |
| 1616 |
} |
| 1617 |
} |
| 1618 |
|
| 1619 |
sub convert_scientific_numbers { |
| 1620 |
my ($self, $n) = @_; |
| 1621 |
# mostly used to convert numbers in scientific notation |
| 1622 |
if ($n =~ /^\s*\d+\s*$/) { |
| 1623 |
return $n; |
| 1624 |
} elsif ($n =~ /^\s*([-+]?)(\d*[\.,]*\d*)[eE]{1}([-+]?)(\d+)\s*$/) { |
| 1625 |
my ($vor, $num, $sign, $exp) = ($1, $2, $3, $4); |
| 1626 |
$n =~ s/E/e/g; |
| 1627 |
$n =~ s/,/\./g; |
| 1628 |
$num =~ s/,/\./g; |
| 1629 |
my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : ''; |
| 1630 |
my $dec = sprintf "%${sig}f", $n; |
| 1631 |
$dec =~ s/\.[0]+$//g; |
| 1632 |
return $dec; |
| 1633 |
} elsif ($n =~ /^\s*([-+]?)(\d+)[\.,]*(\d*)\s*$/) { |
| 1634 |
return $1.$2.".".$3; |
| 1635 |
} elsif ($n =~ /^\s*(.*?)\s*$/) { |
| 1636 |
return $1; |
| 1637 |
} else { |
| 1638 |
return $n; |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
sub compatibility_methods { |
| 1643 |
my ($self) = @_; |
| 1644 |
# add_perfdata |
| 1645 |
# add_message |
| 1646 |
# nagios_exit |
| 1647 |
# ->{warningrange} |
| 1648 |
# ->{criticalrange} |
| 1649 |
# ... |
| 1650 |
$self->{warningrange} = ($self->get_thresholds())[0]; |
| 1651 |
$self->{criticalrange} = ($self->get_thresholds())[1]; |
| 1652 |
my $old_init = $self->can('init'); |
| 1653 |
my %params = ( |
| 1654 |
'mode' => join('::', split(/-/, $self->opts->mode)), |
| 1655 |
'name' => $self->opts->name, |
| 1656 |
'name2' => $self->opts->name2, |
| 1657 |
); |
| 1658 |
{ |
| 1659 |
no strict 'refs'; |
| 1660 |
no warnings 'redefine'; |
| 1661 |
*{ref($self).'::init'} = sub { |
| 1662 |
$self->$old_init(%params); |
| 1663 |
$self->nagios(%params); |
| 1664 |
}; |
| 1665 |
*{ref($self).'::add_nagios'} = \&{"Monitoring::GLPlugin::add_message"}; |
| 1666 |
*{ref($self).'::add_nagios_ok'} = \&{"Monitoring::GLPlugin::add_ok"}; |
| 1667 |
*{ref($self).'::add_nagios_warning'} = \&{"Monitoring::GLPlugin::add_warning"}; |
| 1668 |
*{ref($self).'::add_nagios_critical'} = \&{"Monitoring::GLPlugin::add_critical"}; |
| 1669 |
*{ref($self).'::add_nagios_unknown'} = \&{"Monitoring::GLPlugin::add_unknown"}; |
| 1670 |
*{ref($self).'::add_perfdata'} = sub { |
| 1671 |
my $self = shift; |
| 1672 |
my $message = shift; |
| 1673 |
foreach my $perfdata (split(/\s+/, $message)) { |
| 1674 |
my ($label, $perfstr) = split(/=/, $perfdata); |
| 1675 |
my ($value, $warn, $crit, $min, $max) = split(/;/, $perfstr); |
| 1676 |
$value =~ /^([\d\.\-\+]+)(.*)$/; |
| 1677 |
$value = $1; |
| 1678 |
my $uom = $2; |
| 1679 |
$Monitoring::GLPlugin::plugin->add_perfdata( |
| 1680 |
label => $label, |
| 1681 |
value => $value, |
| 1682 |
uom => $uom, |
| 1683 |
warn => $warn, |
| 1684 |
crit => $crit, |
| 1685 |
min => $min, |
| 1686 |
max => $max, |
| 1687 |
); |
| 1688 |
} |
| 1689 |
}; |
| 1690 |
*{ref($self).'::check_thresholds'} = sub { |
| 1691 |
my $self = shift; |
| 1692 |
my $value = shift; |
| 1693 |
my $defaultwarningrange = shift; |
| 1694 |
my $defaultcriticalrange = shift; |
| 1695 |
$Monitoring::GLPlugin::plugin->set_thresholds( |
| 1696 |
metric => 'default', |
| 1697 |
warning => $defaultwarningrange, |
| 1698 |
critical => $defaultcriticalrange, |
| 1699 |
); |
| 1700 |
$self->{warningrange} = ($self->get_thresholds())[0]; |
| 1701 |
$self->{criticalrange} = ($self->get_thresholds())[1]; |
| 1702 |
return $Monitoring::GLPlugin::plugin->check_thresholds( |
| 1703 |
metric => 'default', |
| 1704 |
value => $value, |
| 1705 |
warning => $defaultwarningrange, |
| 1706 |
critical => $defaultcriticalrange, |
| 1707 |
); |
| 1708 |
}; |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
sub AUTOLOAD { |
| 1713 |
my ($self, @params) = @_; |
| 1714 |
return if ($AUTOLOAD =~ /DESTROY/); |
| 1715 |
$self->debug("AUTOLOAD %s\n", $AUTOLOAD) |
| 1716 |
if $self->opts->verbose >= 2; |
| 1717 |
if ($AUTOLOAD =~ /^(.*)::analyze_and_check_(.*)_subsystem$/) { |
| 1718 |
my $class = $1; |
| 1719 |
my $subsystem = $2; |
| 1720 |
my $analyze = sprintf "analyze_%s_subsystem", $subsystem; |
| 1721 |
my $check = sprintf "check_%s_subsystem", $subsystem; |
| 1722 |
if (@params) { |
| 1723 |
# analyzer class |
| 1724 |
my $subsystem_class = shift @params; |
| 1725 |
$self->{components}->{$subsystem.'_subsystem'} = $subsystem_class->new(); |
| 1726 |
$self->debug(sprintf "\$self->{components}->{%s_subsystem} = %s->new()", |
| 1727 |
$subsystem, $subsystem_class); |
| 1728 |
} else { |
| 1729 |
$self->$analyze(); |
| 1730 |
$self->debug("call %s()", $analyze); |
| 1731 |
} |
| 1732 |
$self->$check(); |
| 1733 |
} elsif ($AUTOLOAD =~ /^(.*)::check_(.*)_subsystem$/) { |
| 1734 |
my $class = $1; |
| 1735 |
my $subsystem = sprintf "%s_subsystem", $2; |
| 1736 |
$self->{components}->{$subsystem}->check(); |
| 1737 |
$self->{components}->{$subsystem}->dump() |
| 1738 |
if $self->opts->verbose >= 2; |
| 1739 |
} elsif ($AUTOLOAD =~ /^.*::(status_code|check_messages|nagios_exit|html_string|perfdata_string|selected_perfdata|check_thresholds|get_thresholds|opts|pandora_string|strequal)$/) { |
| 1740 |
return $Monitoring::GLPlugin::plugin->$1(@params); |
| 1741 |
} elsif ($AUTOLOAD =~ /^.*::(reduce_messages|reduce_messages_short|clear_messages|suppress_messages|add_html|add_perfdata|override_opt|create_opt|set_thresholds|force_thresholds|add_pandora)$/) { |
| 1742 |
$Monitoring::GLPlugin::plugin->$1(@params); |
| 1743 |
} elsif ($AUTOLOAD =~ /^.*::mod_arg_(.*)$/) { |
| 1744 |
return $Monitoring::GLPlugin::plugin->mod_arg($1, @params); |
| 1745 |
} else { |
| 1746 |
$self->debug("AUTOLOAD: class %s has no method %s\n", |
| 1747 |
ref($self), $AUTOLOAD); |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
1; |
| 1752 |
|
| 1753 |
__END__ |