Skip to content

Commit

Permalink
Fixing #5825 - Allow remove_device.php to use a list of device ids
Browse files Browse the repository at this point in the history
* Allow remove_device.php to use a list of device id's
  • Loading branch information
TheWitness committed Sep 17, 2024
1 parent bb032e8 commit 47bf41b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Cacti CHANGELOG
-feature#5784: Provide a means by which a user can track Cacti connections and disconnects from the php error_log
-feature#5796: Due to Conflicting Interpretations of TCP Ping, Introduce new Ping Method
-feature#5819: Be more precise on the Graph End time when an end time is not provided
-feature#5825: Allow remove_device.php to use a list of device id's
-feature: Update jQuery to 3.7.1
-feature: Update jQueryUI to 1.14.0
-feature: Update billboard.js to 3.13.0
Expand Down
100 changes: 65 additions & 35 deletions cli/remove_device.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
array_shift($parms);

if (cacti_sizeof($parms)) {

/* setup defaults */
$description = '';
$ip = '';
$host_id = '';
$id_ids = false;

$quietMode = false;
$confirm = false;
Expand Down Expand Up @@ -80,6 +80,16 @@
case '--ip':
$ip = trim($value);

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

if (strpos($id, ',') !== false) {
$ids_id = explode(',', $id);
} else {
$ids_id = array($id);
}

break;
case '--version':
case '-V':
Expand All @@ -96,7 +106,7 @@

break;
default:
print "ERROR: Invalid Argument: ($arg)\n\n";
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL;
display_help();
exit(1);
}
Expand All @@ -105,74 +115,86 @@
/* process the various lists into validation arrays */
$hosts = getHostsByDescription();
$addresses = getAddresses();
$ids_host = array();
$ids_host = array();
$ids_ip = array();

/* process host description */
if ($description > '') {
if ($debug) {
print "Searching hosts by description...\n";
print "Searching hosts by description..." . PHP_EOL;
}

$ids_host = preg_array_key_match("/$description/", $hosts);
if (cacti_sizeof($ids_host) == 0) {
print "ERROR: Unable to find host in the database matching description ($description)\n";
print "ERROR: Unable to find host in the database matching description ($description)" . PHP_EOL;
exit(1);
}
}

if ($ip > '') {
if ($ip != '') {
if ($debug) {
print "Searching hosts by IP...\n";
print "Searching hosts by IP..." . PHP_EOL;
}

$ids_ip = preg_array_key_match("/$ip/", $addresses);
if (cacti_sizeof($ids_ip) == 0) {
print "ERROR: Unable to find host in the database matching IP ($ip)\n";
print "ERROR: Unable to find host in the database matching IP ($ip)" . PHP_EOL;
exit(1);
}
}

if (cacti_sizeof($ids_host) == 0 && cacti_sizeof($ids_ip) == 0) {
print "ERROR: No matches found, was IP or Description set properly?\n";
print "ERROR: No matches found, was IP or Description set properly?" . PHP_EOL;
exit(1);
}

$ids = array_merge($ids_host, $ids_ip);
$ids = array_unique($ids, SORT_NUMERIC);

$ids_sql = implode(',',$ids);
if ($ids_id !== false) {
$ids = array_merge($ids, $ids_id);
$ids = array_unique($ids, SORT_NUMERIC);
}

$ids_sql = implode(',', $ids);
if ($debug) {
print "Finding devices with ids $ids_sql\n\n";
print "Finding devices with ids $ids_sql" . PHP_EOL;
}

$hosts = db_fetch_assoc("SELECT id, hostname, description FROM host WHERE id IN ($ids_sql) ORDER by description");
$hosts = db_fetch_assoc("SELECT id, hostname, description
FROM host
WHERE id IN ($ids_sql)
ORDER BY description");

$ids_found = array();
if (!$quiet) {
printf("%8.s | %30.s | %30.s\n",'id','host','description');
printf('%8.s | %30.s | %30.s' . PHP_EOL, 'id', 'host', 'description');

foreach ($hosts as $host) {
printf("%8.d | %30.s | %30.s\n",$host['id'],$host['hostname'],$host['description']);
printf('%8.d | %30.s | %30.s' . PHP_EOL,$host['id'],$host['hostname'],$host['description']);
$ids_found[] = $host['id'];
}
print "\n";

print PHP_EOL;
}

if ($confirm) {
$ids_confirm = implode(', ',$ids_found);
$ids_confirm = implode(', ', $ids_found);
if (!$quiet) {
print "Removing devices with ids: $ids_confirm\n";
print "Removing devices with ids: $ids_confirm" . PHP_EOL;
}

$host_id = api_device_remove_multi($ids);

if (is_error_message()) {
print "ERROR: Failed to remove devices\n";
print "ERROR: Failed to remove devices" . PHP_EOL;
exit(1);
} else {
print "Success - removed device-ids: $ids_confirm\n";
print "Success - removed device-ids: $ids_confirm" . PHP_EOL;
exit(0);
}
} else {
print "Please use --confirm to remove these devices\n";
print "Please use --confirm to remove these devices" . PHP_EOL;
}
} else {
display_help();
Expand All @@ -182,27 +204,33 @@
/* display_version - displays version information */
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Remove Device Utility, Version $version, " . COPYRIGHT_YEARS . "\n";
print "Cacti Remove Device Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}

function display_help() {
display_version();

print "\nusage: remove_device.php --description=[description] --ip=[IP]\n";
print " [--confirm] [--quiet]\n\n";
print "Required:\n";
print " --description the name that will be displayed by Cacti in the graphs\n";
print " --ip self explanatory (can also be a FQDN)\n";
print " (either one or both fields can be used and may be regex)\n\n";
print "Optional:\n";
print " -confirm confirms that you wish to remove matches\n\n";
print "List Options:\n";
print " --quiet - batch mode value return\n\n";
print PHP_EOL;
print 'usage: remove_device.php --description=\'S\' | --ip=\'S\' | --id=N,N,N,...' . PHP_EOL;
print ' [--confirm] [--quiet]' . PHP_EOL . PHP_EOL;

print 'Required: (on or more)' . PHP_EOL;
print " --description='S' A substring or regular expression of the hostname or description." . PHP_EOL;
print " --ip='S' A IP or hostname (can also be a FQDN)." . PHP_EOL;
print ' --id=N,N,... A column delimited list of device ids.' . PHP_EOL . PHP_EOL;

print ' (both --description and --ip can be a regex)' . PHP_EOL . PHP_EOL;
print 'Optional:' . PHP_EOL;
print ' --confirm confirms that you wish to remove matches' . PHP_EOL . PHP_EOL;

print 'List Options:' . PHP_EOL;
print ' --quiet batch mode value return' . PHP_EOL . PHP_EOL;
}

function preg_array_key_match($needle, $haystack) {
global $debug;
$matches = array ();

$matches = array();

if (isset($haystack)) {
if (!is_array($haystack)) {
Expand All @@ -213,21 +241,23 @@ function preg_array_key_match($needle, $haystack) {
}

if ($debug) {
print "Attempting to match against '$needle' against ".cacti_sizeof($haystack)." entries\n";
print "Attempting to match against '$needle' against " . cacti_sizeof($haystack) . " entries" . PHP_EOL;
}

foreach ($haystack as $str => $value) {
if ($debug) {
print " - Key $str => Value $value\n";
print " - Key $str => Value $value" . PHP_EOL;
}

if (preg_match ($needle, $str, $m)) {
if ($debug) {
print " + $str: $value\n";
print " + $str: $value" . PHP_EOL;
}

$matches[] = $value;
}
}

return $matches;
}

0 comments on commit 47bf41b

Please sign in to comment.