Skip to content

Commit

Permalink
Fixing Issue #4512 Graph API Issues
Browse files Browse the repository at this point in the history
The Cacti API can not remove a Graph that has no Data Sources
  • Loading branch information
TheWitness committed Dec 21, 2021
1 parent cdaf94e commit 3b9c2e7
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
182 changes: 182 additions & 0 deletions cli/remove_broken_graphs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2021 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/ |
+-------------------------------------------------------------------------+
*/

include ('../include/cli_check.php');
include_once('../lib/api_data_source.php');
include_once('../lib/api_graph.php');

/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);

global $debug;

$debug = false;
$report = false;
$remove = false;

$columns = explode(' ', shell_exec('stty size'))[1];

if (empty($columns)) {
$columns = 80;
}

if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}

switch ($arg) {
case '--debug':
$debug = true;
break;
case '--report':
$report = true;
break;
case '--remove':
$remove = true;
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 . "\n\n";
display_help();
exit(1);
}
}
}

if ($report && $remove) {
print 'ERROR: The options --report and --remove are mutually exclusive' . PHP_EOL;
display_help();
exit(1);
}

if (!$report && !$remove) {
$report = true;
}

print 'Running Query to find Broken Graphs. This Query may run for some time depending the number of Graph in your System' . PHP_EOL;

$sql = "SELECT name, graph_template_id, graphs, local_graph_ids
FROM (
SELECT graph_template_id, GROUP_CONCAT(DISTINCT local_graph_id SEPARATOR ', ') AS local_graph_ids, COUNT(DISTINCT local_graph_id) AS graphs
FROM graph_templates_item
WHERE local_graph_id > 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;
}
}
2 changes: 2 additions & 0 deletions lib/api_graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3b9c2e7

Please sign in to comment.