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 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
|
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../site/include/cli_check.php');
require_once(__DIR__ . '/../site/lib/template.php');
require_once(__DIR__ . '/../site/lib/utility.php');
include_once(__DIR__ . '/../site/lib/data_query.php');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
global $total_errors, $total_repairs, $repaired_hosts;
global $local, $debug, $force, $rtables, $form, $dynamic, $base_tables;
$debug = false;
$form = '';
$force = false;
$rtables = false;
$dynamic = false;
$local = false;
$total_errors = 0;
$total_repairs = 0;
$repaired_hosts = array();
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
case '--debug':
$debug = true;
break;
case '--tables':
$rtables = true;
break;
case '--force':
$force = true;
break;
case '--dynamic':
$dynamic = true;
break;
case '--local':
$local = true;
break;
case '-form':
case '--form':
$form = ' USE_FRM';
break;
case '--version':
case '-V':
case '-v':
display_version();
exit(0);
case '--help':
case '-H':
case '-h':
display_help();
exit(0);
default:
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
}
$base_tables = get_cacti_base_tables();
table_structural_repair();
simple_checks();
detailed_checks();
snmp_repairs();
snmp_index_repairs();
if (cacti_sizeof($repaired_hosts) && $total_repairs > 0) {
print_separator(true);
printf('NOTE: Pushing out %s Devices after repairs!' . PHP_EOL . PHP_EOL, cacti_sizeof($repaired_hosts));
foreach($repaired_hosts as $host_id) {
$h = db_fetch_row_prepared('SELECT description, hostname
FROM host
WHERE id = ?',
array($host_id));
printf('NOTE: Pushing out Device %s (%s) after repair!' . PHP_EOL, $h['description'], $h['hostname']);
push_out_host($host_id);
}
}
print_separator(true);
if ($total_errors == 0 && $total_repairs == 0) {
printf('NOTE: Found 0 Cacti database issues to repair.' . PHP_EOL . PHP_EOL);
} elseif (($total_errors > 0 || $total_repairs > 0) && !$force) {
printf('WARNING: Found %s problems in your Cacti database and automatically repaired %s of them.' . PHP_EOL, $total_errors, $total_repairs);
printf('WARNING: Using the \'--force\' option will either repair, remove or ignore any additional issues' . PHP_EOL);
printf('WARNING: if they can not be repaired.' . PHP_EOL . PHP_EOL);
printf('WARNING: Because these changes can not be reversed, make sure you make a Cacti backup first.' . PHP_EOL . PHP_EOL);
} else {
printf('WARNING: Found %s and repaired %s Cacti database issues.' . PHP_EOL . PHP_EOL, $total_errors, $total_repairs);
}
function table_structural_repair() {
global $config, $local, $total_errors, $total_repairs;
global $debug, $force, $rtables, $form, $dynamic, $base_tables, $database_default;
print_separator();
if ($rtables) {
if (!$local && $config['poller_id'] > 1) {
db_switch_remote_to_main();
printf("NOTE: Repairing tables for main database" . PHP_EOL);
} else {
printf("NOTE: Repairing tables for local database" . PHP_EOL);
}
printf('NOTE: Repairing all %s Cacti base database tables' . PHP_EOL, cacti_sizeof($base_tables));
db_execute('UNLOCK TABLES');
if (cacti_sizeof($base_tables)) {
foreach($base_tables AS $table) {
printf("Repairing table '%s'", $table);
$status = db_execute("REPAIR TABLE $table QUICK" . $form);
printf(($status == 0 ? ' Failed' : ' Successful') . PHP_EOL);
if ($dynamic && stripos($table['CREATE_OPTIONS'], 'dynamic') === false && $table['ENGINE'] != 'MEMORY') {
printf("Changing table row format to Dynamic '%s'", $table);
$status = db_execute("ALTER TABLE $table ROW_FORMAT=DYNAMIC");
print ($status == 0 ? ' Failed' : ' Successful') . PHP_EOL;
}
}
}
} else {
printf('NOTE: Skipping table physical repair' . PHP_EOL);
printf('NOTE: %s Cacti base tables would be checked/repaired if using --tables option.' . PHP_EOL, cacti_sizeof($base_tables));
}
}
function simple_checks() {
global $total_errors, $total_repairs;
print_separator(true);
printf('Simple Checks. Automatically repair if Found' . PHP_EOL . PHP_EOL);
printf('NOTE: Repairing potential issues with Data Query ids and indexes.' . PHP_EOL);
db_execute('UPDATE graph_local AS gl
INNER JOIN graph_templates_item AS gti
ON gti.local_graph_id = gl.id
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id = dtr.id
INNER JOIN data_local AS dl
ON dl.id = dtr.local_data_id
SET gl.snmp_query_id = dl.snmp_query_id, gl.snmp_index = dl.snmp_index
WHERE gl.graph_template_id IN (SELECT graph_template_id FROM snmp_query_graph)
AND (gl.snmp_query_id != dl.snmp_query_id OR gl.snmp_index != dl.snmp_index)
AND gl.snmp_query_id = 0');
$fixes = db_affected_rows();
$total_repairs += $fixes;
$total_errors += $fixes;
if ($fixes) {
printf('NOTE: Found and repaired %s problems with the simple Data Query id and index check.' . PHP_EOL, $fixes);
} else {
printf('NOTE: Found 0 problems with the simple Data Query id and index check.' . PHP_EOL);
}
printf('NOTE: Repairing incorrectly set Data Query Graph ids - This can take a while.' . PHP_EOL);
db_execute("UPDATE graph_local AS gl
INNER JOIN (
SELECT DISTINCT local_graph_id, task_item_id
FROM graph_templates_item
WHERE local_graph_id > 0
AND task_item_id > 0
AND graph_template_id IN (SELECT DISTINCT graph_template_id FROM snmp_query_graph)
) AS gti
ON gl.id = gti.local_graph_id
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id = dtr.id
INNER JOIN data_template_data AS dtd
ON dtr.local_data_id = dtd.local_data_id
INNER JOIN data_input_data AS did
ON did.data_template_data_id = dtd.id
INNER JOIN snmp_query_graph_rrd AS sqgr
ON sqgr.snmp_query_graph_id = did.value
SET gl.snmp_query_graph_id = did.value
WHERE did.data_input_field_id IN(
SELECT id
FROM data_input_fields
WHERE input_output = 'in'
AND type_code = 'output_type'
)
AND gl.graph_template_id IN (SELECT DISTINCT graph_template_id FROM snmp_query_graph)
AND gl.snmp_query_graph_id != CAST(did.value AS signed)");
$fixes = db_affected_rows();
$total_repairs += $fixes;
$total_errors += $fixes;
if ($fixes) {
printf('NOTE: Found and repaired %s problems with Data Query Graph ids.' . PHP_EOL, $fixes);
} else {
printf('NOTE: Found 0 problems with Data Query Graph ids.' . PHP_EOL);
}
printf('NOTE: Repairing Data Input Data hostname or host_id Type Code issues.' . PHP_EOL);
// Correct bad hostnames and host_id's in the data_input_data table
$entries = db_fetch_assoc("SELECT did.*, dif.type_code
FROM data_input_data AS did
INNER JOIN data_input_fields AS dif
ON did.data_input_field_id = dif.id
WHERE data_input_field_id in (
SELECT id
FROM data_input_fields
WHERE type_code != ''
)
AND data_template_data_id IN (
SELECT id
FROM data_template_data
WHERE local_data_id > 0
AND data_template_id > 0
)
AND type_code in ('host_id', 'hostname')
AND value = ''");
$fixes = 0;
if (cacti_sizeof($entries)) {
foreach($entries as $e) {
$data_template_data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE id = ?',
array($e['data_template_data_id']));
if (cacti_sizeof($data_template_data)) {
$local_data = db_fetch_row_prepared('SELECT *
FROM data_local
WHERE id = ?',
array($data_template_data['local_data_id']));
if (cacti_sizeof($local_data)) {
switch($e['type_code']) {
case 'hostname':
$hostname = db_fetch_cell_prepared('SELECT hostname
FROM host
WHERE id = ?',
array($local_data['host_id']));
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($hostname, $e['data_input_field_id'], $e['data_template_data_id']));
$fixes++;
break;
case 'host_id':
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($local_data['host_id'], $e['data_input_field_id'], $e['data_template_data_id']));
$fixes++;
break;
}
}
}
}
}
$total_repairs += $fixes;
$total_errors += $fixes;
if ($fixes) {
printf('NOTE: Found and repaired %s invalid Data Input hostname or host_id Type Code issues.' . PHP_EOL, $fixes);
} else {
printf('NOTE: Found 0 Data Input Data hostname or host_id Type Code issues.' . PHP_EOL);
}
printf('NOTE: Removing incomplete Data Sources.' . PHP_EOL);
db_execute('DELETE dl
FROM data_local AS dl
LEFT JOIN data_template_data AS dtd
ON dl.id = dtd.local_data_id
WHERE dtd.local_data_id IS NULL');
$fixes = db_affected_rows();
$total_repairs += $fixes;
$total_errors += $fixes;
if ($fixes) {
printf('NOTE: Found and removed %s incomplete Data Sources.' . PHP_EOL, $fixes);
} else {
printf('NOTE: Found 0 incomplete Data Sources.' . PHP_EOL);
}
printf('NOTE: Repairing orphaned Poller Items.' . PHP_EOL);
db_execute('DELETE pi
FROM poller_item AS pi
LEFT JOIN data_local AS dl
ON pi.local_data_id = dl.id
WHERE dl.id IS NULL');
$fixes = db_affected_rows();
$total_repairs += $fixes;
$total_errors += $fixes;
if ($fixes) {
printf('NOTE: Found and removed %s orphaned Poller Items.' . PHP_EOL, $fixes);
} else {
printf('NOTE: Found 0 problems with orphaned Poller Items.' . PHP_EOL);
}
}
function detailed_checks() {
global $force, $total_errors, $total_repairs;
print_separator(true);
if (!$force) {
printf('Detailed Basic Checks. Use --force to repair if found.' . PHP_EOL . PHP_EOL);
} else {
printf('Detailed Basic Checks and Repairs.' . PHP_EOL . PHP_EOL);
}
printf('NOTE: Searching for invalid Cacti GPRINT Presets.' . PHP_EOL);
/* remove invalid GPrint Presets from the Database, validated */
$rows = db_fetch_cell('SELECT COUNT(*)
FROM graph_templates_item
LEFT JOIN graph_templates_gprint
ON graph_templates_item.gprint_id = graph_templates_gprint.id
WHERE graph_templates_gprint.id IS NULL
AND graph_templates_item.gprint_id > 0');
$total_errors += $rows;
if ($rows > 0) {
if ($force) {
db_execute('DELETE FROM graph_templates_item
WHERE gprint_id NOT IN (SELECT id FROM graph_templates_gprint)
AND gprint_id>0');
$fixes = db_affected_rows();
$total_repairs += $fixes;
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . "$rows invalid GPRINT Preset rows in Graph Templates." . PHP_EOL);
} else {
printf('NOTE: Found 0 invalid Cacti GPRINT Presets.' . PHP_EOL);
}
printf('NOTE: Searching for invalid Cacti CDEFs.' . PHP_EOL);
/* remove invalid CDEF Items from the Database, validated */
$rows = db_fetch_cell('SELECT COUNT(*)
FROM cdef_items
LEFT JOIN cdef
ON cdef_items.cdef_id=cdef.id
WHERE cdef.id IS NULL');
$total_errors += $rows;
if ($rows > 0) {
if ($force) {
db_execute('DELETE FROM cdef_items
WHERE cdef_id NOT IN (SELECT id FROM cdef)');
$fixes = db_affected_rows();
$total_repairs += $fixes;
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . "$fixes of $rows invalid CDEFs in Graph Templates." . PHP_EOL);
} else {
printf('NOTE: Found 0 invalid Cacti CDEFs.' . PHP_EOL);
}
printf('NOTE: Searching for invalid Cacti Data Inputs.' . PHP_EOL);
/* remove invalid Data Templates from the Database, validated */
$rows = db_fetch_cell('SELECT COUNT(*)
FROM data_template_data
LEFT JOIN data_input
ON data_template_data.data_input_id=data_input.id
WHERE data_input.id IS NULL');
$total_errors += $rows;
if ($rows > 0) {
if ($force) {
db_execute('DELETE FROM data_template_data
WHERE data_input_id NOT IN (SELECT id FROM data_input)');
$fixes = db_affected_rows();
$total_repairs += $fixes;
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . "$fixes of $rows invalid Data Inputs in Data Templates." . PHP_EOL);
} else {
printf('NOTE: Found 0 invalid Cacti Data Inputs.' . PHP_EOL);
}
printf('NOTE: Searching for Graph Templates whose Graphs have invalid item counts.' . PHP_EOL);
$rows = db_fetch_assoc('SELECT gt.graph_template_id, gt.items, gt1.graph_items, gt1.graphs
FROM (
SELECT graph_template_id, COUNT(*) AS items
FROM graph_templates_item
WHERE local_graph_id=0
GROUP BY graph_template_id
) AS gt
INNER JOIN (
SELECT graph_template_id, MAX(items) AS graph_items, COUNT(*) AS graphs
FROM (
SELECT graph_template_id, COUNT(*) AS items
FROM graph_templates_item
WHERE local_graph_id > 0
AND graph_template_id > 0
GROUP BY local_graph_id
) AS rs
GROUP BY graph_template_id
) AS gt1
ON gt.graph_template_id = gt1.graph_template_id
HAVING graph_items != items');
$total_errors += cacti_sizeof($rows);
if (cacti_sizeof($rows)) {
$total_errors += cacti_sizeof($rows);
$total_graphs = 0;
if ($force) {
foreach($rows as $row) {
$name = db_fetch_cell_prepared('SELECT name
FROM graph_templates
WHERE id = ?',
array($row['graph_template_id']));
printf('NOTE: Re-Templating Graphs for Template: %s (%s)' . PHP_EOL, $name, $row['graph_template_id']);
retemplate_graphs($row['graph_template_id'], 0, true);
$total_graphs += $row['graphs'];
}
} else {
foreach($rows as $row) {
$total_graphs += $row['graphs'];
}
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . '%s Graphs from %s Graph Templates that had invalid item counts.' . PHP_EOL, $total_graphs, cacti_sizeof($rows));
} else {
printf('NOTE: Found 0 Graph Templates whose Graphs had incorrect item counts.' . PHP_EOL);
}
printf('NOTE: Searching for invalid Cacti Data Input fields.' . PHP_EOL);
/* remove invalid Data Input fields from the Database, validated */
$rows = db_fetch_cell('SELECT COUNT(*)
FROM data_input_fields
LEFT JOIN data_input
ON data_input_fields.data_input_id=data_input.id
WHERE data_input.id IS NULL');
$total_errors += $rows;
if ($rows > 0) {
if ($force) {
$total_repairs += $rows;
db_execute('DELETE FROM data_input_fields
WHERE data_input_fields.data_input_id NOT IN (SELECT id FROM data_input)');
update_replication_crc(0, 'poller_replicate_data_input_fields_crc');
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . "$rows invalid Data Input fields in Data Templates." . PHP_EOL);
} else {
printf('NOTE: Found 0 invalid Cacti Data Input fields.' . PHP_EOL);
}
printf('NOTE: Searching for invalid Cacti Data Input Data rows (Pass 1).' . PHP_EOL);
/* remove invalid Data Input Data Rows from the Database in two passes */
$rows = db_fetch_cell('SELECT COUNT(*)
FROM data_input_data
LEFT JOIN data_template_data
ON data_input_data.data_template_data_id=data_template_data.id
WHERE data_template_data.id IS NULL');
$total_errors += $rows;
if ($rows > 0) {
if ($force) {
$total_repairs += $rows;
db_execute('DELETE FROM data_input_data
WHERE data_input_data.data_template_data_id NOT IN (SELECT id FROM data_template_data)');
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . "$rows invalid Data Input Data rows in Data Templates" . PHP_EOL);
} else {
printf('NOTE: Found 0 invalid Cacti Data Input Data rows (Pass 1).' . PHP_EOL);
}
printf('NOTE: Searching for invalid Cacti Data Input Data rows (Pass 2).' . PHP_EOL);
$rows = db_fetch_cell('SELECT COUNT(*)
FROM data_input_data
LEFT JOIN data_input_fields
ON data_input_fields.id=data_input_data.data_input_field_id
WHERE data_input_fields.id IS NULL');
$total_errors += $rows;
if ($rows > 0) {
if ($force) {
$total_repairs += $rows;
db_execute('DELETE FROM data_input_data
WHERE data_input_data.data_input_field_id NOT IN (SELECT id FROM data_input_fields)');
}
printf('NOTE: Found ' . ($force ? 'and repaired ':'') . "$rows invalid Data Input Data rows based upon field mappings in Data Templates." . PHP_EOL);
} else {
printf('NOTE: Found 0 invalid Cacti Data Input Data rows (Pass 2).' . PHP_EOL);
}
}
/**
* There have been reports of data_input_data not including the correct information for
* snmp columns. This is likely caused by a legacy bug in Cacti where snmp information
* was not properly copied to the data_input_data table upon change.
*
* Therefore, let's detect that bogus information for the snmp Data Input types only
* for now.
*/
function snmp_repairs() {
global $force, $total_errors, $total_repairs, $repaired_hosts;
print_separator(true);
if (!$force) {
printf('Detailed SNMP Checks. Use --force to repair if found.' . PHP_EOL . PHP_EOL);
} else {
printf('Detailed SNMP Checks and Repairs.' . PHP_EOL . PHP_EOL);
}
printf('NOTE: Searching for Devices with invalid SNMP information propagated to poller cache.' . PHP_EOL);
$snmp_hashes = array(
'3eb92bb845b9660a7445cf9740726522',
'bf566c869ac6443b0c75d1c32b5a350e'
);
$data_names = array(
'management_ip' => 'hostname',
'ip' => 'hostname',
'hostname' => 'hostname',
'snmp_version' => 'snmp_version',
'snmp_community' => 'snmp_community',
'snmp_username' => 'snmp_username',
'snmp_password' => 'snmp_password',
'snmp_port' => 'snmp_port',
'snmp_auth_protocol' => 'snmp_auth_protocol',
'snmp_auth_passphrase' => 'snmp_auth_passphrase',
'snmp_priv_protocol' => 'snmp_priv_protocol',
'snmp_priv_passphrase' => 'snmp_priv_passphrase'
);
$hosts = db_fetch_assoc('SELECT * FROM host ORDER BY id');
$errors = array();
$snmp_errors = 0;
if (cacti_sizeof($hosts)) {
foreach($hosts as $h) {
$errors[$h['id']] = 0;
foreach($snmp_hashes as $data_input) {
$fields = db_fetch_assoc_prepared('SELECT did.*, dif.name, dif.data_name
FROM data_input_data AS did
INNER JOIN data_input_fields AS dif
ON did.data_input_field_id = dif.id
INNER JOIN data_template_data AS dtd
ON dtd.id = did.data_template_data_id
INNER JOIN data_input AS di
ON dtd.data_input_id = di.id
INNER JOIN data_local AS dl
ON dl.id = dtd.local_data_id
WHERE dl.host_id = ?
AND di.hash = ?',
array($h['id'], $data_input));
if (cacti_sizeof($fields)) {
foreach($fields as $f) {
if ($f['t_value'] == null || $f['t_value'] == '') {
if (isset($data_names[$f['data_name']])) {
$hcolumn = $data_names[$f['data_name']];
if ($f['value'] != $h[$hcolumn]) {
if ($force) {
$total_repairs++;
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_template_data_id = ?
AND data_input_field_id = ?',
array($h[$hcolumn], $f['data_template_data_id'], $f['data_input_field_id']));
}
$errors[$h['id']]++;
$snmp_errors++;
}
}
}
}
}
}
if ($errors[$h['id']] > 0 && $force) {
$repaired_hosts[$h['id']] = $h['id'];
}
}
if (cacti_sizeof($errors)) {
if ($force) {
printf('NOTE: Found and repaired %s Device SNMP issues in %s Devices.' . PHP_EOL, $snmp_errors, cacti_sizeof($errors));
} else {
printf('NOTE: Not repairing %s Device SNMP issues in %s Devices.' . PHP_EOL, $snmp_errors, cacti_sizeof($errors));
}
} else {
printf('NOTE: Found 0 Device SNMP issues in %s Devices.' . PHP_EOL, cacti_sizeof($errors));
}
}
}
function print_separator($nl = false) {
print ($nl ? PHP_EOL:'') . str_repeat('-', 90) . PHP_EOL;
}
function snmp_index_repairs() {
global $config, $force, $total_errors, $total_repairs, $repaired_hosts;
print_separator(true);
if (!$force) {
printf('Detailed SNMP Index Checks. Use --force to repair if found.' . PHP_EOL . PHP_EOL);
} else {
printf('Detailed SNMP Index Checks and Repairs.' . PHP_EOL . PHP_EOL);
}
printf('NOTE: Searching for Data Sources with damaged Data Query infromation' . PHP_EOL);
printf(' using multiple techniques to attempt the repair.' . PHP_EOL);
// Correct bad hostnames and host_id's in the data_input_data table
$entries = db_fetch_assoc("SELECT did.*, dif.type_code
FROM data_input_data AS did
INNER JOIN data_input_fields AS dif
ON did.data_input_field_id = dif.id
WHERE data_input_field_id in (
SELECT id
FROM data_input_fields
WHERE type_code != ''
)
AND data_template_data_id IN (
SELECT id
FROM data_template_data
WHERE local_data_id > 0
AND data_template_id > 0
AND local_data_id IN (SELECT id FROM data_local)
)
AND type_code in ('host_id', 'hostname')
AND value = ''");
if (cacti_sizeof($entries)) {
printf('NOTE: Found and repairing %s Data Sources with missing host information.' . PHP_EOL, cacti_sizeof($entries));
$fixes = 0;
foreach($entries as $e) {
$data_template_data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE id = ?',
array($e['data_template_data_id']));
if (cacti_sizeof($data_template_data)) {
$local_data = db_fetch_row_prepared('SELECT *
FROM data_local
WHERE id = ?',
array($data_template_data['local_data_id']));
if (cacti_sizeof($local_data)) {
switch($e['type_code']) {
case 'hostname':
$hostname = db_fetch_cell_prepared('SELECT hostname
FROM host
WHERE id = ?',
array($local_data['host_id']));
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($hostname, $e['data_input_field_id'], $e['data_template_data_id']));
$fixes++;
$repaired_hosts[$local_data['host_id']] = $local_data['host_id'];
break;
case 'host_id':
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($local_data['host_id'], $e['data_input_field_id'], $e['data_template_data_id']));
$fixes++;
$repaired_hosts[$local_data['host_id']] = $local_data['host_id'];
break;
}
}
}
}
printf('NOTE: Found and repaired %s of %s Data Sources entries with invalid Device information.' . PHP_EOL, $fixes, cacti_sizeof($entries));
$total_errors += $fixes;
$total_repairs += $fixes;
} else {
printf('NOTE: Found 0 Data Sources with invalid Device infromation.' . PHP_EOL);
}
// Correct issues with non-checked data input columns that must be checked.
printf('NOTE: Searching for and repairing all Data Query required checked Data Input columns.' . PHP_EOL, cacti_sizeof($entries));
db_execute("UPDATE data_input_data
SET t_value = 'on'
WHERE data_input_field_id IN (
SELECT id
FROM data_input_fields
WHERE type_code IN ('output_type', 'index_type', 'index_value')
)
AND t_value != 'on'
AND data_template_data_id IN (
SELECT id
FROM data_template_data
WHERE data_template_id > 0
)");
$fixes = db_affected_rows();
printf('NOTE: Found %s and repaired Data Query rows missing the required checked columns.' . PHP_EOL, $fixes);
$total_errors += $fixes;
$total_repairs += $fixes;
// Correct missing host_id checkmark value in the data input data table
printf('NOTE: Searching for and repairing all Data Query rows with invalid host_id attributes.' . PHP_EOL);
// Host ID should not be checked, but should not be 'on' either
db_execute("UPDATE data_input_data
SET t_value = ''
WHERE data_input_field_id IN (
SELECT id
FROM data_input_fields
WHERE type_code IN ('host_id')
)
AND t_value != ''
AND data_template_data_id IN (
SELECT id
FROM data_template_data
WHERE data_template_id > 0
)");
$fixes = db_affected_rows();
printf('NOTE: Found and repaired %s Data Query rows with invalid host_id attributes.' . PHP_EOL, $fixes);
$total_errors += $fixes;
$total_repairs += $fixes;
printf('NOTE: Searching for damaged Data Query indexes (Pass 1).' . PHP_EOL);
// Repairing Data Input values using graph_local and data_local data if available
$broken_data_rows = db_fetch_assoc("SELECT did.*
FROM data_input_data AS did
INNER JOIN data_template_data AS dtd
ON dtd.id = did.data_template_data_id
INNER JOIN data_local AS dl
ON dl.id = dtd.local_data_id
WHERE data_input_field_id IN (
SELECT id
FROM data_input_fields
WHERE type_code = 'index_value'
)
AND value = ''
AND dl.snmp_query_id > 0");
$broken_local_data_ids = db_fetch_cell("SELECT COUNT(DISTINCT dtd.local_data_id)
FROM data_input_data AS did
INNER JOIN data_template_data AS dtd
ON dtd.id = did.data_template_data_id
INNER JOIN data_local AS dl
ON dl.id = dtd.local_data_id
WHERE data_input_field_id IN (
SELECT id
FROM data_input_fields
WHERE type_code = 'index_value'
)
AND value = ''
AND dl.snmp_query_id > 0");
if (cacti_sizeof($broken_data_rows)) {
$total_errors += cacti_sizeof($broken_data_rows);
if ($force) {
printf('NOTE: Attempting to repair %s Data Query indexes from other Cacti tables.' . PHP_EOL, $broken_local_data_ids);
$fixes = 0;
foreach($broken_data_rows as $ds) {
$data_template_data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE id = ?',
array($ds['data_template_data_id']));
$field_data = db_fetch_row_prepared('SELECT *
FROM data_input_fields
WHERE id = ?',
array($ds['data_input_field_id']));
if (cacti_sizeof($data_template_data)) {
$local_data_id = $data_template_data['local_data_id'];
$local_data = db_fetch_row_prepared('SELECT *
FROM data_local
WHERE id = ?',
array($local_data_id));
if (cacti_sizeof($local_data)) {
$local_graph_ids = db_fetch_assoc_prepared('SELECT DISTINCT local_graph_id
FROM data_template_rrd AS dtr
INNER JOIN graph_templates_item AS gti
ON dtr.id = gti.task_item_id
WHERE dtr.local_data_id = ?',
array($local_data_id));
if (cacti_sizeof($local_graph_ids)) {
foreach($local_graph_ids as $id) {
$local_graph = db_fetch_row_prepared('SELECT *
FROM graph_local
WHERE id = ?',
array($id['local_graph_id']));
switch($field_data['type_code']) {
case 'index_type':
$index_type = get_best_data_query_index_type($local_graph['host_id'], $local_graph['snmp_query_id']);
if ($index_type != '') {
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($index_type, $ds['data_input_field_id'], $ds['data_template_data_id']));
}
break;
case 'index_value':
if ($local_graph['snmp_index'] != '') {
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($local_graph['snmp_index'], $ds['data_input_field_id'], $ds['data_template_data_id']));
$repaired_hosts[$local_graph['host_id']] = $local_graph['host_id'];
$fixes++;
}
break;
case 'output_type_id':
if ($local_graph['snmp_query_graph_id'] == 0) {
$local_graph['snmp_query_graph_id'] = db_fetch_cell_prepared('SELECT id
FROM snmp_query_graph
WHERE graph_template_id = ?
AND snmp_query_id = ?',
array($local_graph['graph_template_id'], $local_graph['snmp_query_id']));
}
if ($local_graph['snmp_query_graph_id'] > 0) {
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($local_graph['snmp_query_graph_id'], $ds['data_input_field_id'], $ds['data_template_data_id']));
}
break;
}
}
}
}
}
}
$total_repairs += $fixes;
printf('NOTE: Found and repaired %s of %s Data Query Index entries in (Pass 1).' . PHP_EOL, $fixes, cacti_sizeof($broken_data_rows));
} else {
printf('NOTE: Skipping attempt to repair %s Data Query indexes in (Pass 1).' . PHP_EOL, cacti_sizeof($broken_data_rows));
}
} else {
printf('NOTE: Found 0 damaged Data Query indexes in (Pass 1).' . PHP_EOL, cacti_sizeof($broken_data_rows));
}
printf('NOTE: Searching for damaged Data Query indexes (Pass 2).' . PHP_EOL);
// Repairing Broken Data Query indexes by Graph Name
$hosts = db_fetch_assoc("SELECT DISTINCT host_id
FROM data_local
WHERE snmp_query_id > 0
AND snmp_index = ''");
$reindexes = array();
$reindex_ds_cnt = 0;
$nomatch_cnt = 0;
if (cacti_sizeof($hosts)) {
printf('NOTE: Found %s Devices with damaged Data Query indexes in (Pass 2).' . PHP_EOL, cacti_sizeof($hosts));
if ($force) {
printf('NOTE: Attempting to repair Data Query indexes from Data Source titles.' . PHP_EOL);
$match_cnt = 0;
$misses_cnt = 0;
$check_cnt = 0;
foreach($hosts as $h) {
$data_query_ids = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT snmp_query_id
FROM data_local
WHERE host_id = ?
AND snmp_index = ""',
array($h['host_id'])),
'snmp_query_id', 'snmp_query_id'
);
foreach($data_query_ids as $dqid) {
$local_data_ids = db_fetch_assoc_prepared('SELECT *
FROM data_local
WHERE snmp_query_id = ?
AND snmp_index = ""
AND host_id = ?',
array($dqid, $h['host_id']));
foreach($local_data_ids as $ldi) {
$matches = db_fetch_assoc_prepared('SELECT DISTINCT dl.id, dl.data_template_id, dl.host_id,
hsc.snmp_query_id, hsc.snmp_index, dtd.name_cache, dtd.id AS data_template_data_id, field_value
FROM host_snmp_cache AS hsc
INNER JOIN data_local AS dl
ON hsc.host_id = dl.host_id
AND hsc.snmp_query_id = dl.snmp_query_id
INNER JOIN data_template_data AS dtd
ON dl.id = dtd.local_data_id
WHERE (
dtd.name_cache LIKE CONCAT("% ", field_value, " %")
OR dtd.name_cache LIKE CONCAT("% ", field_value)
)
AND dl.id = ?
AND dl.host_id = ?
AND dl.snmp_query_id = ?
AND field_value NOT LIKE "%\%T"
AND field_value NOT IN ("_", "%", "", "-")',
array($ldi['id'], $h['host_id'], $dqid));
$total_matches = cacti_sizeof($matches);
$check_cnt++;
if ($total_matches == 1) {
$repaired_hosts[$h['host_id']] = $h['host_id'];
$match_cnt++;
db_execute_prepared('UPDATE data_local
SET snmp_index = ?, orphan = 0
WHERE id = ?',
array($matches[0]['snmp_index'], $matches[0]['id']));
db_execute('DELETE FROM user_auth_row_cache WHERE class IN ("graphs", "data_sources")');
$graphs = db_fetch_assoc_prepared('SELECT DISTINCT gl.id
FROM graph_local AS gl
INNER JOIN graph_templates_item AS gti
ON gl.id = gti.local_graph_id
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id = dtr.id
WHERE dtr.local_data_id = ?',
array($matches[0]['id']));
if (cacti_sizeof($graphs)) {
foreach($graphs as $g) {
db_execute_prepared('UPDATE graph_local
SET snmp_index = ?
WHERE id = ?',
array($matches[0]['snmp_index'], $g['id']));
}
}
db_execute_prepared('UPDATE data_input_data AS did
INNER JOIN data_input_fields AS dif
ON did.data_input_field_id = dif.id
SET value = ?
WHERE dif.type_code = "index_value"
AND did.data_template_data_id = ?',
array($matches[0]['snmp_index'], $matches[0]['data_template_data_id']));
$fixes++;
} elseif ($total_matches > 1) {
$misses_cnt++;
$reindexes[$matches[0]['snmp_query_id']][$matches[0]['host_id']] = true;
$reindex_ds_cnt++;
} else {
$misses_cnt++;
$nomatch_cnt++;
}
if ($check_cnt % 1000 == 0) {
printf("NOTE: Checks Completed: %s, Matches: %s, Missed: %s" . PHP_EOL, $check_cnt, $match_cnt, $misses_cnt);
}
}
}
}
$total_errors += ($fixes + $misses_cnt);
$total_repairs += $fixes;
printf("NOTE: Checks Completed: %s, Matches: %s, Missed: %s" . PHP_EOL, $check_cnt, $match_cnt, $misses_cnt);
if (cacti_sizeof($reindexes)) {
printf('WARNING: Found multiple valid indexes for %s Data Sources!' . PHP_EOL, $reindex_ds_cnt);
printf(' Found %s Data Sources that had no corresponding possible index.' . PHP_EOL, $nomatch_cnt);
printf(' Suggest you reindex your Devices for the following' . PHP_EOL);
printf(' Data Queries and then rerun this repair tool.' . PHP_EOL . PHP_EOL);
printf(' Eg: ./poller_reindex_hosts --host-id=N --qid=N' . PHP_EOL . PHP_EOL);
foreach($reindexes as $snmp_query_id => $hosts) {
$name = db_fetch_cell_prepared('SELECT name
FROM snmp_query
WHERE id = ?',
array($snmp_query_id));
$total_hosts = cacti_sizeof($hosts);
printf(' Data Query: %s (%s) with %s Devices impacted.' . PHP_EOL, $name, $snmp_query_id, $total_hosts);
}
}
} else {
printf('NOTE: Skipping attempt to repair Data Query indexes in (Pass 2).' . PHP_EOL);
}
} else {
printf('NOTE: Found 0 Devices with damaged Data Query indexes in (Pass 2).' . PHP_EOL);
}
printf('NOTE: Searching for orphaned Data Sources that are not orphaned.' . PHP_EOL);
$local_data_ids = db_fetch_assoc('SELECT *
FROM data_local
WHERE snmp_index != ""
AND snmp_query_id > 0
AND orphan = 1');
$total_errors += cacti_sizeof($local_data_ids);
if (cacti_sizeof($local_data_ids)) {
printf('NOTE: Found %s Devices with orphaned Data Sources that may not be orphaned.' . PHP_EOL, cacti_sizeof($local_data_ids));
if ($force) {
printf('NOTE: Attempting to confirm and repair Data Sources are in fact not orphaned.' . PHP_EOL);
$fixes = 0;
foreach($local_data_ids as $ldi) {
$snmp_index = $ldi['snmp_index'];
$found = db_fetch_cell_prepared('SELECT count(*)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND snmp_index = ?',
array($ldi['host_id'], $ldi['snmp_query_id'], $ldi['snmp_index']));
if ($found) {
$repaired_hosts[$ldi['host_id']] = $ldi['host_id'];
db_execute_prepared('UPDATE data_local
SET orphan = 0
WHERE id = ?',
array($ldi['id']));
$fixes++;
}
}
$total_repairs += $fixes;
printf('NOTE: Found and repaired %s of %s Data Sources with invalid orphan status.' . PHP_EOL, $fixes, cacti_sizeof($local_data_ids));
} else {
printf('NOTE: Skipping attempt to confirm and repair Data Sources orphan status.' . PHP_EOL);
}
} else {
printf('NOTE: Found 0 Devices with Data Sources with a potentially incorrect orphan status.' . PHP_EOL);
}
$broken_ds = db_fetch_cell("SELECT COUNT(*)
FROM data_local
WHERE snmp_query_id > 0
AND snmp_index = ''");
if ($broken_ds > 0) {
print_separator(true);
printf('WARNING: There remain %s Data Sources with invalid SNMP index information.' . PHP_EOL, $broken_ds);
printf(' Go to Console > Management > Data Sources and filter for the'. PHP_EOL);
printf(' Status of Bad Indexes and repair by hand.' . PHP_EOL);
}
}
/**
* display_version - displays version information
*/
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Database Repair Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* display_help - displays the usage of the function
*/
function display_help () {
display_version();
print PHP_EOL . 'usage: repair_database.php [--dynamic] [--debug] [--force] [--form]' . PHP_EOL . PHP_EOL;
print 'A utility designed to repair the Cacti database if damaged, and optionally repair any' . PHP_EOL;
print 'corruption found in the Cacti databases various Templates.' . PHP_EOL . PHP_EOL;
print 'Optional:' . PHP_EOL;
print ' --dynamic - Convert a table to Dynamic row format if available' . PHP_EOL;
print ' --form - Force rebuilding the indexes from the database creation syntax.' . PHP_EOL;
print ' --tables - Repair Tables as well as possible database corruptions.' . PHP_EOL;
print ' --local - Perform the action on the Remote Data Collector if run from there' . PHP_EOL;
print ' --force - Remove Invalid Template records from the database.' . PHP_EOL;
print ' --debug - Display verbose output during execution.' . PHP_EOL . PHP_EOL;
}
|