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
|
#!/usr/bin/perl
package HaCi::HaCi;
use warnings;
use strict;
local $| = 1;
use FindBin;
use lib "$FindBin::Bin/../modules";
chdir "$FindBin::Bin/..";
my $workDir = qx/pwd/;
chomp($workDir);
$ENV{workdir} = $workDir;
use Getopt::Std;
use Data::Dumper qw/Dumper/;
use HaCi::Conf;
use HaCi::Utils qw/getConfig getPlugins getTable initTables finalizeTables initCache/;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
$main::VERSION = 0.1;
our $opts = {};
getopts('cd', $opts);
my $debug = (exists $opts->{d}) ? $opts->{d} : 0;
my @busy = ('|', '/', '-', '\\', '-');
my ($bc, $ec, $sc) = (0, 0, 0);
my ($userTable, $groupTable, $rootTable, $rootACTable, $networkTable, $networkV6Table, $networkACTable, $networkPluginTable,
$templateTable, $templateEntryTable, $templateValueTable, $pluginTable, $pluginConfTable, $pluginValueTable, $settingTable) = ();
our $conf; *conf = \$HaCi::Conf::conf;
&HaCi::Conf::init($workDir);
&getConfig();
&init();
&main();
exit 0;
#--------------------------------------------
END {
&finalizeTables();
}
sub init {
&initTables();
$userTable = $conf->{var}->{TABLES}->{user};
unless (defined $userTable) {
die "Cannot get users. DB Error (user)\n";
}
$groupTable = $conf->{var}->{TABLES}->{group};
unless (defined $groupTable) {
die "Cannot get groups. DB Error (group)\n";
}
$rootTable = $conf->{var}->{TABLES}->{root};
unless (defined $rootTable) {
die "Cannot get roots. DB Error (root)\n";
}
$rootACTable = $conf->{var}->{TABLES}->{rootAC};
unless (defined $rootACTable) {
die "Cannot get rootACs. DB Error (rootAC)\n";
}
$networkTable = $conf->{var}->{TABLES}->{network};
unless (defined $networkTable) {
die "Cannot get Networks. DB Error (network)\n";
}
$networkV6Table = $conf->{var}->{TABLES}->{networkV6};
unless (defined $networkV6Table) {
die "Cannot get Networks. DB Error (networkV6)\n";
}
$networkACTable = $conf->{var}->{TABLES}->{networkAC};
unless (defined $networkACTable) {
die "Cannot get networkACs. DB Error (networkAC)\n";
}
$networkPluginTable = $conf->{var}->{TABLES}->{networkPlugin};
unless (defined $networkPluginTable) {
die "Cannot get networkPlugins. DB Error (networkPlugin)\n";
}
$templateTable = $conf->{var}->{TABLES}->{template};
unless (defined $templateTable) {
die "Cannot get templates. DB Error (template)\n";
}
$templateEntryTable = $conf->{var}->{TABLES}->{templateEntry};
unless (defined $templateEntryTable) {
die "Cannot get templateEntrys. DB Error (templateEntry)\n";
}
$templateValueTable = $conf->{var}->{TABLES}->{templateValue};
unless (defined $templateValueTable) {
die "Cannot get templateValues. DB Error (templateValue)\n";
}
$pluginTable = $conf->{var}->{TABLES}->{plugin};
unless (defined $pluginTable) {
die "Cannot get plugins. DB Error (plugin)\n";
}
$pluginConfTable = $conf->{var}->{TABLES}->{pluginConf};
unless (defined $pluginConfTable) {
die "Cannot get pluginConfs. DB Error (pluginConf)\n";
}
$pluginValueTable = $conf->{var}->{TABLES}->{pluginValue};
unless (defined $pluginValueTable) {
die "Cannot get pluginValues. DB Error (pluginValue)\n";
}
$settingTable = $conf->{var}->{TABLES}->{setting};
unless (defined $settingTable) {
die "Cannot get settings. DB Error (setting)\n";
}
}
sub main {
if ($opts->{c}) {
print "\nPlease make sure to make a database backup before cleaning it up.\n";
if (system("which mysqldump >/dev/null") == 0) {
my $md = qx/which mysqldump/;
chomp($md);
my $dbConf = $conf->{user}->{db};
my $pwd = qx/pwd/;
chomp($pwd);
my $dbnameO = "$pwd/$dbConf->{dbname}.sql";
my $dbname = $dbnameO;
my $cnter = 1;
while ( -f $dbname ) {
$dbname = $dbnameO . '.' . $cnter++;
}
my $backupT = $md . " -u'$dbConf->{dbuser}' -p'XXX' -h'$dbConf->{dbhost}' '$dbConf->{dbname}' > $dbname";
print "Found 'mysqldump':\n\t$backupT\nShould I run this dump? [y|N]: ";
my $bb = <STDIN>;
chomp($bb);
if (lc($bb) eq 'y') {
my $backupStr = $md . " -u'$dbConf->{dbuser}' -p'$dbConf->{dbpass}' -h'$dbConf->{dbhost}' '$dbConf->{dbname}' > $dbname";
print qx($backupStr);
if ($?) {
print "... Dump was NOT successful!!!\n";
} else {
print "... Dump was successful\n";
}
}
}
print "Continue cleaning up database? [y|N]: ";
my $con = <STDIN>;
print "\n";
chomp($con);
exit 0 unless lc($con) eq 'y';
}
&checkRootStuff();
&checkBrokenNets();
&checkNetIDStuff();
&checkpluginIDStuff();
&checkTmplIDStuff();
print "\n$bc Items checked!\n";
print '' . ($sc + $ec) . " bad entries found. Deleted [" . $sc . " successful | " . $ec . " unsuccessful]\n" if $opts->{c};
if ($opts->{c} && $sc > 0) {
my $error = 0;
my ($aclCacheHandle, $netCacheHandle) = &initCache();
if (defined $netCacheHandle) {
warn "Cannot set Cache (netCache-DB!)\n" unless $netCacheHandle->set('DB', {});
warn "Cannot set Cache (netCache-FILL!)\n" unless $netCacheHandle->set('FILL', {});
warn "Cannot set Cache (netCache-NET!)\n" unless $netCacheHandle->set('NET', {});
} else {
$error = 1;
}
if (defined $aclCacheHandle) {
warn "Cannot set Cache (aclCache!)\n" unless $aclCacheHandle->set('HASH', {});
} else {
$error = 1;
}
warn "Cannot flush the cache. Please press the 'Flush Cache' button in the web gui.\n\n" if $error;
} else {
print "\nCall this script with '-c' in order to cleanup these issues: $0 -c\n\n";
}
}
sub checkRootStuff {
my $oEC = $ec;
my $oSC = $sc;
print "\nSearch for entries with bad rootID...\n";
# Get Roots
my @rootst = $rootTable->search(['ID'], 0, 0, 0, 1);
if ($#rootst == -1) {
print "\r No roots available\n";
return;
}
my $roots = '';
foreach (@rootst) {
$roots .= ',' if $roots;
$roots .= $_->{ID}
}
# Get stale V4
my @networksNR = $networkTable->search(['ID', 'network', 'description', 'rootID', 'ipv6ID'], {}, 0, "WHERE rootID NOT IN ($roots)");
foreach (@networksNR) {
my $ip = $_;
print "\r" . $busy[$bc++%5];
print "\r $ip->{network}:$ip->{rootID} ($ip->{ipv6ID}) => $ip->{description}: Stale V4 network entry (root doesn't exists)\n" if $debug;
&delEntry($networkTable, $ip->{ID});
}
print "\r " . ($#networksNR + 1) . " stale V4 network entrys found without matching root\n";
# Get stale V6
my @networkV6sNR = $networkV6Table->search(['ID', 'rootID'], {}, 0, "WHERE rootID NOT IN ($roots)");
foreach (@networkV6sNR) {
my $ip = $_;
print "\r" . $busy[$bc++%5];
print "\r $ip->{ID} : $ip->{rootID}: Stale V6 network entry (root doesn't exists)\n" if $debug;
&delEntry($networkV6Table, {ID => $ip->{ID}, rootID => $ip->{rootID}});
}
print "\r " . ($#networkV6sNR + 1) . " stale V6 network entrys found without matching root\n";
# Get stale networkAC
my @networkACsNR = $networkACTable->search(['ID', 'rootID'], {}, 0, "WHERE rootID NOT IN ($roots)");
foreach (@networkACsNR) {
my $ac = $_;
print "\r" . $busy[$bc++%5];
print "\r $ac->{ID} : $ac->{rootID}: Stale network ACL entry (root doesn't exists)\n" if $debug;
&delEntry($networkACTable, $ac->{ID});
}
print "\r " . ($#networkACsNR + 1) . " stale network ACL entrys found without matching root\n";
# Get stale rootAC
my @rootACsNR = $rootACTable->search(['ID', 'rootID'], {}, 0, "WHERE rootID NOT IN ($roots)");
foreach (@rootACsNR) {
my $ac = $_;
print "\r" . $busy[$bc++%5];
print "\r $ac->{ID} : $ac->{rootID}: Stale root ACL entry (root doesn't exists)\n" if $debug;
&delEntry($rootACTable, $ac->{ID});
}
print "\r " . ($#rootACsNR + 1) . " stale root ACL entrys found without matching root\n";
print "\n" if $debug;
print '' . (($sc - $oSC) + ($ec - $oEC)) . " bad entries found. Deleted [" . ($sc - $oSC) . " successful | " . ($ec - $oEC) . " unsuccessful]\n" if $opts->{c};
}
sub checkBrokenNets {
my $oEC = $ec;
my $oSC = $sc;
print "\nSearch for corrupt V6 networks...\n";
# get corrupt V6 networks
my $sNError = 0;
my $brokenNError = 0;
my @networks = $networkTable->search(['ID', 'network', 'description', 'rootID', 'ipv6ID'], {network => 0});
foreach (@networks) {
my $ip = $_;
my $v6ID = $_->{ipv6ID};
print "\r" . $busy[$bc++%5];
unless ($v6ID) {
print "\r $ip->{network}:$ip->{rootID} ($ip->{ipv6ID}) => $ip->{description}: NO network AND no v6ID!\n" if $debug;
$brokenNError++;
&delEntry($networkTable, $ip->{ID});
next;
}
my $ip6 = ($networkV6Table->search(['ID'], {ID => $ip->{ipv6ID}, rootID => $ip->{rootID}}))[0];
if (!defined $ip6) {
print "\r $ip->{network}:$ip->{rootID} ($ip->{ipv6ID}) => $ip->{description}: Stale network entry (no V6 part)\n" if $debug;
&delEntry($networkTable, $ip->{ID});
$sNError++;
}
}
print "\r $sNError stale network entrys found without matching V6 Part\n";
print " $brokenNError broken network entrys found\n";
my $sN6Error = 0;
my @v6Networks = $networkV6Table->search(['ID', 'rootID']);
foreach (@v6Networks) {
my $ip = $_;
my $v6ID = $_->{ID};
print "\r" . $busy[$bc++%5];
my $ip4 = ($networkTable->search(['ID'], {ipv6ID => $v6ID, rootID => $ip->{rootID}}))[0];
if (!defined $ip4) {
print "\r $v6ID : $ip->{rootID}: Stale V6 network entry (no network part)\n" if $debug;
&delEntry($networkV6Table, {ID => $ip->{ID}, rootID => $ip->{rootID}});
$sN6Error++;
}
}
print "\r $sN6Error stale V6 network entrys found without matching network Part\n";
print "\n" if $debug;
print '' . (($sc - $oSC) + ($ec - $oEC)) . " bad entries found. Deleted [" . ($sc - $oSC) . " successful | " . ($ec - $oEC) . " unsuccessful]\n" if $opts->{c};
}
sub checkNetIDStuff {
my $oEC = $ec;
my $oSC = $sc;
print "\nSearch for entries with bad network ID...\n";
# Get Networks
my @netst = $networkTable->search(['ID'], 0, 0, 0, 1);
if ($#netst == -1) {
print "\r No networks available\n";
return;
}
my $nets = {};
foreach (@netst) {
$nets->{$_->{ID}} = 1;
}
# Get stale networkAC
my @networkACs = $networkACTable->search(['ID', 'netID', 'ACL']);
my $cnter = 0;
foreach (@networkACs) {
my $ac = $_;
next if !$ac->{netID} && $ac->{ACL} == 4;
print "\r" . $busy[$bc++%5];
if (!exists $nets->{$ac->{netID}}) {
$cnter++;
&delEntry($networkACTable, $ac->{ID});
print "\r ACL $ac->{ID} : $ac->{netID}: Stale network ACL entry (network doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale network ACL entrys found without matching network\n";
print "\n" if $debug;
# Get stale network plugins
my @networkPlugins = $networkPluginTable->search(['ID', 'netID']);
$cnter = 0;
foreach (@networkPlugins) {
my $plugin = $_;
print "\r" . $busy[$bc++%5];
if ($plugin->{netID} != -1 && !exists $nets->{$plugin->{netID}}) {
$cnter++;
&delEntry($networkPluginTable, $plugin->{ID});
print "\r ID $plugin->{ID} - netID $plugin->{netID}: Stale network plugin entry (network doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale network Plugin entrys found without matching network\n";
print "\n" if $debug;
# Get stale plugin confs
my @pluginConfs = $pluginConfTable->search(['ID', 'netID']);
$cnter = 0;
foreach (@pluginConfs) {
my $plugin = $_;
print "\r" . $busy[$bc++%5];
if ($plugin->{netID} != -1 && !exists $nets->{$plugin->{netID}}) {
$cnter++;
&delEntry($pluginConfTable, $plugin->{ID});
print "\r ID $plugin->{ID} - netID $plugin->{netID}: Stale plugin config entry (network doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale plugin config entrys found without matching network\n";
print "\n" if $debug;
# Get stale plugin values
my @pluginValues = $pluginValueTable->search(['ID', 'netID']);
$cnter = 0;
foreach (@pluginValues) {
my $plugin = $_;
print "\r" . $busy[$bc++%5];
if ($plugin->{netID} != -1 && !exists $nets->{$plugin->{netID}}) {
$cnter++;
&delEntry($pluginValueTable, $plugin->{ID});
print "\r ID $plugin->{ID} - netID $plugin->{netID}: Stale plugin value entry (network doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale plugin value entrys found without matching network\n";
print "\n" if $debug;
# Get stale template values
my @templateValues = $templateValueTable->search(['ID', 'netID']);
$cnter = 0;
foreach (@templateValues) {
my $template = $_;
print "\r" . $busy[$bc++%5];
if ($template->{netID} != -1 && !exists $nets->{$template->{netID}}) {
$cnter++;
&delEntry($templateValueTable, $template->{ID});
print "\r ID $template->{ID} - netID $template->{netID}: Stale template value entry (network doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale template value entrys found without matching network\n";
print "\n" if $debug;
print '' . (($sc - $oSC) + ($ec - $oEC)) . " bad entries found. Deleted [" . ($sc - $oSC) . " successful | " . ($ec - $oEC) . " unsuccessful]\n" if $opts->{c};
}
sub checkpluginIDStuff {
my $oEC = $ec;
my $oSC = $sc;
print "\nSearch for entries with bad plugin ID...\n";
# Get Plugins
my @pluginst = $pluginTable->search(['ID'], 0, 0, 0, 1);
if ($#pluginst == -1) {
print "\r No plugins available\n";
return;
}
my $plugins = {};
foreach (@pluginst) {
$plugins->{$_->{ID}} = 1;
}
# Get stale network plugins
my @networkPlugins = $networkPluginTable->search(['ID', 'pluginID']);
my $cnter = 0;
foreach (@networkPlugins) {
my $plugin = $_;
print "\r" . $busy[$bc++%5];
if (!exists $plugins->{$plugin->{pluginID}}) {
$cnter++;
&delEntry($networkPluginTable, $plugin->{ID});
print "\r ID $plugin->{ID} - netID $plugin->{pluginID}: Stale network plugin entry (plugin doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale network Plugin entrys found without matching plugin\n";
print "\n" if $debug;
# Get stale plugin confs
my @pluginConfs = $pluginConfTable->search(['ID', 'pluginID']);
$cnter = 0;
foreach (@pluginConfs) {
my $plugin = $_;
print "\r" . $busy[$bc++%5];
if (!exists $plugins->{$plugin->{pluginID}}) {
$cnter++;
&delEntry($pluginConfTable, $plugin->{ID});
print "\r ID $plugin->{ID} - pluginID $plugin->{pluginID}: Stale plugin config entry (plugin doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale plugin config entrys found without matching plugin\n";
print "\n" if $debug;
# Get stale plugin values
my @pluginValues = $pluginValueTable->search(['ID', 'pluginID']);
$cnter = 0;
foreach (@pluginValues) {
my $plugin = $_;
print "\r" . $busy[$bc++%5];
if (!exists $plugins->{$plugin->{pluginID}}) {
$cnter++;
&delEntry($pluginValueTable, $plugin->{ID});
print "\r ID $plugin->{ID} - pluginID $plugin->{pluginID}: Stale plugin value entry (plugin doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale plugin value entrys found without matching plugin\n";
print "\n" if $debug;
print '' . (($sc - $oSC) + ($ec - $oEC)) . " bad entries found. Deleted [" . ($sc - $oSC) . " successful | " . ($ec - $oEC) . " unsuccessful]\n" if $opts->{c};
}
sub checkTmplIDStuff {
my $oEC = $ec;
my $oSC = $sc;
print "\nSearch for entries with bad template ID...\n";
# Get tmpls
my @tmplst = $templateTable->search(['ID'], 0, 0, 0, 1);
if ($#tmplst == -1) {
print "\r No templates available\n";
return;
}
my $tmpls = {
0 => 1
};
foreach (@tmplst) {
$tmpls->{$_->{ID}} = 1;
}
# Get stale networks
my @networks = $networkTable->search(['ID', 'tmplID']);
my $cnter = 0;
foreach (@networks) {
my $net = $_;
print "\r" . $busy[$bc++%5];
if (!exists $tmpls->{$net->{tmplID}}) {
$cnter++;
if ($opts->{c}) {
$networkTable->tmplID(0);
unless ($networkTable->update({ID => $net->{ID}})) {
warn $networkTable->errorStrs();
$ec++;
} else {
$sc++;
}
}
print "\r ID $net->{ID} - tmplID $net->{tmplID}: Stale network entry (template doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale network entrys found without matching template\n";
print "\n" if $debug;
# Get stale template Entrys
my @tmplEntrys = $templateEntryTable->search(['ID', 'tmplID']);
$cnter = 0;
foreach (@tmplEntrys) {
my $tmplEntry = $_;
print "\r" . $busy[$bc++%5];
if (!exists $tmpls->{$tmplEntry->{tmplID}}) {
$cnter++;
&delEntry($templateEntryTable, $tmplEntry->{ID});
print "\r ID $tmplEntry->{ID} - tmplID $tmplEntry->{tmplID}: Stale templateEntry entry (template doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale templateEntry entrys found without matching template\n";
print "\n" if $debug;
# Get stale template Values
my @tmplValues = $templateValueTable->search(['ID', 'tmplID']);
$cnter = 0;
foreach (@tmplValues) {
my $tmplValue = $_;
print "\r" . $busy[$bc++%5];
if (!exists $tmpls->{$tmplValue->{tmplID}}) {
$cnter++;
&delEntry($templateValueTable, $tmplValue->{ID});
print "\r ID $tmplValue->{ID} - tmplID $tmplValue->{tmplID}: Stale templateValue entry (template doesn't exists)\n" if $debug;
}
}
print "\r $cnter stale templateValue entrys found without matching template\n";
print "\n" if $debug;
print '' . (($sc - $oSC) + ($ec - $oEC)) . " bad entries found. Deleted/Modified [" . ($sc - $oSC) . " successful | " . ($ec - $oEC) . " unsuccessful]\n" if $opts->{c};
}
sub main::HELP_MESSAGE {
print "
NAME
cleanupDatabase.pl - clean up HaCi database
SYNOPSIS
$0 [-c] [-d]
OPTIONS
-c Clean up issues. Without this option issues will only be reported.
-d Debug mode. Show every issue.
Please make sure to make a database backup before cleaning it up.
";
}
sub delEntry {
my $table = shift;
my $ID = shift;
my $pk = (ref($ID) eq 'HASH') ? $ID : {ID => $ID};
if ($opts->{c}) {
unless($table->delete($pk)) {
warn $table->errorStrs();
$ec++;
} else {
$sc++;
}
}
}
# vim:ts=2:sts=2:sw=2
|