Skip to content

Commit

Permalink
Porting #5279 - Rebuild poller cache does not work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWitness committed Apr 1, 2023
1 parent 75298d2 commit bf4d69e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 53 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Expand Up @@ -57,6 +57,9 @@ Cacti CHANGELOG
-feature: Upgrade billboard.js to version 3.7.4
-feature: Upgrade d3.js to version 7.8.2

1.2.25
-issue#5279: Rebuild poller cache does not work as expected

1.2.24
-issue#5127: Importing Local_Linux_Machine.xml.gz template raises PHP error
-issue#5134: Display of "Memory Limit" on Boost Status page shows -1 and not "Unlimited"
Expand Down
108 changes: 55 additions & 53 deletions cli/rebuild_poller_cache.php
Expand Up @@ -29,39 +29,36 @@

if ($config['poller_id'] > 1) {
print 'FATAL: This utility is designed for the main Data Collector only' . PHP_EOL;

exit(1);
}

/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);

$debug = false;
$host_id = 0;
$debug = false;
$host_id = 0;
$host_template_id = 0;

if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$arg = $parameter;
$value = '';
}

switch ($arg) {
case '-d':
case '--debug':
$debug = true;

break;
case '--host-id':
$host_id = trim($value);

if (!is_numeric($host_id)) {
print 'ERROR: You must supply a valid device id to run this script!' . PHP_EOL;

exit(1);
}

Expand All @@ -71,7 +68,6 @@

if (!is_numeric($host_id)) {
print 'ERROR: You must supply a valid device template id to run this script!' . PHP_EOL;

exit(1);
}

Expand All @@ -80,19 +76,15 @@
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);
}
}
Expand All @@ -109,56 +101,66 @@

if ($host_id > 0) {
$sql_where = 'WHERE dl.host_id = ?';
$params[] = $host_id;
$params[] = $host_id;
}

if ($host_template_id > 0) {
$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . ' h.host_template_id = ?';
$params[] = $host_template_id;
}

/* get the data_local Id's for the poller cache */
$poller_data = db_fetch_assoc_prepared("SELECT dl.*
FROM data_local AS dl
INNER JOIN host AS h
ON dl.host_id = h.id
$sql_where",
$params);

/* initialize some variables */
$current_ds = 1;
$total_ds = cacti_sizeof($poller_data);

/* setting local_data_ids to an empty array saves time during updates */
$local_data_ids = array();
$poller_items = array();

/* issue warnings and start message if applicable */
print 'WARNING: Do not interrupt this script. Rebuilding the Poller Cache can take quite some time' . PHP_EOL;
debug("There are '" . cacti_sizeof($poller_data) . "' data source elements to update.");

/* start rebuilding the poller cache */
if (cacti_sizeof($poller_data)) {
if (!$debug) {
$tcount = 0;
print '\n';
}

foreach ($poller_data as $data) {
if (!$debug) {
$tcount++;
print CLI_CSI . CLI_EL_WHOLE . CLI_CR . "$tcount / " . count($poller_data) .
' (' . round($tcount / count($poller_data) * 100,1) . '%)';
$pollers = array_rekey(
db_fetch_assoc('SELECT DISTINCT poller_id
FROM host
WHERE disabled = ""'),
'poller_id', 'poller_id'
);

if (cacti_sizeof($pollers)) {
print 'NOTE: Do not interrupt this script. Rebuilding the Poller Cache can take quite some time' . PHP_EOL;

foreach($pollers as $poller_id) {
/* get the hosts for this poller_id */
$hosts = array_rekey(
db_fetch_assoc_prepared("SELECT DISTINCT dl.host_id
FROM data_local AS dl
INNER JOIN host AS h
ON dl.host_id = h.id
$sql_where
AND poller_id = $poller_id
ORDER BY dl.host_id",
$params),
'host_id', 'host_id'
);

/* add special host 0 */
if ($poller_id == 1) {
$hosts[0] = 0;
}
$local_data_ids[] = $data['id'];
$poller_items = array_merge($poller_items, update_poller_cache($data));

debug("Data Source Item '$current_ds' of '$total_ds' updated");
$current_ds++;
}
/* issue warnings and start message if applicable */
debug("There are '" . cacti_sizeof($hosts) . "' Devices to rebuild poller items for on poller $poller_id.");

/* start rebuilding the poller cache */
if (cacti_sizeof($hosts)) {
foreach ($hosts as $host_id) {
if ($host_id > 0) {
$description = db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($host_id));
} else {
$description = 'Special Host Zero';
}

$data_sources = db_fetch_cell_prepared('SELECT COUNT(*) FROM data_local WHERE host_id = ?', array($host_id));

if (cacti_sizeof($local_data_ids)) {
poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
if ($data_sources > 0) {
print "NOTE: Processing Device:'$description' on Poller:'$poller_id' having $data_sources Data Sources." . PHP_EOL;
push_out_host($host_id);
} else {
print "NOTE: Removing Poller Cache Items for Device:'$description' on Poller:'$poller_id' as it has no Data Sources." . PHP_EOL;
db_execute_prepared('DELETE FROM poller_item WHERE host_id = ?', array($host_id));
}
}
}
}
}

Expand All @@ -176,7 +178,7 @@ function display_version() {
}

/* display_help - displays the usage of the function */
function display_help() {
function display_help () {
display_version();

print PHP_EOL . 'usage: rebuild_poller_cache.php [--host-id=ID] [--debug]' . PHP_EOL . PHP_EOL;
Expand Down

0 comments on commit bf4d69e

Please sign in to comment.