Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Future-Proof db-cleanup on device deletion #592

Merged
merged 3 commits into from Mar 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion html/pages/delhost.inc.php
Expand Up @@ -23,7 +23,7 @@
');
if ($_REQUEST['confirm'])
{
print_message(delete_device(mres($_REQUEST['id']))."\n");
print_message(nl2br(delete_device(mres($_REQUEST['id'])))."\n");
}
else
{
Expand Down
21 changes: 11 additions & 10 deletions includes/functions.php
Expand Up @@ -236,7 +236,7 @@ function renamehost($id, $new, $source = 'console')

function delete_device($id)
{
global $config;
global $config, $debug;
$ret = '';

$host = dbFetchCell("SELECT hostname FROM devices WHERE device_id = ?", array($id));
Expand All @@ -249,19 +249,20 @@ function delete_device($id)
$ret .= "Removed interface $int_id ($int_if)\n";
}

dbDelete('devices', "`device_id` = ?", array($id));

$device_tables = array('entPhysical', 'devices_attribs', 'devices_perms', 'bgpPeers', 'vlans', 'vrfs', 'storage', 'alerts', 'eventlog',
'syslog', 'ports', 'services', 'toner', 'frequency', 'current', 'sensors','ciscoASA');

foreach ($device_tables as $table)
{
dbDelete($table, "`device_id` = ?", array($id));
$fields = array('device_id','host');
foreach( $fields as $field ) {
foreach( dbFetch("SELECT table_name FROM information_schema.columns WHERE table_schema = ? AND column_name = ?",array($config['db_name'],$field)) as $table ) {
$table = $table['table_name'];
$entries = (int) dbDelete($table, "`$field` = ?", array($id));
if( $entries > 0 && $debug === true ) {
$ret .= "$field@$table = #$entries\n";
}
}
}

shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host");

$ret = "Removed device $host";
$ret .= "Removed device $host\n";
return $ret;
}

Expand Down