#!/usr/bin/env php rrd association select id from graph_templates_item where local_graph_template_item_id=0 and local_graph_id=0 and task_item_id= // Get graph associations which corresponds to supplied data template select id from graph_templates_item where local_graph_id=$g["id"] and local_graph_template_item_id in // But I'm too lazy to write such a lot of code, so let's better make one long query below */ $graph_templates_items_wrong = db_fetch_assoc("select id, task_item_id from graph_templates_item WHERE task_item_id!=" . $rrd_data[0]["id"] . " and graph_template_id=" . $graph_template_id . " and local_graph_id=" . $g["id"] . " and local_graph_template_item_id in (select id from graph_templates_item where local_graph_template_item_id=0 and local_graph_id=0 and task_item_id=(select id from data_template_rrd where local_data_template_rrd_id=0 and local_data_id=0 and data_template_id=" . $data_template_id . "))"); if (!cacti_sizeof($graph_templates_items_wrong)) { // Everything correct here. continue; } else { foreach($graph_templates_items_wrong as $graph_templates_item_wrong) { // Here is a list of graph_templates_item ids to be fixed and their wrong task_item_id $graph_templates_item[] = $graph_templates_item_wrong["id"]; $task_item_id[] = $graph_templates_item_wrong["task_item_id"]; } } print "Host " . $g["host_id"] . ", graph " . $g["id"] . ", graph item " . implode(",",$graph_templates_item) . ", task_item_id " . implode(",",$task_item_id) . "->" . $rrd_data[0]["id"] . "\n"; $query = "UPDATE graph_templates_item SET task_item_id=" . $rrd_data[0]["id"] . " WHERE task_item_id!=" . $rrd_data[0]["id"] . " and graph_template_id=" . $graph_template_id . " and local_graph_id=" . $g["id"] . " and id in (" . implode(",",$graph_templates_item) . ")"; if ($show_sql) { print $query . ";\n"; } if ($execute) { db_execute($query); } unset($graph_templates_item); unset($task_item_id); } } function display_version() { $version = get_cacti_cli_version(); print "Cacti Graph Repair Tool, Version $version, " . COPYRIGHT_YEARS . PHP_EOL; } /* display_help - displays the usage of the function */ function display_help() { print "usage: repair_graphs.php [--host-id=ID] --data-template-id=[ID]\n"; print " --graph-template-id=[ID] [--show-sql] [--execute]\n\n"; print "Cacti utility for repairing graph<->datasource relationship via a command line interface.\n\n"; print "--execute - Perform the repair\n"; print "--show-sql - Show SQL lines for the repair (optional)\n"; print "--host-id=id - The host_id to repair or leave empty to process all hosts\n"; print "--data-template-id=id - The numerical ID of the data template to be fixed\n"; print "--graph-template-id=id - The numerical ID of the graph template to be fixed\n"; }