Files
cacti/scripts/aruba_instant_wlan_client.php
Thomas VincentandCopilot 78e4644f9f fix: harden data collection scripts — SQL injection, counter math, column allow-listing (#6651)
* cleanup: remove unreachable defensive error logs in aggregate graph item validation

* fix: harden data collection scripts with correct SNMP retry parameter

Replace $host['ping_retries'] with $host['snmp_retries'] across all
SNMP data collection scripts. Add column allow-lists for MikroTik
scripts to prevent SQL injection via column name interpolation.

Add Pest tests verifying column allow-lists reject injection attempts
and all scripts reference snmp_retries instead of ping_retries.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: consolidate disk device detection with preg_match

Replace 8 str_starts_with() calls with a single preg_match() in
ss_net_snmp_disk_io.php for readability. Align ss_net_snmp_disk_bytes.php
with the same device prefixes (xvd, vd, hd, md, dm-) and improved
nvme-aware partition filter.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* chore: drop unrelated lib/api_aggregate changes

Keep PR scope focused on the issue for this branch.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-05 20:09:08 -05:00

205 lines
5.3 KiB
PHP
Executable File

<?php
include(__DIR__ . '/../include/cli_check.php');
include(__DIR__ . '/../lib/snmp.php');
// define all OIDs we need for further processing
$oids = [
'index' => '.1.3.6.1.4.1.14823.2.3.3.1.1.7.1.1',
'wlan' => '.1.3.6.1.4.1.14823.2.3.3.1.1.7.1.2',
'count' => '.1.3.6.1.4.1.14823.2.3.3.1.1.7.1.4',
];
$xml_delimiter = '!';
if (count($_SERVER['argv']) < 2) {
print 'Invalid use of script query, required parameters:' . PHP_EOL;
print ' <host_id> <cmd> [query_field]' . PHP_EOL;
die();
}
$host_id = intval($_SERVER['argv'][1]);
$cmd = $_SERVER['argv'][2];
$query_index = 0;
if ($_SERVER['argv'][3]) {
$query_field = $_SERVER['argv'][3];
}
if ($_SERVER['argv'][4]) {
$query_index = intval($_SERVER['argv'][4]);
$query_index--;
}
if ($host_id <= 0) {
return '0';
}
$host = db_fetch_row_prepared('SELECT *
FROM host
WHERE id = ?',
[$host_id]);
if (!cacti_sizeof($host)) {
print '';
exit;
}
// -------------------------------------------------------------------------
// main code starts here
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// script MUST respond to index queries
// the command for this is defined within the XML file as
// <arg_index>index</arg_index>
// you may replace the string "index" both in the XML and here
// -------------------------------------------------------------------------
// php -q <script> <parms> index
// will list all indices of the target values
// e.g. in case of interfaces
// it has to respond with the list of interface indices
// -------------------------------------------------------------------------
if ($cmd == 'index') {
$return_arr = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$oids['index'],
$host['snmp_version'],
$host['snmp_username'],
$host['snmp_password'],
$host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'],
$host['snmp_context'],
$host['snmp_port'],
$host['snmp_timeout'],
$host['snmp_retries'],
SNMP_POLLER,
$host['snmp_engine_id']);
foreach ($return_arr as $key=>$value) {
print ($key + 1) . PHP_EOL;
}
} elseif ($cmd == 'query' && isset($query_field)) {
if ($query_field == 'name') {
$return_arr = reindex(cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$oids['wlan'],
$host['snmp_version'],
$host['snmp_username'],
$host['snmp_password'],
$host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'],
$host['snmp_context'],
$host['snmp_port'],
$host['snmp_timeout'],
$host['snmp_retries'],
SNMP_POLLER,
$host['snmp_engine_id']));
// index begins with 0, need increment
foreach ($return_arr as $key=>$value) {
print ($key + 1) . $xml_delimiter . $value . PHP_EOL;
}
} elseif ($query_field == 'count') {
$new = [];
$return_arr = reindex(cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$oids['count'],
$host['snmp_version'],
$host['snmp_username'],
$host['snmp_password'],
$host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'],
$host['snmp_context'],
$host['snmp_port'],
$host['snmp_timeout'],
$host['snmp_retries'],
SNMP_POLLER,
$host['snmp_engine_id']));
foreach ($return_arr as $key=>$value) {
$new[$key + 1] = $value;
}
foreach ($new as $key=>$value) {
print $key . $xml_delimiter . $value . PHP_EOL;
}
// output "1!5\n2!60\n3!13\n";
} elseif ($query_field == 'index') {
$return_arr = reindex(cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$oids['count'],
$host['snmp_version'],
$host['snmp_username'],
$host['snmp_password'],
$host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'],
$host['snmp_context'],
$host['snmp_port'],
$host['snmp_timeout'],
$host['snmp_retries'],
SNMP_POLLER,
$host['snmp_engine_id']));
foreach ($return_arr as $key=>$value) {
print ($key + 1) . $xml_delimiter . ($key + 1) . PHP_EOL;
}
// output "1!1\n2!2\n3!3\n";
}
} elseif ($cmd == 'get' && isset($query_field)) {
if ($query_field == 'name') {
$pom = cacti_snmp_get($host['hostname'],
$host['snmp_community'],
$oids['wlan'] . '.' . $query_index,
$host['snmp_version'],
$host['snmp_username'],
$host['snmp_password'],
$host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'],
$host['snmp_context'],
$host['snmp_port'],
$host['snmp_timeout'],
$host['snmp_retries'],
SNMP_POLLER,
$host['snmp_engine_id']);
print $pom;
} elseif ($query_field == 'count') {
$pom = cacti_snmp_get($host['hostname'],
$host['snmp_community'],
$oids['count'] . '.' . $query_index,
$host['snmp_version'],
$host['snmp_username'],
$host['snmp_password'],
$host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'],
$host['snmp_context'],
$host['snmp_port'],
$host['snmp_timeout'],
$host['snmp_retries'],
SNMP_POLLER,
$host['snmp_engine_id']);
print $pom;
}
}
function reindex(array $arr) : array {
$return_arr = [];
for ($i = 0; ($i < sizeof($arr)); $i++) {
$return_arr[$i] = $arr[$i]['value'];
}
return $return_arr;
}