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
|
#!/usr/bin/perl -w
#
# ~/check_logfiles/test/080configdir.t
#
# like 011searches. Two configfiles with overlapping tags
#
use strict;
use Test::More tests => 8;
use Cwd;
use lib "../plugins-scripts";
use Nagios::CheckLogfiles::Test;
use constant TESTDIR => ".";
if ($^O =~ /MSWin/) {
system ('rd .\etc\etc');
system ('md etc\etc');
system ('DEL /Q /S /F .\var\tmp\*');
} else {
system ("mkdir -p etc/etc");
system ("rm -rf etc/etc/*");
system ("rm -rf var/tmp/*");
}
my $configfile =<<EOCFG;
\$options = "supersmartpostscript";
\$protocolsdir = "./var/tmp";
\$seekfilesdir = "./var/tmp";
\@searches = (
{
tag => "ssh",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user8",
warningpatterns => "Unknown user",
options => "perfdata,nologfilenocry"
},
{
tag => "test",
logfile => "./var/adm/messages",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user9",
warningpatterns => "Unknown user",
options => "noperfdata,nologfilenocry"
},
{
tag => "null",
logfile => "./var/adm/messages",
logfile => "./var/adm/messages",
criticalpatterns => ".*nonsense.*",
warningpatterns => "crap",
options => "perfdata,nologfilenocry"
},
);
\$postscript = sub {
printf "uiuiuiuiuiui\\n";
return 2;
};
EOCFG
open CCC, ">./etc/etc/001searches.cfg";
print CCC $configfile;
close CCC;
$configfile =<<EOCFG;
\$options = "supersmartpostscript";
\$protocolsdir = "./var/tmp";
\$seekfilesdir = "./var/tmp";
\@searches = (
{
# this is a patch for the user8-version
tag => "ssh",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user18",
warningpatterns => "Unknown user",
options => "perfdata,nologfilenocry"
},
{
tag => "rulzn",
logfile => "./var/adm/messages",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user ruhland",
warningpatterns => "Unknown user",
options => "noperfdata,nologfilenocry"
},
{
tag => "null",
logfile => "./var/adm/messages",
logfile => "./var/adm/messages",
criticalpatterns => ".*nonsense.*",
warningpatterns => "crap",
options => "perfdata,nologfilenocry"
},
);
\$postscript = sub {
printf "oioioioioioioi %s | %s\\n",
\$ENV{CHECK_LOGFILES_SERVICEOUTPUT},
\$ENV{CHECK_LOGFILES_SERVICEPERFDATA};
return \$ENV{CHECK_LOGFILES_SERVICESTATEID};
};
EOCFG
open CCC, ">./etc/etc/002searches.cfg";
print CCC $configfile;
close CCC;
my $cl = Nagios::CheckLogfiles::Test->new({ cfgfile => ["./etc/etc/001searches.cfg", "./etc/etc/002searches.cfg"], selectedsearches => ['ssh', 'null', 'rulzn'] });
# ssh null rulzn postscript
diag("================================================================");
diag("reset run");
ok(scalar @{$cl->{searches}} == 4);
my $ssh = $cl->get_search_by_tag("ssh");
$ssh->delete_logfile();
$ssh->delete_seekfile();
$ssh->trace("deleted logfile and seekfile");
#my $test = $cl->get_search_by_tag("test");
#$test->delete_logfile();
#$test->delete_seekfile();
#$test->trace("deleted logfile and seekfile");
my $null = $cl->get_search_by_tag("null");
$null->delete_logfile();
$null->delete_seekfile();
$null->trace("deleted logfile and seekfile");
my $rulzn = $cl->get_search_by_tag("rulzn");
$rulzn->delete_logfile();
$rulzn->delete_seekfile();
$rulzn->trace("deleted logfile and seekfile");
printf "%s\n", Data::Dumper::Dumper($cl->{allerrors});
$cl->run();
diag($cl->has_result());
printf "%s\n", Data::Dumper::Dumper($cl->{allerrors});
diag($cl->{exitmessage});
ok($cl->expect_result(1, 0, 0, 0, 0)); # 1ok kommt vom postscript
$ssh->trace("==== 3-4 ====");
diag("================================================================");
diag("3 - 4");
sleep 1;
$cl->reset();
$ssh->loggercrap(undef, undef, 2);
$ssh->logger(undef, undef, 20, "Failed password for invalid user8");
$ssh->logger(undef, undef, 2, "Failed password for invalid user18");
$ssh->loggercrap(undef, undef, 2);
# ssh logs instead of test
$ssh->loggercrap(undef, undef, 2);
$ssh->logger(undef, undef, 2, "Failed password for invalid user9");
$ssh->loggercrap(undef, undef, 2);
$null->loggercrap(undef, undef, 2);
$null->logger(undef, undef, 2, "Failed password is nonsense");
$null->loggercrap(undef, undef, 2);
printf "calling run for 3, 4\n";
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 1, 0, 2)); # 1 statt 4 wegen postscript (das resettet)
ok($cl->{exitmessage} =~ /.*CRITICAL - \(4 errors.* Failed password is nonsense /);
$ssh->trace("==== 5 - 6 ====");
diag("================================================================");
diag("5 - 6");
sleep 1;
$cl->reset();
$ssh->loggercrap(undef, undef, 20);
$rulzn->logger(undef, undef, 3, "Failed password for invalid user ruhland");
$ssh->logger(undef, undef, 30, "Failed password for invalid user8");
$ssh->logger(undef, undef, 3, "Failed password for invalid user18");
$ssh->loggercrap(undef, undef, 20);
# ssh logs instead of test
$ssh->loggercrap(undef, undef, 20);
$ssh->logger(undef, undef, 200, "Failed password for invalid user9");
$ssh->loggercrap(undef, undef, 20);
$null->loggercrap(undef, undef, 20);
$null->logger(undef, undef, 4, "Failed password is nonsense");
$null->loggercrap(undef, undef, 20);
my $command = sprintf 'perl ../plugins-scripts/check_logfiles -F ./etc/etc --searches=ssh,null,rulzn';
$ssh->trace("executing %s", $command);
my $output = `$command`;
diag($output);
diag("done");
diag($? >> 8);
ok(($? >> 8) == 2);
ok($output =~ /CRITICAL - \(10 errors .* Failed password is nonsense /);
$ssh->trace("==== 7 ====");
diag("================================================================");
diag("7 - ");
#
## 1 main configfile
# etc/etc is a directory for many patch files
# define test ssh in main
# define ssh again in 001patch 002patch
##
if ($^O =~ /MSWin/) {
system ('rd /Q /S .\etc\etc');
system ('md etc\etc');
system ('DEL /Q /S /F etc\searches.cfg');
} else {
system ("rm -rf etc/etc/*");
system ("rm -f etc/searches.cfg");
}
$configfile =<<EOCFG;
\$options = "supersmartpostscript";
\$protocolsdir = "./var/tmp";
\$seekfilesdir = "./var/tmp";
\@searches = (
{
tag => "ssh",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user8",
warningpatterns => "Unknown user",
options => "perfdata,nologfilenocry"
},
{
tag => "test",
logfile => "./var/adm/messages",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user9",
warningpatterns => "Unknown user",
options => "noperfdata,nologfilenocry"
},
{
tag => "null",
logfile => "./var/adm/messages",
logfile => "./var/adm/messages",
criticalpatterns => ".*nonsense.*",
warningpatterns => "crap",
options => "perfdata,nologfilenocry"
},
);
\$postscript = sub {
printf "uiuiuiuiuiui\\n";
return 2;
};
EOCFG
open CCC, ">./etc/searches.cfg";
print CCC $configfile;
close CCC;
$configfile =<<EOCFG;
\$options = "supersmartpostscript";
\$protocolsdir = "./var/tmp";
\$seekfilesdir = "./var/tmp";
\@searches = (
{
tag => "ssh",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user81",
warningpatterns => "Unknown user",
options => "perfdata,nologfilenocry"
},
);
\$postscript = sub {
printf "hundsglump varreckts nommol!!! %s | %s\\n",
\$ENV{CHECK_LOGFILES_SERVICEOUTPUT},
\$ENV{CHECK_LOGFILES_SERVICEPERFDATA};
return \$ENV{CHECK_LOGFILES_SERVICESTATEID};
};
EOCFG
open CCC, ">./etc/etc/001patch.cfg";
print CCC $configfile;
close CCC;
$configfile =<<EOCFG;
\$options = "supersmartpostscript";
\$protocolsdir = "./var/tmp";
\$seekfilesdir = "./var/tmp";
\@searches = (
{
tag => "ssh",
logfile => "./var/adm/messages",
criticalpatterns => "Failed password for invalid user723",
warningpatterns => "Unknown user",
warningexceptions => ['Unknown user hiasl'],
options => "perfdata,nologfilenocry"
},
);
\$postscript = sub {
printf "hundsglump varreckts!!! %s | %s\\n",
\$ENV{CHECK_LOGFILES_SERVICEOUTPUT},
\$ENV{CHECK_LOGFILES_SERVICEPERFDATA};
return \$ENV{CHECK_LOGFILES_SERVICESTATEID};
};
EOCFG
open CCC, ">./etc/etc/002patch.cfg";
print CCC $configfile;
close CCC;
sleep 10;
$cl->reset();
$ssh->loggercrap(undef, undef, 20);
$ssh->logger(undef, undef, 30, "Failed password for invalid user8");
$ssh->logger(undef, undef, 3, "Failed password for invalid user81");
$ssh->logger(undef, undef, 3, "Unknown user oash");
$ssh->logger(undef, undef, 3, "Unknown user hiasl");
$ssh->loggercrap(undef, undef, 20);
# ssh logs instead of test
$ssh->loggercrap(undef, undef, 20);
$ssh->logger(undef, undef, 200, "Failed password for invalid user9");
$ssh->loggercrap(undef, undef, 20);
$null->loggercrap(undef, undef, 20);
$null->logger(undef, undef, 4, "Failed password is nonsense");
$null->loggercrap(undef, undef, 20);
# 3 warnings wg unknown user oash
# criticals sind nicht betroffen user723 taucht nirgends auf.
# 4 wegen null: nonsense
#
$command = sprintf 'perl ../plugins-scripts/check_logfiles -f ./etc/searches.cfg -F ./etc/etc --searches=ssh,null,rulzn';
$ssh->trace("executing %s", $command);
$output = `$command`;
diag($output);
diag($? >> 8);
ok(($? >> 8) == 2);
ok($output =~ /CRITICAL - \(4 errors, 3 warnings .* Failed password is nonsense /);
exit;
sleep 1;
diag("now with only a file again");
$cl->reset();
$ssh->loggercrap(undef, undef, 20);
$ssh->logger(undef, undef, 30, "Failed password for invalid user8");
$ssh->logger(undef, undef, 3, "Failed password for invalid user81");
$ssh->logger(undef, undef, 3, "Unknown user oash");
$ssh->loggercrap(undef, undef, 20);
# ssh logs instead of test
$ssh->loggercrap(undef, undef, 20);
$ssh->logger(undef, undef, 200, "Failed password for invalid user9");
$ssh->loggercrap(undef, undef, 20);
$null->loggercrap(undef, undef, 20);
#$null->logger(undef, undef, 4, "Failed password is nonsense");
$null->loggercrap(undef, undef, 20);
# 30 crit wg user8
# 3 crit wg user 81
# 3 warn wgg unkn oash
# criticals sind nicht betroffen
#
$command = sprintf 'perl ../plugins-scripts/check_logfiles -f ./etc/searches.cfg --searches=ssh,null,rulzn';
$ssh->trace("executing %s", $command);
diag ("=====================================");
diag ("=====================================");
diag ("=====================================");
$output = `$command`;
diag($output);
diag($? >> 8);
ok(($? >> 8) == 2);
ok($output =~ /CRITICAL - \(30 errors .* Failed password is nonsense /);
|