Skip to content

Commit

Permalink
Fixing #4479 - Updated Repair tools
Browse files Browse the repository at this point in the history
As a repair tool, the rebuild poller cache scripts lacks critical options to reduce runtimes
  • Loading branch information
TheWitness committed Nov 27, 2021
1 parent ca0a12b commit da8530c
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Cacti CHANGELOG
-issue#4460: Polling does not complete as expected on larger systems
-issue#4472: Utilities view has calculation errors when there are no data sources
-issue#4477: Remote pollers do not force connection when online
-issue#4479: As a repair tool, the rebuild poller cache scripts lacks critical options to reduce runtimes

1.2.19
-security#4356: Further fixes for grave character security protection
Expand Down
169 changes: 169 additions & 0 deletions cli/push_out_hosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2021 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/ |
+-------------------------------------------------------------------------+
*/

include(dirname(__FILE__) . '/../include/cli_check.php');
include_once($config['base_path'] . '/lib/utility.php');
include_once($config['base_path'] . '/lib/api_data_source.php');
include_once($config['base_path'] . '/lib/poller.php');

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

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

foreach($parms as $parameter) {
@list($arg, $value) = @explode('=', $parameter);

switch ($arg) {
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);
}

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

if (!is_numeric($host_template_id)) {
print 'ERROR: You must supply a valid Device Template Id to run this script!' . PHP_EOL;
exit(1);
}

break;
case '--data-template-id':
$data_template_id = trim($value);

if (!is_numeric($data_template_id)) {
print 'ERROR: You must supply a valid Data Template Id to run this script!' . PHP_EOL;
exit(1);
}

break;
case '-d':
case '--debug':
$debug = true;

break;
case '-h':
case '-H':
case '--help':
display_help();

exit;
case '-v':
case '-V':
case '--version':
display_version();

exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;

display_help();
exit;
}
}

/* obtain timeout settings */
$max_execution = ini_get('max_execution_time');
$max_memory = ini_get('memory_limit');

/* set new timeout and memory settings */
ini_set('max_execution_time', '0');
ini_set('memory_limit', '-1');

$sql_where = '';
$params = array();

if ($host_id > 0) {
$sql_where = ' AND h.id = ?';
$params[] = $host_id;
}

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

/* clear the poller cache first */
$hosts = db_fetch_assoc_prepared("SELECT h.id
FROM host AS h
WHERE h.disabled = ''
$sql_where",
$params);

/* initialize some variables */
$current_host = 1;
$total_hosts = sizeof($hosts);

/* 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 '$total_hosts' hosts to push out.");

/* start rebuilding the poller cache */
if (cacti_sizeof($hosts) > 0) {
foreach ($hosts as $host) {
if (!$debug) print '.';
push_out_host($host['id'], 0, $data_template_id);
debug("Host ID '" . $host['id'] . "' or '$current_host' of '$total_hosts' updated");
$current_host++;
}
}
if (!$debug) {
print PHP_EOL;
}

/* display_version - displays version information */
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Push Out Host Poller Cache Script, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}

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

print PHP_EOL . 'usage: push_out_hosts.php [--host-id=N] [--host-template-id=N] [--debug]' . PHP_EOL . PHP_EOL;

print 'Optional:' . PHP_EOL;
print ' --host-id=N - Run for a specific Device' . PHP_EOL;
print ' --host-template-id=N - Run for a specific Device Template' . PHP_EOL;
print ' --data-template-id=N - Run for a specific Data Template' . PHP_EOL;
print ' --debug - Display verbose output during execution' . PHP_EOL . PHP_EOL;
}

function debug($message) {
global $debug;

if ($debug) {
print('DEBUG: ' . trim($message) . PHP_EOL);
}
}
59 changes: 44 additions & 15 deletions cli/rebuild_poller_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

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

if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
Expand All @@ -52,7 +53,16 @@
$host_id = trim($value);

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

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

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 @@ -68,7 +78,7 @@
display_help();
exit(0);
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
Expand All @@ -81,13 +91,27 @@
/* set new timeout */
ini_set('max_execution_time', '0');

/* get the data_local Id's for the poller cache */
$sql_where = '';
$params = array();

if ($host_id > 0) {
$poller_data = db_fetch_assoc('SELECT * FROM data_local WHERE host_id=' . $host_id);
} else {
$poller_data = db_fetch_assoc('SELECT * FROM data_local');
$sql_where = 'WHERE dl.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);
Expand All @@ -97,7 +121,7 @@
$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\n";
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 */
Expand All @@ -115,27 +139,32 @@
poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
}
}
if (!$debug) print "\n";
if (!$debug) {
print PHP_EOL;
}

/* poller cache rebuilt, restore runtime parameters */
ini_set('max_execution_time', $max_execution);

/* display_version - displays version information */
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Rebuild Poller Cache Utility, Version $version, " . COPYRIGHT_YEARS . "\n";
print "Cacti Rebuild Poller Cache Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}

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

print "\nusage: rebuild_poller_cache.php [--host-id=ID] [--debug]\n\n";
print "A utility to repopulate Cacti's poller cache for a host or a system. Note: That when performing\n";
print "for an entire Cacti system, expecially a large one, this may take some time.\n\n";
print "Optional:\n";
print " --host-id=ID - Limit the repopulation to a single Cacti Device\n";
print " --debug - Display verbose output during execution\n\n";
print PHP_EOL . 'usage: rebuild_poller_cache.php [--host-id=ID] [--debug]' . PHP_EOL . PHP_EOL;

print 'A utility to repopulate Cacti\'s poller cache for a host or a system. Note: That when performing' . PHP_EOL;
print 'for an entire Cacti system, expecially a large one, this may take some time.' . PHP_EOL . PHP_EOL;

print 'Optional:' . PHP_EOL;
print ' --host-id=ID - Limit the repopulation to a single Device' . PHP_EOL;
print ' --host-template-id=ID - Limit the repopulation to a single Device Template' . PHP_EOL;
print ' --debug - Display verbose output during execution' . PHP_EOL . PHP_EOL;
}

function debug($message) {
Expand Down

0 comments on commit da8530c

Please sign in to comment.