1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
|
#!/usr/bin/perl -w
#
# ~/check_logfiles/test/100flooddetect.t
#
# Test that all the Perl modules we require are available.
# Events need not exceed a certain rate. Timestamps need to be taken into account.
# A short history of events needs to be saved after every run.
# With the advent of a critical pattern, the rate of events needs to be calculated.
#
# Test 1:
# Maximum of 10 event per minute can be tolerated
# Simulate a check_period of 5 minutes
# Start - within the first minute create an event every 2 seconds
#
use strict;
use Test::More tests => 2;
use Cwd;
use Data::Dumper;
use lib "../plugins-scripts";
use Nagios::CheckLogfiles::Test;
use constant TESTDIR => ".";
my $configfile = <<'EOCFG';
sub timestamp {
my $string = shift;
my($sec, $min, $hour, $mday, $mon, $year) =
(localtime)[0, 1, 2, 3, 4, 5];
# Oct 31 15:49:21
$string =~ /^(\w+)\s+(\d+) (\d+):(\d+):(\d+)/;
my $time = POSIX::mktime($5, $4, $3, $2, {
"Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10, "Dec" => 11
}->{$1}, $year);
return $time;
}
sub flood_check
{
my $fc = shift; # max flood events count
my $fp = shift; # max flood time period for $fc events
my $en = shift; # event name (key) which identifies flood check data
$FLOOD{ $en } ||= []; # make empty flood array for this event name
my $ar = $FLOOD{ $en }; # get array ref for event's flood array
my $ec = @$ar; # events count in the flood array
if( $ec >= $fc )
{
# flood array has enough events to do real flood check
my $ot = $$ar[0]; # oldest event timestamp in the flood array
my $tp = time() - $ot; # time period between current and oldest event
# now calculate time in seconds until next allowed event
my $wait = int( ( $ot + ( $ec * $fp / $fc ) ) - time() );
if( $wait > 0 )
{
# positive number of seconds means flood in progress
# event should be rejected or postponed
return $wait;
}
# negative or 0 seconds means that event should be accepted
# oldest event is removed from the flood array
shift @$ar;
}
# flood array is not full or oldest event is already removed
# so current event has to be added
push @$ar, time();
# event is ok
return 0;
}
sub is_flood {
my $events = shift;
my $event_time = shift;
my $flood_events = shift; # max flood events count
my $flood_period = shift; # max flood time period for $fc events
my $num_events = scalar(@{$events});
if ($num_events >= $flood_events) {
printf STDERR "num_events %d flood_events %d\n", $num_events, $flood_events;
# enough elements to make a reliable calculation
my $oldest_event_time = $$events[0];
my $period = $event_time - $oldest_event_time;
printf STDERR "oldest %s ... period %d\n", scalar localtime $oldest_event_time, $period;
my $wait = int( ( $oldest_event_time + ( $num_events * $flood_period / $flood_events ) ) - $event_time );
if ($wait > 0) {
return $wait;
} else {
shift @{$events};
return 0;
}
} else {
push(@{$events}, $event_time);
return 0;
}
}
$protocolsdir = "./var/tmp";
$seekfilesdir = "./var/tmp";
$scriptpath = "./bin";
@searches = ({
tag => "flood",
logfile => "./var/adm/messages",
criticalpatterns => [
'connection refused',
'connection on port\s+\d+',
'session aborted'
],
options => "supersmartscript",
script => sub {
my $line = $ENV{CHECK_LOGFILES_SERVICEOUTPUT};
if (! exists $CHECK_LOGFILES_PRIVATESTATE->{eventhistory}) {
$CHECK_LOGFILES_PRIVATESTATE->{eventhistory} = [];
printf STDERR "initialized eventhistory\n";
}
my $flood = is_flood($CHECK_LOGFILES_PRIVATESTATE->{eventhistory}, timestamp($line), 5, 10);
if ($flood) {
printf "floot reached %.3f at %s\n", $flood, scalar localtime timestamp($line);
return 2;
} else {
return 0;
}
},
});
#$options = "supersmartpostscript";
$postscript = sub {
my $state = $CHECK_LOGFILES_PRIVATESTATE;
#printf STDERR "%s\n", Data::Dumper::Dumper($state);
return 0;
};
EOCFG
open CCC, ">./etc/check_flood.cfg";
print CCC $configfile;
close CCC;
my $cl = Nagios::CheckLogfiles::Test->new({ cfgfile => "./etc/check_flood.cfg" });
my $flood = $cl->get_search_by_tag("flood");
$cl->reset();
$flood->delete_logfile();
$flood->delete_seekfile();
diag("deleted logfile and seekfile");
$flood->trace("deleted logfile and seekfile");
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
diag("wrote 100 messages");
sleep 1;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0)); # 1
diag("now write an event every two seconds. duration is 2 minutes");
my $tic = time;
#foreach my $sec (1..120) {
foreach my $sec (1..20) {
$flood->loggercrap(undef, undef, 10);
$flood->logger(undef, undef, 1, "connection refused");
printf STDERR "write event at %s\n", scalar localtime time;
$flood->loggercrap(undef, undef, 10);
sleep 1;
}
foreach my $sec (1..40) {
$flood->loggercrap(undef, undef, 10);
sleep 1;
}
my $tac = time;
printf "elapsed %d\n", $tac - $tic;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(15, 0, 5, 0, 2));
exit 0;
# write 4 events in 10 sec
# write crap in 10 sec
# repeat until a minute
$tic = time;
foreach my $loop (1..10) {
foreach my $sec (1..3) {
$flood->loggercrap(undef, undef, 10);
$flood->logger(undef, undef, 1, "connection refused");
printf STDERR "write event at %s\n", scalar localtime time;
sleep 1;
}
diag("---");
foreach my $sec (1..7) {
$flood->loggercrap(undef, undef, 10);
sleep 1;
}
diag("...");
}
$tac = time;
printf "4/10 sequence elapsed %d\n", $tac - $tic;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(15, 0, 5, 0, 0)); # 1
exit;
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..70) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 70 C
}
sleep 1;
$cl->run();
#diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
$flood->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 17, 0, 2)); # 170 C insges. / 10 # 2
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..100) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 99 C 1 W
}
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 30, "connection on port 80"); # 30 W
$flood->loggercrap(undef, undef, 100);
sleep 1;
$cl->run();
$flood->dump_protocol();
#diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 10, 19, 0, 2)); # 199 C 31 W # 3
# 199 = 19 * _10_ + 9, 31 = 10 * _3_ + 1
# rest 9 C 1 W
$cl->reset();
$cl->run(); # logfile did not change. do nothing
ok($cl->expect_result(0, 0, 0, 0, 0)); # 4
sleep 10;
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 12, "connection refused"); # 9 waren uebrig, + 12 = 21
# es ist auch noch eine warning uebrig. diese ist aber erst dann wieder relevant, wenn noch weitere neue warnings dazukommen und die schwelle ueberschreiten
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 2, 0, 2)); # 5
ok(($flood->{newstate}->{thresholdcnt}->{CRITICAL} == 1) &&
($flood->{newstate}->{thresholdcnt}->{WARNING} == 1)); # 6
$cl->reset();
$flood->logger(undef, undef, 9, "connection refused"); # 1 C uebrig, + 9 = 10
$flood->logger(undef, undef, 5, "connection on port 80"); # 1 W uebrig + 5 = 6
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 2, 1, 0, 2)); # 7
ok(($flood->{newstate}->{thresholdcnt}->{CRITICAL} == 0) &&
($flood->{newstate}->{thresholdcnt}->{WARNING} == 0)); # 8
$cl = undef;
$flood = undef;
$cl = Nagios::CheckLogfiles::Test->new({
protocolsdir => TESTDIR."/var/tmp",
seekfilesdir => TESTDIR."/var/tmp",
searches => [
{
tag => "flood",
logfile => TESTDIR."/var/adm/messages",
criticalpatterns => [
'connection refused',
'connection on port\s+\d+',
'session aborted'
],
criticalexceptions => 'connection on port\s+80[^\d]*',
criticalthreshold => 10,
warningpatterns => [
'.*total size is 0 .*',
'connection on port\s+80[^\d]*',
],
warningthreshold => 3,
options => "nosavethresholdcount",
}
] });
$flood = $cl->get_search_by_tag("flood");
$cl->reset();
$flood->delete_logfile();
$flood->delete_seekfile();
diag(Data::Dumper::Dumper($flood->{options}));
diag("deleted logfile and seekfile");
$flood->trace("deleted logfile and seekfile");
$flood->logger(undef, undef, 100, "connection refused");
diag("wrote 100 messages");
sleep 1;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0)); ## reset run 9
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..70) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 70 C
}
sleep 1;
$cl->run();
#diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
$flood->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 17, 0, 2)); # 10
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..100) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 99C 1 W
}
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 30, "connection on port 80"); # 30 W
$flood->loggercrap(undef, undef, 100);
sleep 1;
$cl->run();
$flood->dump_protocol();
diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 10, 19, 0, 2)); # 11
$cl->reset();
$cl->run();
ok($cl->expect_result(0, 0, 0, 0, 0)); # 12
$cl->reset();
diag("thresholdcnt must be 0");
diag(Data::Dumper::Dumper($cl->{allerrors}));
diag(Data::Dumper::Dumper($flood->{threshold}));
diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 12, "connection refused hihi"); # 12 C
$cl->run();
diag("now it is");
diag(Data::Dumper::Dumper($flood->{matchlines}));
diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
diag(Data::Dumper::Dumper($cl->{allerrors}));
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 1, 0, 2)); # 13
$cl->reset();
$flood->logger(undef, undef, 9, "connection refused hoho");
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0)); # 14
ok($flood->{thresholdcnt}->{CRITICAL} == 9);
$cl->reset();
$flood->logger(undef, undef, 15, "connection refused haha");
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 1, 0, 2)); # 16
ok($flood->{thresholdcnt}->{CRITICAL} == 5);
####################################################################
# now the same but with the new method
# options => 'criticalthreshold=x,warningthreshold=y
$cl = Nagios::CheckLogfiles::Test->new({
protocolsdir => TESTDIR."/var/tmp",
seekfilesdir => TESTDIR."/var/tmp",
searches => [ {
tag => "flood",
logfile => TESTDIR."/var/adm/messages",
criticalpatterns => [
'connection refused',
'connection on port\s+\d+',
'session aborted'
],
criticalexceptions => 'connection on port\s+80[^\d]*',
warningpatterns => [
'.*total size is 0 .*',
'connection on port\s+80[^\d]*',
],
options => 'criticalthreshold=10,warningthreshold=3',
} ] });
$flood = $cl->get_search_by_tag("flood");
$cl->reset();
$flood->delete_logfile();
$flood->delete_seekfile();
diag("deleted logfile and seekfile");
$flood->trace("deleted logfile and seekfile");
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
diag("wrote 100 messages");
sleep 1;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0)); # 1
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..70) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 70 C
}
sleep 1;
$cl->run();
#diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
$flood->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 17, 0, 2)); # 170 C insges. / 10 # 2
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..100) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 99 C 1 W
}
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 30, "connection on port 80"); # 30 W
$flood->loggercrap(undef, undef, 100);
sleep 1;
$cl->run();
$flood->dump_protocol();
#diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 10, 19, 0, 2)); # 199 C 31 W # 3
# 199 = 19 * _10_ + 9, 31 = 10 * _3_ + 1
# rest 9 C 1 W
$cl->reset();
$cl->run(); # logfile did not change. do nothing
ok($cl->expect_result(0, 0, 0, 0, 0)); # 4
sleep 10;
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 12, "connection refused"); # 9 waren uebrig, + 12 = 21
# es ist auch noch eine warning uebrig. diese ist aber erst dann wieder relevant, wenn noch weitere neue warnings dazukommen und die schwelle ueberschreiten
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 2, 0, 2)); # 5
ok(($flood->{newstate}->{thresholdcnt}->{CRITICAL} == 1) &&
($flood->{newstate}->{thresholdcnt}->{WARNING} == 1)); # 6
$cl->reset();
$flood->logger(undef, undef, 9, "connection refused"); # 1 C uebrig, + 9 = 10
$flood->logger(undef, undef, 5, "connection on port 80"); # 1 W uebrig + 5 = 6
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 2, 1, 0, 2)); # 7
ok(($flood->{newstate}->{thresholdcnt}->{CRITICAL} == 0) &&
($flood->{newstate}->{thresholdcnt}->{WARNING} == 0)); # 8
$cl = undef;
$flood = undef;
$cl = Nagios::CheckLogfiles::Test->new({
protocolsdir => TESTDIR."/var/tmp",
seekfilesdir => TESTDIR."/var/tmp",
searches => [
{
tag => "flood",
logfile => TESTDIR."/var/adm/messages",
criticalpatterns => [
'connection refused',
'connection on port\s+\d+',
'session aborted'
],
criticalexceptions => 'connection on port\s+80[^\d]*',
warningpatterns => [
'.*total size is 0 .*',
'connection on port\s+80[^\d]*',
],
options => 'criticalthreshold=10,warningthreshold=3,nosavethresholdcount',
}
] });
$flood = $cl->get_search_by_tag("flood");
$cl->reset();
$flood->delete_logfile();
$flood->delete_seekfile();
diag(Data::Dumper::Dumper($flood->{options}));
diag("deleted logfile and seekfile");
$flood->trace("deleted logfile and seekfile");
$flood->logger(undef, undef, 100, "connection refused");
diag("wrote 100 messages");
sleep 1;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0)); ## reset run 9
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..70) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 70 C
}
sleep 1;
$cl->run();
#diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
$flood->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 17, 0, 2)); # 10
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..100) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 99C 1 W
}
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 30, "connection on port 80"); # 30 W
$flood->loggercrap(undef, undef, 100);
sleep 1;
$cl->run();
$flood->dump_protocol();
diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
#diag(Data::Dumper::Dumper($flood->{laststate}));
#diag(Data::Dumper::Dumper($flood->{newstate}));
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 10, 19, 0, 2)); # 11
$cl->reset();
$cl->run();
ok($cl->expect_result(0, 0, 0, 0, 0)); # 12
$cl->reset();
diag("thresholdcnt must be 0");
diag(Data::Dumper::Dumper($cl->{allerrors}));
diag(Data::Dumper::Dumper($flood->{threshold}));
diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 12, "connection refused hihi"); # 12 C
$cl->run();
diag("now it is");
diag(Data::Dumper::Dumper($flood->{matchlines}));
diag(Data::Dumper::Dumper($flood->{thresholdcnt}));
diag(Data::Dumper::Dumper($cl->{allerrors}));
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 1, 0, 2)); # 13
$cl->reset();
$flood->logger(undef, undef, 9, "connection refused hoho");
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0)); # 14
ok($flood->{thresholdcnt}->{CRITICAL} == 9);
$cl->reset();
$flood->logger(undef, undef, 15, "connection refused haha");
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 1, 0, 2)); # 16
ok($flood->{thresholdcnt}->{CRITICAL} == 5);
###### now with --criticalthreshold --warningtheshold
$cl = Nagios::CheckLogfiles::Test->new({
protocolsdir => TESTDIR."/var/tmp",
seekfilesdir => TESTDIR."/var/tmp",
searches => [
{
tag => "flood",
logfile => TESTDIR."/var/adm/messages",
criticalpatterns => [
'connection refused',
'connection on port\s+\d+',
'session aborted'
],
criticalexceptions => 'connection on port\s+80[^\d]*',
warningpatterns => [
'.*total size is 0 .*',
'connection on port\s+80[^\d]*',
],
options => 'criticalthreshold=10,warningthreshold=3,nosavethresholdcount',
}
] });
$flood = $cl->get_search_by_tag("flood");
$cl->reset();
$flood->delete_logfile();
$flood->delete_seekfile();
diag(Data::Dumper::Dumper($flood->{options}));
diag("deleted logfile and seekfile");
$flood->trace("deleted logfile and seekfile");
$flood->logger(undef, undef, 100, "connection refused");
diag("wrote 100 messages");
sleep 1;
# dummy command
my $perlpath = `which perl`;
chomp $perlpath;
if ($^O =~ /MSWin/) {
if (-f 'C:\strawberry\perl\bin\perl.exe') {
$perlpath = 'C:\strawberry\perl\bin\perl';
} else {
$perlpath = 'C:\Perl\bin\perl';
}
$flood->{logfile} =~ s/\//\\/g;
}
my $command = sprintf $perlpath.' ../plugins-scripts/check_logfiles --tag=flood --criticalpattern="(connection refused)|(connection on port\\s+\\d+)|(session aborted)" --warningpattern="(.*total size is 0 .*)|(connection on port\\s+80[^\\d]*)" --logfile %s --warningthreshold 3 --criticalthreshold 10 --seekfilesdir "%s"',
$flood->{logfile}, $flood->{seekfilesdir};
diag("now run a dummy commandline");
my $output = `$command`;
diag($output);
ok(($output =~ /OK - no errors or warnings/) && (($? >> 8) == 0));
$cl->reset();
$flood->loggercrap(undef, undef, 100);
$flood->logger(undef, undef, 100, "connection refused"); # 100 C
$flood->loggercrap(undef, undef, 100);
foreach my $port (1..70) {
$flood->logger(undef, undef, 1, "connection on port $port"); # 70 C
}
sleep 1;
$flood->trace("executing %s", $command);
diag("now run the real commandline which expects 17 criticals");
$output = `$command`;
diag($output);
ok(($output =~ /CRITICAL - \(17 errors/) && (($? >> 8) == 2));
|