From 1f5d5eeecaa4a264fedc7667631af5142de2d443 Mon Sep 17 00:00:00 2001 From: f0o Date: Thu, 5 Mar 2015 22:57:02 +0000 Subject: [PATCH 1/3] Future proof db-cleanup on device deletion. --- includes/functions.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 161757107..a6ef4cd42 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -249,14 +249,11 @@ 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 ) { + dbDelete($table, "`$field` = ?", array($id)); + } } shell_exec("rm -rf ".trim($config['rrd_dir'])."/$host"); From 0f8074afa5f5630f0ade5bfe3a3606998a8746a1 Mon Sep 17 00:00:00 2001 From: f0o Date: Thu, 12 Mar 2015 15:51:40 +0000 Subject: [PATCH 2/3] Added Debug-Info if required. --- includes/functions.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index a6ef4cd42..0f4620cd9 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -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)); @@ -252,13 +252,17 @@ function delete_device($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 ) { - dbDelete($table, "`$field` = ?", array($id)); + $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; } From 33e8d05c1d25c089a304a17a2d70ceec8c7a2bac Mon Sep 17 00:00:00 2001 From: f0o Date: Thu, 12 Mar 2015 15:54:09 +0000 Subject: [PATCH 3/3] Added nl2br to GUI's output of delete_device --- html/pages/delhost.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/pages/delhost.inc.php b/html/pages/delhost.inc.php index 1c89e4297..58b3b89a5 100644 --- a/html/pages/delhost.inc.php +++ b/html/pages/delhost.inc.php @@ -23,7 +23,7 @@ if (is_numeric($_REQUEST['id'])) '); if ($_REQUEST['confirm']) { - print_message(delete_device(mres($_REQUEST['id']))."\n"); + print_message(nl2br(delete_device(mres($_REQUEST['id'])))."\n"); } else {