#!/usr/bin/php -q '" . $table['Tables_in_' . $database_default] . "'"; $status = db_execute('REPAIR TABLE ' . $table['Tables_in_' . $database_default] . $form); print ($status == 0 ? ' Failed' : ' Successful') . "\n"; if ($dynamic) { print "Changing Table Row Format to Dynamic -> '" . $table['Tables_in_' . $database_default] . "'"; $status = db_execute('ALTER TABLE ' . $table['Tables_in_' . $database_default] . ' ROW_FORMAT=DYNAMIC'); print ($status == 0 ? ' Failed' : ' Successful') . "\n"; } } } print "\nNOTE: Running some Data Query repair scripts\n"; 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 = 0"); db_execute("UPDATE graph_local AS gl INNER JOIN ( SELECT DISTINCT local_graph_id, task_item_id FROM 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 INNER JOIN data_template_data AS dtd ON dtr.local_data_id = dtd.local_data_id INNER JOIN data_input_fields AS dif ON dif.data_input_id = dtd.data_input_id INNER JOIN data_input_data AS did ON did.data_template_data_id = dtd.id AND did.data_input_field_id = dif.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 input_output = 'in' AND type_code = 'output_type' AND gl.graph_template_id IN (SELECT graph_template_id FROM snmp_query_graph)"); print "NOTE: Checking for Invalid Cacti Templates\n"; /* keep track of total rows */ $total_rows = 0; /* 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_rows += $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'); } print "NOTE: $rows Invalid GPrint Preset Rows " . ($force ? 'removed from':'found in') . " Graph Templates\n"; } /* 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_rows += $rows; if ($rows > 0) { if ($force) { db_execute('DELETE FROM cdef_items WHERE cdef_id NOT IN (SELECT id FROM cdef)'); } print "NOTE: $rows Invalid CDEF Item Rows " . ($force ? 'removed from':'found in') . " Graph Templates\n"; } /* 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_rows += $rows; if ($rows > 0) { if ($force) { db_execute('DELETE FROM data_template_data WHERE data_input_id NOT IN (SELECT id FROM data_input)'); } print "NOTE: $rows Invalid Data Input Rows " . ($force ? 'removed from':'found in') . " Data Templates\n"; } /* 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_rows += $rows; if ($rows > 0) { if ($force) { 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'); } print "NOTE: $rows Invalid Data Input Field Rows " . ($force ? 'removed from':'found in') . " Data Templates\n"; } /* --------------------------------------------------------------------*/ /* 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_rows += $rows; if ($rows > 0) { if ($force) { db_execute('DELETE FROM data_input_data WHERE data_input_data.data_template_data_id NOT IN (SELECT id FROM data_template_data)'); } print "NOTE: $rows Invalid Data Input Data Rows based upon template mappings " . ($force ? 'removed from':'found in') . " Data Templates\n"; } $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_rows += $rows; if ($rows > 0) { if ($force) { db_execute('DELETE FROM data_input_data WHERE data_input_data.data_input_field_id NOT IN (SELECT id FROM data_input_fields)'); } print "NOTE: $rows Invalid Data Input Data rows based upon field mappings " . ($force ? 'removed from':'found in') . " Data Templates\n"; } if ($total_rows > 0 && !$force) { print "\nWARNING: Cacti Template Problems found in your Database. Using the '--force' option will remove\n"; print "the invalid records. However, these changes can be catastrophic to existing data sources. Therefore, you \n"; print "should contact your support organization prior to proceeding with that repair.\n\n"; } elseif ($total_rows == 0) { print "NOTE: No Invalid Cacti Template Records found in your Database\n\n"; } /* display_version - displays version information */ function display_version() { $version = get_cacti_cli_version(); print "Cacti Database Repair Utility, Version $version, " . COPYRIGHT_YEARS . "\n"; } /* display_help - displays the usage of the function */ function display_help () { display_version(); print "\nusage: repair_database.php [--dynamic] [--debug] [--force] [--form]\n\n"; print "A utility designed to repair the Cacti database if damaged, and optionally repair any\n"; print "corruption found in the Cacti databases various Templates.\n\n"; print "Optional:\n"; print " --dynamic - Convert a table to Dynamic row format if available\n"; print " --form - Force rebuilding the indexes from the database creation syntax.\n"; print " --force - Remove Invalid Template records from the database.\n"; print " --debug - Display verbose output during execution.\n\n"; }