mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-30 16:47:14 +09:00
* refactor: replace strpos() boolean checks with str_contains/str_starts_with Completes the PHP 8.x string function migration started in lib/. Converts remaining legacy patterns across 81 files: - strpos($x, $y) !== false → str_contains($x, $y) - strpos($x, $y) === false → !str_contains($x, $y) - strpos($x, $y) != false → str_contains($x, $y) - bare if (strpos($x, $y)) → if (str_contains($x, $y)) - substr($x, 0, strlen($y)) == $y → str_starts_with($x, $y) Intentionally preserves strpos() calls that use the return value as an integer offset: - user_admin.php / user_group_admin.php (4 calls, ternary for position) - auth_profile.php (2 calls, offset for substr); guards fixed from bare if ($pos) to if ($pos !== false) Behavior fix: ss_esxi_vhosts.php:55 bare strpos() missed position-0 matches; str_contains() now correctly detects 'not installed' at any position. Adjacent line already used str_contains(). Ref: https://github.com/Cacti/cacti/issues/6641 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix: correct dead-code elseif in plugin_display_compat() version comparison The if/elseif chain in plugin_display_compat() checked >=, >=, >, < — the second >= was unreachable dead code. Corrected to <= to complete the operator set (>=, <=, >, <). Without this fix, <= constraints (e.g. "<=1.2") fell through to the < branch and were rendered as "< =1.2" instead of "<= 1.2". This pre-existing bug was exposed when converting strpos() to str_contains(), which made PHPStan detect the always-false condition. Ref: https://github.com/Cacti/cacti/issues/6641 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix: add limit to explode('=') in CLI argument parsers 17 CLI scripts used explode('=', $parameter) without a limit of 2, silently truncating values containing = (e.g. --note=a=b parsed as $value='a' instead of 'a=b'). The other 40 CLI scripts already had the correct limit. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> Co-authored-by: TheWitness <thewitness@cacti.net>
259 lines
7.9 KiB
PHP
Executable File
259 lines
7.9 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
/*
|
|
+-------------------------------------------------------------------------+
|
|
| Copyright (C) 2004-2026 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/ |
|
|
+-------------------------------------------------------------------------+
|
|
*/
|
|
|
|
// Original script is located here https://forums.cacti.net/viewtopic.php?t=35816, but this one was modified quite a lot
|
|
|
|
require(__DIR__ . '/../include/cli_check.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/poller.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/utility.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/template.php');
|
|
|
|
// switch to main database for cli's
|
|
if (POLLER_ID > 1) {
|
|
db_switch_remote_to_main();
|
|
}
|
|
|
|
// process calling arguments
|
|
$parms = $_SERVER['argv'];
|
|
array_shift($parms);
|
|
|
|
$execute = false;
|
|
$show_sql = false;
|
|
|
|
unset($host_id);
|
|
unset($graph_template_id);
|
|
unset($data_template_id);
|
|
|
|
foreach ($parms as $parameter) {
|
|
if (str_contains($parameter, '=')) {
|
|
[$arg, $value] = explode('=', $parameter, 2);
|
|
} else {
|
|
$arg = $parameter;
|
|
$value = '';
|
|
}
|
|
|
|
switch ($arg) {
|
|
case '--execute':
|
|
$execute = true;
|
|
|
|
break;
|
|
case '--show-sql':
|
|
$show_sql = true;
|
|
|
|
break;
|
|
case '--host-id':
|
|
$host_id = trim($value);
|
|
|
|
if (!is_numeric($host_id)) {
|
|
print 'ERROR: You must supply a valid host-id to run this script!' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
break;
|
|
case '--graph-template-id':
|
|
$graph_template_id = $value;
|
|
|
|
if (!is_numeric($graph_template_id)) {
|
|
print 'ERROR: You must supply a numeric graph-template-id!' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
break;
|
|
case '--data-template-id':
|
|
$data_template_id = $value;
|
|
|
|
if (!is_numeric($data_template_id)) {
|
|
print 'ERROR: You must supply a numeric data-template-id!' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
if (!$show_sql && !$execute) {
|
|
display_help();
|
|
|
|
exit(1);
|
|
}
|
|
|
|
if (!isset($data_template_id)) {
|
|
print 'ERROR: You must supply a valid data-template-id!' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
if (!isset($graph_template_id)) {
|
|
print 'ERROR: You must supply a valid graph-template-id!' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
if ($execute) {
|
|
print 'NOTE: Repairing Graphs' . PHP_EOL;
|
|
} else {
|
|
print 'NOTE: Performing Check of Graphs' . PHP_EOL;
|
|
}
|
|
|
|
// Get all graphs for supplied graph template
|
|
$graph = db_fetch_assoc('SELECT *
|
|
FROM graph_local
|
|
WHERE ' . (!isset($host_id) ? '' : 'host_id=' . $host_id . ' AND ') . ' graph_template_id=' . $graph_template_id . '');
|
|
|
|
if (cacti_sizeof($graph)) {
|
|
if (!$show_sql) {
|
|
print PHP_EOL;
|
|
print 'Corrupted graphs:' . PHP_EOL;
|
|
}
|
|
|
|
foreach ($graph as $g) {
|
|
$graph_templates_item = [];
|
|
$task_item_id = 0;
|
|
|
|
// Get datasource for supplied data template for current host
|
|
$ds = db_fetch_assoc('SELECT * FROM data_local where host_id=' . $g['host_id'] . ' and data_template_id=' . $data_template_id);
|
|
|
|
if (!cacti_sizeof($ds)) {
|
|
continue;
|
|
}
|
|
$ds = $ds[0];
|
|
|
|
// Get rrd for found datasource
|
|
$rrd_data = db_fetch_assoc('SELECT * FROM data_template_rrd where local_data_id=' . $ds['id']);
|
|
|
|
if (!cacti_sizeof($rrd_data)) {
|
|
print 'Could not get correct rrd id for datasource=' . $ds['id'] . PHP_EOL;
|
|
|
|
continue;
|
|
}
|
|
|
|
/*
|
|
// Here we will find graph items that should point to our data template
|
|
// Get templated rrd id for given data template
|
|
select id from data_template_rrd where local_data_template_rrd_id=0 and local_data_id=0 and data_template_id=520
|
|
// Get templated graph->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_prepared('SELECT id, task_item_id
|
|
FROM graph_templates_item
|
|
WHERE task_item_id != ?
|
|
AND graph_template_id = ?
|
|
AND local_graph_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 = ?
|
|
)
|
|
)', [$rrd_data[0]['id'], $graph_template_id, $g['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'] . PHP_EOL;
|
|
|
|
$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 . ';' . PHP_EOL;
|
|
}
|
|
|
|
if ($execute) {
|
|
db_execute($query);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* display_version - displays version information
|
|
*
|
|
* @return void
|
|
*/
|
|
function display_version() : void {
|
|
$version = get_cacti_cli_version();
|
|
print "Cacti Graph Repair Tool, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
|
|
}
|
|
|
|
/**
|
|
* display_help - displays the usage of the function
|
|
*
|
|
* @return void
|
|
*/
|
|
function display_help() : void {
|
|
print 'usage: repair_graphs.php [--host-id=ID] --data-template-id=[ID]' . PHP_EOL;
|
|
print ' --graph-template-id=[ID] [--show-sql] [--execute]' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Cacti utility for repairing graph<->datasource relationship via a command line interface.' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Optional:' . PHP_EOL;
|
|
print '--execute - Perform the repair' . PHP_EOL;
|
|
print '--show-sql - Show SQL lines for the repair (optional)' . PHP_EOL;
|
|
print '--host-id=id - The host_id to repair or leave empty to process all hosts' . PHP_EOL;
|
|
print '--data-template-id=id - The numerical ID of the data template to be fixed' . PHP_EOL;
|
|
print '--graph-template-id=id - The numerical ID of the graph template to be fixed' . PHP_EOL . PHP_EOL;
|
|
}
|