Files
cacti/cli/add_data_query.php
T
Thomas VincentandTheWitness fcbcbdd1d7 refactor: replace strpos() boolean checks with str_contains/str_starts_with (#6645)
* 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>
2026-02-25 11:38:59 -05:00

279 lines
8.1 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/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../include/cli_check.php');
require_once(CACTI_PATH_LIBRARY . '/api_automation_tools.php');
require_once(CACTI_PATH_LIBRARY . '/api_automation.php');
require_once(CACTI_PATH_LIBRARY . '/api_data_source.php');
require_once(CACTI_PATH_LIBRARY . '/api_graph.php');
require_once(CACTI_PATH_LIBRARY . '/api_device.php');
require_once(CACTI_PATH_LIBRARY . '/data_query.php');
require_once(CACTI_PATH_LIBRARY . '/poller.php');
require_once(CACTI_PATH_LIBRARY . '/snmp.php');
require_once(CACTI_PATH_LIBRARY . '/sort.php');
require_once(CACTI_PATH_LIBRARY . '/template.php');
require_once(CACTI_PATH_LIBRARY . '/utility.php');
// switch to main database for cli's
if (POLLER_ID > 1) {
db_switch_remote_to_main();
}
global $reindex_types;
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms)) {
$displayHosts = false;
$displayDataQueries = false;
$quietMode = false;
unset($host_id);
unset($data_query_id);
unset($reindex_method);
foreach ($parms as $parameter) {
if (str_contains($parameter, '=')) {
[$arg, $value] = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
$debug = 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 '--data-query-id':
$data_query_id = $value;
if (!is_numeric($data_query_id)) {
print 'ERROR: You must supply a numeric data-query-id for all hosts!' . PHP_EOL;
exit(1);
}
break;
case '--reindex-method':
if (is_numeric($value) &&
($value >= DATA_QUERY_AUTOINDEX_NONE) &&
($value <= DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION)) {
$reindex_method = $value;
} else {
switch (strtolower($value)) {
case 'none':
$reindex_method = DATA_QUERY_AUTOINDEX_NONE;
break;
case 'uptime':
$reindex_method = DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME;
break;
case 'index':
$reindex_method = DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE;
break;
case 'fields':
$reindex_method = DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION;
break;
default:
print 'ERROR: You must supply a valid reindex method for all hosts!' . 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);
case '--list-hosts':
$displayHosts = true;
break;
case '--list-data-queries':
$displayDataQueries = true;
break;
case '--quiet':
$quietMode = true;
break;
default:
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
// list options, recognizing $quietMode
if ($displayHosts) {
$hosts = getHosts();
displayHosts($hosts, $quietMode);
exit;
}
if ($displayDataQueries) {
$data_queries = getSNMPQueries();
displaySNMPQueries($data_queries, $quietMode);
exit;
}
/*
* verify required parameters
* for update / insert options
*/
if (!isset($host_id)) {
print 'ERROR: You must supply a valid host-id for all hosts!' . PHP_EOL;
exit(1);
}
if (!isset($data_query_id)) {
print 'ERROR: You must supply a valid data-query-id for all hosts!' . PHP_EOL;
exit(1);
}
if (!isset($reindex_method)) {
print 'ERROR: You must supply a valid reindex-method for all hosts!' . PHP_EOL;
exit(1);
}
// verify valid host id and get a name for it
$host_name = db_fetch_cell('SELECT hostname FROM host WHERE id = ' . $host_id);
if (!isset($host_name)) {
print "ERROR: Unknown Host Id ($host_id)" . PHP_EOL;
exit(1);
}
// verify valid data query and get a name for it
$data_query_name = db_fetch_cell('SELECT name FROM snmp_query WHERE id = ' . $data_query_id);
if (!isset($data_query_name)) {
print "ERROR: Unknown Data Query Id ($data_query_id)" . PHP_EOL;
exit(1);
}
// Now, add the data query and run it once to get the cache filled
$exists_already = db_fetch_cell("SELECT host_id FROM host_snmp_query WHERE host_id=$host_id AND snmp_query_id=$data_query_id AND reindex_method=$reindex_method");
if ((isset($exists_already)) &&
($exists_already > 0)) {
print "ERROR: Data Query is already associated for host: ($host_id: $host_name) data query ($data_query_id: $data_query_name) reindex method ($reindex_method: " . $reindex_types[$reindex_method] . ')' . PHP_EOL;
exit(1);
} else {
db_execute('REPLACE INTO host_snmp_query
(host_id,snmp_query_id,reindex_method)
VALUES (' .
$host_id . ',' .
$data_query_id . ',' .
$reindex_method . ')');
// recache snmp data
run_data_query($host_id, $data_query_id);
}
if (is_error_message()) {
print "ERROR: Failed to add this data query for host ($host_id: $host_name) data query ($data_query_id: $data_query_name) reindex method ($reindex_method: " . $reindex_types[$reindex_method] . ')' . PHP_EOL;
exit(1);
} else {
print "Success - Host ($host_id: $host_name) data query ($data_query_id: $data_query_name) reindex method ($reindex_method: " . $reindex_types[$reindex_method] . ')' . PHP_EOL;
exit;
}
} else {
display_help();
exit;
}
/**
* display_version - displays version information
*
* @return void
*/
function display_version() : void {
$version = get_cacti_cli_version();
print "Cacti Add Data Query Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* display_help - displays help information
*
* @return void
*/
function display_help() : void {
display_version();
print PHP_EOL;
print 'usage: add_data_query.php --host-id=[ID] --data-query-id=[dq_id] --reindex-method=[method] [--quiet]' . PHP_EOL . PHP_EOL;
print 'Required Options:' . PHP_EOL;
print ' --host-id the numerical ID of the host' . PHP_EOL;
print ' --data-query-id the numerical ID of the data_query to be added' . PHP_EOL;
print ' --reindex-method the reindex method to be used for that data query' . PHP_EOL;
print ' 0|None = no reindexing' . PHP_EOL;
print ' 1|Uptime = Uptime goes Backwards' . PHP_EOL;
print ' 2|Index = Index Count Changed' . PHP_EOL;
print ' 3|Fields = Verify all Fields' . PHP_EOL . PHP_EOL;
print 'List Options:' . PHP_EOL;
print ' --list-hosts' . PHP_EOL;
print ' --list-data-queries' . PHP_EOL;
print ' --quiet - batch mode value return' . PHP_EOL . PHP_EOL;
print 'If the data query was already associated, it will be reindexed.' . PHP_EOL . PHP_EOL;
}