mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-23 03:27:02 +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>
216 lines
6.4 KiB
PHP
Executable File
216 lines
6.4 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 . '/api_tree.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/data_query.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/functions.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/poller.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/reports.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/template.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/utility.php');
|
|
|
|
ini_set('max_execution_time', '0');
|
|
|
|
// 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);
|
|
|
|
global $debug;
|
|
|
|
$debug = false;
|
|
$hostname = '';
|
|
$description = '';
|
|
$ids = [];
|
|
|
|
if (cacti_sizeof($parms)) {
|
|
foreach ($parms as $parameter) {
|
|
if (str_contains($parameter, '=')) {
|
|
[$arg, $value] = explode('=', $parameter, 2);
|
|
} else {
|
|
$arg = $parameter;
|
|
$value = '';
|
|
}
|
|
|
|
switch ($arg) {
|
|
case '-d':
|
|
case '--debug':
|
|
$debug = true;
|
|
|
|
break;
|
|
case '--hostname':
|
|
$hostname = $value;
|
|
|
|
break;
|
|
case '--description':
|
|
$description = $value;
|
|
|
|
break;
|
|
case '--ids':
|
|
$ids = explode(' ', $value);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check for matching like/regex
|
|
if (!cacti_sizeof($ids) && $hostname == '' && $description == '') {
|
|
print 'FATAL: You must specify either ids, a hostname or host description pattern' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
|
|
$sql_where = '';
|
|
|
|
// Check device id range
|
|
if (cacti_sizeof($ids)) {
|
|
foreach ($ids as $id) {
|
|
if (!is_numeric($id) || $id <= 0) {
|
|
print 'FATAL: Device id ' . $id . ' is not a valid device. Can not continue.' . PHP_EOL;
|
|
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$sql_where = 'WHERE h.id IN (' . implode(',', $ids) . ')';
|
|
}
|
|
|
|
// Check hostname
|
|
if ($hostname != '') {
|
|
$sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . '(';
|
|
$regex = false;
|
|
|
|
if (validate_is_regex($hostname)) {
|
|
$regex = true;
|
|
$sql_where .= 'h.hostname RLIKE ' . db_qstr($hostname);
|
|
}
|
|
|
|
$hostname = '%' . $hostname . '%';
|
|
$sql_where .= ($regex == true ? ' OR ' : '') . 'h.hostname LIKE ' . db_qstr($hostname) . ')';
|
|
}
|
|
|
|
$dregex = false;
|
|
|
|
if ($description != '') {
|
|
$sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . '(';
|
|
$regex = false;
|
|
|
|
if (validate_is_regex($description)) {
|
|
$regex = true;
|
|
$sql_where .= 'h.description RLIKE ' . db_qstr($description);
|
|
}
|
|
|
|
$description = '%' . $description . '%';
|
|
$sql_where .= ($regex == true ? ' OR ' : '') . 'h.description LIKE ' . db_qstr($description) . ')';
|
|
}
|
|
|
|
$devices = array_rekey(
|
|
db_fetch_assoc('SELECT id FROM host AS h ' . $sql_where),
|
|
'id', 'id'
|
|
);
|
|
|
|
if (cacti_sizeof($devices)) {
|
|
if ($debug) {
|
|
print 'DEBUG: Found ' . cacti_sizeof($devices) . ' devices to run automation on' . PHP_EOL;
|
|
}
|
|
|
|
foreach ($devices as $device_id) {
|
|
if ($debug) {
|
|
print 'DEBUG: Running automation for Device ID ' . $device_id . PHP_EOL;
|
|
}
|
|
|
|
cacti_log('NOTE: Running CLI based Batch Automation on Device[' . $device_id . ']', false, 'AUTOM8');
|
|
|
|
automation_update_device($device_id);
|
|
}
|
|
|
|
if ($debug) {
|
|
print 'DEBUG: automation run complete!' . PHP_EOL;
|
|
}
|
|
} elseif ($debug) {
|
|
print 'DEBUG: No devices found for this automation pass.' . PHP_EOL;
|
|
}
|
|
|
|
/**
|
|
* display_version - displays version information
|
|
*
|
|
* @return void
|
|
*/
|
|
function display_version() : void {
|
|
$version = get_cacti_cli_version();
|
|
print "Cacti Apply Automation Rules Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
|
|
}
|
|
|
|
/**
|
|
* display_help - displays the usage of the function
|
|
*
|
|
* @return void
|
|
*/
|
|
function display_help() : void {
|
|
display_version();
|
|
|
|
print PHP_EOL;
|
|
print 'usage: apply_automation_rules.php --ids="id1 id2 ..." || --description=S || --hostname=S [--debug]' . PHP_EOL . PHP_EOL;
|
|
print 'A utility to execute Cacti automation rules for a devices or devices. Any of the following' . PHP_EOL;
|
|
print 'three options can be used, but at least one must be specified.' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Required:' . PHP_EOL;
|
|
print ' --ids="id1 id2 ..." - A space delimited list of device ids.' . PHP_EOL;
|
|
print ' --hostname="S" - Either a SQL where clause or REGEX of the devices hostname.' . PHP_EOL;
|
|
print ' --description="S" - Either a SQL where clause or REGEX of the devices description.' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Optional:' . PHP_EOL;
|
|
print ' --debug - Provide verbose output during automation' . PHP_EOL . PHP_EOL;
|
|
}
|