From 3b9c2e789a6c4ed38f663e4bd0b1a77d12449e5d Mon Sep 17 00:00:00 2001 From: TheWitness Date: Tue, 21 Dec 2021 13:01:52 -0500 Subject: [PATCH] Fixing Issue #4512 Graph API Issues The Cacti API can not remove a Graph that has no Data Sources --- CHANGELOG | 1 + cli/remove_broken_graphs.php | 182 +++++++++++++++++++++++++++++++++++ lib/api_graph.php | 2 + 3 files changed, 185 insertions(+) create mode 100644 cli/remove_broken_graphs.php diff --git a/CHANGELOG b/CHANGELOG index 68f56ae768..a785f24e03 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -25,6 +25,7 @@ Cacti CHANGELOG -issue#4500: DSStats Does not Scale on Large Systems -issue#4509: Updating Graph Template Items for a Template is Slow on Large Systems -issue#4511: When viewing SVG Graphs the Graphs do not size properly when using the Time View UI +-issue#4512: The Cacti API can not remove a Graph that has no Data Sources 1.2.19 -security#4356: Further fixes for grave character security protection diff --git a/cli/remove_broken_graphs.php b/cli/remove_broken_graphs.php new file mode 100644 index 0000000000..f6bdd97e9d --- /dev/null +++ b/cli/remove_broken_graphs.php @@ -0,0 +1,182 @@ +#!/usr/bin/env php + 0 + AND graph_template_id > 0 + AND task_item_id > 0 + AND task_item_id NOT IN ( + SELECT id + FROM data_template_rrd + WHERE local_data_id > 0 + ) + GROUP BY graph_template_id + ) AS gti + INNER JOIN graph_templates AS gt + ON gt.id = gti.graph_template_id"; + +$entries = db_fetch_assoc($sql); + +if (cacti_sizeof($entries)) { + print 'There are ' . cacti_sizeof($entries) . ' Graph Templates with Broken Graphs.' . PHP_EOL; +} else { + print 'There are no Graph Templates with Broken Graphs.' . PHP_EOL; +} + +if (cacti_sizeof($entries)) { + print '-------------------------------------------------------------------------------------' . PHP_EOL; + if ($report) { + print 'Broken Graph Report' . PHP_EOL; + } else { + print 'Broken Graph Report Removal' . PHP_EOL; + } + print '-------------------------------------------------------------------------------------' . PHP_EOL; + + foreach($entries as $e) { + if ($report) { + printf('Graph Template: %s, Contains \'%s\' broken Graphs' . PHP_EOL, $e['name'], $e['graphs']); + print 'Graphs:' . PHP_EOL; + print '-------------------------------------------------------------------------------------' . PHP_EOL; + print wordwrap($e['local_graph_ids'], $columns-5, PHP_EOL) . PHP_EOL; + print '-------------------------------------------------------------------------------------' . PHP_EOL; + } else { + print '-------------------------------------------------------------------------------------' . PHP_EOL; + $local_graph_ids = explode(', ', $e['local_graph_ids']); + printf('Started removing \'%s\' broken Graphs for Graph Template %s' . PHP_EOL, $e['graphs'], $e['name']); + print '-------------------------------------------------------------------------------------' . PHP_EOL; + if (cacti_sizeof($local_graph_ids)) { + foreach($local_graph_ids as $local_graph_id) { + $title = get_graph_title_cache($local_graph_id); + printf('Removing Graph %s, "%s"' . PHP_EOL, $title, $local_graph_id); + $id = array(); + $id[$local_graph_id] = $local_graph_id; + api_delete_graphs($id, '2'); + } + } + print '-------------------------------------------------------------------------------------' . PHP_EOL; + printf('Completed removing Graphs for Graph Template %s' . PHP_EOL, $e['name']); + } + } +} + +/* display_version - displays version information */ +function display_version() { + $version = get_cacti_cli_version(); + print "Cacti Remove Broken Graphs Utility, Version $version, " . COPYRIGHT_YEARS . "\n"; +} + +/* display_help - displays the usage of the function */ +function display_help () { + display_version(); + + print PHP_EOL . 'usage: remove_broken_graphs.php [--report | --remove] [-d|--debug]' . PHP_EOL . PHP_EOL; + print 'A utility to remove broken graphs from Cacti. A broken Graph is one that.' . PHP_EOL; + print 'lacks Data Sources. This can happen from time to time when working with and modifying templates.' . PHP_EOL; + print 'It\'s important to periodically run this utility expecially on larger systems.' . PHP_EOL . PHP_EOL; + print 'Optional:' . PHP_EOL; + print '--report - Display the Graph Templates and Count of Broken Graphs' . PHP_EOL; + print '--remove - Remove the Broken Graphs as Reported using the --report Option' . PHP_EOL; + print '--debug - Display verbose output during execution' . PHP_EOL . PHP_EOL; +} + +function debug($message) { + global $debug; + + if ($debug) { + print date('H:i:s') . ' DEBUG: ' . trim($message) . PHP_EOL; + } +} diff --git a/lib/api_graph.php b/lib/api_graph.php index 3685ddfb73..2f2009db2e 100644 --- a/lib/api_graph.php +++ b/lib/api_graph.php @@ -88,6 +88,8 @@ function api_delete_graphs(&$local_graph_ids, $delete_type) { if (cacti_sizeof($data_sources)) { api_data_source_remove_multi($data_sources); } + } else { + api_graph_remove_multi($local_graph_ids); } break;