From ba28732c3bd00a1194a094acf42f5f1f75461a28 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Fri, 25 May 2012 10:34:01 +0000 Subject: [PATCH] ports sort done in php git-svn-id: http://www.observium.org/svn/observer/trunk@3238 61d68cd4-352d-0410-923a-c4978735b2b8 --- html/includes/functions.inc.php | 29 ++++++++++ html/pages/ports.inc.php | 95 +++++++++++++++++++-------------- html/pages/ports/list.inc.php | 2 +- includes/functions.php | 45 +++++++++++----- includes/polling/os.inc.php | 2 +- 5 files changed, 117 insertions(+), 56 deletions(-) diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index 156e747cb..20a9cfcbd 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -1,5 +1,17 @@ + * @copyright (C) 2006 - 2012 Adam Armstrong + * + */ + include("../includes/alerts.inc.php"); function data_uri($file, $mime) @@ -9,6 +21,23 @@ function data_uri($file, $mime) return ('data:' . $mime . ';base64,' . $base64); } +function nicecase($item) +{ + switch ($item) + { + case "dbm": + return "dBm"; + case "mysql": + return" MySQL"; + case "powerdns": + return "PowerDNS"; + case "bind": + return "BIND"; + default: + return ucfirst($item); + } +} + function toner2colour($descr, $percent) { $colour = get_percentage_colours(100-$percent); diff --git a/html/pages/ports.inc.php b/html/pages/ports.inc.php index d46ac5086..04a4d0fe7 100644 --- a/html/pages/ports.inc.php +++ b/html/pages/ports.inc.php @@ -298,46 +298,6 @@ foreach ($vars as $var => $value) } } -switch ($vars['sort']) -{ - case 'traffic': - $query_sort = " ORDER BY (I.ifInOctets_rate+I.ifOutOctets_rate) DESC"; - break; - case 'traffic_in': - $query_sort = " ORDER BY I.ifInOctets_rate DESC"; - break; - case 'traffic_out': - $query_sort = " ORDER BY I.ifOutOctets_rate DESC"; - break; - case 'packets': - $query_sort = " ORDER BY (I.ifInUcastPkts_rate+I.ifOutUcastPkts_rate) DESC"; - break; - case 'packets_in': - $query_sort = " ORDER BY I.ifInUcastPkts_rate DESC"; - break; - case 'packets_out': - $query_sort = " ORDER BY I.ifOutUcastPkts_rate DESC"; - break; - case 'errors': - $query_sort = " ORDER BY (I.ifInErrors + I.ifOutErrors) DESC"; - break; - case 'speed': - $query_sort = " ORDER BY (I.ifSpeed) DESC"; - break; - case 'port': - $query_sort = " ORDER BY (I.ifDescr) ASC"; - break; - case 'media': - $query_sort = " ORDER BY (I.ifType) ASC"; - break; - case 'descr': - $query_sort = " ORDER BY (I.ifAlias) ASC"; - break; - case 'device': - default: - $query_sort = " ORDER BY D.hostname, I.ifIndex ASC"; -} - $query = "SELECT * FROM `ports` AS I, `devices` AS D WHERE I.device_id = D.device_id ".$where." ".$query_sort; $row = 1; @@ -346,6 +306,61 @@ list($format, $subformat) = explode("_", $vars['format']); $ports = dbFetchRows($query, $param); +### FIXME - only populate what we need to search at this point, because we shouldn't show *all* ports, as it's silly. + +foreach ($ports as $p) +{ + if ($config['memcached']['enable']) + { + $oids = array('ifInOctets', 'ifOutOctets', 'ifInUcastPkts', 'ifOutUcastPkts', 'ifInErrors', 'ifOutErrors'); + foreach ($oids as $oid) + { + $ports[$port['port_id']][$oid.'_rate'] = $memcache->get('port-'.$port['port_id'].'-'.$oid.'_rate'); + if ($debug) { echo("MC[".$oid."->".$port[$oid.'_rate']."]"); } + } + } +} + +switch ($vars['sort']) +{ + case 'traffic': + $ports = array_sort($ports, 'ifOctets_rate', SORT_DESC); + break; + case 'traffic_in': + $ports = array_sort($ports, 'ifInOctets_rate', SORT_DESC); + break; + case 'traffic_out': + $ports = array_sort($ports, 'ifOutOctets_rate', SORT_DESC); + break; + case 'packets': + $ports = array_sort($ports, 'ifUcastPkts_rate', SORT_DESC); + break; + case 'packets_in': + $ports = array_sort($ports, 'ifInUcastOctets_rate', SORT_DESC); + break; + case 'packets_out': + $ports = array_sort($ports, 'ifOutUcastOctets_rate', SORT_DESC); + break; + case 'errors': + $ports = array_sort($ports, 'ifErrors_rate', SORT_DESC); + break; + case 'speed': + $ports = array_sort($ports, 'ifSpeed', SORT_DESC); + break; + case 'port': + $ports = array_sort($ports, 'ifDescr', SORT_ASC); + break; + case 'media': + $ports = array_sort($ports, 'ifType', SORT_ASC); + break; + case 'descr': + $ports = array_sort($ports, 'ifAlias', SORT_ASC); + break; + case 'device': + default: + $ports = array_sort($ports, 'hostname', SORT_ASC); +} + if(file_exists('pages/ports/'.$format.'.inc.php')) { include('pages/ports/'.$format.'.inc.php'); diff --git a/html/pages/ports/list.inc.php b/html/pages/ports/list.inc.php index 375fabc0c..50e4787eb 100644 --- a/html/pages/ports/list.inc.php +++ b/html/pages/ports/list.inc.php @@ -52,7 +52,7 @@ foreach ($ports as $port) $port = ifLabel($port, $device); echo(" - ".$port['hostname']." + ".generate_device_link($port, shorthost($port['hostname'], "20"))." ".fixIfName($port['label'])." $error_img $speed ".$port['in_rate']." diff --git a/includes/functions.php b/includes/functions.php index 2f80ec4f6..611c8ab4c 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -36,21 +36,38 @@ if ($config['alerts']['email']['enable']) include_once($config['install_dir'] . "/includes/phpmailer/class.smtp.php"); } -function nicecase($item) +function array_sort($array, $on, $order=SORT_ASC) { - switch ($item) - { - case "dbm": - return "dBm"; - case "mysql": - return" MySQL"; - case "powerdns": - return "PowerDNS"; - case "bind": - return "BIND"; - default: - return ucfirst($item); - } + $new_array = array(); + $sortable_array = array(); + + if (count($array) > 0) { + foreach ($array as $k => $v) { + if (is_array($v)) { + foreach ($v as $k2 => $v2) { + if ($k2 == $on) { + $sortable_array[$k] = $v2; + } + } + } else { + $sortable_array[$k] = $v; + } + } + + switch ($order) { + case SORT_ASC: + asort($sortable_array); + break; + case SORT_DESC: + arsort($sortable_array); + break; + } + + foreach ($sortable_array as $k => $v) { + $new_array[$k] = $array[$k]; + } + } + return $new_array; } function mac_clean_to_readable($mac) diff --git a/includes/polling/os.inc.php b/includes/polling/os.inc.php index 4c99a6357..845330cf0 100644 --- a/includes/polling/os.inc.php +++ b/includes/polling/os.inc.php @@ -42,7 +42,7 @@ if ($serial && $serial != $device['serial']) if ($icon && $icon != $device['icon']) { $update_array['icon'] = $icon; - log_event("Icon -> ".nicecase($icon), $device, 'system'); + log_event("Icon -> ".$icon, $device, 'system'); } echo("\nHardware: ".$hardware." Version: ".$version." Features: ".$features." Serial: ".$serial."\n");