From 2b5fbd67f2069d051eeb8bc471846e729815414d Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Sat, 24 Apr 2010 23:16:12 +0000 Subject: [PATCH] clean up device/interface removal code (it's still fucking foul, but meh) git-svn-id: http://www.observium.org/svn/observer/trunk@1076 61d68cd4-352d-0410-923a-c4978735b2b8 --- delhost.php | 2 +- html/pages/delhost.php | 4 ++-- html/pages/ports/deleted.inc.php | 8 ++++---- includes/functions.php | 24 ++++++++++++++++-------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/delhost.php b/delhost.php index 9582ce398b..e7fe8df5a0 100755 --- a/delhost.php +++ b/delhost.php @@ -11,7 +11,7 @@ if($argv[1]) { $host = strtolower($argv[1]); $id = getidbyname($host); if($id) { - delHost($id); + delete_device($id); echo("Removed $host\n"); } else { echo("Host doesn't exist!\n"); diff --git a/html/pages/delhost.php b/html/pages/delhost.php index e2927c881b..3bd8d7a063 100644 --- a/html/pages/delhost.php +++ b/html/pages/delhost.php @@ -6,9 +6,9 @@ if($_SESSION['userlevel'] < 10) { } if($_POST['id']) { - delHost(mres($_POST['id'])); + delete_device(mres($_POST['id'])); } elseif (mres($_GET['id'])) { - delHost(mres($_GET['id'])); + delete_device(mres($_GET['id'])); } ?> diff --git a/html/pages/ports/deleted.inc.php b/html/pages/ports/deleted.inc.php index bc16671fde..46ccd2e67f 100644 --- a/html/pages/ports/deleted.inc.php +++ b/html/pages/ports/deleted.inc.php @@ -6,15 +6,15 @@ if($_GET['optb'] == "purge" && $_GET['optc'] == "all") { $query = mysql_query($sql); while($interface = mysql_fetch_assoc($query)) { if(interfacepermitted($interface['interface_id'], $interface['device_id'])){ - mysql_query("DELETE FROM `ports` WHERE `interface_id` = '".$interface['interface_id']."'"); - if(mysql_affected_rows()) { echo("
Deleted ".generatedevicelink($interface)." - ".generateiflink($interface)."
"); } + delete_port($interface['interface_id']); + echo("
Deleted ".generatedevicelink($interface)." - ".generateiflink($interface)."
"); } } } elseif($_GET['optb'] == "purge" && $_GET['optc']) { $interface = mysql_fetch_assoc(mysql_query("SELECT * from `ports` AS P, `devices` AS D WHERE `interface_id` = '".mres($_GET['optc'])."' AND D.device_id = P.device_id")); if(interfacepermitted($interface['interface_id'], $interface['device_id'])) - mysql_query("DELETE FROM `ports` WHERE `interface_id` = '".mres($_GET['optc'])."' AND `deleted` = '1'"); - if(mysql_affected_rows()) { echo("
Deleted ".generatedevicelink($interface)." - ".generateiflink($interface)."
"); } + delete_port($interface['interface_id']); + echo("
Deleted ".generatedevicelink($interface)." - ".generateiflink($interface)."
"); } diff --git a/includes/functions.php b/includes/functions.php index e37b47916b..2787e248bf 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -325,15 +325,9 @@ function renamehost($id, $new) { eventlog("Hostname changed -> $new (console)", $id); } -function delHost($id) +function delete_port($int_id) { - global $config; - $host = mysql_result(mysql_query("SELECT hostname FROM devices WHERE device_id = '$id'"), 0); - mysql_query("DELETE FROM `devices` WHERE `device_id` = '$id'"); - $int_query = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '$id'"); - while($int_data = mysql_fetch_array($int_query)) { - $int_if = $int_data['ifDescr']; - $int_id = $int_data['interface_id']; + $interface = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` AS P, `devices` AS D WHERE P.interface_id = '".$int_id."' AND D.device_id = P.device_id")); mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'"); mysql_query("DELETE from `links` WHERE `local_interface_id` = '$int_id'"); mysql_query("DELETE from `links` WHERE `remote_interface_id` = '$int_id'"); @@ -343,6 +337,20 @@ function delHost($id) mysql_query("DELETE from `mac_accounting` WHERE `interface_id` = '$int_id'"); mysql_query("DELETE FROM `bill_ports` WHERE `port_id` = '$int_id'"); mysql_query("DELETE from `pseudowires` WHERE `interface_id` = '$int_id'"); + mysql_query("DELETE FROM `ports` WHERE `interface_id` = '$int_id'"); + shell_exec("rm -rf ".$config['rrd_dir']."/".$interface['hostname']."/".$interface['ifIndex'].".rrd"); +} + +function delete_device($id) +{ + global $config; + $host = mysql_result(mysql_query("SELECT hostname FROM devices WHERE device_id = '$id'"), 0); + mysql_query("DELETE FROM `devices` WHERE `device_id` = '$id'"); + $int_query = mysql_query("SELECT * FROM `ports` WHERE `device_id` = '$id'"); + while($int_data = mysql_fetch_array($int_query)) { + $int_if = $int_data['ifDescr']; + $int_id = $int_data['interface_id']; + delete_port($int_id); echo("Removed interface $int_id ($int_if)
"); } mysql_query("DELETE FROM `entPhysical` WHERE `device_id` = '$id'");