mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-27 15:17:05 +09:00
* PHPStan Level 8 - Phase 1 Conversion
Reduces the real (no-baseline) PHPStan level 8 error count from ~2,724 to 425
and regenerates phpstan-baseline.neon accordingly (8,941 -> 1,921 lines).
Core type-safety changes
- lib/database.php: narrow db_fetch_row()/db_fetch_row_prepared() to return
array (was bool|array) and db_fetch_assoc()/db_fetch_assoc_prepared() to
return array (was mixed); the _prepared workers coerce the connection-failure
path to an empty array so the declared type is honest. empty()/cacti_sizeof()
checks are unaffected.
- lib/database.php: narrow $db_conn guards from is_object() to instanceof PDO in
the transaction/quote/insert-id helpers so the PDO calls resolve.
- lib/mib_cache.php: the fluent MibCache methods mib()/object()/table()/row()
now return self instead of a bare object so chained calls type-check.
Type-safety casts and guards (mechanical, behaviour-preserving)
- Cast string|false/|null builtin results feeding string parameters
(json_decode, base64_encode, trim, htmle, preg_replace, explode, etc.).
- Cast int|string / int|false values feeding int id/pid parameters.
- Guard resource|false fopen()/popen() handles before fwrite/fclose/fread, and
wrap foreach over array|false/list|false results with `?: []`.
Genuine bugs found and fixed
- cli/add_tree.php / lib/api_tree.php: site tree nodes could not be created via
the CLI - the --site-id option was never parsed, $nodeTypes had no 'site'
entry (passing null to api_tree_item_save(int $type) -> TypeError), and
api_tree_item_save() never persisted site_id. All three fixed.
- tree.php: leaves_exist() is typed : int but returned db_fetch_assoc_prepared()
(an array of rows) for a SELECT COUNT(*); now uses db_fetch_cell_prepared()
and returns the scalar count.
- lib/mib_cache.php: $entries_per_object was a float (division / ceil) used as
an array key (PHP 8.1 implicit float-to-int key deprecation); now cast to int.
Config
- .phpstan.neon: add notIdentical.alwaysTrue and booleanAnd.alwaysFalse to the
existing always-true/false ignore family (same "PHPStan gets confused"
dynamic-constant cause already covered by identical.alwaysTrue,
notEqual.alwaysTrue, booleanAnd.leftAlwaysTrue, etc.).
- phpstan-baseline.neon: regenerated; the remaining 425 entries are the
untouched long tail for Phase 2.
* Remove redundant @phpstan-ignore comments
Follow-up cleanup for the level 8 Phase 1 work. Removes 14 inline
@phpstan-ignore comments that no longer suppress any reported error:
- cli/repair_database.php: `@phpstan-ignore booleanAnd.alwaysFalse` is now
covered by the global booleanAnd.alwaysFalse ignore added to .phpstan.neon.
- 13 others (host.php, include/global.php, include/session.php,
lib/api_automation.php x2, lib/functions.php x2, lib/html_utility.php x2,
lib/installer.php, lib/package.php, lib/reports.php, lib/rrd.php) no longer
match any reported error after the type-safety fixes (e.g. the
db_fetch_row_prepared() return-type narrowing removed the offset-assign
errors the api_automation ignores were suppressing).
Verified with reportUnmatchedIgnoredErrors enabled: 0 unmatched ignores
remain and the full level 8 run still reports 0 errors.
* ci: satisfy validator ratchet and refresh security sink baselines
- lib/api_tree.php: give the new tree-item site_id a real numeric
constraint ('^[-0-9]+$', matching html_reports.php) instead of an
empty regex, so the validator constraint ratchet passes without
bumping its baseline.
- tests/security/baselines: refresh the three sink/hotspot signatures
whose code text changed from the (string) casts added in this branch
(lib/poller.php setsid, cli/rrdresize.php json_encode, lib/import.php
simplexml_load_string).
* ci(js): allow DOMPurify 3.4.16 and update sync_js pin test
The dependabot bump (#8134) moved package.json/package-lock.json to
DOMPurify 3.4.16 on develop but left tests/js/sync_js.test.mjs pinned to
3.4.15, so the PR merge commit fails the 'DOMPurify is pinned' contract.
Bring package.json/package-lock.json to 3.4.16 (matching develop) and
update the pin test to expect 3.4.16.
* test: update sql.php null-handling assertion for the (string) cast
This branch changed scripts/sql.php from trim(\ ?? '') to
trim((string) \) (the phpstan-safe form that coerces both null and
false from shell_exec()). Update SqlScriptsTest's null-return assertion
to match the new cast instead of the removed '?? ''.
* fix: address Copilot review - ownership IDs, fopen guards, mib_cache bound
- chown()/chgrp(): pass integer UID/GID from fileowner()/filegroup()
(guarding the false lookup) instead of string-casting them, which made
them resolve account/group names like '1001' and fail. Covers
lib/api_device.php, lib/boost.php and poller_maintenance.php (x2).
- fopen() failure now short-circuits instead of reporting success:
lib/package.php returns false, lib/clog_webapi.php and utilities.php
render an error, and snmpagent_mibcachechild.php skips the cache write
and sentinel unlink so a concurrent writer's lock is preserved.
- lib/mib_cache.php: keep the float quotient as the loop bound and cast
only the array offset, so a non-even entries/columns split no longer
truncates the loop and drops a cached row.
* revert: keep db_fetch_* returning false on error; baseline Level 8 instead
Per review, restoring the intentional fail-safe: db_fetch_row[_prepared]()
return bool|array and db_fetch_assoc[_prepared]() return mixed again, so a
genuine SQL error (not just a missing connection) is signalled as false and
the === false checks in cdef_is_in_use(), boost_process_* and aggregate.php
work as before. Dropped the is_array() ? : [] coercion.
Level 8 stays green by regenerating phpstan-baseline.neon to capture the
re-introduced bool|array/mixed call-site errors (the ~1,850 the narrowing
had eliminated). Tightening those callers to drop them from the baseline is
Phase 2 work.
---------
Co-authored-by: TheWitness <copilot@users.noreply.github.com>
1157 lines
38 KiB
PHP
Executable File
1157 lines
38 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_once(__DIR__ . '/include/cli_check.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/snmp.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/poller.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/rrd.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/ping.php');
|
|
require_once(CACTI_PATH_LIBRARY . '/boost.php');
|
|
|
|
if (function_exists('pcntl_async_signals')) {
|
|
pcntl_async_signals(true);
|
|
} else {
|
|
declare(ticks = 100);
|
|
}
|
|
|
|
ini_set('max_execution_time', '0');
|
|
ini_set('memory_limit', '-1');
|
|
|
|
// process calling arguments
|
|
$parms = $_SERVER['argv'];
|
|
array_shift($parms);
|
|
|
|
$first = null;
|
|
$last = null;
|
|
$allhost = true;
|
|
$debug = false;
|
|
$mibs = false;
|
|
$mode = 'online';
|
|
$sessions = [];
|
|
$downhosts = [];
|
|
$host_count = 0;
|
|
$tot_errors = 0;
|
|
$poller_id = POLLER_ID;
|
|
$pmessage = false;
|
|
$help = false;
|
|
$version = false;
|
|
|
|
if (cacti_sizeof($parms)) {
|
|
foreach ($parms as $parameter) {
|
|
if (str_contains($parameter, '=')) {
|
|
[$arg, $value] = explode('=', $parameter, 2);
|
|
} else {
|
|
$arg = $parameter;
|
|
$value = '';
|
|
}
|
|
|
|
switch ($arg) {
|
|
case '--version':
|
|
case '-V':
|
|
case '-v':
|
|
$version = true;
|
|
|
|
break;
|
|
case '--help':
|
|
case '-H':
|
|
case '-h':
|
|
$help = true;
|
|
|
|
break;
|
|
case '--poller':
|
|
case '-p':
|
|
$pmessage = true;
|
|
$poller_id = $value;
|
|
|
|
break;
|
|
case '--first':
|
|
case '-f':
|
|
$first = $value;
|
|
$allhost = false;
|
|
|
|
break;
|
|
case '--last':
|
|
case '-l':
|
|
$last = $value;
|
|
$allhost = false;
|
|
|
|
break;
|
|
case '--mibs':
|
|
case '-m':
|
|
$mibs = true;
|
|
|
|
break;
|
|
case '--mode':
|
|
case '-N':
|
|
$mode = $value;
|
|
|
|
break;
|
|
case '--debug':
|
|
case '-d':
|
|
$debug = true;
|
|
|
|
break;
|
|
default:
|
|
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
|
|
display_help();
|
|
|
|
exit(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($version) {
|
|
display_version();
|
|
|
|
exit;
|
|
}
|
|
|
|
if ($help) {
|
|
display_help();
|
|
|
|
exit;
|
|
}
|
|
|
|
global $poller_db_cnn_id, $remote_db_cnn_id, $cactiphp, $using_proc_function;
|
|
global $poller_id, $sessions, $downhosts, $print_data_to_stdout;
|
|
|
|
$maxwidth = get_max_column_width();
|
|
|
|
if ($pmessage) {
|
|
cacti_log('Forcing poller to poll poller_id ' . $poller_id, true, 'POLLER', POLLER_VERBOSITY_HIGH);
|
|
}
|
|
|
|
if (POLLER_ID > 1 && CACTI_CONNECTION == 'online') { // @phpstan-ignore-line
|
|
$poller_db_cnn_id = $remote_db_cnn_id;
|
|
} else {
|
|
$poller_db_cnn_id = false;
|
|
}
|
|
|
|
if ($first == null && $last == null) {
|
|
// This is valid
|
|
} elseif (!is_numeric($first) || $first < 0) {
|
|
cacti_log('FATAL: The first host in the host range is invalid!', true, 'POLLER');
|
|
|
|
exit(-1);
|
|
} elseif (!is_numeric($last) || $last < 0) {
|
|
cacti_log('FATAL: The last host in the host range is invalid!', true, 'POLLER');
|
|
|
|
exit(-1);
|
|
} elseif ($last < $first) {
|
|
cacti_log('FATAL: The first host must always be less or equal to the last host!', true, 'POLLER');
|
|
|
|
exit(-1);
|
|
}
|
|
|
|
// verify the poller_id
|
|
if (!is_numeric($poller_id) || $poller_id < 1) {
|
|
cacti_log('FATAL: The poller needs to be a positive numeric value', true, 'POLLER');
|
|
|
|
exit(-1);
|
|
}
|
|
|
|
// notify cacti processes that a poller is running
|
|
record_cmdphp_started();
|
|
|
|
$exists = db_fetch_cell_prepared('SELECT COUNT(*)
|
|
FROM host
|
|
WHERE poller_id = ?',
|
|
[$poller_id]
|
|
);
|
|
|
|
if ($exists == 0 && $poller_id > 1) {
|
|
record_cmdphp_done();
|
|
db_close();
|
|
|
|
exit(-1);
|
|
}
|
|
|
|
// install signal handlers for UNIX only
|
|
if (function_exists('pcntl_signal')) {
|
|
pcntl_signal(SIGTERM, 'sig_handler');
|
|
pcntl_signal(SIGINT, 'sig_handler');
|
|
}
|
|
|
|
// record the start time
|
|
$start = microtime(true);
|
|
$poller_interval = read_config_option('poller_interval');
|
|
$script_timeout = read_config_option('script_timeout');
|
|
$cron_interval = read_config_option('cron_interval');
|
|
$active_profiles = read_config_option('active_profiles');
|
|
|
|
// check arguments
|
|
if ($allhost) {
|
|
$sql_where1 = '';
|
|
$sql_where3 = '';
|
|
|
|
$params1 = [$poller_id];
|
|
|
|
if ($cron_interval == $poller_interval) {
|
|
$params3 = [$poller_interval, $poller_interval, 0, $poller_interval, $poller_id];
|
|
} else {
|
|
$params3 = [$poller_interval, $poller_interval, $poller_interval, $poller_interval, $poller_id];
|
|
}
|
|
} else {
|
|
$sql_where0 = 'WHERE poller_id > ?';
|
|
$sql_where1 = ' AND ((h.id >= ? AND h.id <= ?) OR h.id IS NULL)';
|
|
$sql_where3 = ' AND pi.host_id >= ? AND pi.host_id <= ?';
|
|
|
|
$params1 = [$poller_id, $first, $last];
|
|
|
|
if ($cron_interval == $poller_interval) {
|
|
$params3 = [$poller_interval, $poller_interval, 0, $poller_interval, $poller_id, $first, $last];
|
|
} else {
|
|
$params3 = [$poller_interval, $poller_interval, $poller_interval, $poller_interval, $poller_id, $first, $last];
|
|
}
|
|
}
|
|
|
|
if ($debug) {
|
|
$print_data_to_stdout = true;
|
|
$medium = POLLER_VERBOSITY_LOW;
|
|
$hmedium = POLLER_VERBOSITY_LOW;
|
|
} elseif ($allhost) {
|
|
$print_data_to_stdout = true;
|
|
$medium = POLLER_VERBOSITY_LOW;
|
|
$hmedium = POLLER_VERBOSITY_MEDIUM;
|
|
} else {
|
|
$print_data_to_stdout = false;
|
|
$medium = POLLER_VERBOSITY_MEDIUM;
|
|
$hmedium = POLLER_VERBOSITY_MEDIUM;
|
|
}
|
|
|
|
// address potential exploits
|
|
input_validate_input_number($first, 'first');
|
|
input_validate_input_number($last, 'last');
|
|
|
|
if (db_column_exists('sites', 'disabled')) {
|
|
$sql_where = 'AND IFNULL(s.disabled, \'\') != \'on\'';
|
|
} else {
|
|
$sql_where = '';
|
|
}
|
|
|
|
if ($active_profiles != 1) {
|
|
// pi.* only: poller_item and host share ~13 identically-named snmp_*/hostname
|
|
// columns, and FETCH_ASSOC collapses them to one key (last table wins). Selecting
|
|
// pi.* keeps poller_item's cached copies authoritative instead of silently reading
|
|
// host's live values. h./s.disabled are only referenced in the WHERE below, not in
|
|
// PHP, but are aliased distinctly here in case that changes.
|
|
$poller_items = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . " pi.*,
|
|
h.disabled AS host_disabled,
|
|
s.disabled AS site_disabled
|
|
FROM poller_item AS pi
|
|
LEFT JOIN host AS h
|
|
ON h.id = pi.host_id
|
|
LEFT JOIN sites AS s
|
|
ON s.id = h.site_id
|
|
WHERE pi.poller_id = ?
|
|
$sql_where
|
|
AND IFNULL(TRIM(h.disabled),'') != 'on'
|
|
$sql_where1
|
|
AND pi.rrd_next_step <= 0
|
|
ORDER by pi.host_id",
|
|
$params1
|
|
);
|
|
|
|
// count in PHP over the rows already fetched above instead of re-running the join
|
|
$script_server_calls = count(array_filter($poller_items, function ($item) {
|
|
return in_array((int) $item['action'], [POLLER_ACTION_SCRIPT_PHP, POLLER_ACTION_SCRIPT_PHP_COUNT], true);
|
|
}));
|
|
|
|
// setup next polling interval
|
|
db_execute_prepared("UPDATE poller_item AS pi
|
|
SET rrd_next_step = IF(rrd_step = ?, 0, IF(rrd_next_step - ? < 0, rrd_step - ?, rrd_next_step - ?))
|
|
WHERE poller_id = ?
|
|
$sql_where3",
|
|
$params3);
|
|
} else {
|
|
$poller_items = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . " pi.*,
|
|
h.disabled AS host_disabled,
|
|
s.disabled AS site_disabled
|
|
FROM poller_item AS pi
|
|
LEFT JOIN host AS h
|
|
ON h.id = pi.host_id
|
|
LEFT JOIN sites AS s
|
|
ON s.id = h.site_id
|
|
WHERE pi.poller_id = ?
|
|
$sql_where
|
|
AND IFNULL(TRIM(h.disabled),'') != 'on'
|
|
$sql_where1
|
|
ORDER by pi.host_id",
|
|
$params1
|
|
);
|
|
|
|
$script_server_calls = count(array_filter($poller_items, function ($item) {
|
|
return in_array((int) $item['action'], [POLLER_ACTION_SCRIPT_PHP, POLLER_ACTION_SCRIPT_PHP_COUNT], true);
|
|
}));
|
|
}
|
|
|
|
if (cacti_sizeof($poller_items) && read_config_option('poller_enabled') == 'on') {
|
|
$host_down = false;
|
|
$new_host = true;
|
|
$last_host = '';
|
|
$output_array = [];
|
|
$output_count = 0;
|
|
$error_ds = [];
|
|
$width_dses = [];
|
|
$errors = 0;
|
|
$host_id = 0;
|
|
$itemcnt = 0;
|
|
$host_start = microtime(true);
|
|
$set_spike_kill = false;
|
|
|
|
/* startup Cacti php polling server and include the
|
|
* include file for script processing
|
|
*/
|
|
if ($script_server_calls > 0) {
|
|
$cactides = [
|
|
0 => ['pipe', 'r'], // stdin is a pipe that the child will read from
|
|
1 => ['pipe', 'w'], // stdout is a pipe that the child will write to
|
|
2 => ['pipe', 'w'] // stderr is a pipe to write to
|
|
];
|
|
|
|
$php_path = read_config_option('path_php_binary');
|
|
|
|
$command = $php_path . ' -q ' .
|
|
CACTI_PATH_BASE . '/script_server.php ' .
|
|
' --environ=cmd ' .
|
|
' --poller=' . $poller_id .
|
|
' --mode=' . CACTI_CONNECTION;
|
|
|
|
$cactiphp = proc_open($command, $cactides, $pipes);
|
|
|
|
// proc_open returns false if the child could not be spawned; fall back to
|
|
// the non-proc path rather than reading from non-existent pipes
|
|
if (!is_resource($cactiphp)) {
|
|
cacti_log('WARNING: Unable to start PHP Script Server, falling back to direct execution', $print_data_to_stdout, 'POLLER', POLLER_VERBOSITY_LOW);
|
|
|
|
$using_proc_function = false;
|
|
$cactiphp = false;
|
|
} else {
|
|
$output = fgets($pipes[1], 1024);
|
|
|
|
if (substr_count((string) $output, 'Started') != 0) {
|
|
cacti_log('PHP Script Server Started Properly', $print_data_to_stdout, 'POLLER', POLLER_VERBOSITY_HIGH);
|
|
}
|
|
|
|
$using_proc_function = true;
|
|
}
|
|
} else {
|
|
$using_proc_function = false;
|
|
$cactiphp = false;
|
|
}
|
|
|
|
foreach ($poller_items as $item) {
|
|
$ds = $item['local_data_id'];
|
|
$host_id = $item['host_id'];
|
|
|
|
// check for host change
|
|
if ($host_id != $last_host) {
|
|
$new_host = true;
|
|
$host_down = false;
|
|
$set_spike_kill = false;
|
|
|
|
if ($last_host != '') {
|
|
$host_end = microtime(true);
|
|
|
|
if ($output_count > 0) {
|
|
cacti_log("Device[$last_host] Writing $output_count items to Poller Output Table", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
db_execute('INSERT IGNORE INTO poller_output
|
|
(local_data_id, rrd_name, time, output)
|
|
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
|
|
|
|
if (read_config_option('boost_redirect') == 'on' && read_config_option('boost_rrd_update_enable') == 'on') {
|
|
boost_flush_output_batch($output_array, $poller_db_cnn_id);
|
|
}
|
|
|
|
$output_array = [];
|
|
$output_count = 0;
|
|
}
|
|
|
|
$errors = cacti_sizeof($error_ds);
|
|
|
|
if (db_column_exists('host', 'current_errors')) {
|
|
db_execute_prepared('UPDATE host
|
|
SET polling_time = ?, current_errors = ?
|
|
WHERE id = ?
|
|
AND deleted = ""',
|
|
[($host_end - $host_start), $errors, $last_host]
|
|
);
|
|
} else {
|
|
db_execute_prepared('UPDATE host
|
|
SET polling_time = ?
|
|
WHERE id = ?
|
|
AND deleted = ""',
|
|
[($host_end - $host_start), $last_host]
|
|
);
|
|
}
|
|
|
|
cacti_log(sprintf('Device[%d] Time[%3.2f] Items[%d] Errors[%d]', $last_host, $host_end - $host_start, $itemcnt, $errors), $print_data_to_stdout, 'POLLER', $hmedium);
|
|
|
|
if ($errors > 0) {
|
|
if (read_config_option('spine_log_level') == 1) {
|
|
cacti_log('WARNING: Invalid Response(s), Errors[' . $errors . '] Device[' . $last_host . '] Thread[1] DS[' . implode(', ', $error_ds) . ']', false, 'POLLER');
|
|
}
|
|
|
|
$tot_errors += $errors;
|
|
|
|
db_execute_prepared('INSERT INTO host_errors
|
|
(host_id, poller_id, errors, local_data_ids)
|
|
VALUES (?, ?, ?, ?)',
|
|
[$last_host, $poller_id, $errors, implode(',', $error_ds)]);
|
|
}
|
|
}
|
|
|
|
$error_ds = [];
|
|
$itemcnt = 0;
|
|
$host_start = microtime(true);
|
|
|
|
$host_count++;
|
|
}
|
|
|
|
// ping host, find out if there are re-index checks required
|
|
if ($new_host && !empty($host_id)) {
|
|
$host_down = ping_and_reindex_check($item, $mibs, $script_timeout);
|
|
}
|
|
|
|
$new_host = false;
|
|
$last_host = $host_id;
|
|
|
|
if (!$host_down) {
|
|
$output = collect_device_data($item, $error_ds, $script_timeout);
|
|
$itemcnt++;
|
|
|
|
if (read_config_option('poller_debug') == 'on' && strlen($output) > $maxwidth) {
|
|
$width_dses[] = $ds;
|
|
}
|
|
|
|
if ($set_spike_kill && !substr_count($output, ':')) {
|
|
// insert a U in place of the actual value if the snmp agent restarts
|
|
$output_array[] = sprintf('(%d, %s, CURRENT_TIMESTAMP(), %s)', $item['local_data_id'], db_qstr($item['rrd_name'], $poller_db_cnn_id), db_qstr('U', $poller_db_cnn_id));
|
|
} else {
|
|
// otherwise, just insert the value received from the poller
|
|
$output_array[] = sprintf('(%d, %s, CURRENT_TIMESTAMP(), %s)', $item['local_data_id'], db_qstr($item['rrd_name'], $poller_db_cnn_id), db_qstr($output, $poller_db_cnn_id));
|
|
}
|
|
|
|
if ($output_count > 2000) {
|
|
cacti_log("Device[$host_id] Writing $output_count items to Poller Output Table", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
db_execute('INSERT IGNORE INTO poller_output
|
|
(local_data_id, rrd_name, time, output)
|
|
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
|
|
|
|
if (read_config_option('boost_redirect') == 'on' && read_config_option('boost_rrd_update_enable') == 'on') {
|
|
boost_flush_output_batch($output_array, $poller_db_cnn_id);
|
|
}
|
|
|
|
$output_array = [];
|
|
$output_count = 0;
|
|
} else {
|
|
$output_count++;
|
|
}
|
|
}
|
|
|
|
// check for an over running poller
|
|
$now = microtime(true);
|
|
|
|
if ($now - $start > $poller_interval) {
|
|
cacti_log('WARNING: cmd.php poller has run over its polling interval and therefore is ending');
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Record the last hosts polling time
|
|
$host_end = microtime(true);
|
|
|
|
// Flush the items to the output table
|
|
if ($output_count > 0) {
|
|
cacti_log("Device[$host_id] Writing $output_count items to Poller Output Table", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
db_execute('INSERT IGNORE INTO poller_output
|
|
(local_data_id, rrd_name, time, output)
|
|
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
|
|
|
|
if (read_config_option('boost_redirect') == 'on' && read_config_option('boost_rrd_update_enable') == 'on') {
|
|
boost_flush_output_batch($output_array, $poller_db_cnn_id);
|
|
}
|
|
}
|
|
|
|
$errors = cacti_sizeof($error_ds);
|
|
|
|
if (db_column_exists('host', 'current_errors')) {
|
|
db_execute_prepared('UPDATE host
|
|
SET polling_time = ?, current_errors = ?
|
|
WHERE id = ?
|
|
AND deleted = ""',
|
|
[($host_end - $host_start), $errors, $host_id]
|
|
);
|
|
} else {
|
|
db_execute_prepared('UPDATE host
|
|
SET polling_time = ?
|
|
WHERE id = ?
|
|
AND deleted = ""',
|
|
[($host_end - $host_start), $host_id]
|
|
);
|
|
}
|
|
|
|
cacti_log(sprintf('Device[%d] Time[%3.2f] Items[%d] Errors[%d]', $last_host, $host_end - $host_start, $itemcnt, $errors), $print_data_to_stdout, 'POLLER', $hmedium);
|
|
|
|
if ($host_id > 0 && $errors > 0) {
|
|
if (read_config_option('spine_log_level') == 1) {
|
|
cacti_log('WARNING: Invalid Response(s), Errors[' . $errors . '] Device[' . $host_id . '] Thread[1] DS[' . implode(', ', $error_ds) . ']', false, 'POLLER');
|
|
}
|
|
|
|
$tot_errors += $errors;
|
|
|
|
db_execute_prepared('INSERT INTO host_errors
|
|
(host_id, poller_id, errors, local_data_ids)
|
|
VALUES (?, ?, ?, ?)',
|
|
[$last_host, $poller_id, $errors, implode(',', $error_ds)]);
|
|
}
|
|
|
|
if (cacti_sizeof($width_dses)) {
|
|
cacti_log('WARNING: Long Responses Errors[' . cacti_sizeof($width_dses) . '] DS[' . implode(', ', $width_dses) . ']', false, 'POLLER');
|
|
}
|
|
|
|
if ($using_proc_function && $script_server_calls > 0) {
|
|
// close php server process
|
|
@fwrite($pipes[0], "quit\r\n");
|
|
fclose($pipes[0]);
|
|
fclose($pipes[1]);
|
|
fclose($pipes[2]);
|
|
|
|
$return_value = proc_close($cactiphp);
|
|
}
|
|
|
|
// take time and log performance data
|
|
$end = microtime(true);
|
|
|
|
cacti_log(sprintf(
|
|
'Time: %01.4f s, ' .
|
|
'Poller: %s, ' .
|
|
'Threads: N/A, ' .
|
|
'Devices: %d, ' .
|
|
'Items: %d, ' .
|
|
'Errors: %d',
|
|
round($end - $start, 4),
|
|
$poller_id,
|
|
$host_count,
|
|
cacti_sizeof($poller_items),
|
|
$tot_errors
|
|
), $print_data_to_stdout, 'POLLER', $medium);
|
|
} else {
|
|
cacti_log('NOTE: There are no items in your poller for this polling cycle!', true, 'POLLER', $medium);
|
|
}
|
|
|
|
// Try to check device status only for devices without poller items
|
|
if ($allhost) {
|
|
$sql_where = 'AND h.poller_id = ?';
|
|
$params = [$poller_id];
|
|
} else {
|
|
$sql_where = 'AND h.poller_id = ? AND h.id >= ? AND h.id <= ? ';
|
|
$params = [$poller_id, $first, $last];
|
|
}
|
|
|
|
$hosts = db_fetch_assoc_prepared("SELECT h.id, h.hostname, h.ping_port, h.ping_method, h.ping_retries, h.ping_timeout, h.availability_method
|
|
FROM host AS h
|
|
LEFT JOIN poller_item AS pi
|
|
ON h.id=pi.host_id
|
|
WHERE pi.host_id IS NULL
|
|
AND (h.disabled = '' OR h.disabled IS NULL)
|
|
$sql_where",
|
|
$params);
|
|
|
|
if (cacti_sizeof($hosts)) {
|
|
cacti_log('NOTE: Found ' . cacti_sizeof($hosts) . ' Device(s) for up/down validation only', true, 'POLLER', $medium);
|
|
|
|
foreach ($hosts as $host) {
|
|
$ping = new Net_Ping;
|
|
$ping->host = $host;
|
|
$ping->port = $host['ping_port'];
|
|
|
|
// perform the appropriate ping check of the host
|
|
if ($ping->ping($host['availability_method'], $host['ping_method'], $host['ping_timeout'], $host['ping_retries'])) {
|
|
update_host_status(HOST_UP, $host['id'], $ping, $host['availability_method'], $print_data_to_stdout);
|
|
cacti_log('Device[' . $host['id'] . "] STATUS: Device '" . $host['hostname'] . "' is UP.", $print_data_to_stdout, 'POLLER', debug_level($host['id'], POLLER_VERBOSITY_MEDIUM));
|
|
} else {
|
|
update_host_status(HOST_DOWN, $host['id'], $ping, $host['availability_method'], $print_data_to_stdout);
|
|
cacti_log('Device[' . $host['id'] . "] STATUS: Device '" . $host['hostname'] . "' is Down.", $print_data_to_stdout, 'POLLER', debug_level($host['id'], POLLER_VERBOSITY_MEDIUM));
|
|
}
|
|
}
|
|
}
|
|
|
|
// record the process as having completed
|
|
record_cmdphp_done();
|
|
|
|
// close the database connection
|
|
db_close();
|
|
|
|
exit(0);
|
|
|
|
// function to assist in logging
|
|
function debug_level(int $host_id, int $level) : int {
|
|
global $debug;
|
|
|
|
static $debug_enabled = [];
|
|
|
|
if (!is_bool($debug_enabled[$host_id] ?? null)) {
|
|
if ($debug) {
|
|
$debug_enabled[$host_id] = true;
|
|
} else {
|
|
$debug_enabled[$host_id] = is_device_debug_enabled($host_id);
|
|
}
|
|
}
|
|
|
|
$level = $debug_enabled[$host_id] ? POLLER_VERBOSITY_NONE : $level;
|
|
|
|
return $level;
|
|
}
|
|
|
|
// let the poller server know about cmd.php being finished
|
|
function record_cmdphp_done(int $pid = 0) : void {
|
|
global $poller_id, $poller_db_cnn_id;
|
|
|
|
if ($pid == 0) {
|
|
$pid = getmypid();
|
|
}
|
|
|
|
db_execute_prepared('UPDATE poller_time
|
|
SET end_time=NOW()
|
|
WHERE poller_id = ?
|
|
AND pid = ?',
|
|
[$poller_id, $pid],
|
|
true,
|
|
$poller_db_cnn_id
|
|
);
|
|
}
|
|
|
|
// let cacti processes know that a poller has started
|
|
function record_cmdphp_started() : void {
|
|
global $poller_id, $poller_db_cnn_id;
|
|
|
|
db_execute_prepared("INSERT INTO poller_time
|
|
(poller_id, pid, start_time, end_time)
|
|
VALUES (?, ?, NOW(), '0000-00-00 00:00:00')",
|
|
[$poller_id, getmypid()],
|
|
true,
|
|
$poller_db_cnn_id
|
|
);
|
|
}
|
|
|
|
function open_snmp_session(int $host_id, array &$item) : mixed {
|
|
global $sessions, $downhosts;
|
|
|
|
if (!isset($item['max_oids'])) {
|
|
$item['max_oids'] = read_config_option('max_get_size');
|
|
}
|
|
|
|
if (!isset($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']]) && !isset($downhosts[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']])) {
|
|
$sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] = cacti_snmp_session(
|
|
$item['hostname'],
|
|
$item['snmp_community'],
|
|
$item['snmp_version'],
|
|
$item['snmp_username'],
|
|
$item['snmp_password'],
|
|
$item['snmp_auth_protocol'],
|
|
$item['snmp_priv_passphrase'],
|
|
$item['snmp_priv_protocol'],
|
|
$item['snmp_context'],
|
|
$item['snmp_engine_id'],
|
|
$item['snmp_port'],
|
|
$item['snmp_timeout'],
|
|
$item['snmp_retries'] ?? read_config_option('ping_retries'),
|
|
$item['max_oids']
|
|
);
|
|
|
|
if ($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] === false) {
|
|
unset($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']]);
|
|
$downhosts[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] = true;
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return $sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']];
|
|
}
|
|
|
|
function snmp_mark_host_down(int $host_id, array &$item) : void {
|
|
global $sessions, $downhosts;
|
|
|
|
unset($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']]);
|
|
$downhosts[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] = true;
|
|
}
|
|
|
|
function update_system_mibs(int $host_id) : void {
|
|
$system_mibs = [
|
|
'snmp_sysDescr' => '.1.3.6.1.2.1.1.1.0',
|
|
'snmp_sysObjectID' => '.1.3.6.1.2.1.1.2.0',
|
|
'snmp_sysUpTimeInstanceAlt' => '.1.3.6.1.6.3.10.2.1.3.0',
|
|
'snmp_sysUpTimeInstance' => '.1.3.6.1.2.1.1.3.0',
|
|
'snmp_sysContact' => '.1.3.6.1.2.1.1.4.0',
|
|
'snmp_sysName' => '.1.3.6.1.2.1.1.5.0',
|
|
'snmp_sysLocation' => '.1.3.6.1.2.1.1.6.0'
|
|
];
|
|
|
|
$h = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', [$host_id]);
|
|
|
|
if (cacti_sizeof($h)) {
|
|
$session = open_snmp_session($host_id, $h);
|
|
$uptime = false;
|
|
$engine_time = false;
|
|
$system_uptime = false;
|
|
|
|
if ($session !== false) {
|
|
foreach ($system_mibs as $name => $oid) {
|
|
$value = cacti_snmp_session_get($session, $oid);
|
|
|
|
if ($name == 'snmp_sysUpTimeInstanceAlt') {
|
|
$engine_time = $value;
|
|
} elseif ($name == 'snmp_sysUpTimeInstance') {
|
|
$system_uptime = $value;
|
|
} elseif (!empty($value)) {
|
|
db_execute_prepared("UPDATE host SET $name = ?
|
|
WHERE deleted = ''
|
|
AND id = ?",
|
|
[$value, $host_id]
|
|
);
|
|
}
|
|
}
|
|
|
|
// spine always prefers a numeric, non-wall-clock engine time when
|
|
// updating host.snmp_sysUpTimeInstance; match that here so this PHP
|
|
// poller path and spine agree on the stored value
|
|
$uptime = cacti_snmp_select_uptime($system_uptime, $engine_time, null, true);
|
|
|
|
if ($uptime !== false) {
|
|
db_execute_prepared("UPDATE host SET snmp_sysUpTimeInstance = ?
|
|
WHERE deleted = ''
|
|
AND id = ?",
|
|
[$uptime, $host_id]
|
|
);
|
|
}
|
|
} else {
|
|
cacti_log("WARNING: Unable to open session for System Mib collection for Device[$host_id]", false, 'POLLER');
|
|
}
|
|
}
|
|
}
|
|
|
|
function collect_device_data(array &$item, array &$error_ds, int $script_timeout) : string {
|
|
global $print_data_to_stdout, $using_proc_function, $pipes, $cactiphp;
|
|
|
|
$thread_start = microtime(true);
|
|
$ds = $item['local_data_id'];
|
|
$host_id = $item['host_id'];
|
|
$output = 'U';
|
|
|
|
switch ($item['action']) {
|
|
case POLLER_ACTION_SNMP:
|
|
if (($item['snmp_version'] == 0) || (($item['snmp_community'] == '') && ($item['snmp_version'] != 3))) {
|
|
cacti_log("Device[$host_id] DS[$ds] ERROR: Invalid SNMP Data Source. Please either delete it from the database, or correct it.", $print_data_to_stdout, 'POLLER');
|
|
$output = 'U';
|
|
} else {
|
|
$session = open_snmp_session($host_id, $item);
|
|
|
|
if ($session !== false) {
|
|
$output = cacti_snmp_session_get($session, $item['arg1'], true);
|
|
|
|
if (!is_numeric($output)) {
|
|
if (prepare_validate_result($output) == false) {
|
|
$error_ds[$ds] = $ds;
|
|
|
|
if (read_config_option('spine_log_level') == 2) {
|
|
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] OID:" . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER');
|
|
}
|
|
|
|
$output = 'U';
|
|
}
|
|
}
|
|
} else {
|
|
snmp_mark_host_down($host_id, $item);
|
|
|
|
$output = 'U';
|
|
|
|
$error_ds[$ds] = $ds;
|
|
|
|
if (read_config_option('spine_log_level') == 2) {
|
|
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] OID:" . $item['arg1'] . ', output: U', $print_data_to_stdout, 'POLLER');
|
|
}
|
|
}
|
|
}
|
|
|
|
$total_time = (microtime(true) - $thread_start) * 1000;
|
|
|
|
cacti_log("Device[$host_id] DS[$ds] TT[" . round($total_time, 2) . '] SNMP: v' . $item['snmp_version'] . ': ' . $item['hostname'] . ', dsname: ' . $item['rrd_name'] . ', oid: ' . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SCRIPT:
|
|
$output = trim(exec_poll($item['arg1'], $script_timeout));
|
|
|
|
if ($output == 'U') {
|
|
$error_ds[$ds] = $ds;
|
|
|
|
if (read_config_option('spine_log_level') == 2) {
|
|
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SCRIPT: " . $item['arg1'] . ', output: U', $print_data_to_stdout, 'POLLER');
|
|
}
|
|
} elseif (!is_numeric($output)) {
|
|
if (prepare_validate_result($output) == false) {
|
|
$error_ds[$ds] = $ds;
|
|
|
|
if (read_config_option('spine_log_level') == 2) {
|
|
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SCRIPT: " . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER');
|
|
}
|
|
}
|
|
}
|
|
|
|
$total_time = (microtime(true) - $thread_start) * 1000;
|
|
|
|
cacti_log("Device[$host_id] DS[$ds] TT[" . round($total_time, 2) . '] SCRIPT: ' . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SCRIPT_PHP:
|
|
$output = trim(str_replace("\n", '', exec_poll_php($item['arg1'], $using_proc_function, $pipes, $cactiphp)));
|
|
|
|
if ($output == 'U') {
|
|
$error_ds[$ds] = $ds;
|
|
|
|
if (read_config_option('spine_log_level') == 2) {
|
|
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SERVER: " . $item['arg1'] . ', output: U', $print_data_to_stdout, 'POLLER');
|
|
}
|
|
} elseif (!is_numeric($output)) {
|
|
if (prepare_validate_result($output) == false) {
|
|
$error_ds[$ds] = $ds;
|
|
|
|
if (read_config_option('spine_log_level') == 2) {
|
|
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SERVER: " . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER');
|
|
}
|
|
}
|
|
}
|
|
|
|
$total_time = (microtime(true) - $thread_start) * 1000;
|
|
|
|
cacti_log("Device[$host_id] DS[$ds] TT[" . round($total_time, 2) . '] SERVER: ' . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
default:
|
|
$error_ds[$ds] = $ds;
|
|
|
|
cacti_log("Device[$host_id] DS[$ds] ERROR: Invalid polling option: " . $item['action'], $print_data_to_stdout, 'POLLER');
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
function ping_and_reindex_check(array &$item, bool $mibs, int $script_timeout) : bool {
|
|
global $poller_id, $print_data_to_stdout, $set_spike_kill, $poller_db_cnn_id, $pipes, $cactiphp, $using_proc_function;
|
|
|
|
$ping = new Net_Ping;
|
|
$host_id = $item['host_id'];
|
|
|
|
$host = db_fetch_row_prepared('SELECT hostname, ping_port, ping_method, ping_retries, ping_timeout, availability_method
|
|
FROM host
|
|
WHERE id = ?',
|
|
[$host_id]
|
|
);
|
|
|
|
$ping->host = $item;
|
|
$ping->port = $host['ping_port'];
|
|
|
|
// perform the appropriate ping check of the host
|
|
if ($ping->ping($host['availability_method'], $host['ping_method'], $host['ping_timeout'], $host['ping_retries'])) {
|
|
$host_down = false;
|
|
|
|
update_host_status(HOST_UP, $host_id, $ping, $host['availability_method'], $print_data_to_stdout);
|
|
|
|
cacti_log("Device[$host_id] STATUS: Device '" . $item['hostname'] . "' is UP.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
|
|
|
|
if ($mibs && $host['availability_method'] != 0 && $host['availability_method'] != 3) {
|
|
update_system_mibs($host_id);
|
|
}
|
|
} else {
|
|
$host_down = true;
|
|
update_host_status(HOST_DOWN, $host_id, $ping, $host['availability_method'], $print_data_to_stdout);
|
|
|
|
cacti_log("Device[$host_id] STATUS: Device '" . $item['hostname'] . "' is Down.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
|
|
}
|
|
|
|
if ($host_down == false) {
|
|
// do the reindex check for this host
|
|
$reindex = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' pr.data_query_id, pr.action,
|
|
pr.op, pr.assert_value, pr.arg1
|
|
FROM poller_reindex AS pr
|
|
WHERE pr.host_id=?',
|
|
[$item['host_id']]
|
|
);
|
|
|
|
if (cacti_sizeof($reindex)) {
|
|
cacti_log("Device[$host_id] RECACHE: Processing " . cacti_sizeof($reindex) . " items in the auto reindex cache for '" . $item['hostname'] . "'.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
|
|
|
|
foreach ($reindex as $index_item) {
|
|
$assert_fail = false;
|
|
|
|
switch ($index_item['action']) {
|
|
case POLLER_ACTION_SNMP:
|
|
$session = open_snmp_session($host_id, $item);
|
|
|
|
if ($session !== false) {
|
|
if (trim($index_item['arg1']) == '.1.3.6.1.2.1.1.3.0') {
|
|
$engine_time = cacti_snmp_session_get($session, '.1.3.6.1.6.3.10.2.1.3.0');
|
|
$system_uptime = cacti_snmp_session_get($session, $index_item['arg1']);
|
|
|
|
// spine always prefers a numeric, non-wall-clock engine time when
|
|
// re-checking this assert, with no magnitude comparison of its own;
|
|
// match that here so the stored baseline and this live re-check
|
|
// can't permanently disagree
|
|
$output = cacti_snmp_select_uptime($system_uptime, $engine_time, null, true);
|
|
|
|
if ($output === false) {
|
|
$output = 'U';
|
|
}
|
|
} else {
|
|
$output = cacti_snmp_session_get($session, $index_item['arg1']);
|
|
|
|
if ($output === false) {
|
|
$output = 'U';
|
|
}
|
|
}
|
|
} else {
|
|
$output = 'U';
|
|
}
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE OID: ' . $index_item['arg1'] . ', (assert:' . $index_item['assert_value'] . ' ' . $index_item['op'] . ' output:' . $output . ')', $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SCRIPT:
|
|
$output = trim(exec_poll($index_item['arg1'], $script_timeout));
|
|
|
|
if (!is_numeric($output)) {
|
|
if (prepare_validate_result($output) == false) {
|
|
if (strlen($output) > 20) {
|
|
$strout = 20;
|
|
} else {
|
|
$strout = strlen($output);
|
|
}
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE Warning: Result from Script not valid. Partial Result: ' . substr($output, 0, $strout), $print_data_to_stdout, 'POLLER');
|
|
}
|
|
}
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE SCRIPT: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SCRIPT_PHP:
|
|
$output = trim(str_replace("\n", '', exec_poll_php($index_item['arg1'], $using_proc_function, $pipes, $cactiphp)));
|
|
|
|
if (!is_numeric($output)) {
|
|
if (prepare_validate_result($output) == false) {
|
|
if (strlen($output) > 20) {
|
|
$strout = 20;
|
|
} else {
|
|
$strout = strlen($output);
|
|
}
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE WARNING: Result from Script Server not valid. Partial Result: ' . substr($output, 0, $strout), $print_data_to_stdout, 'POLLER');
|
|
}
|
|
}
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE SERVER: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SNMP_COUNT:
|
|
$session = open_snmp_session($host_id, $item);
|
|
|
|
if ($session !== false) {
|
|
$output = cacti_sizeof(cacti_snmp_session_walk($session, $index_item['arg1']));
|
|
} else {
|
|
$output = 'U';
|
|
}
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE OID COUNT: ' . $index_item['arg1'] . ', output: ' . $output, $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SCRIPT_COUNT:
|
|
// count items found
|
|
$script_index_array = exec_into_array($index_item['arg1']);
|
|
$output = cacti_sizeof($script_index_array);
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE CMD COUNT: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
case POLLER_ACTION_SCRIPT_PHP_COUNT:
|
|
$output = exec_into_array($index_item['arg1']);
|
|
$output = cacti_sizeof($output);
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE SERVER COUNT: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
|
|
|
|
break;
|
|
default:
|
|
$output = '';
|
|
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE ERROR: Invalid reindex option: ' . $index_item['action'], $print_data_to_stdout, 'POLLER');
|
|
}
|
|
|
|
/* assert the result with the expected value in the
|
|
* db; recache if the assert fails
|
|
*/
|
|
// TODO: remove magic ":" from poller_command["command"]; this may interfere with scripts
|
|
if (($index_item['op'] == '=') && ($index_item['assert_value'] != trim($output))) {
|
|
cacti_log("Device[$host_id] HT[1] DQ[" . $index_item['data_query_id'] . "] RECACHE ASSERT FAILED '" . $index_item['assert_value'] . '=' . trim($output), $print_data_to_stdout, 'POLLER');
|
|
|
|
db_execute_prepared('REPLACE INTO poller_command
|
|
(poller_id, time, action, command)
|
|
VALUES (?, NOW(), ?, ?)',
|
|
[$poller_id, POLLER_COMMAND_REINDEX, $item['host_id'] . ':' . $index_item['data_query_id']],
|
|
true,
|
|
$poller_db_cnn_id
|
|
);
|
|
|
|
$assert_fail = true;
|
|
} elseif (($index_item['op'] == '>') && ($index_item['assert_value'] < trim($output))) {
|
|
cacti_log("Device[$host_id] HT[1] DQ[" . $index_item['data_query_id'] . "] RECACHE ASSERT FAILED '" . $index_item['assert_value'] . '>' . trim($output), $print_data_to_stdout, 'POLLER');
|
|
|
|
db_execute_prepared('REPLACE INTO poller_command
|
|
(poller_id, time, action, command)
|
|
VALUES (?, NOW(), ?, ?)',
|
|
[$poller_id, POLLER_COMMAND_REINDEX, $item['host_id'] . ':' . $index_item['data_query_id']],
|
|
true,
|
|
$poller_db_cnn_id
|
|
);
|
|
|
|
$assert_fail = true;
|
|
} elseif (($index_item['op'] == '<') && ($index_item['assert_value'] > trim($output))) {
|
|
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . "] RECACHE ASSERT FAILED '" . $index_item['assert_value'] . '<' . trim($output), $print_data_to_stdout, 'POLLER');
|
|
|
|
db_execute_prepared('REPLACE INTO poller_command
|
|
(poller_id, time, action, command)
|
|
VALUES (?, NOW(), ?, ?)',
|
|
[$poller_id, POLLER_COMMAND_REINDEX, $item['host_id'] . ':' . $index_item['data_query_id']],
|
|
true,
|
|
$poller_db_cnn_id
|
|
);
|
|
|
|
$assert_fail = true;
|
|
}
|
|
|
|
/* update 'poller_reindex' with the correct information if:
|
|
* 1) the assert fails
|
|
* 2) the OP code is > or < meaning the current value could have changed without causing
|
|
* the assert to fail */
|
|
if (($assert_fail == true) || ($index_item['op'] == '>') || ($index_item['op'] == '<')) {
|
|
db_execute_prepared('UPDATE poller_reindex
|
|
SET assert_value = ?
|
|
WHERE host_id = ?
|
|
AND data_query_id = ?
|
|
AND arg1 = ?',
|
|
[$output, $host_id, $index_item['data_query_id'], $index_item['arg1']]
|
|
);
|
|
|
|
// spike kill logic
|
|
if (($assert_fail) &&
|
|
(($index_item['op'] == '<') || ($index_item['arg1'] == '.1.3.6.1.2.1.1.3.0' || $index_item['arg1'] == '.1.3.6.1.6.3.10.2.1.3.0'))) {
|
|
// don't spike kill unless we are certain
|
|
if (!empty($output)) {
|
|
$set_spike_kill = true;
|
|
|
|
cacti_log("Device[$host_id] NOTICE: Spike Kill in Effect for '" . $item['hostname'] . "'.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $host_down;
|
|
}
|
|
|
|
function get_max_column_width() : int {
|
|
$pmax = $bmax = 512;
|
|
|
|
$pcol_data = db_fetch_row("SHOW COLUMNS FROM poller_output WHERE Field='output'");
|
|
$bcol_data = db_fetch_row("SHOW COLUMNS FROM poller_output_boost WHERE Field='output'");
|
|
|
|
if (isset($pcol_data['Type'])) {
|
|
$pcol = $pcol_data['Type'];
|
|
$data = explode('(', $pcol);
|
|
$pmax = trim($data[1], ')');
|
|
}
|
|
|
|
if (cacti_sizeof($bcol_data)) {
|
|
$bcol = $bcol_data['Type'];
|
|
$data = explode('(', $bcol);
|
|
$bmax = trim($data[1], ')');
|
|
}
|
|
|
|
return min($pmax, $bmax);
|
|
}
|
|
|
|
function sig_handler(int $signo) : void {
|
|
switch ($signo) {
|
|
case SIGTERM:
|
|
case SIGINT:
|
|
cacti_log('WARNING: Cacti Poller process terminated by user', true, 'POLLER');
|
|
|
|
// record the process as having completed
|
|
record_cmdphp_done();
|
|
|
|
db_close();
|
|
|
|
exit;
|
|
default:
|
|
// ignore all other signals
|
|
}
|
|
}
|
|
|
|
function display_version() : void {
|
|
$version = get_cacti_cli_version();
|
|
print "Cacti Legacy Host Data Collector, 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: cmd.php --first=ID --last=ID [--poller=ID] [--mibs] [--debug]' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Cacti\'s legacy data collector. This data collector is called by poller.php' . PHP_EOL;
|
|
print 'every poller interval to gather information from devices. It is recommended' . PHP_EOL;
|
|
print 'that every system deploy spine instead of cmd.php in production due to the built' . PHP_EOL;
|
|
print 'in scalability limits of cmd.php.' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Required' . PHP_EOL;
|
|
print ' --first - First host id in the range to collect from.' . PHP_EOL;
|
|
print ' --last - Last host id in the range to collect from.' . PHP_EOL . PHP_EOL;
|
|
|
|
print 'Optional:' . PHP_EOL;
|
|
print ' --poller - The poller to run as. Defaults to the system poller.' . PHP_EOL;
|
|
print ' --mode - The poller mode, either online, offline, or recovery.' . PHP_EOL;
|
|
print ' --mibs - Refresh all system mibs from hosts supporting snmp.' . PHP_EOL;
|
|
print ' --debug - Display verbose output during execution.' . PHP_EOL . PHP_EOL;
|
|
}
|