| 1 |
package DBD::MySQL::Server; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Time::HiRes; |
| 5 |
use IO::File; |
| 6 |
use File::Copy 'cp'; |
| 7 |
use Data::Dumper; |
| 8 |
|
| 9 |
my %ERRORS=( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 ); |
| 10 |
my %ERRORCODES=( 0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN' ); |
| 11 |
|
| 12 |
{ |
| 13 |
our $verbose = 0; |
| 14 |
our $scream = 0; # scream if something is not implemented |
| 15 |
our $access = "dbi"; # how do we access the database. |
| 16 |
our $my_modules_dyn_dir = ""; # where we look for self-written extensions |
| 17 |
|
| 18 |
my @servers = (); |
| 19 |
my $initerrors = undef; |
| 20 |
|
| 21 |
sub add_server { |
| 22 |
push(@servers, shift); |
| 23 |
} |
| 24 |
|
| 25 |
sub return_servers { |
| 26 |
return @servers; |
| 27 |
} |
| 28 |
|
| 29 |
sub return_first_server() { |
| 30 |
return $servers[0]; |
| 31 |
} |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
sub new { |
| 36 |
my $class = shift; |
| 37 |
my %params = @_; |
| 38 |
my $self = { |
| 39 |
mode => $params{mode}, |
| 40 |
access => $params{method} || 'dbi', |
| 41 |
hostname => $params{hostname}, |
| 42 |
database => $params{database} || 'information_schema', |
| 43 |
port => $params{port}, |
| 44 |
socket => $params{socket}, |
| 45 |
username => $params{username}, |
| 46 |
password => $params{password}, |
| 47 |
replication_user => $params{replication_user}, |
| 48 |
mycnf => $params{mycnf}, |
| 49 |
mycnfgroup => $params{mycnfgroup}, |
| 50 |
timeout => $params{timeout}, |
| 51 |
warningrange => $params{warningrange}, |
| 52 |
criticalrange => $params{criticalrange}, |
| 53 |
verbose => $params{verbose}, |
| 54 |
report => $params{report}, |
| 55 |
negate => $params{negate}, |
| 56 |
labelformat => $params{labelformat}, |
| 57 |
version => 'unknown', |
| 58 |
instance => undef, |
| 59 |
handle => undef, |
| 60 |
}; |
| 61 |
bless $self, $class; |
| 62 |
$self->init_nagios(); |
| 63 |
if ($self->dbconnect(%params)) { |
| 64 |
($self->{dummy}, $self->{version}) = $self->{handle}->fetchrow_array( |
| 65 |
#q{ SHOW VARIABLES WHERE Variable_name = 'version' } |
| 66 |
q{ SHOW VARIABLES LIKE 'version' } |
| 67 |
); |
| 68 |
$self->{version} = (split "-", $self->{version})[0]; |
| 69 |
($self->{dummy}, $self->{uptime}) = $self->{handle}->fetchrow_array( |
| 70 |
q{ SHOW STATUS LIKE 'Uptime' } |
| 71 |
); |
| 72 |
DBD::MySQL::Server::add_server($self); |
| 73 |
$self->init(%params); |
| 74 |
} |
| 75 |
return $self; |
| 76 |
} |
| 77 |
|
| 78 |
sub init { |
| 79 |
my $self = shift; |
| 80 |
my %params = @_; |
| 81 |
$params{handle} = $self->{handle}; |
| 82 |
$params{uptime} = $self->{uptime}; |
| 83 |
$self->set_global_db_thresholds(\%params); |
| 84 |
if ($params{mode} =~ /^server::instance/) { |
| 85 |
$self->{instance} = DBD::MySQL::Server::Instance->new(%params); |
| 86 |
} elsif ($params{mode} =~ /^server::sql/) { |
| 87 |
$self->set_local_db_thresholds(%params); |
| 88 |
if ($params{regexp}) { |
| 89 |
# sql output is treated as text |
| 90 |
if ($params{name2} eq $params{name}) { |
| 91 |
$self->add_nagios_unknown(sprintf "where's the regexp????"); |
| 92 |
} else { |
| 93 |
$self->{genericsql} = |
| 94 |
$self->{handle}->fetchrow_array($params{selectname}); |
| 95 |
if (! defined $self->{genericsql}) { |
| 96 |
$self->add_nagios_unknown(sprintf "got no valid response for %s", |
| 97 |
$params{selectname}); |
| 98 |
} |
| 99 |
} |
| 100 |
} else { |
| 101 |
# sql output must be a number (or array of numbers) |
| 102 |
@{$self->{genericsql}} = |
| 103 |
$self->{handle}->fetchrow_array($params{selectname}); |
| 104 |
if ($self->{handle}->{errstr}) { |
| 105 |
$self->add_nagios_unknown(sprintf "got no valid response for %s: %s", |
| 106 |
$params{selectname}, $self->{handle}->{errstr}); |
| 107 |
} elsif (! (defined $self->{genericsql} && |
| 108 |
(scalar(grep { |
| 109 |
/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/ |
| 110 |
} @{$self->{genericsql}})) == |
| 111 |
scalar(@{$self->{genericsql}}))) { |
| 112 |
$self->add_nagios_unknown(sprintf "got no valid response for %s", |
| 113 |
$params{selectname}); |
| 114 |
} elsif (! defined $self->{genericsql}) { |
| 115 |
$self->add_nagios_unknown(sprintf "got no valid response for %s", |
| 116 |
$params{selectname}); |
| 117 |
} else { |
| 118 |
# name2 in array |
| 119 |
# units in array |
| 120 |
} |
| 121 |
} |
| 122 |
} elsif ($params{mode} =~ /^server::uptime/) { |
| 123 |
# already set with the connection. but use minutes here |
| 124 |
} elsif ($params{mode} =~ /^server::connectiontime/) { |
| 125 |
$self->{connection_time} = $self->{tac} - $self->{tic}; |
| 126 |
} elsif ($params{mode} =~ /^my::([^:.]+)/) { |
| 127 |
my $class = $1; |
| 128 |
my $loaderror = undef; |
| 129 |
substr($class, 0, 1) = uc substr($class, 0, 1); |
| 130 |
foreach my $libpath (split(":", $DBD::MySQL::Server::my_modules_dyn_dir)) { |
| 131 |
foreach my $extmod (glob $libpath."/CheckMySQLHealth*.pm") { |
| 132 |
eval { |
| 133 |
$self->trace(sprintf "loading module %s", $extmod); |
| 134 |
require $extmod; |
| 135 |
}; |
| 136 |
if ($@) { |
| 137 |
$loaderror = $extmod; |
| 138 |
$self->trace(sprintf "failed loading module %s: %s", $extmod, $@); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
my $obj = { |
| 143 |
handle => $params{handle}, |
| 144 |
warningrange => $params{warningrange}, |
| 145 |
criticalrange => $params{criticalrange}, |
| 146 |
}; |
| 147 |
bless $obj, "My$class"; |
| 148 |
$self->{my} = $obj; |
| 149 |
if ($self->{my}->isa("DBD::MySQL::Server")) { |
| 150 |
my $dos_init = $self->can("init"); |
| 151 |
my $dos_nagios = $self->can("nagios"); |
| 152 |
my $my_init = $self->{my}->can("init"); |
| 153 |
my $my_nagios = $self->{my}->can("nagios"); |
| 154 |
if ($my_init == $dos_init) { |
| 155 |
$self->add_nagios_unknown( |
| 156 |
sprintf "Class %s needs an init() method", ref($self->{my})); |
| 157 |
} elsif ($my_nagios == $dos_nagios) { |
| 158 |
$self->add_nagios_unknown( |
| 159 |
sprintf "Class %s needs a nagios() method", ref($self->{my})); |
| 160 |
} else { |
| 161 |
$self->{my}->init_nagios(%params); |
| 162 |
$self->{my}->init(%params); |
| 163 |
} |
| 164 |
} else { |
| 165 |
$self->add_nagios_unknown( |
| 166 |
sprintf "Class %s is not a subclass of DBD::MySQL::Server%s", |
| 167 |
ref($self->{my}), |
| 168 |
$loaderror ? sprintf " (syntax error in %s?)", $loaderror : "" ); |
| 169 |
} |
| 170 |
} else { |
| 171 |
printf "broken mode %s\n", $params{mode}; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
sub dump { |
| 176 |
my $self = shift; |
| 177 |
my $message = shift || ""; |
| 178 |
printf "%s %s\n", $message, Data::Dumper::Dumper($self); |
| 179 |
} |
| 180 |
|
| 181 |
sub nagios { |
| 182 |
my $self = shift; |
| 183 |
my %params = @_; |
| 184 |
if (! $self->{nagios_level}) { |
| 185 |
if ($params{mode} =~ /^server::instance/) { |
| 186 |
$self->{instance}->nagios(%params); |
| 187 |
$self->merge_nagios($self->{instance}); |
| 188 |
} elsif ($params{mode} =~ /^server::database/) { |
| 189 |
$self->{database}->nagios(%params); |
| 190 |
$self->merge_nagios($self->{database}); |
| 191 |
} elsif ($params{mode} =~ /^server::uptime/) { |
| 192 |
$self->add_nagios( |
| 193 |
$self->check_thresholds($self->{uptime} / 60, "10:", "5:"), |
| 194 |
sprintf "database is up since %d minutes", $self->{uptime} / 60); |
| 195 |
$self->add_perfdata(sprintf "uptime=%ds", |
| 196 |
$self->{uptime}); |
| 197 |
} elsif ($params{mode} =~ /^server::connectiontime/) { |
| 198 |
$self->add_nagios( |
| 199 |
$self->check_thresholds($self->{connection_time}, 1, 5), |
| 200 |
sprintf "%.2f seconds to connect as %s", |
| 201 |
$self->{connection_time}, ($self->{username} || getpwuid($<))); |
| 202 |
$self->add_perfdata(sprintf "connection_time=%.4fs;%d;%d", |
| 203 |
$self->{connection_time}, |
| 204 |
$self->{warningrange}, $self->{criticalrange}); |
| 205 |
} elsif ($params{mode} =~ /^server::sql/) { |
| 206 |
if ($params{regexp}) { |
| 207 |
if (substr($params{name2}, 0, 1) eq '!') { |
| 208 |
$params{name2} =~ s/^!//; |
| 209 |
if ($self->{genericsql} !~ /$params{name2}/) { |
| 210 |
$self->add_nagios_ok( |
| 211 |
sprintf "output %s does not match pattern %s", |
| 212 |
$self->{genericsql}, $params{name2}); |
| 213 |
} else { |
| 214 |
$self->add_nagios_critical( |
| 215 |
sprintf "output %s matches pattern %s", |
| 216 |
$self->{genericsql}, $params{name2}); |
| 217 |
} |
| 218 |
} else { |
| 219 |
if ($self->{genericsql} =~ /$params{name2}/) { |
| 220 |
$self->add_nagios_ok( |
| 221 |
sprintf "output %s matches pattern %s", |
| 222 |
$self->{genericsql}, $params{name2}); |
| 223 |
} else { |
| 224 |
$self->add_nagios_critical( |
| 225 |
sprintf "output %s does not match pattern %s", |
| 226 |
$self->{genericsql}, $params{name2}); |
| 227 |
} |
| 228 |
} |
| 229 |
} else { |
| 230 |
$self->add_nagios( |
| 231 |
# the first item in the list will trigger the threshold values |
| 232 |
$self->check_thresholds($self->{genericsql}[0], 1, 5), |
| 233 |
sprintf "%s: %s%s", |
| 234 |
$params{name2} ? lc $params{name2} : lc $params{selectname}, |
| 235 |
# float as float, integers as integers |
| 236 |
join(" ", map { |
| 237 |
(sprintf("%d", $_) eq $_) ? $_ : sprintf("%f", $_) |
| 238 |
} @{$self->{genericsql}}), |
| 239 |
$params{units} ? $params{units} : ""); |
| 240 |
my $i = 0; |
| 241 |
# workaround... getting the column names from the database would be nicer |
| 242 |
my @names2_arr = split(/\s+/, $params{name2}); |
| 243 |
foreach my $t (@{$self->{genericsql}}) { |
| 244 |
$self->add_perfdata(sprintf "\'%s\'=%s%s;%s;%s", |
| 245 |
$names2_arr[$i] ? lc $names2_arr[$i] : lc $params{selectname}, |
| 246 |
# float as float, integers as integers |
| 247 |
(sprintf("%d", $t) eq $t) ? $t : sprintf("%f", $t), |
| 248 |
$params{units} ? $params{units} : "", |
| 249 |
($i == 0) ? $self->{warningrange} : "", |
| 250 |
($i == 0) ? $self->{criticalrange} : "" |
| 251 |
); |
| 252 |
$i++; |
| 253 |
} |
| 254 |
} |
| 255 |
} elsif ($params{mode} =~ /^my::([^:.]+)/) { |
| 256 |
$self->{my}->nagios(%params); |
| 257 |
$self->merge_nagios($self->{my}); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
sub init_nagios { |
| 264 |
my $self = shift; |
| 265 |
no strict 'refs'; |
| 266 |
if (! ref($self)) { |
| 267 |
my $nagiosvar = $self."::nagios"; |
| 268 |
my $nagioslevelvar = $self."::nagios_level"; |
| 269 |
$$nagiosvar = { |
| 270 |
messages => { |
| 271 |
0 => [], |
| 272 |
1 => [], |
| 273 |
2 => [], |
| 274 |
3 => [], |
| 275 |
}, |
| 276 |
perfdata => [], |
| 277 |
}; |
| 278 |
$$nagioslevelvar = $ERRORS{OK}, |
| 279 |
} else { |
| 280 |
$self->{nagios} = { |
| 281 |
messages => { |
| 282 |
0 => [], |
| 283 |
1 => [], |
| 284 |
2 => [], |
| 285 |
3 => [], |
| 286 |
}, |
| 287 |
perfdata => [], |
| 288 |
}; |
| 289 |
$self->{nagios_level} = $ERRORS{OK}, |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
sub check_thresholds { |
| 294 |
my $self = shift; |
| 295 |
my $value = shift; |
| 296 |
my $defaultwarningrange = shift; |
| 297 |
my $defaultcriticalrange = shift; |
| 298 |
my $level = $ERRORS{OK}; |
| 299 |
$self->{warningrange} = defined $self->{warningrange} ? |
| 300 |
$self->{warningrange} : $defaultwarningrange; |
| 301 |
$self->{criticalrange} = defined $self->{criticalrange} ? |
| 302 |
$self->{criticalrange} : $defaultcriticalrange; |
| 303 |
|
| 304 |
if ($self->{warningrange} =~ /^([-+]?[0-9]*\.?[0-9]+)$/) { |
| 305 |
# warning = 10, warn if > 10 or < 0 |
| 306 |
$level = $ERRORS{WARNING} |
| 307 |
if ($value > $1 || $value < 0); |
| 308 |
} elsif ($self->{warningrange} =~ /^([-+]?[0-9]*\.?[0-9]+):$/) { |
| 309 |
# warning = 10:, warn if < 10 |
| 310 |
$level = $ERRORS{WARNING} |
| 311 |
if ($value < $1); |
| 312 |
} elsif ($self->{warningrange} =~ /^~:([-+]?[0-9]*\.?[0-9]+)$/) { |
| 313 |
# warning = ~:10, warn if > 10 |
| 314 |
$level = $ERRORS{WARNING} |
| 315 |
if ($value > $1); |
| 316 |
} elsif ($self->{warningrange} =~ /^([-+]?[0-9]*\.?[0-9]+):([-+]?[0-9]*\.?[0-9]+)$/) { |
| 317 |
# warning = 10:20, warn if < 10 or > 20 |
| 318 |
$level = $ERRORS{WARNING} |
| 319 |
if ($value < $1 || $value > $2); |
| 320 |
} elsif ($self->{warningrange} =~ /^@([-+]?[0-9]*\.?[0-9]+):([-+]?[0-9]*\.?[0-9]+)$/) { |
| 321 |
# warning = @10:20, warn if >= 10 and <= 20 |
| 322 |
$level = $ERRORS{WARNING} |
| 323 |
if ($value >= $1 && $value <= $2); |
| 324 |
} |
| 325 |
if ($self->{criticalrange} =~ /^([-+]?[0-9]*\.?[0-9]+)$/) { |
| 326 |
# critical = 10, crit if > 10 or < 0 |
| 327 |
$level = $ERRORS{CRITICAL} |
| 328 |
if ($value > $1 || $value < 0); |
| 329 |
} elsif ($self->{criticalrange} =~ /^([-+]?[0-9]*\.?[0-9]+):$/) { |
| 330 |
# critical = 10:, crit if < 10 |
| 331 |
$level = $ERRORS{CRITICAL} |
| 332 |
if ($value < $1); |
| 333 |
} elsif ($self->{criticalrange} =~ /^~:([-+]?[0-9]*\.?[0-9]+)$/) { |
| 334 |
# critical = ~:10, crit if > 10 |
| 335 |
$level = $ERRORS{CRITICAL} |
| 336 |
if ($value > $1); |
| 337 |
} elsif ($self->{criticalrange} =~ /^([-+]?[0-9]*\.?[0-9]+):([-+]?[0-9]*\.?[0-9]+)$/) { |
| 338 |
# critical = 10:20, crit if < 10 or > 20 |
| 339 |
$level = $ERRORS{CRITICAL} |
| 340 |
if ($value < $1 || $value > $2); |
| 341 |
} elsif ($self->{criticalrange} =~ /^@([-+]?[0-9]*\.?[0-9]+):([-+]?[0-9]*\.?[0-9]+)$/) { |
| 342 |
# critical = @10:20, crit if >= 10 and <= 20 |
| 343 |
$level = $ERRORS{CRITICAL} |
| 344 |
if ($value >= $1 && $value <= $2); |
| 345 |
} |
| 346 |
return $level; |
| 347 |
# |
| 348 |
# syntax error must be reported with returncode -1 |
| 349 |
# |
| 350 |
} |
| 351 |
|
| 352 |
sub add_nagios { |
| 353 |
my $self = shift; |
| 354 |
my $level = shift; |
| 355 |
my $message = shift; |
| 356 |
push(@{$self->{nagios}->{messages}->{$level}}, $message); |
| 357 |
# recalc current level |
| 358 |
foreach my $llevel (qw(CRITICAL WARNING UNKNOWN OK)) { |
| 359 |
if (scalar(@{$self->{nagios}->{messages}->{$ERRORS{$llevel}}})) { |
| 360 |
$self->{nagios_level} = $ERRORS{$llevel}; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
sub add_nagios_ok { |
| 366 |
my $self = shift; |
| 367 |
my $message = shift; |
| 368 |
$self->add_nagios($ERRORS{OK}, $message); |
| 369 |
} |
| 370 |
|
| 371 |
sub add_nagios_warning { |
| 372 |
my $self = shift; |
| 373 |
my $message = shift; |
| 374 |
$self->add_nagios($ERRORS{WARNING}, $message); |
| 375 |
} |
| 376 |
|
| 377 |
sub add_nagios_critical { |
| 378 |
my $self = shift; |
| 379 |
my $message = shift; |
| 380 |
$self->add_nagios($ERRORS{CRITICAL}, $message); |
| 381 |
} |
| 382 |
|
| 383 |
sub add_nagios_unknown { |
| 384 |
my $self = shift; |
| 385 |
my $message = shift; |
| 386 |
$self->add_nagios($ERRORS{UNKNOWN}, $message); |
| 387 |
} |
| 388 |
|
| 389 |
sub add_perfdata { |
| 390 |
my $self = shift; |
| 391 |
my $data = shift; |
| 392 |
push(@{$self->{nagios}->{perfdata}}, $data); |
| 393 |
} |
| 394 |
|
| 395 |
sub merge_nagios { |
| 396 |
my $self = shift; |
| 397 |
my $child = shift; |
| 398 |
foreach my $level (0..3) { |
| 399 |
foreach (@{$child->{nagios}->{messages}->{$level}}) { |
| 400 |
$self->add_nagios($level, $_); |
| 401 |
} |
| 402 |
#push(@{$self->{nagios}->{messages}->{$level}}, |
| 403 |
# @{$child->{nagios}->{messages}->{$level}}); |
| 404 |
} |
| 405 |
push(@{$self->{nagios}->{perfdata}}, @{$child->{nagios}->{perfdata}}); |
| 406 |
} |
| 407 |
|
| 408 |
sub calculate_result { |
| 409 |
my $self = shift; |
| 410 |
my $labels = shift || {}; |
| 411 |
my $multiline = 0; |
| 412 |
map { |
| 413 |
$self->{nagios_level} = $ERRORS{$_} if |
| 414 |
(scalar(@{$self->{nagios}->{messages}->{$ERRORS{$_}}})); |
| 415 |
} ("OK", "UNKNOWN", "WARNING", "CRITICAL"); |
| 416 |
if ($ENV{NRPE_MULTILINESUPPORT} && |
| 417 |
length join(" ", @{$self->{nagios}->{perfdata}}) > 200) { |
| 418 |
$multiline = 1; |
| 419 |
} |
| 420 |
my $all_messages = join(($multiline ? "\n" : ", "), map { |
| 421 |
join(($multiline ? "\n" : ", "), @{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 422 |
} grep { |
| 423 |
scalar(@{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 424 |
} ("CRITICAL", "WARNING", "UNKNOWN", "OK")); |
| 425 |
my $bad_messages = join(($multiline ? "\n" : ", "), map { |
| 426 |
join(($multiline ? "\n" : ", "), @{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 427 |
} grep { |
| 428 |
scalar(@{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 429 |
} ("CRITICAL", "WARNING", "UNKNOWN")); |
| 430 |
my $good_messages = join(($multiline ? "\n" : ", "), map { |
| 431 |
join(($multiline ? "\n" : ", "), @{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 432 |
} grep { |
| 433 |
scalar(@{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 434 |
} ("OK")); |
| 435 |
my $all_messages_short = $bad_messages ? $bad_messages : 'no problems'; |
| 436 |
# if mode = my-.... |
| 437 |
# and there are some ok-messages |
| 438 |
# output them instead of "no problems" |
| 439 |
if ($self->{mode} =~ /^my\:\:/ && $good_messages) { |
| 440 |
$all_messages_short = $bad_messages ? $bad_messages : $good_messages; |
| 441 |
} |
| 442 |
my $all_messages_html = "<table style=\"border-collapse: collapse;\">". |
| 443 |
join("", map { |
| 444 |
my $level = $_; |
| 445 |
join("", map { |
| 446 |
sprintf "<tr valign=\"top\"><td class=\"service%s\">%s</td></tr>", |
| 447 |
$level, $_; |
| 448 |
} @{$self->{nagios}->{messages}->{$ERRORS{$_}}}); |
| 449 |
} grep { |
| 450 |
scalar(@{$self->{nagios}->{messages}->{$ERRORS{$_}}}) |
| 451 |
} ("CRITICAL", "WARNING", "UNKNOWN", "OK")). |
| 452 |
"</table>"; |
| 453 |
if (exists $self->{identstring}) { |
| 454 |
$self->{nagios_message} .= $self->{identstring}; |
| 455 |
} |
| 456 |
if ($self->{report} eq "long") { |
| 457 |
$self->{nagios_message} .= $all_messages; |
| 458 |
} elsif ($self->{report} eq "short") { |
| 459 |
$self->{nagios_message} .= $all_messages_short; |
| 460 |
} elsif ($self->{report} eq "html") { |
| 461 |
$self->{nagios_message} .= $all_messages_short."\n".$all_messages_html; |
| 462 |
} |
| 463 |
foreach my $from (keys %{$self->{negate}}) { |
| 464 |
if ((uc $from) =~ /^(OK|WARNING|CRITICAL|UNKNOWN)$/ && |
| 465 |
(uc $self->{negate}->{$from}) =~ /^(OK|WARNING|CRITICAL|UNKNOWN)$/) { |
| 466 |
if ($self->{nagios_level} == $ERRORS{uc $from}) { |
| 467 |
$self->{nagios_level} = $ERRORS{uc $self->{negate}->{$from}}; |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
if ($self->{labelformat} eq "pnp4nagios") { |
| 472 |
$self->{perfdata} = join(" ", @{$self->{nagios}->{perfdata}}); |
| 473 |
} else { |
| 474 |
$self->{perfdata} = join(" ", map { |
| 475 |
my $perfdata = $_; |
| 476 |
if ($perfdata =~ /^(.*?)=(.*)/) { |
| 477 |
my $label = $1; |
| 478 |
my $data = $2; |
| 479 |
if (exists $labels->{$label} && |
| 480 |
exists $labels->{$label}->{$self->{labelformat}}) { |
| 481 |
$labels->{$label}->{$self->{labelformat}}."=".$data; |
| 482 |
} else { |
| 483 |
$perfdata; |
| 484 |
} |
| 485 |
} else { |
| 486 |
$perfdata; |
| 487 |
} |
| 488 |
} @{$self->{nagios}->{perfdata}}); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
sub set_global_db_thresholds { |
| 493 |
my $self = shift; |
| 494 |
my $params = shift; |
| 495 |
my $warning = undef; |
| 496 |
my $critical = undef; |
| 497 |
return unless defined $params->{dbthresholds}; |
| 498 |
$params->{name0} = $params->{dbthresholds}; |
| 499 |
# :pluginmode :name :warning :critical |
| 500 |
# mode empty |
| 501 |
# |
| 502 |
eval { |
| 503 |
if ($self->{handle}->fetchrow_array(q{ |
| 504 |
SELECT table_name FROM information_schema.tables |
| 505 |
WHERE table_schema = ? |
| 506 |
AND table_name = 'CHECK_MYSQL_HEALTH_THRESHOLDS'; |
| 507 |
}, $self->{database})) { # either --database... or information_schema |
| 508 |
my @dbthresholds = $self->{handle}->fetchall_array(q{ |
| 509 |
SELECT * FROM check_mysql_health_thresholds |
| 510 |
}); |
| 511 |
$params->{dbthresholds} = \@dbthresholds; |
| 512 |
foreach (@dbthresholds) { |
| 513 |
if (($_->[0] eq $params->{cmdlinemode}) && |
| 514 |
(! defined $_->[1] || ! $_->[1])) { |
| 515 |
($warning, $critical) = ($_->[2], $_->[3]); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
}; |
| 520 |
if (! $@) { |
| 521 |
if ($warning) { |
| 522 |
$params->{warningrange} = $warning; |
| 523 |
$self->trace("read warningthreshold %s from database", $warning); |
| 524 |
} |
| 525 |
if ($critical) { |
| 526 |
$params->{criticalrange} = $critical; |
| 527 |
$self->trace("read criticalthreshold %s from database", $critical); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
sub set_local_db_thresholds { |
| 533 |
my $self = shift; |
| 534 |
my %params = @_; |
| 535 |
my $warning = undef; |
| 536 |
my $critical = undef; |
| 537 |
# :pluginmode :name :warning :critical |
| 538 |
# mode name0 |
| 539 |
# mode name2 |
| 540 |
# mode name |
| 541 |
# |
| 542 |
# first: argument of --dbthresholds, it it exists |
| 543 |
# second: --name2 |
| 544 |
# third: --name |
| 545 |
if (ref($params{dbthresholds}) eq 'ARRAY') { |
| 546 |
my $marker; |
| 547 |
foreach (@{$params{dbthresholds}}) { |
| 548 |
if ($_->[0] eq $params{cmdlinemode}) { |
| 549 |
if (defined $_->[1] && $params{name0} && $_->[1] eq $params{name0}) { |
| 550 |
($warning, $critical) = ($_->[2], $_->[3]); |
| 551 |
$marker = $params{name0}; |
| 552 |
last; |
| 553 |
} elsif (defined $_->[1] && $params{name2} && $_->[1] eq $params{name2}) { |
| 554 |
($warning, $critical) = ($_->[2], $_->[3]); |
| 555 |
$marker = $params{name2}; |
| 556 |
last; |
| 557 |
} elsif (defined $_->[1] && $params{name} && $_->[1] eq $params{name}) { |
| 558 |
($warning, $critical) = ($_->[2], $_->[3]); |
| 559 |
$marker = $params{name}; |
| 560 |
last; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
if ($warning) { |
| 565 |
$self->{warningrange} = $warning; |
| 566 |
$self->trace("read warningthreshold %s for %s from database", |
| 567 |
$marker, $warning); |
| 568 |
} |
| 569 |
if ($critical) { |
| 570 |
$self->{criticalrange} = $critical; |
| 571 |
$self->trace("read criticalthreshold %s for %s from database", |
| 572 |
$marker, $critical); |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
sub debug { |
| 578 |
my $self = shift; |
| 579 |
my $msg = shift; |
| 580 |
if ($DBD::MySQL::Server::verbose) { |
| 581 |
printf "%s %s\n", $msg, ref($self); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
sub dbconnect { |
| 586 |
my $self = shift; |
| 587 |
my %params = @_; |
| 588 |
my $retval = undef; |
| 589 |
$self->{tic} = Time::HiRes::time(); |
| 590 |
$self->{handle} = DBD::MySQL::Server::Connection->new(%params); |
| 591 |
if ($self->{handle}->{errstr}) { |
| 592 |
if ($params{mode} =~ /^server::tnsping/ && |
| 593 |
$self->{handle}->{errstr} =~ /ORA-01017/) { |
| 594 |
$self->add_nagios($ERRORS{OK}, |
| 595 |
sprintf "connection established to %s.", $self->{connect}); |
| 596 |
$retval = undef; |
| 597 |
} elsif ($self->{handle}->{errstr} eq "alarm\n") { |
| 598 |
$self->add_nagios($ERRORS{CRITICAL}, |
| 599 |
sprintf "connection could not be established within %d seconds", |
| 600 |
$self->{timeout}); |
| 601 |
} else { |
| 602 |
$self->add_nagios($ERRORS{CRITICAL}, |
| 603 |
sprintf "cannot connect to %s. %s", |
| 604 |
$self->{database}, $self->{handle}->{errstr}); |
| 605 |
$retval = undef; |
| 606 |
} |
| 607 |
} else { |
| 608 |
$retval = $self->{handle}; |
| 609 |
} |
| 610 |
$self->{tac} = Time::HiRes::time(); |
| 611 |
return $retval; |
| 612 |
} |
| 613 |
|
| 614 |
sub trace { |
| 615 |
my $self = shift; |
| 616 |
my $format = shift; |
| 617 |
$self->{trace} = -f "/tmp/check_mysql_health.trace" ? 1 : 0; |
| 618 |
if ($self->{verbose}) { |
| 619 |
printf("%s: ", scalar localtime); |
| 620 |
printf($format, @_); |
| 621 |
} |
| 622 |
if ($self->{trace}) { |
| 623 |
my $logfh = new IO::File; |
| 624 |
$logfh->autoflush(1); |
| 625 |
if ($logfh->open("/tmp/check_mysql_health.trace", "a")) { |
| 626 |
$logfh->printf("%s: ", scalar localtime); |
| 627 |
$logfh->printf($format, @_); |
| 628 |
$logfh->printf("\n"); |
| 629 |
$logfh->close(); |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
sub DESTROY { |
| 635 |
my $self = shift; |
| 636 |
my $handle1 = "null"; |
| 637 |
my $handle2 = "null"; |
| 638 |
if (defined $self->{handle}) { |
| 639 |
$handle1 = ref($self->{handle}); |
| 640 |
if (defined $self->{handle}->{handle}) { |
| 641 |
$handle2 = ref($self->{handle}->{handle}); |
| 642 |
} |
| 643 |
} |
| 644 |
$self->trace(sprintf "DESTROY %s with handle %s %s", ref($self), $handle1, $handle2); |
| 645 |
if (ref($self) eq "DBD::MySQL::Server") { |
| 646 |
} |
| 647 |
$self->trace(sprintf "DESTROY %s exit with handle %s %s", ref($self), $handle1, $handle2); |
| 648 |
if (ref($self) eq "DBD::MySQL::Server") { |
| 649 |
#printf "humpftata\n"; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
sub save_state { |
| 654 |
my $self = shift; |
| 655 |
my %params = @_; |
| 656 |
my $extension = ""; |
| 657 |
my $mode = $params{mode}; |
| 658 |
if ($params{connect} && $params{connect} =~ /(\w+)\/(\w+)@(\w+)/) { |
| 659 |
$params{connect} = $3; |
| 660 |
} elsif ($params{connect}) { |
| 661 |
# just to be sure |
| 662 |
$params{connect} =~ s/\//_/g; |
| 663 |
} |
| 664 |
if ($^O =~ /MSWin/) { |
| 665 |
$mode =~ s/::/_/g; |
| 666 |
$params{statefilesdir} = $self->system_vartmpdir(); |
| 667 |
} |
| 668 |
if (! -d $params{statefilesdir}) { |
| 669 |
eval { |
| 670 |
use File::Path; |
| 671 |
mkpath $params{statefilesdir}; |
| 672 |
}; |
| 673 |
} |
| 674 |
if ($@ || ! -w $params{statefilesdir}) { |
| 675 |
$self->add_nagios($ERRORS{CRITICAL}, |
| 676 |
sprintf "statefilesdir %s does not exist or is not writable\n", |
| 677 |
$params{statefilesdir}); |
| 678 |
return; |
| 679 |
} |
| 680 |
my $statefile = sprintf "%s_%s", $params{hostname}, $mode; |
| 681 |
$extension .= $params{differenciator} ? "_".$params{differenciator} : ""; |
| 682 |
$extension .= $params{socket} ? "_".$params{socket} : ""; |
| 683 |
$extension .= $params{port} ? "_".$params{port} : ""; |
| 684 |
$extension .= $params{database} ? "_".$params{database} : ""; |
| 685 |
$extension .= $params{tablespace} ? "_".$params{tablespace} : ""; |
| 686 |
$extension .= $params{datafile} ? "_".$params{datafile} : ""; |
| 687 |
$extension .= $params{name} ? "_".$params{name} : ""; |
| 688 |
$extension =~ s/\//_/g; |
| 689 |
$extension =~ s/\(/_/g; |
| 690 |
$extension =~ s/\)/_/g; |
| 691 |
$extension =~ s/\*/_/g; |
| 692 |
$extension =~ s/\s/_/g; |
| 693 |
$statefile .= $extension; |
| 694 |
$statefile = lc $statefile; |
| 695 |
$statefile = sprintf "%s/%s", $params{statefilesdir}, $statefile; |
| 696 |
if (open(STATE, ">$statefile")) { |
| 697 |
if ((ref($params{save}) eq "HASH") && exists $params{save}->{timestamp}) { |
| 698 |
$params{save}->{localtime} = scalar localtime $params{save}->{timestamp}; |
| 699 |
} |
| 700 |
printf STATE Data::Dumper::Dumper($params{save}); |
| 701 |
close STATE; |
| 702 |
} else { |
| 703 |
$self->add_nagios($ERRORS{CRITICAL}, |
| 704 |
sprintf "statefile %s is not writable", $statefile); |
| 705 |
} |
| 706 |
$self->debug(sprintf "saved %s to %s", |
| 707 |
Data::Dumper::Dumper($params{save}), $statefile); |
| 708 |
} |
| 709 |
|
| 710 |
sub load_state { |
| 711 |
my $self = shift; |
| 712 |
my %params = @_; |
| 713 |
my $extension = ""; |
| 714 |
my $mode = $params{mode}; |
| 715 |
if ($params{connect} && $params{connect} =~ /(\w+)\/(\w+)@(\w+)/) { |
| 716 |
$params{connect} = $3; |
| 717 |
} elsif ($params{connect}) { |
| 718 |
# just to be sure |
| 719 |
$params{connect} =~ s/\//_/g; |
| 720 |
} |
| 721 |
if ($^O =~ /MSWin/) { |
| 722 |
$mode =~ s/::/_/g; |
| 723 |
$params{statefilesdir} = $self->system_vartmpdir(); |
| 724 |
} |
| 725 |
my $statefile = sprintf "%s_%s", $params{hostname}, $mode; |
| 726 |
$extension .= $params{differenciator} ? "_".$params{differenciator} : ""; |
| 727 |
$extension .= $params{socket} ? "_".$params{socket} : ""; |
| 728 |
$extension .= $params{port} ? "_".$params{port} : ""; |
| 729 |
$extension .= $params{database} ? "_".$params{database} : ""; |
| 730 |
$extension .= $params{tablespace} ? "_".$params{tablespace} : ""; |
| 731 |
$extension .= $params{datafile} ? "_".$params{datafile} : ""; |
| 732 |
$extension .= $params{name} ? "_".$params{name} : ""; |
| 733 |
$extension =~ s/\//_/g; |
| 734 |
$extension =~ s/\(/_/g; |
| 735 |
$extension =~ s/\)/_/g; |
| 736 |
$extension =~ s/\*/_/g; |
| 737 |
$extension =~ s/\s/_/g; |
| 738 |
$statefile .= $extension; |
| 739 |
$statefile = lc $statefile; |
| 740 |
$statefile = sprintf "%s/%s", $params{statefilesdir}, $statefile; |
| 741 |
if ( -f $statefile) { |
| 742 |
our $VAR1; |
| 743 |
eval { |
| 744 |
require $statefile; |
| 745 |
}; |
| 746 |
if($@) { |
| 747 |
$self->add_nagios($ERRORS{CRITICAL}, |
| 748 |
sprintf "statefile %s is corrupt", $statefile); |
| 749 |
} |
| 750 |
$self->debug(sprintf "load %s", Data::Dumper::Dumper($VAR1)); |
| 751 |
return $VAR1; |
| 752 |
} else { |
| 753 |
return undef; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
sub valdiff { |
| 758 |
my $self = shift; |
| 759 |
my $pparams = shift; |
| 760 |
my %params = %{$pparams}; |
| 761 |
my @keys = @_; |
| 762 |
my $now = time; |
| 763 |
my $last_values = $self->load_state(%params) || eval { |
| 764 |
my $empty_events = {}; |
| 765 |
foreach (@keys) { |
| 766 |
$empty_events->{$_} = 0; |
| 767 |
} |
| 768 |
$empty_events->{timestamp} = 0; |
| 769 |
if ($params{lookback}) { |
| 770 |
$empty_events->{lookback_history} = {}; |
| 771 |
} |
| 772 |
$empty_events; |
| 773 |
}; |
| 774 |
foreach (@keys) { |
| 775 |
if ($params{lookback}) { |
| 776 |
# find a last_value in the history which fits lookback best |
| 777 |
# and overwrite $last_values->{$_} with historic data |
| 778 |
if (exists $last_values->{lookback_history}->{$_}) { |
| 779 |
foreach my $date (sort {$a <=> $b} keys %{$last_values->{lookback_history}->{$_}}) { |
| 780 |
if ($date >= ($now - $params{lookback})) { |
| 781 |
$last_values->{$_} = $last_values->{lookback_history}->{$_}->{$date}; |
| 782 |
$last_values->{timestamp} = $date; |
| 783 |
last; |
| 784 |
} else { |
| 785 |
delete $last_values->{lookback_history}->{$_}->{$date}; |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
$last_values->{$_} = 0 if ! exists $last_values->{$_}; |
| 791 |
if ($self->{$_} >= $last_values->{$_}) { |
| 792 |
$self->{'delta_'.$_} = $self->{$_} - $last_values->{$_}; |
| 793 |
} else { |
| 794 |
# vermutlich db restart und zaehler alle auf null |
| 795 |
$self->{'delta_'.$_} = $self->{$_}; |
| 796 |
} |
| 797 |
$self->debug(sprintf "delta_%s %f", $_, $self->{'delta_'.$_}); |
| 798 |
} |
| 799 |
$self->{'delta_timestamp'} = $now - $last_values->{timestamp}; |
| 800 |
$params{save} = eval { |
| 801 |
my $empty_events = {}; |
| 802 |
foreach (@keys) { |
| 803 |
$empty_events->{$_} = $self->{$_}; |
| 804 |
} |
| 805 |
$empty_events->{timestamp} = $now; |
| 806 |
if ($params{lookback}) { |
| 807 |
$empty_events->{lookback_history} = $last_values->{lookback_history}; |
| 808 |
foreach (@keys) { |
| 809 |
$empty_events->{lookback_history}->{$_}->{$now} = $self->{$_}; |
| 810 |
} |
| 811 |
} |
| 812 |
$empty_events; |
| 813 |
}; |
| 814 |
$self->save_state(%params); |
| 815 |
} |
| 816 |
|
| 817 |
sub requires_version { |
| 818 |
my $self = shift; |
| 819 |
my $version = shift; |
| 820 |
my @instances = DBD::MySQL::Server::return_servers(); |
| 821 |
my $instversion = $instances[0]->{version}; |
| 822 |
if (! $self->version_is_minimum($version)) { |
| 823 |
$self->add_nagios($ERRORS{UNKNOWN}, |
| 824 |
sprintf "not implemented/possible for MySQL release %s", $instversion); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
sub version_is_minimum { |
| 829 |
# the current version is newer or equal |
| 830 |
my $self = shift; |
| 831 |
my $version = shift; |
| 832 |
my $newer = 1; |
| 833 |
my @instances = DBD::MySQL::Server::return_servers(); |
| 834 |
my @v1 = map { $_ eq "x" ? 0 : $_ } split(/\./, $version); |
| 835 |
my @v2 = split(/\./, $instances[0]->{version}); |
| 836 |
if (scalar(@v1) > scalar(@v2)) { |
| 837 |
push(@v2, (0) x (scalar(@v1) - scalar(@v2))); |
| 838 |
} elsif (scalar(@v2) > scalar(@v1)) { |
| 839 |
push(@v1, (0) x (scalar(@v2) - scalar(@v1))); |
| 840 |
} |
| 841 |
foreach my $pos (0..$#v1) { |
| 842 |
if ($v2[$pos] > $v1[$pos]) { |
| 843 |
$newer = 1; |
| 844 |
last; |
| 845 |
} elsif ($v2[$pos] < $v1[$pos]) { |
| 846 |
$newer = 0; |
| 847 |
last; |
| 848 |
} |
| 849 |
} |
| 850 |
#printf STDERR "check if %s os minimum %s\n", join(".", @v2), join(".", @v1); |
| 851 |
return $newer; |
| 852 |
} |
| 853 |
|
| 854 |
sub instance_thread { |
| 855 |
my $self = shift; |
| 856 |
my @instances = DBD::MySQL::Server::return_servers(); |
| 857 |
return $instances[0]->{thread}; |
| 858 |
} |
| 859 |
|
| 860 |
sub windows_server { |
| 861 |
my $self = shift; |
| 862 |
my @instances = DBD::MySQL::Server::return_servers(); |
| 863 |
if ($instances[0]->{os} =~ /Win/i) { |
| 864 |
return 1; |
| 865 |
} else { |
| 866 |
return 0; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
sub system_vartmpdir { |
| 871 |
my $self = shift; |
| 872 |
if ($^O =~ /MSWin/) { |
| 873 |
return $self->system_tmpdir(); |
| 874 |
} else { |
| 875 |
return "/var/tmp/check_mysql_health"; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
sub system_oldvartmpdir { |
| 880 |
my $self = shift; |
| 881 |
return "/tmp"; |
| 882 |
} |
| 883 |
|
| 884 |
sub system_tmpdir { |
| 885 |
my $self = shift; |
| 886 |
if ($^O =~ /MSWin/) { |
| 887 |
return $ENV{TEMP} if defined $ENV{TEMP}; |
| 888 |
return $ENV{TMP} if defined $ENV{TMP}; |
| 889 |
return File::Spec->catfile($ENV{windir}, 'Temp') |
| 890 |
if defined $ENV{windir}; |
| 891 |
return 'C:\Temp'; |
| 892 |
} else { |
| 893 |
return "/tmp"; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
sub decode_password { |
| 898 |
my $self = shift; |
| 899 |
my $password = shift; |
| 900 |
if ($password && $password =~ /^rfc3986:\/\/(.*)/) { |
| 901 |
$password = $1; |
| 902 |
$password =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; |
| 903 |
} |
| 904 |
return $password; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
package DBD::MySQL::Server::Connection; |
| 909 |
|
| 910 |
use strict; |
| 911 |
|
| 912 |
our @ISA = qw(DBD::MySQL::Server); |
| 913 |
|
| 914 |
my %ERRORS=( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 ); |
| 915 |
my %ERRORCODES=( 0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN' ); |
| 916 |
|
| 917 |
sub new { |
| 918 |
my $class = shift; |
| 919 |
my %params = @_; |
| 920 |
my $self = { |
| 921 |
mode => $params{mode}, |
| 922 |
timeout => $params{timeout}, |
| 923 |
access => $params{method} || "dbi", |
| 924 |
hostname => $params{hostname}, |
| 925 |
database => $params{database} || "information_schema", |
| 926 |
port => $params{port}, |
| 927 |
socket => $params{socket}, |
| 928 |
username => $params{username}, |
| 929 |
password => $params{password}, |
| 930 |
mycnf => $params{mycnf}, |
| 931 |
mycnfgroup => $params{mycnfgroup}, |
| 932 |
handle => undef, |
| 933 |
}; |
| 934 |
bless $self, $class; |
| 935 |
if ($params{method} eq "dbi") { |
| 936 |
bless $self, "DBD::MySQL::Server::Connection::Dbi"; |
| 937 |
} elsif ($params{method} eq "mysql") { |
| 938 |
bless $self, "DBD::MySQL::Server::Connection::Mysql"; |
| 939 |
} elsif ($params{method} eq "sqlrelay") { |
| 940 |
bless $self, "DBD::MySQL::Server::Connection::Sqlrelay"; |
| 941 |
} |
| 942 |
$self->init(%params); |
| 943 |
return $self; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
package DBD::MySQL::Server::Connection::Dbi; |
| 948 |
|
| 949 |
use strict; |
| 950 |
use Net::Ping; |
| 951 |
|
| 952 |
our @ISA = qw(DBD::MySQL::Server::Connection); |
| 953 |
|
| 954 |
my %ERRORS=( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 ); |
| 955 |
my %ERRORCODES=( 0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN' ); |
| 956 |
|
| 957 |
sub init { |
| 958 |
my $self = shift; |
| 959 |
my %params = @_; |
| 960 |
my $retval = undef; |
| 961 |
if ($self->{mode} =~ /^server::tnsping/) { |
| 962 |
if (! $self->{connect}) { |
| 963 |
$self->{errstr} = "Please specify a database"; |
| 964 |
} else { |
| 965 |
$self->{sid} = $self->{connect}; |
| 966 |
$self->{username} ||= time; # prefer an existing user |
| 967 |
$self->{password} = time; |
| 968 |
} |
| 969 |
} else { |
| 970 |
if ( |
| 971 |
($self->{hostname} ne 'localhost' && (! $self->{username} || ! $self->{password})) && |
| 972 |
(! $self->{mycnf}) ) { |
| 973 |
$self->{errstr} = "Please specify hostname, username and password or a .cnf file"; |
| 974 |
return undef; |
| 975 |
} |
| 976 |
$self->{dsn} = "DBI:mysql:"; |
| 977 |
$self->{dsn} .= sprintf "database=%s", $self->{database}; |
| 978 |
if ($self->{mycnf}) { |
| 979 |
$self->{dsn} .= sprintf ";mysql_read_default_file=%s", $self->{mycnf}; |
| 980 |
if ($self->{mycnfgroup}) { |
| 981 |
$self->{dsn} .= sprintf ";mysql_read_default_group=%s", $self->{mycnfgroup}; |
| 982 |
} |
| 983 |
} else { |
| 984 |
$self->{dsn} .= sprintf ";host=%s", $self->{hostname}; |
| 985 |
$self->{dsn} .= sprintf ";port=%s", $self->{port} |
| 986 |
unless $self->{socket} || $self->{hostname} eq 'localhost'; |
| 987 |
$self->{dsn} .= sprintf ";mysql_socket=%s", $self->{socket} |
| 988 |
if $self->{socket}; |
| 989 |
} |
| 990 |
} |
| 991 |
if (! exists $self->{errstr}) { |
| 992 |
eval { |
| 993 |
require DBI; |
| 994 |
use POSIX ':signal_h'; |
| 995 |
if ($^O =~ /MSWin/) { |
| 996 |
local $SIG{'ALRM'} = sub { |
| 997 |
die "alarm\n"; |
| 998 |
}; |
| 999 |
} else { |
| 1000 |
my $mask = POSIX::SigSet->new( SIGALRM ); |
| 1001 |
my $action = POSIX::SigAction->new( |
| 1002 |
sub { die "alarm\n" ; }, $mask); |
| 1003 |
my $oldaction = POSIX::SigAction->new(); |
| 1004 |
sigaction(SIGALRM ,$action ,$oldaction ); |
| 1005 |
} |
| 1006 |
alarm($self->{timeout} - 1); # 1 second before the global unknown timeout |
| 1007 |
if ($self->{handle} = DBI->connect( |
| 1008 |
$self->{dsn}, |
| 1009 |
$self->{username}, |
| 1010 |
$self->decode_password($self->{password}), |
| 1011 |
{ RaiseError => 0, AutoCommit => 0, PrintError => 1 })) { |
| 1012 |
# $self->{handle}->do(q{ |
| 1013 |
# ALTER SESSION SET NLS_NUMERIC_CHARACTERS=".," }); |
| 1014 |
$retval = $self; |
| 1015 |
} else { |
| 1016 |
$self->{errstr} = DBI::errstr(); |
| 1017 |
} |
| 1018 |
}; |
| 1019 |
if ($@) { |
| 1020 |
$self->{errstr} = $@; |
| 1021 |
$retval = undef; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
$self->{tac} = Time::HiRes::time(); |
| 1025 |
return $retval; |
| 1026 |
} |
| 1027 |
|
| 1028 |
sub selectrow_hashref { |
| 1029 |
my $self = shift; |
| 1030 |
my $sql = shift; |
| 1031 |
my @arguments = @_; |
| 1032 |
my $sth = undef; |
| 1033 |
my $hashref = undef; |
| 1034 |
eval { |
| 1035 |
$self->trace(sprintf "SQL:\n%s\nARGS:\n%s\n", |
| 1036 |
$sql, Data::Dumper::Dumper(\@arguments)); |
| 1037 |
# helm auf! jetzt wirds dreckig. |
| 1038 |
if ($sql =~ /^\s*SHOW/) { |
| 1039 |
$hashref = $self->{handle}->selectrow_hashref($sql); |
| 1040 |
} else { |
| 1041 |
$sth = $self->{handle}->prepare($sql); |
| 1042 |
if (scalar(@arguments)) { |
| 1043 |
$sth->execute(@arguments); |
| 1044 |
} else { |
| 1045 |
$sth->execute(); |
| 1046 |
} |
| 1047 |
$hashref = $sth->selectrow_hashref(); |
| 1048 |
} |
| 1049 |
$self->trace(sprintf "RESULT:\n%s\n", |
| 1050 |
Data::Dumper::Dumper($hashref)); |
| 1051 |
}; |
| 1052 |
if ($@) { |
| 1053 |
$self->debug(sprintf "bumm %s", $@); |
| 1054 |
} |
| 1055 |
if (-f "/tmp/check_mysql_health_simulation/".$self->{mode}) { |
| 1056 |
my $simulation = do { local (@ARGV, $/) = |
| 1057 |
"/tmp/check_mysql_health_simulation/".$self->{mode}; <> }; |
| 1058 |
# keine lust auf den scheiss |
| 1059 |
} |
| 1060 |
return $hashref; |
| 1061 |
} |
| 1062 |
|
| 1063 |
sub fetchrow_array { |
| 1064 |
my $self = shift; |
| 1065 |
my $sql = shift; |
| 1066 |
my @arguments = @_; |
| 1067 |
my $sth = undef; |
| 1068 |
my @row = (); |
| 1069 |
my $stderrvar; |
| 1070 |
*SAVEERR = *STDERR; |
| 1071 |
open ERR ,'>',\$stderrvar; |
| 1072 |
*STDERR = *ERR; |
| 1073 |
eval { |
| 1074 |
$self->trace(sprintf "SQL:\n%s\nARGS:\n%s\n", |
| 1075 |
$sql, Data::Dumper::Dumper(\@arguments)); |
| 1076 |
$sth = $self->{handle}->prepare($sql); |
| 1077 |
if (scalar(@arguments)) { |
| 1078 |
$sth->execute(@arguments); |
| 1079 |
} else { |
| 1080 |
$sth->execute(); |
| 1081 |
} |
| 1082 |
@row = $sth->fetchrow_array(); |
| 1083 |
$self->trace(sprintf "RESULT:\n%s\n", |
| 1084 |
Data::Dumper::Dumper(\@row)); |
| 1085 |
}; |
| 1086 |
*STDERR = *SAVEERR; |
| 1087 |
if ($@) { |
| 1088 |
$self->debug(sprintf "bumm %s", $@); |
| 1089 |
$self->{errstr} = $@; |
| 1090 |
return (undef); |
| 1091 |
} elsif ($stderrvar) { |
| 1092 |
$self->{errstr} = $stderrvar; |
| 1093 |
return (undef); |
| 1094 |
} elsif ($sth->errstr()) { |
| 1095 |
$self->{errstr} = $sth->errstr(); |
| 1096 |
return (undef); |
| 1097 |
} |
| 1098 |
if (-f "/tmp/check_mysql_health_simulation/".$self->{mode}) { |
| 1099 |
my $simulation = do { local (@ARGV, $/) = |
| 1100 |
"/tmp/check_mysql_health_simulation/".$self->{mode}; <> }; |
| 1101 |
@row = split(/\s+/, (split(/\n/, $simulation))[0]); |
| 1102 |
} |
| 1103 |
return $row[0] unless wantarray; |
| 1104 |
return @row; |
| 1105 |
} |
| 1106 |
|
| 1107 |
sub fetchall_array { |
| 1108 |
my $self = shift; |
| 1109 |
my $sql = shift; |
| 1110 |
my @arguments = @_; |
| 1111 |
my $sth = undef; |
| 1112 |
my $rows = undef; |
| 1113 |
eval { |
| 1114 |
$self->trace(sprintf "SQL:\n%s\nARGS:\n%s\n", |
| 1115 |
$sql, Data::Dumper::Dumper(\@arguments)); |
| 1116 |
$sth = $self->{handle}->prepare($sql); |
| 1117 |
if (scalar(@arguments)) { |
| 1118 |
$sth->execute(@arguments); |
| 1119 |
} else { |
| 1120 |
$sth->execute(); |
| 1121 |
} |
| 1122 |
$rows = $sth->fetchall_arrayref(); |
| 1123 |
$self->trace(sprintf "RESULT:\n%s\n", |
| 1124 |
Data::Dumper::Dumper($rows)); |
| 1125 |
}; |
| 1126 |
if ($@) { |
| 1127 |
printf STDERR "bumm %s\n", $@; |
| 1128 |
} |
| 1129 |
if (-f "/tmp/check_mysql_health_simulation/".$self->{mode}) { |
| 1130 |
my $simulation = do { local (@ARGV, $/) = |
| 1131 |
"/tmp/check_mysql_health_simulation/".$self->{mode}; <> }; |
| 1132 |
@{$rows} = map { [ split(/\s+/, $_) ] } split(/\n/, $simulation); |
| 1133 |
} |
| 1134 |
return @{$rows}; |
| 1135 |
} |
| 1136 |
|
| 1137 |
sub func { |
| 1138 |
my $self = shift; |
| 1139 |
$self->{handle}->func(@_); |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
sub execute { |
| 1144 |
my $self = shift; |
| 1145 |
my $sql = shift; |
| 1146 |
eval { |
| 1147 |
my $sth = $self->{handle}->prepare($sql); |
| 1148 |
$sth->execute(); |
| 1149 |
}; |
| 1150 |
if ($@) { |
| 1151 |
printf STDERR "bumm %s\n", $@; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
sub errstr { |
| 1156 |
my $self = shift; |
| 1157 |
return $self->{errstr}; |
| 1158 |
} |
| 1159 |
|
| 1160 |
sub DESTROY { |
| 1161 |
my $self = shift; |
| 1162 |
$self->trace(sprintf "disconnecting DBD %s", |
| 1163 |
$self->{handle} ? "with handle" : "without handle"); |
| 1164 |
$self->{handle}->disconnect() if $self->{handle}; |
| 1165 |
} |
| 1166 |
|
| 1167 |
package DBD::MySQL::Server::Connection::Mysql; |
| 1168 |
|
| 1169 |
use strict; |
| 1170 |
use File::Temp qw/tempfile/; |
| 1171 |
|
| 1172 |
our @ISA = qw(DBD::MySQL::Server::Connection); |
| 1173 |
|
| 1174 |
my %ERRORS=( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 ); |
| 1175 |
my %ERRORCODES=( 0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN' ); |
| 1176 |
|
| 1177 |
sub init { |
| 1178 |
my $self = shift; |
| 1179 |
my %params = @_; |
| 1180 |
my $retval = undef; |
| 1181 |
$self->{loginstring} = "traditional"; |
| 1182 |
($self->{sql_commandfile_handle}, $self->{sql_commandfile}) = |
| 1183 |
tempfile($self->{mode}."XXXXX", SUFFIX => ".sql", |
| 1184 |
DIR => $self->system_tmpdir() ); |
| 1185 |
close $self->{sql_commandfile_handle}; |
| 1186 |
($self->{sql_resultfile_handle}, $self->{sql_resultfile}) = |
| 1187 |
tempfile($self->{mode}."XXXXX", SUFFIX => ".out", |
| 1188 |
DIR => $self->system_tmpdir() ); |
| 1189 |
close $self->{sql_resultfile_handle}; |
| 1190 |
if ($self->{mode} =~ /^server::tnsping/) { |
| 1191 |
if (! $self->{connect}) { |
| 1192 |
$self->{errstr} = "Please specify a database"; |
| 1193 |
} else { |
| 1194 |
$self->{sid} = $self->{connect}; |
| 1195 |
$self->{username} ||= time; # prefer an existing user |
| 1196 |
$self->{password} = time; |
| 1197 |
} |
| 1198 |
} else { |
| 1199 |
if (! $self->{username} || ! $self->{password}) { |
| 1200 |
$self->{errstr} = "Please specify database, username and password"; |
| 1201 |
return undef; |
| 1202 |
} elsif (! (($self->{hostname} && $self->{port}) || $self->{socket})) { |
| 1203 |
$self->{errstr} = "Please specify hostname and port or socket"; |
| 1204 |
return undef; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
if (! exists $self->{errstr}) { |
| 1208 |
$self->{password} = $self->decode_password($self->{password}); |
| 1209 |
eval { |
| 1210 |
my $mysql = '/'.'usr'.'/'.'bin'.'/'.'mysql'; |
| 1211 |
if (! -x $mysql) { |
| 1212 |
die "nomysql\n"; |
| 1213 |
} |
| 1214 |
if ($self->{loginstring} eq "traditional") { |
| 1215 |
$self->{sqlplus} = sprintf "%s ", $mysql; |
| 1216 |
$self->{sqlplus} .= sprintf "--batch --raw --skip-column-names "; |
| 1217 |
$self->{sqlplus} .= sprintf "--database=%s ", $self->{database}; |
| 1218 |
$self->{sqlplus} .= sprintf "--host=%s ", $self->{hostname}; |
| 1219 |
$self->{sqlplus} .= sprintf "--port=%s ", $self->{port} |
| 1220 |
unless $self->{socket} || $self->{hostname} eq "localhost"; |
| 1221 |
$self->{sqlplus} .= sprintf "--socket=%s ", $self->{socket} |
| 1222 |
if $self->{socket}; |
| 1223 |
$self->{sqlplus} .= sprintf "--user=%s --password='%s' < %s > %s", |
| 1224 |
$self->{username}, $self->{password}, |
| 1225 |
$self->{sql_commandfile}, $self->{sql_resultfile}; |
| 1226 |
} |
| 1227 |
|
| 1228 |
use POSIX ':signal_h'; |
| 1229 |
if ($^O =~ /MSWin/) { |
| 1230 |
local $SIG{'ALRM'} = sub { |
| 1231 |
die "alarm\n"; |
| 1232 |
}; |
| 1233 |
} else { |
| 1234 |
my $mask = POSIX::SigSet->new( SIGALRM ); |
| 1235 |
my $action = POSIX::SigAction->new( |
| 1236 |
sub { die "alarm\n" ; }, $mask); |
| 1237 |
my $oldaction = POSIX::SigAction->new(); |
| 1238 |
sigaction(SIGALRM ,$action ,$oldaction ); |
| 1239 |
} |
| 1240 |
alarm($self->{timeout} - 1); # 1 second before the global unknown timeout |
| 1241 |
|
| 1242 |
my $answer = $self->fetchrow_array( |
| 1243 |
q{ SELECT 42 FROM dual}); |
| 1244 |
die unless defined $answer and $answer == 42; |
| 1245 |
$retval = $self; |
| 1246 |
}; |
| 1247 |
if ($@) { |
| 1248 |
$self->{errstr} = $@; |
| 1249 |
$self->{errstr} =~ s/at $0 .*//g; |
| 1250 |
chomp $self->{errstr}; |
| 1251 |
$retval = undef; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
$self->{tac} = Time::HiRes::time(); |
| 1255 |
return $retval; |
| 1256 |
} |
| 1257 |
|
| 1258 |
sub selectrow_hashref { |
| 1259 |
my $self = shift; |
| 1260 |
my $sql = shift; |
| 1261 |
my @arguments = @_; |
| 1262 |
my $sth = undef; |
| 1263 |
my $hashref = undef; |
| 1264 |
foreach (@arguments) { |
| 1265 |
# replace the ? by the parameters |
| 1266 |
if (/^\d+$/) { |
| 1267 |
$sql =~ s/\?/$_/; |
| 1268 |
} else { |
| 1269 |
$sql =~ s/\?/'$_'/; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
if ($sql =~ /^\s*SHOW/) { |
| 1273 |
$sql .= '\G'; # http://dev.mysql.com/doc/refman/5.1/de/show-slave-status.html |
| 1274 |
} |
| 1275 |
$self->trace(sprintf "SQL (? resolved):\n%s\nARGS:\n%s\n", |
| 1276 |
$sql, Data::Dumper::Dumper(\@arguments)); |
| 1277 |
$self->create_commandfile($sql); |
| 1278 |
my $exit_output = `$self->{sqlplus}`; |
| 1279 |
if ($?) { |
| 1280 |
printf STDERR "fetchrow_array exit bumm \n"; |
| 1281 |
my $output = do { local (@ARGV, $/) = $self->{sql_resultfile}; <> }; |
| 1282 |
my @oerrs = map { |
| 1283 |
/((ERROR \d+).*)/ ? $1 : (); |
| 1284 |
} split(/\n/, $output); |
| 1285 |
$self->{errstr} = join(" ", @oerrs); |
| 1286 |
} else { |
| 1287 |
my $output = do { local (@ARGV, $/) = $self->{sql_resultfile}; <> }; |
| 1288 |
if ($sql =~ /^\s*SHOW/) { |
| 1289 |
map { |
| 1290 |
if (/^\s*([\w_]+):\s*(.*)/) { |
| 1291 |
$hashref->{$1} = $2; |
| 1292 |
} |
| 1293 |
} split(/\n/, $output); |
| 1294 |
} else { |
| 1295 |
# i dont mess around here and you shouldn't either |
| 1296 |
} |
| 1297 |
$self->trace(sprintf "RESULT:\n%s\n", |
| 1298 |
Data::Dumper::Dumper($hashref)); |
| 1299 |
} |
| 1300 |
unlink $self->{sql_commandfile}; |
| 1301 |
unlink $self->{sql_resultfile}; |
| 1302 |
return $hashref; |
| 1303 |
} |
| 1304 |
|
| 1305 |
sub fetchrow_array { |
| 1306 |
my $self = shift; |
| 1307 |
my $sql = shift; |
| 1308 |
my @arguments = @_; |
| 1309 |
my $sth = undef; |
| 1310 |
my @row = (); |
| 1311 |
foreach (@arguments) { |
| 1312 |
# replace the ? by the parameters |
| 1313 |
if (/^\d+$/) { |
| 1314 |
$sql =~ s/\?/$_/; |
| 1315 |
} else { |
| 1316 |
$sql =~ s/\?/'$_'/; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
$self->trace(sprintf "SQL (? resolved):\n%s\nARGS:\n%s\n", |
| 1320 |
$sql, Data::Dumper::Dumper(\@arguments)); |
| 1321 |
$self->create_commandfile($sql); |
| 1322 |
my $exit_output = `$self->{sqlplus}`; |
| 1323 |
if ($?) { |
| 1324 |
printf STDERR "fetchrow_array exit bumm \n"; |
| 1325 |
my $output = do { local (@ARGV, $/) = $self->{sql_resultfile}; <> }; |
| 1326 |
my @oerrs = map { |
| 1327 |
/((ERROR \d+).*)/ ? $1 : (); |
| 1328 |
} split(/\n/, $output); |
| 1329 |
$self->{errstr} = join(" ", @oerrs); |
| 1330 |
} else { |
| 1331 |
my $output = do { local (@ARGV, $/) = $self->{sql_resultfile}; <> }; |
| 1332 |
@row = map { convert($_) } |
| 1333 |
map { s/^\s+([\.\d]+)$/$1/g; $_ } # strip leading space from numbers |
| 1334 |
map { s/\s+$//g; $_ } # strip trailing space |
| 1335 |
split(/\t/, (split(/\n/, $output))[0]); |
| 1336 |
$self->trace(sprintf "RESULT:\n%s\n", |
| 1337 |
Data::Dumper::Dumper(\@row)); |
| 1338 |
} |
| 1339 |
if ($@) { |
| 1340 |
$self->debug(sprintf "bumm %s", $@); |
| 1341 |
} |
| 1342 |
unlink $self->{sql_commandfile}; |
| 1343 |
unlink $self->{sql_resultfile}; |
| 1344 |
return $row[0] unless wantarray; |
| 1345 |
return @row; |
| 1346 |
} |
| 1347 |
|
| 1348 |
sub fetchall_array { |
| 1349 |
my $self = shift; |
| 1350 |
my $sql = shift; |
| 1351 |
my @arguments = @_; |
| 1352 |
my $sth = undef; |
| 1353 |
my $rows = undef; |
| 1354 |
foreach (@arguments) { |
| 1355 |
# replace the ? by the parameters |
| 1356 |
if (/^\d+$/) { |
| 1357 |
$sql =~ s/\?/$_/; |
| 1358 |
} else { |
| 1359 |
$sql =~ s/\?/'$_'/; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
$self->trace(sprintf "SQL (? resolved):\n%s\nARGS:\n%s\n", |
| 1363 |
$sql, Data::Dumper::Dumper(\@arguments)); |
| 1364 |
$self->create_commandfile($sql); |
| 1365 |
my $exit_output = `$self->{sqlplus}`; |
| 1366 |
if ($?) { |
| 1367 |
printf STDERR "fetchrow_array exit bumm %s\n", $exit_output; |
| 1368 |
my $output = do { local (@ARGV, $/) = $self->{sql_resultfile}; <> }; |
| 1369 |
my @oerrs = map { |
| 1370 |
/((ERROR \d+).*)/ ? $1 : (); |
| 1371 |
} split(/\n/, $output); |
| 1372 |
$self->{errstr} = join(" ", @oerrs); |
| 1373 |
} else { |
| 1374 |
my $output = do { local (@ARGV, $/) = $self->{sql_resultfile}; <> }; |
| 1375 |
my @rows = map { [ |
| 1376 |
map { convert($_) } |
| 1377 |
map { s/^\s+([\.\d]+)$/$1/g; $_ } |
| 1378 |
map { s/\s+$//g; $_ } |
| 1379 |
split /\t/ |
| 1380 |
] } grep { ! /^\d+ rows selected/ } |
| 1381 |
grep { ! /^Elapsed: / } |
| 1382 |
grep { ! /^\s*$/ } split(/\n/, $output); |
| 1383 |
$rows = \@rows; |
| 1384 |
$self->trace(sprintf "RESULT:\n%s\n", |
| 1385 |
Data::Dumper::Dumper($rows)); |
| 1386 |
} |
| 1387 |
if ($@) { |
| 1388 |
$self->debug(sprintf "bumm %s", $@); |
| 1389 |
} |
| 1390 |
unlink $self->{sql_commandfile}; |
| 1391 |
unlink $self->{sql_resultfile}; |
| 1392 |
return @{$rows}; |
| 1393 |
} |
| 1394 |
|
| 1395 |
sub func { |
| 1396 |
my $self = shift; |
| 1397 |
my $function = shift; |
| 1398 |
$self->{handle}->func(@_); |
| 1399 |
} |
| 1400 |
|
| 1401 |
sub convert { |
| 1402 |
my $n = shift; |
| 1403 |
# mostly used to convert numbers in scientific notation |
| 1404 |
if ($n =~ /^\s*\d+\s*$/) { |
| 1405 |
return $n; |
| 1406 |
} elsif ($n =~ /^\s*([-+]?)(\d*[\.,]*\d*)[eE]{1}([-+]?)(\d+)\s*$/) { |
| 1407 |
my ($vor, $num, $sign, $exp) = ($1, $2, $3, $4); |
| 1408 |
$n =~ s/E/e/g; |
| 1409 |
$n =~ s/,/\./g; |
| 1410 |
$num =~ s/,/\./g; |
| 1411 |
my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : ''; |
| 1412 |
my $dec = sprintf "%${sig}f", $n; |
| 1413 |
$dec =~ s/\.[0]+$//g; |
| 1414 |
return $dec; |
| 1415 |
} elsif ($n =~ /^\s*([-+]?)(\d+)[\.,]*(\d*)\s*$/) { |
| 1416 |
return $1.$2.".".$3; |
| 1417 |
} elsif ($n =~ /^\s*(.*?)\s*$/) { |
| 1418 |
return $1; |
| 1419 |
} else { |
| 1420 |
return $n; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
sub execute { |
| 1426 |
my $self = shift; |
| 1427 |
my $sql = shift; |
| 1428 |
eval { |
| 1429 |
my $sth = $self->{handle}->prepare($sql); |
| 1430 |
$sth->execute(); |
| 1431 |
}; |
| 1432 |
if ($@) { |
| 1433 |
printf STDERR "bumm %s\n", $@; |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
sub errstr { |
| 1438 |
my $self = shift; |
| 1439 |
return $self->{errstr}; |
| 1440 |
} |
| 1441 |
|
| 1442 |
sub DESTROY { |
| 1443 |
my $self = shift; |
| 1444 |
$self->trace("try to clean up command and result files"); |
| 1445 |
unlink $self->{sql_commandfile} if -f $self->{sql_commandfile}; |
| 1446 |
unlink $self->{sql_resultfile} if -f $self->{sql_resultfile}; |
| 1447 |
} |
| 1448 |
|
| 1449 |
sub create_commandfile { |
| 1450 |
my $self = shift; |
| 1451 |
my $sql = shift; |
| 1452 |
open CMDCMD, "> $self->{sql_commandfile}"; |
| 1453 |
printf CMDCMD "%s\n", $sql; |
| 1454 |
close CMDCMD; |
| 1455 |
} |
| 1456 |
|
| 1457 |
sub decode_password { |
| 1458 |
my $self = shift; |
| 1459 |
my $password = shift; |
| 1460 |
$password = $self->SUPER::decode_password($password); |
| 1461 |
# we call '...%s/%s@...' inside backticks where the second %s is the password |
| 1462 |
# abc'xcv -> ''abc'\''xcv'' |
| 1463 |
# abc'`xcv -> ''abc'\''\`xcv'' |
| 1464 |
if ($password && $password =~ /'/) { |
| 1465 |
$password = "'".join("\\'", map { "'".$_."'"; } split("'", $password))."'"; |
| 1466 |
} |
| 1467 |
return $password; |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
package DBD::MySQL::Server::Connection::Sqlrelay; |
| 1472 |
|
| 1473 |
use strict; |
| 1474 |
use Net::Ping; |
| 1475 |
|
| 1476 |
our @ISA = qw(DBD::MySQL::Server::Connection); |
| 1477 |
|
| 1478 |
|
| 1479 |
sub init { |
| 1480 |
my $self = shift; |
| 1481 |
my %params = @_; |
| 1482 |
my $retval = undef; |
| 1483 |
if ($self->{mode} =~ /^server::tnsping/) { |
| 1484 |
if (! $self->{connect}) { |
| 1485 |
$self->{errstr} = "Please specify a database"; |
| 1486 |
} else { |
| 1487 |
if ($self->{connect} =~ /([\.\w]+):(\d+)/) { |
| 1488 |
$self->{host} = $1; |
| 1489 |
$self->{port} = $2; |
| 1490 |
$self->{socket} = ""; |
| 1491 |
} elsif ($self->{connect} =~ /([\.\w]+):([\w\/]+)/) { |
| 1492 |
$self->{host} = $1; |
| 1493 |
$self->{socket} = $2; |
| 1494 |
$self->{port} = ""; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
} else { |
| 1498 |
if (! $self->{hostname} || ! $self->{username} || ! $self->{password}) { |
| 1499 |
if ($self->{hostname} && $self->{hostname} =~ /(\w+?)\/(.+)@([\.\w]+):(\d+)/) { |
| 1500 |
$self->{username} = $1; |
| 1501 |
$self->{password} = $2; |
| 1502 |
$self->{hostname} = $3; |
| 1503 |
$self->{port} = $4; |
| 1504 |
$self->{socket} = ""; |
| 1505 |
} elsif ($self->{hostname} && $self->{hostname} =~ /(\w+?)\/(.+)@([\.\w]+):([\w\/]+)/) { |
| 1506 |
$self->{username} = $1; |
| 1507 |
$self->{password} = $2; |
| 1508 |
$self->{hostname} = $3; |
| 1509 |
$self->{socket} = $4; |
| 1510 |
$self->{port} = ""; |
| 1511 |
} else { |
| 1512 |
$self->{errstr} = "Please specify database, username and password"; |
| 1513 |
return undef; |
| 1514 |
} |
| 1515 |
} else { |
| 1516 |
if ($self->{hostname} =~ /([\.\w]+):(\d+)/) { |
| 1517 |
$self->{hostname} = $1; |
| 1518 |
$self->{port} = $2; |
| 1519 |
$self->{socket} = ""; |
| 1520 |
} elsif ($self->{hostname} =~ /([\.\w]+):([\w\/]+)/) { |
| 1521 |
$self->{hostname} = $1; |
| 1522 |
$self->{socket} = $2; |
| 1523 |
$self->{port} = ""; |
| 1524 |
} else { |
| 1525 |
$self->{errstr} = "Please specify hostname, username, password and port/socket"; |
| 1526 |
return undef; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
} |
| 1530 |
if (! exists $self->{errstr}) { |
| 1531 |
eval { |
| 1532 |
require DBI; |
| 1533 |
use POSIX ':signal_h'; |
| 1534 |
if ($^O =~ /MSWin/) { |
| 1535 |
local $SIG{'ALRM'} = sub { |
| 1536 |
die "alarm\n"; |
| 1537 |
}; |
| 1538 |
} else { |
| 1539 |
my $mask = POSIX::SigSet->new( SIGALRM ); |
| 1540 |
my $action = POSIX::SigAction->new( |
| 1541 |
sub { die "alarm\n" ; }, $mask); |
| 1542 |
my $oldaction = POSIX::SigAction->new(); |
| 1543 |
sigaction(SIGALRM ,$action ,$oldaction ); |
| 1544 |
} |
| 1545 |
alarm($self->{timeout} - 1); # 1 second before the global unknown timeout |
| 1546 |
if ($self->{handle} = DBI->connect( |
| 1547 |
sprintf("DBI:SQLRelay:host=%s;port=%d;socket=%s", |
| 1548 |
$self->{hostname}, $self->{port}, $self->{socket}), |
| 1549 |
$self->{username}, |
| 1550 |
$self->decode_password($self->{password}), |
| 1551 |
{ RaiseError => 1, AutoCommit => 0, PrintError => 1 })) { |
| 1552 |
$retval = $self; |
| 1553 |
if ($self->{mode} =~ /^server::tnsping/ && $self->{handle}->ping()) { |
| 1554 |
# database connected. fake a "unknown user" |
| 1555 |
$self->{errstr} = "ORA-01017"; |
| 1556 |
} |
| 1557 |
} else { |
| 1558 |
$self->{errstr} = DBI::errstr(); |
| 1559 |
} |
| 1560 |
}; |
| 1561 |
if ($@) { |
| 1562 |
$self->{errstr} = $@; |
| 1563 |
$self->{errstr} =~ s/at [\w\/\.]+ line \d+.*//g; |
| 1564 |
$retval = undef; |
| 1565 |
} |
| 1566 |
} |
| 1567 |
$self->{tac} = Time::HiRes::time(); |
| 1568 |
return $retval; |
| 1569 |
} |
| 1570 |
|
| 1571 |
sub fetchrow_array { |
| 1572 |
my $self = shift; |
| 1573 |
my $sql = shift; |
| 1574 |
my @arguments = @_; |
| 1575 |
my $sth = undef; |
| 1576 |
my @row = (); |
| 1577 |
$self->trace(sprintf "fetchrow_array: %s", $sql); |
| 1578 |
eval { |
| 1579 |
$sth = $self->{handle}->prepare($sql); |
| 1580 |
if (scalar(@arguments)) { |
| 1581 |
$sth->execute(@arguments); |
| 1582 |
} else { |
| 1583 |
$sth->execute(); |
| 1584 |
} |
| 1585 |
@row = $sth->fetchrow_array(); |
| 1586 |
}; |
| 1587 |
if ($@) { |
| 1588 |
$self->debug(sprintf "bumm %s", $@); |
| 1589 |
} |
| 1590 |
if (-f "/tmp/check_mysql_health_simulation/".$self->{mode}) { |
| 1591 |
my $simulation = do { local (@ARGV, $/) = |
| 1592 |
"/tmp/check_mysql_health_simulation/".$self->{mode}; <> }; |
| 1593 |
@row = split(/\s+/, (split(/\n/, $simulation))[0]); |
| 1594 |
} |
| 1595 |
return $row[0] unless wantarray; |
| 1596 |
return @row; |
| 1597 |
} |
| 1598 |
|
| 1599 |
sub fetchall_array { |
| 1600 |
my $self = shift; |
| 1601 |
my $sql = shift; |
| 1602 |
my @arguments = @_; |
| 1603 |
my $sth = undef; |
| 1604 |
my $rows = undef; |
| 1605 |
$self->trace(sprintf "fetchall_array: %s", $sql); |
| 1606 |
eval { |
| 1607 |
$sth = $self->{handle}->prepare($sql); |
| 1608 |
if (scalar(@arguments)) { |
| 1609 |
$sth->execute(@arguments); |
| 1610 |
} else { |
| 1611 |
$sth->execute(); |
| 1612 |
} |
| 1613 |
$rows = $sth->fetchall_arrayref(); |
| 1614 |
}; |
| 1615 |
if ($@) { |
| 1616 |
printf STDERR "bumm %s\n", $@; |
| 1617 |
} |
| 1618 |
if (-f "/tmp/check_mysql_health_simulation/".$self->{mode}) { |
| 1619 |
my $simulation = do { local (@ARGV, $/) = |
| 1620 |
"/tmp/check_mysql_health_simulation/".$self->{mode}; <> }; |
| 1621 |
@{$rows} = map { [ split(/\s+/, $_) ] } split(/\n/, $simulation); |
| 1622 |
} |
| 1623 |
return @{$rows}; |
| 1624 |
} |
| 1625 |
|
| 1626 |
sub func { |
| 1627 |
my $self = shift; |
| 1628 |
$self->{handle}->func(@_); |
| 1629 |
} |
| 1630 |
|
| 1631 |
sub execute { |
| 1632 |
my $self = shift; |
| 1633 |
my $sql = shift; |
| 1634 |
eval { |
| 1635 |
my $sth = $self->{handle}->prepare($sql); |
| 1636 |
$sth->execute(); |
| 1637 |
}; |
| 1638 |
if ($@) { |
| 1639 |
printf STDERR "bumm %s\n", $@; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
sub DESTROY { |
| 1644 |
my $self = shift; |
| 1645 |
#$self->trace(sprintf "disconnecting DBD %s", |
| 1646 |
# $self->{handle} ? "with handle" : "without handle"); |
| 1647 |
#$self->{handle}->disconnect() if $self->{handle}; |
| 1648 |
} |
| 1649 |
|
| 1650 |
1; |
| 1651 |
|
| 1652 |
|