Files
Cactiplugin_thold/cli_thresholds.php
TheWitnessandCopilot 9b217e0e3a INFO: set compat to 1.2.29 (#838)
* INFO: set compat to 1.2.32

* docs: update Copilot instructions Cacti baseline to 1.2.32 (matches INFO compat)

* INFO: back off compat to 1.2.29

* Add manifest.json + upgrade-time file pruning

* Prune phpunit.xml on upgrade and ignore markdown-lint configs

phpunit.xml ships in the repo for CI but is a dev-only test artifact, so it
is removed from manifest.json 'expected', pruned from installs on upgrade
(like tests/), and excluded from the manifest drift check.

The retired markdown-lint configs (.mdlrc, .md_style.rb) are deleted, and
.md* is now ignored like .git*: protected from pruning and excluded from
drift if it reappears.

* Move thold_functions.php and thold_webapi.php into includes/; flatten css

The main threshold library becomes includes/functions.php and the web API
helper becomes includes/webapi.php; every caller (setup.php, includes/*, the
CLI/poller/notify entry points and the test suite) loads them from includes/.
Each per-theme stylesheet css/<theme>/main.css becomes css/<theme>.css so users
can drop their own css/<theme>.css into the css base, and the page_head hook
looks there. Old paths are tombstoned so existing installs drop the stale
copies on upgrade.

* Regenerate cacti.pot for the includes/ file relocations

Moving the plugin's library files into includes/ changed the source-reference
lines in the translation template; regenerate it so the 'Verify translation
template' CI check passes.

* Harden the hook-include test and make RPN integer ops explicit

The HookFunctionsIncludeTest invokes real hooks with placeholder input purely
to execute their relocated include_once lines; it now swallows engine
notices/warnings/deprecations during those calls (and restores the error
handler and config afterward) so it cannot trip the suite's failOnDeprecation
or leak state into other tests. The RPN % and ^ operators now cast their
operands to int explicitly, matching their long-standing integer-truncation
behaviour while silencing the implicit float-to-int conversion deprecation.

* Harden prune against tampered-manifest path traversal

The upgrade-time prune now rejects any tombstone containing '.'/'..' segments
(which could escape the plugin directory or resolve to its root) and treats
ancestors of whitelist entries as protected, so a tombstone on a parent
directory can no longer delete a whitelisted file beneath it.

* docs: update and align the Project Structure block

Point the structure tree at the relocated files (libraries now under
includes/, data files under docs/, stylesheets under css/) and align the
trailing '# ...' comments to a single column so they no longer drift right.

* docs/tests: standardize test header and correct manifest-contract notes

- Use the full license header (from the plugin's own setup.php) in
  tests/Unit/PruneFilesTest.php instead of the abbreviated copyright banner.
- Document that the manifest drift check and the upgrade-time prune also
  handle phpunit.xml and .md* files, matching the implemented behavior.

* refactor: rename prune helpers to the plugin-slug function prefix

The repository naming contract reserves the plugin_<name>_ prefix for plugin
lifecycle / hook-registration functions; all other functions use the plain
<name>_ prefix. Rename the internal upgrade helpers accordingly:

  plugin_<name>_prune_files() -> <name>_prune_files()
  plugin_<name>_rmtree()      -> <name>_rmtree()

The call site, unit tests, and the copilot-instructions.md references are
updated to match. No behavioral change.

* chore: set the Cacti compatibility floor to 1.2.29

Align the compat metadata (and the contributor-guide references) with the
team decision to standardize the minimum supported Cacti version at 1.2.29;
the previously proposed 1.2.32 floor was not adopted.

---------

Co-authored-by: Copilot <copilot@users.noreply.github.com>
2026-10-01 01:40:00 -05:00

247 lines
7.5 KiB
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/ |
+-------------------------------------------------------------------------+
*/
// let PHP run just as long as it has to
ini_set('max_execution_time', '0');
error_reporting(E_ALL);
$dir = __DIR__;
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
include('./include/cli_check.php');
include_once($config['base_path'] . '/plugins/thold/includes/functions.php');
// set the defaults
$force = false;
$debug = false;
$gtemplate = 0;
$ttemplate = 0;
$hids = '';
$gids = '';
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
if (sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
[$arg, $value] = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--auto-create':
case '-auto':
if (is_numeric($value)) {
thold_cli_autocreate($value);
} else {
print 'ERROR: Invalid Device ID ' . $value . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
exit;
case '--debug':
case '-d':
$debug = true;
break;
case '--force':
case '-f':
$force = true;
break;
case '--graph-template':
case '-gt':
$gtemplate = $value;
break;
case '--thold-template':
case '-tt':
$ttemplate = $value;
break;
case '--graph-ids':
case '-ids':
$gids = $value;
break;
case '--host-ids':
$hids = $value;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
}
}
// validate device ids
if (strlen($hids)) {
$hids = explode(' ', $hids);
foreach ($hids as $id) {
if (!is_numeric($id)) {
print 'ERROR: The Device ID \'' . $id . '\' is NOT numeric. All Device IDs must be numeric.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
}
}
// validate values
if (!is_numeric($ttemplate)) {
print 'ERROR: The Thold Template must be numeric.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
// validate values
if (!is_numeric($gtemplate)) {
print 'ERROR: The Graph Template must be numeric.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
// validate graph ids
if (strlen($gids)) {
$gids = explode(' ', $gids);
foreach ($gids as $id) {
if (!is_numeric($id)) {
print 'ERROR: The Graph ID \'' . $id . '\' is NOT numeric. All Graph IDs must be numeric.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
}
}
// perform some checks
if ($ttemplate == 0 && $gtemplate == 0 && $hids == '' && $gids == '') {
print 'ERROR: You must choose either --auto-create or a combination of Devices, Graphs,' . PHP_EOL;
print 'a Graph Template of Threshold Template.' . PHP_EOL . PHP_EOL;
display_help();
exit(-1);
}
thold_cli_autocreate($hids, $gids, $gtemplate, $ttemplate);
/**
* Auto-creates thresholds for the given devices/graphs by delegating
* to autocreate(), then prints any resulting session flash messages
* (stripped of HTML) to stdout.
*
* @param string $hids Space-delimited host ids to create
* thresholds for (default '').
* @param string $gids Space-delimited graph ids to create
* thresholds for (default '').
* @param int $gtemplate The graph template id to filter by (default
* 0).
* @param int $ttemplate The threshold template id to apply (default
* 0).
*
* @return void
*/
function thold_cli_autocreate($hids = '', $gids = '', $gtemplate = 0, $ttemplate = 0) {
print 'Auto Creating Thresholds' . PHP_EOL;
autocreate($hids, $gids, $gtemplate, $ttemplate);
if (isset($_SESSION['sess_messages'])) {
foreach ($_SESSION['sess_messages'] as $message_id => $message) {
if (strpos($message_id, 'thold_message') !== false) {
print strip_tags(str_replace('<br>', PHP_EOL, $message['message'])) . PHP_EOL;
}
}
}
}
/**
* Prints this script's name, version, and copyright banner to stdout.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* locate setup.php when plugin_thold_version()
* isn't already loaded.
*/
function display_version() {
global $config;
if (!function_exists('plugin_thold_version')) {
include_once($config['base_path'] . '/plugins/thold/setup.php');
}
$info = plugin_thold_version();
print 'Threshold Command Line Interface, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* Prints this script's version banner followed by its command-line
* usage instructions to stdout.
*
* @return void
*/
function display_help() {
display_version();
print PHP_EOL;
print 'usage: cli_thresholds.php --auto-create=N | [--host-ids=\'N1 N2 ...\'] [--graph-template=N] [--thold-template=N] [--graph-ids=\'N1 N2 ...\']' . PHP_EOL . PHP_EOL;
print 'There are two usage methods:' . PHP_EOL . PHP_EOL;
print 'The first requires you to specify the host id of the device and all existing Threshold templates' . PHP_EOL;
print 'are applied to hosts.' . PHP_EOL . PHP_EOL;
print '--auto-create=N - Auto Create all Thresholds for this Device id using Templates associated' . PHP_EOL;
print ' the Devices Device Template.' . PHP_EOL . PHP_EOL;
print 'The second requires you to specify either a series of Devices, a Graph Template, a Threshold Template' . PHP_EOL;
print 'or a series of Graphs or any combination of the above. However, at least one must be specified.' . PHP_EOL;
print 'Threshold Template and Graph IDs of the Graphs to be impacted.' . PHP_EOL . PHP_EOL;
print '--host-ids=\'N1 N2 ...\' - The Devices ID to create Thresholds for' . PHP_EOL;
print '--graph-template=N - The Graph Template to create Thresholds for' . PHP_EOL;
print '--thold-template=N - The Threshold Template to use for creating Thresholds' . PHP_EOL;
print '--graph-ids=\'N1 N2 ...\' - The Threshold Template to use for creating Thresholds' . PHP_EOL . PHP_EOL;
}