From 3dc2f650b8d75e28a7059e40ed2eb7de12c6b77e Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Thu, 27 Nov 2008 12:36:37 +0000 Subject: [PATCH] cleanups git-svn-id: http://www.observium.org/svn/observer/trunk@327 61d68cd4-352d-0410-923a-c4978735b2b8 --- html/.htaccess | 2 + html/includes/print-event-short.inc | 10 +- html/pages/device.php | 60 +++--- html/pages/device/{srv.inc => srv.inc.php} | 0 .../{dev-storage.inc => storage.inc.php} | 0 .../device/{dev-syslog.inc => syslog.inc.php} | 0 .../device/{dev-temp.inc => temp.inc.php} | 0 .../device/{dev-vlans.inc => vlans.inc.php} | 0 .../device/{dev-vrfs.inc => vrfs.inc.php} | 0 includes/discovery/interfaces.php | 2 + includes/functions.php | 172 +++++++----------- 11 files changed, 101 insertions(+), 145 deletions(-) rename html/pages/device/{srv.inc => srv.inc.php} (100%) rename html/pages/device/{dev-storage.inc => storage.inc.php} (100%) rename html/pages/device/{dev-syslog.inc => syslog.inc.php} (100%) rename html/pages/device/{dev-temp.inc => temp.inc.php} (100%) rename html/pages/device/{dev-vlans.inc => vlans.inc.php} (100%) rename html/pages/device/{dev-vrfs.inc => vrfs.inc.php} (100%) diff --git a/html/.htaccess b/html/.htaccess index d3a58c4bd..c890caa6f 100644 --- a/html/.htaccess +++ b/html/.htaccess @@ -18,6 +18,8 @@ RewriteRule ^devices/(.+)/ ?page=devices&type=$1 RewriteRule ^interfaces/(.+)/ ?page=interfaces&type=$1 RewriteRule ^bill/([0-9]+) ?page=bills&bill=$1 + +RewriteRule ^device/([0-9]+)/([a-z]+)/ ?page=device&id=$1§ion=$2 RewriteRule ^device/([0-9]+) ?page=device&id=$1 RewriteRule ^([a-z]+)/$ ?page=$1 diff --git a/html/includes/print-event-short.inc b/html/includes/print-event-short.inc index 77355e384..8b27cd0d2 100644 --- a/html/includes/print-event-short.inc +++ b/html/includes/print-event-short.inc @@ -3,9 +3,6 @@ if($bg == $list_colour_a) { $bg = $list_colour_b; } else { $bg=$list_colour_a; } - $hostname = gethostbyid($entry['host']); - $interface = fixifname(getifbyid($entry['interface'])); - unset($icon); $icon = geteventicon($entry['message']); @@ -17,10 +14,11 @@ if($bg == $list_colour_a) { $bg = $list_colour_b; } else { $bg=$list_colour_a; } ".$entry['humandate']." "); - $if['interface_id'] = $entry['interface']; - $if['ifDescr'] = makeshortif($interface); + + $if = mysql_fetch_array(mysql_query("SELECT * from interfaces AS I, devices as D where I.device_id = D.device_id AND I.interface_id = '".$entry['interface']."'")); + echo(" - " . generateiflink($if) . " + " . generateiflink($if, makeshortif($if['ifDescr'])) . " " . htmlspecialchars(truncate($entry['message'], 45)) diff --git a/html/pages/device.php b/html/pages/device.php index d9c260e59..da5f10dd8 100644 --- a/html/pages/device.php +++ b/html/pages/device.php @@ -7,7 +7,7 @@ if(devicepermitted($_GET['id'])) { $selected['iface'] = "selected"; if(!$_GET['section']) { - $_GET['section'] = "dev-overview"; + $_GET['section'] = "overview"; } $section = mres($_GET['section']); $section = str_replace(".", "", $section); @@ -24,16 +24,16 @@ echo("
"); echo(" "); echo("
"); -include("pages/device/$section.inc"); +include("pages/device/".mres($section).".inc.php"); echo("
"); diff --git a/html/pages/device/srv.inc b/html/pages/device/srv.inc.php similarity index 100% rename from html/pages/device/srv.inc rename to html/pages/device/srv.inc.php diff --git a/html/pages/device/dev-storage.inc b/html/pages/device/storage.inc.php similarity index 100% rename from html/pages/device/dev-storage.inc rename to html/pages/device/storage.inc.php diff --git a/html/pages/device/dev-syslog.inc b/html/pages/device/syslog.inc.php similarity index 100% rename from html/pages/device/dev-syslog.inc rename to html/pages/device/syslog.inc.php diff --git a/html/pages/device/dev-temp.inc b/html/pages/device/temp.inc.php similarity index 100% rename from html/pages/device/dev-temp.inc rename to html/pages/device/temp.inc.php diff --git a/html/pages/device/dev-vlans.inc b/html/pages/device/vlans.inc.php similarity index 100% rename from html/pages/device/dev-vlans.inc rename to html/pages/device/vlans.inc.php diff --git a/html/pages/device/dev-vrfs.inc b/html/pages/device/vrfs.inc.php similarity index 100% rename from html/pages/device/dev-vrfs.inc rename to html/pages/device/vrfs.inc.php diff --git a/includes/discovery/interfaces.php b/includes/discovery/interfaces.php index 20d22043b..29d36f506 100755 --- a/includes/discovery/interfaces.php +++ b/includes/discovery/interfaces.php @@ -25,6 +25,7 @@ $ifDescr = trim(str_replace("\"", "", $ifDescr)); } else { $ifDescr = trim(str_replace("\"", "", $ifName)); } + if(!strstr($entry, "irtual")) { $if = trim(strtolower($ifDescr)); $nullintf = 0; @@ -33,6 +34,7 @@ $nullintf = 1; } } + $ifDescr = fixifName($ifDescr); if (preg_match('/serial[0-9]:/', $if)) { $nullintf = '1'; } if (preg_match('/ng[0-9]+$/', $if)) { $nullintf = '1'; } if ($nullintf == 0) { diff --git a/includes/functions.php b/includes/functions.php index 336a8d965..f2c7a21fe 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -14,10 +14,11 @@ include_once($config['install_dir'] . "/includes/print-functions.php"); include_once($config['install_dir'] . "/includes/billing.php"); include_once($config['install_dir'] . "/includes/cisco-entities.php"); include_once($config['install_dir'] . "/includes/syslog.php"); +include_once($config['install_dir'] . "/includes/rewrites.php"); -function mres($string) { - // short function wrapper because the real one is stupidly long and ugly. aestetics. - return mysql_real_escape_string($string); + +function mres($string) { // short function wrapper because the real one is stupidly long and ugly. aestetics. + return mysql_real_escape_string($string); } function validate_hostip($host) { @@ -37,7 +38,6 @@ function write_dev_attrib($device_id, $attrib_type, $attrib_value) { } function shorthost($hostname, $len=16) { - list ($first, $second, $third, $fourth, $fifth) = explode(".", $hostname); $shorthost = $first; if(strlen($first.".".$second) < $len && $second) { @@ -52,9 +52,7 @@ function shorthost($hostname, $len=16) { } } } - return ($shorthost); - } function rrdtool_update($rrdfile, $rrdupdate) { @@ -86,14 +84,11 @@ function strgen ($length = 16) 'E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n', 'N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w', 'W','x','X','y','Y','z','Z'); - - $string = ""; - + $string = ""; for ($i=0; $i<$length; $i++) { $key = mt_rand(0,61); $string .= $entropy[$key]; } - return $string; } @@ -157,14 +152,16 @@ function format_si($rate) return round($rate, $round[$i]).$ext; } -function format_bi($size) { +function format_bi($size) +{ $sizes = Array('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei'); $ext = $sizes[0]; for ($i=1; (($i < count($sizes)) && ($size >= 1024)); $i++) { $size = $size / 1024; $ext = $sizes[$i]; } return round($size, 2).$ext; } -function arguments($argv) { +function arguments($argv) +{ $_ARG = array(); foreach ($argv as $arg) { if (ereg('--([^=]+)=(.*)',$arg,$reg)) { @@ -185,7 +182,8 @@ function percent_colour($perc) return sprintf('#%02x%02x%02x', $r, $b, $b); } -function percent_colour_old($perc) { +function percent_colour_old($perc) +{ $red = round(5 * $perc); $blue = round(255 - (5 * $perc)); if($red > '255') { $red = "255"; } @@ -198,22 +196,26 @@ function percent_colour_old($perc) { return $colour; } -function print_error($text){ +function print_error($text) +{ echo("
$text
"); } -function print_message($text){ +function print_message($text) +{ echo("
$text
"); } -function truncate($substring, $max = 50, $rep = '...') { +function truncate($substring, $max = 50, $rep = '...') +{ if(strlen($substring) < 1){ $string = $rep; } else { $string = $substring; } $leave = $max - strlen ($rep); if(strlen($string) > $max){ return substr_replace($string, $rep, $leave); } else { return $string; } } -function interface_rates ($interface) { +function interface_rates ($interface) +{ global $config, $rrd_dir; $rrdfile = $rrd_dir . "/" . $interface['hostname'] . "/" . $interface['ifIndex'] . ".rrd"; $cmd = $config['rrdtool']." fetch -s -600s -e now ".$rrdfile." AVERAGE | grep : | cut -d\" \" -f 2,3 | grep e"; @@ -227,7 +229,8 @@ function interface_rates ($interface) { } -function interface_errors ($interface) { +function interface_errors ($interface) +{ global $config, $rrd_dir; $rrdfile = $rrd_dir . "/" . $interface['hostname'] . "/" . $interface['ifIndex'] . ".rrd"; $cmd = $config['rrdtool']." fetch -s -1d -e -300s $rrdfile AVERAGE | grep : | cut -d\" \" -f 4,5"; @@ -243,7 +246,8 @@ function interface_errors ($interface) { } -function geteventicon ($message) { +function geteventicon ($message) +{ if($message == "Device status changed to Down") { $icon = "server_connect.png"; } if($message == "Device status changed to Up") { $icon = "server_go.png"; } if($message == "Interface went down" || $message == "Interface changed state to Down" ) { $icon = "if-disconnect.png"; } @@ -253,19 +257,21 @@ function geteventicon ($message) { if($icon) { return $icon; } else { return false; } } -function generateiflink($interface, $text=0,$type=bits) { +function generateiflink($interface, $text=0,$type=bits) +{ global $twoday; global $now; global $config; global $day; if(!$text) { $text = fixIfName($interface['ifDescr']); } if(!$type) { $type = 'bits'; } $class = ifclass($interface['ifOperStatus'], $interface['ifAdminStatus']); $graph_url = "graph.php?if=" . $interface['interface_id'] . "&from=$day&to=$now&width=400&height=120&type=" . $type; $link = "" . $interface['hostname'] . " - " . $interface['ifDescr'] . "
"; + $link .= "onmouseover=\"return overlib('
" . $interface['hostname'] . " - " . fixifName($interface['ifDescr']) . "
"; $link .= $interface['ifAlias'] . "
'".$config['overlib_defaults'].");\" onmouseout=\"return nd();\">$text
"; return $link; } -function generatedevicelink($device, $text=0, $start=0, $end=0) { +function generatedevicelink($device, $text=0, $start=0, $end=0) +{ global $twoday; global $day; global $now; global $config; if(!$start) { $start = $day; } if(!$end) { $end = $now; } @@ -279,7 +285,8 @@ function generatedevicelink($device, $text=0, $start=0, $end=0) { } -function device_traffic_image($device, $width, $height, $from, $to) { +function device_traffic_image($device, $width, $height, $from, $to) +{ return ""; } @@ -293,7 +300,8 @@ function devclass($device) { } -function getImage($host) { +function getImage($host) +{ $sql = "SELECT * FROM `devices` WHERE `device_id` = '$host'"; $data = mysql_fetch_array(mysql_query($sql)); $type = strtolower($data['os']); @@ -310,7 +318,8 @@ $type = strtolower($data['os']); } -function renamehost($id, $new) { +function renamehost($id, $new) +{ global $config; $host = mysql_result(mysql_query("SELECT hostname FROM devices WHERE device_id = '$id'"), 0); shell_exec("mv ".$config['rrd_dir']."/$host ".$config['rrd_dir']."/$new"); @@ -318,7 +327,8 @@ function renamehost($id, $new) { mysql_query("INSERT INTO eventlog (host, datetime, message) VALUES ('" . $id . "', NULL, NOW(), 'Hostname changed -> $new (console)')"); } -function delHost($id) { +function delHost($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'"); @@ -348,7 +358,8 @@ function delHost($id) { } -function addHost($host, $community, $snmpver) { +function addHost($host, $community, $snmpver) +{ global $config; list($hostshort) = explode(".", $host); if ( isDomainResolves($host)){ @@ -367,7 +378,8 @@ function overlibprint($text) { return "onmouseover=\"return overlib('" . $text . "');\" onmouseout=\"return nd();\""; } -function scanUDP ($host, $port, $timeout) { +function scanUDP ($host, $port, $timeout) +{ $handle = fsockopen($host, $port, &$errno, &$errstr, 2); if (!$handle) { } @@ -389,23 +401,9 @@ function scanUDP ($host, $port, $timeout) { } } -function humanmedia($media) { - $media = preg_replace("/^ethernetCsmacd$/", "Ethernet", $media); - $media = preg_replace("/^softwareLoopback$/", "Loopback", $media); - $media = preg_replace("/^tunnel$/", "Tunnel", $media); - $media = preg_replace("/^propVirtual$/", "Virtual Int", $media); - $media = preg_replace("/^ppp$/", "PPP", $media); - $media = preg_replace("/^ds1$/", "DS1", $media); - $media = preg_replace("/^pos$/", "POS", $media); - $media = preg_replace("/^sonet$/", "SONET", $media); - $media = preg_replace("/^slip$/", "SLIP", $media); - $media = preg_replace("/^mpls$/", "MPLS Layer", $media); - $media = preg_replace("/^l2vlan$/", "VLAN Subif", $media); - $media = preg_replace("/^atm$/", "ATM", $media); - $media = preg_replace("/^aal5$/", "ATM AAL5", $media); - $media = preg_replace("/^atmSubInterface$/", "ATM Subif", $media); - $media = preg_replace("/^propPointToPointSerial$/", "PtP Serial", $media); - +function humanmedia($media) +{ + array_preg_replace($rewrite_iftype, $media); return $media; } @@ -440,17 +438,20 @@ function humanspeed($speed) { } -function netmask2cidr($netmask) { +function netmask2cidr($netmask) +{ list ($network, $cidr) = explode("/", trim(`ipcalc $address/$mask | grep Network | cut -d" " -f 4`)); return $cidr; } -function cidr2netmask() { +function cidr2netmask() +{ return (long2ip(ip2long("255.255.255.255") << (32-$netmask))); } -function formatUptime($diff, $format="long") { +function formatUptime($diff, $format="long") +{ $yearsDiff = floor($diff/31536000); $diff -= $yearsDiff*31536000; $daysDiff = floor($diff/86400); @@ -476,7 +477,8 @@ function formatUptime($diff, $format="long") { return "$uptime"; } -function isSNMPable($hostname, $community, $snmpver) { +function isSNMPable($hostname, $community, $snmpver) +{ global $config; $pos = shell_exec($config['snmpget'] ." -$snmpver -c $community -t 1 $hostname sysDescr.0"); if($pos == '') { @@ -489,7 +491,8 @@ function isSNMPable($hostname, $community, $snmpver) { return $status; } -function isPingable($hostname) { +function isPingable($hostname) +{ global $config; $status = shell_exec($config['fping'] . " $hostname"); if(strstr($status, "alive")) { @@ -500,11 +503,13 @@ function isPingable($hostname) { } -function is_odd($number) { +function is_odd($number) +{ return $number & 1; // 0 = even, 1 = odd } -function isValidInterface($if) { +function isValidInterface($if) +{ global $config; $if = strtolower($if); $nullintf = 0; @@ -521,7 +526,8 @@ function isValidInterface($if) { } else { return 0; } } -function ifclass($ifOperStatus, $ifAdminStatus) { +function ifclass($ifOperStatus, $ifAdminStatus) +{ $ifclass = "interface-upup"; if ($ifAdminStatus == "down") { $ifclass = "interface-admindown"; } if ($ifAdminStatus == "up" && $ifOperStatus== "down") { $ifclass = "interface-updown"; } @@ -529,68 +535,16 @@ function ifclass($ifOperStatus, $ifAdminStatus) { return $ifclass; } -function makeshortif($if) { - $if = strtolower($if); - $if = str_replace("tengigabitethernet","Te", $if); - $if = str_replace("gigabitethernet","Gi", $if); - $if = str_replace("fastethernet","Fa", $if); - $if = str_replace("ethernet","Et", $if); - $if = str_replace("serial","Se", $if); - $if = str_replace("pos","Pos", $if); - $if = str_replace("port-channel","Po", $if); - $if = str_replace("atm","Atm", $if); - $if = str_replace("null", "Null", $if); - $if = str_replace("loopback","Lo", $if); - $if = str_replace("dialer","Di", $if); - $if = str_replace("vlan","Vlan", $if); - $if = str_replace("tunnel","Tunnel", $if); - return $if; -} - -function utime() { +function utime() +{ $time = explode( " ", microtime()); $usec = (double)$time[0]; $sec = (double)$time[1]; return $sec + $usec; } -function fixiftype ($type) { - $type = str_replace("ethernetCsmacd", "Ethernet", $type); - $type = str_replace("tunnel", "Tunnel", $type); - $type = str_replace("softwareLoopback", "Software Loopback", $type); - $type = str_replace("propVirtual", "Ethernet VLAN", $type); - $type = str_replace("ethernetCsmacd", "Ethernet", $type); - $type = str_replace("l2vlan", "Ethernet VLAN", $type); - $type = str_replace("frameRelay", "Frame Relay", $type); - $type = str_replace("propPointToPointSerial", "PointToPoint Serial", $type); - return ($type); -} - -function fixifName ($inf) { - $inf = str_replace("ether", "Ether", $inf); - $inf = str_replace("gig", "Gig", $inf); - $inf = str_replace("fast", "Fast", $inf); - $inf = str_replace("ten", "Ten", $inf); - $inf = str_replace("bvi", "BVI", $inf); - $inf = str_replace("vlan", "Vlan", $inf); - $inf = str_replace("ether", "Ether", $inf); - $inf = str_replace("-802.1q Vlan subif", "", $inf); - $inf = str_replace("-802.1q", "", $inf); - $inf = str_replace("tunnel", "Tunnel", $inf); - $inf = str_replace("serial", "Serial", $inf); - $inf = str_replace("-aal5 layer", " aal5", $inf); - $inf = str_replace("null", "Null", $inf); - $inf = str_replace("atm", "ATM", $inf); - $inf = str_replace("port-channel", "Port-Channel", $inf); - $inf = str_replace("dial", "Dial", $inf); - $inf = str_replace("hp procurve switch software loopback interface", "Loopback Interface", $inf); - $inf = str_replace("control plane interface", "Control Plane", $inf); - $inf = str_replace("loop", "Loop", $inf); - $inf = preg_replace("/^([0-9]+)$/", "Interface \\0", $inf); - return $inf; -} - -function fixIOSFeatures($features){ +function fixIOSFeatures($features) +{ $features = preg_replace("/^PK9S$/", "IP w/SSH LAN Only", $features); $features = str_replace("LANBASEK9", "Lan Base Crypto", $features); $features = str_replace("LANBASE", "Lan Base", $features);