diff --git a/includes/discovery/arp-table.inc.php b/includes/discovery/arp-table.inc.php index 94a1d7e2b..5bc214e04 100644 --- a/includes/discovery/arp-table.inc.php +++ b/includes/discovery/arp-table.inc.php @@ -47,10 +47,10 @@ foreach ($vrfs_lite_cisco as $vrf) { $port_id = $interface['port_id']; $mac_table[$port_id][$clean_mac] = 1; - if (dbFetchCell('SELECT COUNT(*) from ipv4_mac WHERE port_id = ? AND ipv4_address = ? AND `context_name`= ?', array($interface['port_id'], $ip, $device['context_name']))) { + if (dbFetchCell('SELECT COUNT(*) from ipv4_mac WHERE port_id = ? AND ipv4_address = ?', array($interface['port_id'], $ip))) { // Commented below, no longer needed but leaving for reference. // $sql = "UPDATE `ipv4_mac` SET `mac_address` = '$clean_mac' WHERE port_id = '".$interface['port_id']."' AND ipv4_address = '$ip'"; - $old_mac = dbFetchCell('SELECT mac_address from ipv4_mac WHERE ipv4_address=? AND port_id=? AND `context_name`= ?', array($ip, $interface['port_id']), $device['context_name']); + $old_mac = dbFetchCell('SELECT mac_address from ipv4_mac WHERE ipv4_address=?', array($ip, $interface['port_id'])); if ($clean_mac != $old_mac && $clean_mac != '' && $old_mac != '') { d_echo("Changed mac address for $ip from $old_mac to $clean_mac\n"); @@ -58,7 +58,7 @@ foreach ($vrfs_lite_cisco as $vrf) { log_event("MAC change: $ip : ".mac_clean_to_readable($old_mac).' -> '.mac_clean_to_readable($clean_mac), $device, 'interface', $interface['port_id']); } - dbUpdate(array('mac_address' => $clean_mac), 'ipv4_mac', 'port_id=? AND ipv4_address=? AND `context_name`= ?', array($interface['port_id'], $ip, $device['context_name'])); + dbUpdate(array('mac_address' => $clean_mac, 'context_name' => $device['context_name']), 'ipv4_mac', 'port_id=? AND ipv4_address=?', array($interface['port_id'], $ip)); echo '.'; } elseif (isset($interface['port_id'])) { @@ -75,12 +75,12 @@ foreach ($vrfs_lite_cisco as $vrf) { }//end if }//end foreach - $sql = "SELECT * from ipv4_mac AS M, ports as I WHERE M.port_id = I.port_id and I.device_id = ' AND `context_name`= ?".$device['device_id']." AND M.context_name='". $device['context_name'] ."'"; + $sql = "SELECT * from ipv4_mac AS M, ports as I WHERE M.port_id = I.port_id and I.device_id = '".$device['device_id']."'"; foreach (dbFetchRows($sql) as $entry) { $entry_mac = $entry['mac_address']; $entry_if = $entry['port_id']; if (!$mac_table[$entry_if][$entry_mac]) { - dbDelete('ipv4_mac', '`port_id` = ? AND `mac_address` = ? AND `context_name`= ?', array($entry_if, $entry_mac, $device['context_name'])); + dbDelete('ipv4_mac', '`port_id` = ? AND `mac_address` = ?', array($entry_if, $entry_mac)); d_echo("Removing MAC $entry_mac from interface ".$interface['ifName']); echo '-'; diff --git a/includes/discovery/bgp-peers.inc.php b/includes/discovery/bgp-peers.inc.php index 0d82f83ed..8b38f44b2 100644 --- a/includes/discovery/bgp-peers.inc.php +++ b/includes/discovery/bgp-peers.inc.php @@ -3,19 +3,18 @@ if ($config['enable_bgp']) { // Discover BGP peers echo 'BGP Sessions : '; - - if( key_exists('vrf_lite_cisco', $device) && (count($device['vrf_lite_cisco'])!=0) ){ + + if (key_exists('vrf_lite_cisco', $device) && (count($device['vrf_lite_cisco']) != 0)) { $vrfs_lite_cisco = $device['vrf_lite_cisco']; + } else { + $vrfs_lite_cisco = array(array('context_name' => null)); } - else { - $vrfs_lite_cisco = array(array('context_name'=>null)); - } - + $bgpLocalAs = trim(snmp_walk($device, '.1.3.6.1.2.1.15.2', '-Oqvn', 'BGP4-MIB', $config['mibdir'])); - + foreach ($vrfs_lite_cisco as $vrf) { $device['context_name'] = $vrf['context_name']; - + if (is_numeric($bgpLocalAs)) { echo "AS$bgpLocalAs "; @@ -24,12 +23,11 @@ if ($config['enable_bgp']) { echo 'Updated AS '; } - $peer2 = false; + $peer2 = false; $peers_data = snmp_walk($device, 'cbgpPeer2RemoteAs', '-Oq', 'CISCO-BGP4-MIB', $config['mibdir']); if (empty($peers_data)) { $peers_data = snmp_walk($device, 'BGP4-MIB::bgpPeerRemoteAs', '-Oq', 'BGP4-MIB', $config['mibdir']); - } - else { + } else { $peer2 = true; } @@ -46,16 +44,16 @@ if ($config['enable_bgp']) { list($peer_ip, $peer_as) = explode(' ', $peer); if (strstr($peer_ip, ':')) { $peer_ip_snmp = preg_replace('/:/', ' ', $peer_ip); - $peer_ip = preg_replace('/(\S+\s+\S+)\s/', '$1:', $peer_ip_snmp); - $peer_ip = str_replace('"', '', str_replace(' ', '', $peer_ip)); + $peer_ip = preg_replace('/(\S+\s+\S+)\s/', '$1:', $peer_ip_snmp); + $peer_ip = str_replace('"', '', str_replace(' ', '', $peer_ip)); } if ($peer && $peer_ip != '0.0.0.0') { d_echo("Found peer $peer_ip (AS$peer_as)\n"); $peerlist[] = array( - 'ip' => $peer_ip, - 'as' => $peer_as, + 'ip' => $peer_ip, + 'as' => $peer_as, 'ver' => $ver, ); } @@ -65,8 +63,8 @@ if ($config['enable_bgp']) { // Juniper BGP4-V2 MIB // FIXME: needs a big cleanup! also see below. // FIXME: is .0.ipv6 the only possible value here? - $result = snmp_walk($device, 'jnxBgpM2PeerRemoteAs.0.ipv6', '-Onq', 'BGP4-V2-MIB-JUNIPER', $config['install_dir'].'/mibs/junos'); - $peers = trim(str_replace('.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.13.0.', '', $result)); + $result = snmp_walk($device, 'jnxBgpM2PeerRemoteAs.0.ipv6', '-Onq', 'BGP4-V2-MIB-JUNIPER', $config['install_dir'] . '/mibs/junos'); + $peers = trim(str_replace('.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.13.0.', '', $result)); foreach (explode("\n", $peers) as $peer) { list($peer_ip_snmp, $peer_as) = explode(' ', $peer); @@ -83,8 +81,7 @@ if ($config['enable_bgp']) { } } } - } - else { + } else { echo 'No BGP on host'; if ($device['bgpLocalAs']) { dbUpdate(array('bgpLocalAs' => 'NULL'), 'devices', 'device_id=?', array($device['device_id'])); @@ -97,17 +94,17 @@ if ($config['enable_bgp']) { foreach ($peerlist as $peer) { $astext = get_astext($peer['as']); - if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? AND `context_name` = ?', array($device['device_id'], $peer['ip'], $device['context_name'])) < '1') { - $add = dbInsert(array('device_id' => $device['device_id'], 'bgpPeerIdentifier' => $peer['ip'], 'bgpPeerRemoteAs' => $peer['as'],'context_name' => $device['context_name']), 'bgpPeers'); + if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ?', array($device['device_id'], $peer['ip'])) < '1') { + + $add = dbInsert(array('device_id' => $device['device_id'], 'bgpPeerIdentifier' => $peer['ip'], 'bgpPeerRemoteAs' => $peer['as'], 'context_name' => $device['context_name']), 'bgpPeers'); if ($config['autodiscovery']['bgp'] === true) { - $name = gethostbyaddr($peer['ip']); + $name = gethostbyaddr($peer['ip']); $remote_device_id = discover_new_device($name, $device, 'BGP'); } echo '+'; - } - else { - $update = dbUpdate(array('bgpPeerRemoteAs' => $peer['as'], 'astext' => mres($astext)), 'bgpPeers', 'device_id=? AND bgpPeerIdentifier=?', array($device['device_id'], $peer['ip'])); + } else { + $update = dbUpdate(array('bgpPeerRemoteAs' => $peer['as'], 'astext' => mres($astext), 'context_name' => $device['context_name']), 'bgpPeers', 'device_id=? AND bgpPeerIdentifier=?', array($device['device_id'], $peer['ip'])); echo '.'; } @@ -118,8 +115,7 @@ if ($config['enable_bgp']) { if ($peer2 === true) { $af_data = snmpwalk_cache_oid($device, 'cbgpPeer2AddrFamilyEntry', $cbgp, 'CISCO-BGP4-MIB', $config['mibdir']); - } - else { + } else { $af_data = snmpwalk_cache_oid($device, 'cbgpPeerAddrFamilyEntry', $cbgp, 'CISCO-BGP4-MIB', $config['mibdir']); } @@ -128,22 +124,26 @@ if ($config['enable_bgp']) { foreach ($af_data as $k => $v) { if ($peer2 === true) { - list(,$k) = explode('.', $k, 2); + list(, $k) = explode('.', $k, 2); } d_echo("AFISAFI = $k\n"); $afisafi_tmp = explode('.', $k); - $safi = array_pop($afisafi_tmp); - $afi = array_pop($afisafi_tmp); - $bgp_ip = str_replace(".$afi.$safi", '', $k); - $bgp_ip = preg_replace('/:/', ' ', $bgp_ip); - $bgp_ip = preg_replace('/(\S+\s+\S+)\s/', '$1:', $bgp_ip); - $bgp_ip = str_replace('"', '', str_replace(' ', '', $bgp_ip)); + $safi = array_pop($afisafi_tmp); + $afi = array_pop($afisafi_tmp); + $bgp_ip = str_replace(".$afi.$safi", '', $k); + $bgp_ip = preg_replace('/:/', ' ', $bgp_ip); + $bgp_ip = preg_replace('/(\S+\s+\S+)\s/', '$1:', $bgp_ip); + $bgp_ip = str_replace('"', '', str_replace(' ', '', $bgp_ip)); if ($afi && $safi && $bgp_ip == $peer['ip']) { $af_list[$bgp_ip][$afi][$safi] = 1; - if (dbFetchCell('SELECT COUNT(*) from `bgpPeers_cbgp` WHERE device_id = ? AND bgpPeerIdentifier = ?, AND afi=? AND safi=? AND `context_name`=?', array($device['device_id'], $peer['ip'], $afi, $safi, $device['context_name'])) == 0) { + if (dbFetchCell('SELECT COUNT(*) from `bgpPeers_cbgp` WHERE device_id = ? AND bgpPeerIdentifier = ? AND afi=? AND safi=?', array($device['device_id'], $peer['ip'], $afi, $safi)) == 0) { dbInsert(array('device_id' => $device['device_id'], 'bgpPeerIdentifier' => $peer['ip'], 'afi' => $afi, 'safi' => $safi, 'context_name' => $device['context_name']), 'bgpPeers_cbgp'); + } + else { + //Update Context + dbUpdate(array('context_name' => $device['context_name']), 'bgpPeers_cbgp', 'device_id = ? AND bgpPeerIdentifier = ? AND afi=? AND safi=?', array($device['device_id'], $peer['ip'], $afi, $safi)); } } } @@ -154,44 +154,44 @@ if ($config['enable_bgp']) { $safis[2] = 'multicast'; if (!isset($j_peerIndexes)) { - $j_bgp = snmpwalk_cache_multi_oid($device, 'jnxBgpM2PeerTable', $jbgp, 'BGP4-V2-MIB-JUNIPER', $config['install_dir'].'/mibs/junos'); + $j_bgp = snmpwalk_cache_multi_oid($device, 'jnxBgpM2PeerTable', $jbgp, 'BGP4-V2-MIB-JUNIPER', $config['install_dir'] . '/mibs/junos'); foreach ($j_bgp as $index => $entry) { switch ($entry['jnxBgpM2PeerRemoteAddrType']) { case 'ipv4': $ip = long2ip(hexdec($entry['jnxBgpM2PeerRemoteAddr'])); - d_echo("peerindex for ipv4 $ip is ".$entry['jnxBgpM2PeerIndex']."\n"); + d_echo("peerindex for ipv4 $ip is " . $entry['jnxBgpM2PeerIndex'] . "\n"); $j_peerIndexes[$ip] = $entry['jnxBgpM2PeerIndex']; break; case 'ipv6': $ip6 = trim(str_replace(' ', '', $entry['jnxBgpM2PeerRemoteAddr']), '"'); - $ip6 = substr($ip6, 0, 4).':'.substr($ip6, 4, 4).':'.substr($ip6, 8, 4).':'.substr($ip6, 12, 4).':'.substr($ip6, 16, 4).':'.substr($ip6, 20, 4).':'.substr($ip6, 24, 4).':'.substr($ip6, 28, 4); + $ip6 = substr($ip6, 0, 4) . ':' . substr($ip6, 4, 4) . ':' . substr($ip6, 8, 4) . ':' . substr($ip6, 12, 4) . ':' . substr($ip6, 16, 4) . ':' . substr($ip6, 20, 4) . ':' . substr($ip6, 24, 4) . ':' . substr($ip6, 28, 4); $ip6 = Net_IPv6::compress($ip6); - d_echo("peerindex for ipv6 $ip6 is ".$entry['jnxBgpM2PeerIndex']."\n"); + d_echo("peerindex for ipv6 $ip6 is " . $entry['jnxBgpM2PeerIndex'] . "\n"); $j_peerIndexes[$ip6] = $entry['jnxBgpM2PeerIndex']; break; default: - echo "HALP? Don't know RemoteAddrType ".$entry['jnxBgpM2PeerRemoteAddrType']."!\n"; + echo "HALP? Don't know RemoteAddrType " . $entry['jnxBgpM2PeerRemoteAddrType'] . "!\n"; break; } } } if (!isset($j_afisafi)) { - $j_prefixes = snmpwalk_cache_multi_oid($device, 'jnxBgpM2PrefixCountersTable', $jbgp, 'BGP4-V2-MIB-JUNIPER', $config['install_dir'].'/mibs/junos'); + $j_prefixes = snmpwalk_cache_multi_oid($device, 'jnxBgpM2PrefixCountersTable', $jbgp, 'BGP4-V2-MIB-JUNIPER', $config['install_dir'] . '/mibs/junos'); foreach (array_keys($j_prefixes) as $key) { - list($index,$afisafi) = explode('.', $key, 2); - $j_afisafi[$index][] = $afisafi; + list($index, $afisafi) = explode('.', $key, 2); + $j_afisafi[$index][] = $afisafi; } } foreach ($j_afisafi[$j_peerIndexes[$peer['ip']]] as $afisafi) { - list ($afi,$safi) = explode('.', $afisafi); - $safi = $safis[$safi]; + list ($afi, $safi) = explode('.', $afisafi); + $safi = $safis[$safi]; $af_list[$afi][$safi] = 1; if (dbFetchCell('SELECT COUNT(*) from `bgpPeers_cbgp` WHERE device_id = ? AND bgpPeerIdentifier = ?, AND afi=? AND safi=?', array($device['device_id'], $peer['ip'], $afi, $safi)) == 0) { dbInsert(array('device_id' => $device['device_id'], 'bgpPeerIdentifier' => $peer['ip'], 'afi' => $afi, 'safi' => $safi), 'bgpPeers_cbgp'); @@ -199,12 +199,12 @@ if ($config['enable_bgp']) { } } - $af_query = "SELECT * FROM bgpPeers_cbgp WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."' AND `context_name` = '" . $device['context_name'] . "'"; + $af_query = "SELECT * FROM bgpPeers_cbgp WHERE `device_id` = '" . $device['device_id'] . "' AND bgpPeerIdentifier = '" . $peer['ip'] . "'"; foreach (dbFetchRows($af_query) as $entry) { - $afi = $entry['afi']; + $afi = $entry['afi']; $safi = $entry['safi']; if (!$af_list[$afi][$safi] || !$af_list[$entry['bgpPeerIdentifier']][$afi][$safi]) { - dbDelete('bgpPeers_cbgp', '`device_id` = ? AND `bgpPeerIdentifier` = ?, afi=?, safi=? AND `context_name` = ?', array($device['device_id'], $peer['ip'], $afi, $safi, $device['context_name'])); + dbDelete('bgpPeers_cbgp', '`device_id` = ? AND `bgpPeerIdentifier` = ?, afi=?, safi=?', array($device['device_id'], $peer['ip'], $afi, $safi)); } } } @@ -217,7 +217,7 @@ if ($config['enable_bgp']) { } // Delete removed peers - $sql = "SELECT * FROM bgpPeers AS B, devices AS D WHERE B.device_id = D.device_id AND D.device_id = '".$device['device_id']."' AND `context_name` = '" . $device['context_name'] . "'"; + $sql = "SELECT * FROM bgpPeers AS B, devices AS D WHERE B.device_id = D.device_id AND D.device_id = '" . $device['device_id'] . "' AND `context_name` = '" . $device['context_name'] . "'"; foreach (dbFetchRows($sql) as $entry) { unset($exists); diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index e8055e63c..7bffc5c2f 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -12,14 +12,12 @@ * See COPYING for more details. */ - -function discover_new_device($hostname, $device='', $method='', $interface='') { +function discover_new_device($hostname, $device = '', $method = '', $interface = '') { global $config; - if (!empty($config['mydomain']) && isDomainResolves($hostname.'.'.$config['mydomain'])) { - $dst_host = $hostname.'.'.$config['mydomain']; - } - else { + if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) { + $dst_host = $hostname . '.' . $config['mydomain']; + } else { $dst_host = $hostname; } @@ -33,8 +31,7 @@ function discover_new_device($hostname, $device='', $method='', $interface='') { return false; } - } - elseif (filter_var($dst_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true || filter_var($dst_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === true){ + } elseif (filter_var($dst_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true || filter_var($dst_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === true) { // gethostbyname returned a valid $ip, was $dst_host an IP? if ($config['discovery_by_ip'] === false) { d_echo('Discovery by IP disabled, skipping ' . $dst_host); @@ -56,33 +53,31 @@ function discover_new_device($hostname, $device='', $method='', $interface='') { $remote_device_id = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']); if ($remote_device_id) { $remote_device = device_by_id_cache($remote_device_id, 1); - echo '+['.$remote_device['hostname'].'('.$remote_device['device_id'].')]'; + echo '+[' . $remote_device['hostname'] . '(' . $remote_device['device_id'] . ')]'; discover_device($remote_device); device_by_id_cache($remote_device_id, 1); if ($remote_device_id && is_array($device) && !empty($method)) { $extra_log = ''; - $int = ifNameDescr($interface); + $int = ifNameDescr($interface); if (is_array($int)) { - $extra_log = ' (port '.$int['label'].') '; + $extra_log = ' (port ' . $int['label'] . ') '; } - log_event('Device $'.$remote_device['hostname']." ($ip) $extra_log autodiscovered through $method on ".$device['hostname'], $remote_device_id, 'discovery'); - } - else { - log_event("$method discovery of ".$remote_device['hostname']." ($ip) failed - check ping and SNMP access", $device['device_id'], 'discovery'); + log_event('Device $' . $remote_device['hostname'] . " ($ip) $extra_log autodiscovered through $method on " . $device['hostname'], $remote_device_id, 'discovery'); + } else { + log_event("$method discovery of " . $remote_device['hostname'] . " ($ip) failed - check ping and SNMP access", $device['device_id'], 'discovery'); } return $remote_device_id; } - } - else { + } else { d_echo("$ip not in a matched network - skipping\n"); }//end if +} -}//end discover_new_device() +//end discover_new_device() - -function discover_device($device, $options=null) { +function discover_device($device, $options = null) { global $config, $valid; $valid = array(); @@ -91,20 +86,20 @@ function discover_device($device, $options=null) { $device_start = microtime(true); // Start counting device poll time - echo $device['hostname'].' '.$device['device_id'].' '.$device['os'].' '; + echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' '; if ($device['os'] == 'generic') { // verify if OS has changed from generic $device['os'] = getHostOS($device); if ($device['os'] != 'generic') { - echo "\nDevice os was updated to ".$device['os'].'!'; + echo "\nDevice os was updated to " . $device['os'] . '!'; dbUpdate(array('os' => $device['os']), 'devices', '`device_id` = ?', array($device['device_id'])); } } if ($config['os'][$device['os']]['group']) { $device['os_group'] = $config['os'][$device['os']]['group']; - echo ' ('.$device['os_group'].')'; + echo ' (' . $device['os_group'] . ')'; } echo "\n"; @@ -116,19 +111,16 @@ function discover_device($device, $options=null) { include "includes/discovery/$module.inc.php"; } } - } - else { + } else { foreach ($config['discovery_modules'] as $module => $module_status) { - if ($attribs['discover_'.$module] || ( $module_status && !isset($attribs['discover_'.$module]))) { + if ($attribs['discover_' . $module] || ( $module_status && !isset($attribs['discover_' . $module]))) { $module_start = microtime(true); - include 'includes/discovery/'.$module.'.inc.php'; + include 'includes/discovery/' . $module . '.inc.php'; $module_time = microtime(true) - $module_start; echo "Runtime for discovery module '$module': $module_time\n"; - } - else if (isset($attribs['discover_'.$module]) && $attribs['discover_'.$module] == '0') { + } else if (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == '0') { echo "Module [ $module ] disabled on host.\n"; - } - else { + } else { echo "Module [ $module ] disabled globally.\n"; } } @@ -146,8 +138,8 @@ function discover_device($device, $options=null) { } } - $device_end = microtime(true); - $device_run = ($device_end - $device_start); + $device_end = microtime(true); + $device_run = ($device_end - $device_start); $device_time = substr($device_run, 0, 5); dbUpdate(array('last_discovered' => array('NOW()'), 'type' => $device['type'], 'last_discovered_timetaken' => $device_time), 'devices', '`device_id` = ?', array($device['device_id'])); @@ -158,14 +150,13 @@ function discover_device($device, $options=null) { echo "\n"; $discovered_devices++; +} -}//end discover_device() - - +//end discover_device() // Discover sensors -function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor='1', $multiplier='1', $low_limit=null, $low_warn_limit=null, $warn_limit=null, $high_limit=null, $current=null, $poller_type='snmp', $entPhysicalIndex=null, $entPhysicalIndex_measured=null) { +function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = '1', $multiplier = '1', $low_limit = null, $low_warn_limit = null, $warn_limit = null, $high_limit = null, $current = null, $poller_type = 'snmp', $entPhysicalIndex = null, $entPhysicalIndex_measured = null) { global $config; d_echo("Discover sensor: $oid, $index, $type, $descr, $poller_type, $precision, $entPhysicalIndex\n"); @@ -173,9 +164,8 @@ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, if (is_null($low_warn_limit) && !is_null($warn_limit)) { // Warn limits only make sense when we have both a high and a low limit $low_warn_limit = null; - $warn_limit = null; - } - else if ($low_warn_limit > $warn_limit) { + $warn_limit = null; + } else if ($low_warn_limit > $warn_limit) { // Fix high/low thresholds (i.e. on negative numbers) list($warn_limit, $low_warn_limit) = array($low_warn_limit, $warn_limit); } @@ -195,21 +185,21 @@ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, } $insert = array( - 'poller_type' => $poller_type, - 'sensor_class' => $class, - 'device_id' => $device['device_id'], - 'sensor_oid' => $oid, - 'sensor_index' => $index, - 'sensor_type' => $type, - 'sensor_descr' => $descr, - 'sensor_divisor' => $divisor, - 'sensor_multiplier' => $multiplier, - 'sensor_limit' => $high_limit, - 'sensor_limit_warn' => $warn_limit, - 'sensor_limit_low' => $low_limit, - 'sensor_limit_low_warn' => $low_warn_limit, - 'sensor_current' => $current, - 'entPhysicalIndex' => $entPhysicalIndex, + 'poller_type' => $poller_type, + 'sensor_class' => $class, + 'device_id' => $device['device_id'], + 'sensor_oid' => $oid, + 'sensor_index' => $index, + 'sensor_type' => $type, + 'sensor_descr' => $descr, + 'sensor_divisor' => $divisor, + 'sensor_multiplier' => $multiplier, + 'sensor_limit' => $high_limit, + 'sensor_limit_warn' => $warn_limit, + 'sensor_limit_low' => $low_limit, + 'sensor_limit_low_warn' => $low_warn_limit, + 'sensor_current' => $current, + 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured, ); @@ -224,17 +214,15 @@ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, d_echo("( $inserted inserted )\n"); echo '+'; - log_event('Sensor Added: '.mres($class).' '.mres($type).' '.mres($index).' '.mres($descr), $device, 'sensor', $inserted); - } - else { + log_event('Sensor Added: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr), $device, 'sensor', $inserted); + } else { $sensor_entry = dbFetchRow('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', array($class, $device['device_id'], $type, $index)); if (!isset($high_limit)) { if (!$sensor_entry['sensor_limit']) { // Calculate a reasonable limit $high_limit = sensor_limit($class, $current); - } - else { + } else { // Use existing limit $high_limit = $sensor_entry['sensor_limit']; } @@ -244,8 +232,7 @@ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, if (!$sensor_entry['sensor_limit_low']) { // Calculate a reasonable limit $low_limit = sensor_low_limit($class, $current); - } - else { + } else { // Use existing limit $low_limit = $sensor_entry['sensor_limit_low']; } @@ -257,172 +244,169 @@ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, } if ($high_limit != $sensor_entry['sensor_limit'] && $sensor_entry['sensor_custom'] == 'No') { - $update = array('sensor_limit' => ($high_limit == null ? array('NULL') : $high_limit)); + $update = array('sensor_limit' => ($high_limit == null ? array('NULL') : $high_limit)); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); d_echo("( $updated updated )\n"); echo 'H'; - log_event('Sensor High Limit Updated: '.mres($class).' '.mres($type).' '.mres($index).' '.mres($descr).' ('.$high_limit.')', $device, 'sensor', $sensor_id); + log_event('Sensor High Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $high_limit . ')', $device, 'sensor', $sensor_id); } if ($sensor_entry['sensor_limit_low'] != $low_limit && $sensor_entry['sensor_custom'] == 'No') { - $update = array('sensor_limit_low' => ($low_limit == null ? array('NULL') : $low_limit)); + $update = array('sensor_limit_low' => ($low_limit == null ? array('NULL') : $low_limit)); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); d_echo("( $updated updated )\n"); echo 'L'; - log_event('Sensor Low Limit Updated: '.mres($class).' '.mres($type).' '.mres($index).' '.mres($descr).' ('.$low_limit.')', $device, 'sensor', $sensor_id); + log_event('Sensor Low Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $low_limit . ')', $device, 'sensor', $sensor_id); } if ($warn_limit != $sensor_entry['sensor_limit_warn'] && $sensor_entry['sensor_custom'] == 'No') { - $update = array('sensor_limit_warn' => ($warn_limit == null ? array('NULL') : $warn_limit)); + $update = array('sensor_limit_warn' => ($warn_limit == null ? array('NULL') : $warn_limit)); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); d_echo("( $updated updated )\n"); echo 'WH'; - log_event('Sensor Warn High Limit Updated: '.mres($class).' '.mres($type).' '.mres($index).' '.mres($descr).' ('.$warn_limit.')', $device, 'sensor', $sensor_id); + log_event('Sensor Warn High Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $warn_limit . ')', $device, 'sensor', $sensor_id); } if ($sensor_entry['sensor_limit_low_warn'] != $low_warn_limit && $sensor_entry['sensor_custom'] == 'No') { - $update = array('sensor_limit_low_warn' => ($low_warn_limit == null ? array('NULL') : $low_warn_limit)); + $update = array('sensor_limit_low_warn' => ($low_warn_limit == null ? array('NULL') : $low_warn_limit)); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); d_echo("( $updated updated )\n"); echo 'WL'; - log_event('Sensor Warn Low Limit Updated: '.mres($class).' '.mres($type).' '.mres($index).' '.mres($descr).' ('.$low_warn_limit.')', $device, 'sensor', $sensor_id); + log_event('Sensor Warn Low Limit Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr) . ' (' . $low_warn_limit . ')', $device, 'sensor', $sensor_id); } if ($oid == $sensor_entry['sensor_oid'] && $descr == $sensor_entry['sensor_descr'] && $multiplier == $sensor_entry['sensor_multiplier'] && $divisor == $sensor_entry['sensor_divisor'] && $entPhysicalIndex_measured == $sensor_entry['entPhysicalIndex_measured'] && $entPhysicalIndex == $sensor_entry['entPhysicalIndex']) { echo '.'; - } - else { - $update = array( - 'sensor_oid' => $oid, - 'sensor_descr' => $descr, - 'sensor_multiplier' => $multiplier, - 'sensor_divisor' => $divisor, - 'entPhysicalIndex' => $entPhysicalIndex, + } else { + $update = array( + 'sensor_oid' => $oid, + 'sensor_descr' => $descr, + 'sensor_multiplier' => $multiplier, + 'sensor_divisor' => $divisor, + 'entPhysicalIndex' => $entPhysicalIndex, 'entPhysicalIndex_measured' => $entPhysicalIndex_measured, ); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); echo 'U'; - log_event('Sensor Updated: '.mres($class).' '.mres($type).' '.mres($index).' '.mres($descr), $device, 'sensor', $sensor_id); + log_event('Sensor Updated: ' . mres($class) . ' ' . mres($type) . ' ' . mres($index) . ' ' . mres($descr), $device, 'sensor', $sensor_id); d_echo("( $updated updated )\n"); } }//end if $valid[$class][$type][$index] = 1; +} -}//end discover_sensor() - +//end discover_sensor() function sensor_low_limit($class, $current) { $limit = null; switch ($class) { - case 'temperature': - $limit = ($current * 0.7); - break; + case 'temperature': + $limit = ($current * 0.7); + break; - case 'voltage': - if ($current < 0) { - $limit = ($current * (1 + (sgn($current) * 0.15))); - } - else { - $limit = ($current * (1 - (sgn($current) * 0.15))); - } - break; + case 'voltage': + if ($current < 0) { + $limit = ($current * (1 + (sgn($current) * 0.15))); + } else { + $limit = ($current * (1 - (sgn($current) * 0.15))); + } + break; - case 'humidity': - $limit = '70'; - break; + case 'humidity': + $limit = '70'; + break; - case 'frequency': - $limit = ($current * 0.95); - break; + case 'frequency': + $limit = ($current * 0.95); + break; - case 'current': - $limit = null; - break; + case 'current': + $limit = null; + break; - case 'fanspeed': - $limit = ($current * 0.80); - break; + case 'fanspeed': + $limit = ($current * 0.80); + break; - case 'power': - $limit = null; - break; + case 'power': + $limit = null; + break; }//end switch return $limit; +} -}//end sensor_low_limit() - +//end sensor_low_limit() function sensor_limit($class, $current) { $limit = null; switch ($class) { - case 'temperature': - $limit = ($current * 1.60); - break; + case 'temperature': + $limit = ($current * 1.60); + break; - case 'voltage': - if ($current < 0) { - $limit = ($current * (1 - (sgn($current) * 0.15))); - } - else { - $limit = ($current * (1 + (sgn($current) * 0.15))); - } - break; + case 'voltage': + if ($current < 0) { + $limit = ($current * (1 - (sgn($current) * 0.15))); + } else { + $limit = ($current * (1 + (sgn($current) * 0.15))); + } + break; - case 'humidity': - $limit = '70'; - break; + case 'humidity': + $limit = '70'; + break; - case 'frequency': - $limit = ($current * 1.05); - break; + case 'frequency': + $limit = ($current * 1.05); + break; - case 'current': - $limit = ($current * 1.50); - break; + case 'current': + $limit = ($current * 1.50); + break; - case 'fanspeed': - $limit = ($current * 1.80); - break; + case 'fanspeed': + $limit = ($current * 1.80); + break; - case 'power': - $limit = ($current * 1.50); - break; + case 'power': + $limit = ($current * 1.50); + break; }//end switch return $limit; +} -}//end sensor_limit() +//end sensor_limit() - -function check_valid_sensors($device, $class, $valid, $poller_type='snmp') { +function check_valid_sensors($device, $class, $valid, $poller_type = 'snmp') { $entries = dbFetchRows('SELECT * FROM sensors AS S, devices AS D WHERE S.sensor_class=? AND S.device_id = D.device_id AND D.device_id = ? AND S.poller_type = ?', array($class, $device['device_id'], $poller_type)); if (count($entries)) { foreach ($entries as $entry) { $index = $entry['sensor_index']; - $type = $entry['sensor_type']; - d_echo($index.' -> '.$type."\n"); + $type = $entry['sensor_type']; + d_echo($index . ' -> ' . $type . "\n"); if (!$valid[$class][$type][$index]) { echo '-'; dbDelete('sensors', '`sensor_id` = ?', array($entry['sensor_id'])); - log_event('Sensor Deleted: '.$entry['sensor_class'].' '.$entry['sensor_type'].' '.$entry['sensor_index'].' '.$entry['sensor_descr'], $device, 'sensor', $sensor_id); + log_event('Sensor Deleted: ' . $entry['sensor_class'] . ' ' . $entry['sensor_type'] . ' ' . $entry['sensor_index'] . ' ' . $entry['sensor_descr'], $device, 'sensor', $sensor_id); } unset($oid); unset($type); } } +} -}//end check_valid_sensors() - +//end check_valid_sensors() function discover_juniAtmVp(&$valid, $port_id, $vp_id, $vp_descr) { global $config; @@ -432,16 +416,15 @@ function discover_juniAtmVp(&$valid, $port_id, $vp_id, $vp_descr) { d_echo("( $inserted inserted )\n"); // FIXME vv no $device! - log_event('Juniper ATM VP Added: port '.mres($port_id).' vp '.mres($vp_id).' descr'.mres($vp_descr), 'juniAtmVp', $inserted); - } - else { + log_event('Juniper ATM VP Added: port ' . mres($port_id) . ' vp ' . mres($vp_id) . ' descr' . mres($vp_descr), 'juniAtmVp', $inserted); + } else { echo '.'; } $valid[$port_id][$vp_id] = 1; +} -}//end discover_juniAtmVp() - +//end discover_juniAtmVp() function discover_link($local_port_id, $protocol, $remote_port_id, $remote_hostname, $remote_port, $remote_platform, $remote_version, $local_device_id, $remote_device_id) { global $config, $link_exists; @@ -449,23 +432,22 @@ function discover_link($local_port_id, $protocol, $remote_port_id, $remote_hostn d_echo("$local_port_id, $protocol, $remote_port_id, $remote_hostname, $remote_port, $remote_platform, $remote_version"); if (dbFetchCell( - 'SELECT COUNT(*) FROM `links` WHERE `remote_hostname` = ? AND `local_port_id` = ? AND `protocol` = ? AND `remote_port` = ?', - array( - $remote_hostname, - $local_port_id, - $protocol, - $remote_port, - ) - ) == '0') { + 'SELECT COUNT(*) FROM `links` WHERE `remote_hostname` = ? AND `local_port_id` = ? AND `protocol` = ? AND `remote_port` = ?', array( + $remote_hostname, + $local_port_id, + $protocol, + $remote_port, + ) + ) == '0') { $insert_data = array( - 'local_port_id' => $local_port_id, - 'local_device_id' => $local_device_id, - 'protocol' => $protocol, - 'remote_hostname' => $remote_hostname, + 'local_port_id' => $local_port_id, + 'local_device_id' => $local_device_id, + 'protocol' => $protocol, + 'remote_hostname' => $remote_hostname, 'remote_device_id' => $remote_device_id, - 'remote_port' => $remote_port, - 'remote_platform' => $remote_platform, - 'remote_version' => $remote_version, + 'remote_port' => $remote_port, + 'remote_platform' => $remote_platform, + 'remote_version' => $remote_version, ); if (!empty($remote_port_id)) { @@ -476,18 +458,16 @@ function discover_link($local_port_id, $protocol, $remote_port_id, $remote_hostn echo '+'; d_echo("( $inserted inserted )"); - } - else { + } else { $data = dbFetchRow('SELECT * FROM `links` WHERE `remote_hostname` = ? AND `local_port_id` = ? AND `protocol` = ? AND `remote_port` = ?', array($remote_hostname, $local_port_id, $protocol, $remote_port)); if ($data['remote_port_id'] == $remote_port_id && $data['remote_platform'] == $remote_platform && $remote_version == $remote_version && $data['local_device_id'] > 0 && $data['remote_device_id'] > 0) { echo '.'; - } - else { + } else { $update_data = array( - 'remote_platform' => $remote_platform, - 'remote_version' => $remote_version, - 'remote_version' => $remote_version, - 'local_device_id' => $local_device_id, + 'remote_platform' => $remote_platform, + 'remote_version' => $remote_version, + 'remote_version' => $remote_version, + 'local_device_id' => $local_device_id, 'remote_device_id' => $remote_device_id, ); @@ -501,11 +481,11 @@ function discover_link($local_port_id, $protocol, $remote_port_id, $remote_hostn }//end if }//end if $link_exists[$local_port_id][$remote_hostname][$remote_port] = 1; +} -}//end discover_link() +//end discover_link() - -function discover_storage(&$valid, $device, $index, $type, $mib, $descr, $size, $units, $used=null) { +function discover_storage(&$valid, $device, $index, $type, $mib, $descr, $size, $units, $used = null) { global $config; d_echo("$device, $index, $type, $mib, $descr, $units, $used, $size\n"); @@ -514,38 +494,35 @@ function discover_storage(&$valid, $device, $index, $type, $mib, $descr, $size, $storage = dbFetchRow('SELECT * FROM `storage` WHERE `storage_index` = ? AND `device_id` = ? AND `storage_mib` = ?', array($index, $device['device_id'], $mib)); if ($storage === false || !count($storage)) { $insert = dbInsert( - array( - 'device_id' => $device['device_id'], - 'storage_descr' => $descr, - 'storage_index' => $index, - 'storage_mib' => $mib, - 'storage_type' => $type, - 'storage_units' => $units, - 'storage_size' => $size, - 'storage_used' => $used, - ), - 'storage' + array( + 'device_id' => $device['device_id'], + 'storage_descr' => $descr, + 'storage_index' => $index, + 'storage_mib' => $mib, + 'storage_type' => $type, + 'storage_units' => $units, + 'storage_size' => $size, + 'storage_used' => $used, + ), 'storage' ); echo '+'; - } - else { + } else { $updated = dbUpdate(array('storage_descr' => $descr, 'storage_type' => $type, 'storage_units' => $units, 'storage_size' => $size), 'storage', '`device_id` = ? AND `storage_index` = ? AND `storage_mib` = ?', array($device['device_id'], $index, $mib)); if ($updated) { echo 'U'; - } - else { + } else { echo '.'; } }//end if $valid[$mib][$index] = 1; }//end if +} -}//end discover_storage() +//end discover_storage() - -function discover_processor(&$valid, $device, $oid, $index, $type, $descr, $precision='1', $current=null, $entPhysicalIndex=null, $hrDeviceIndex=null) { +function discover_processor(&$valid, $device, $oid, $index, $type, $descr, $precision = '1', $current = null, $entPhysicalIndex = null, $hrDeviceIndex = null) { global $config; d_echo("$device, $oid, $index, $type, $descr, $precision, $current, $entPhysicalIndex, $hrDeviceIndex\n"); @@ -554,12 +531,12 @@ function discover_processor(&$valid, $device, $oid, $index, $type, $descr, $prec $descr = trim(str_replace('"', '', $descr)); if (dbFetchCell('SELECT COUNT(processor_id) FROM `processors` WHERE `processor_index` = ? AND `device_id` = ? AND `processor_type` = ?', array($index, $device['device_id'], $type)) == '0') { $insert_data = array( - 'device_id' => $device['device_id'], - 'processor_descr' => $descr, - 'processor_index' => $index, - 'processor_oid' => $oid, - 'processor_usage' => $current, - 'processor_type' => $type, + 'device_id' => $device['device_id'], + 'processor_descr' => $descr, + 'processor_index' => $index, + 'processor_oid' => $oid, + 'processor_usage' => $current, + 'processor_type' => $type, 'processor_precision' => $precision, ); if (!empty($hrDeviceIndex)) { @@ -572,25 +549,24 @@ function discover_processor(&$valid, $device, $oid, $index, $type, $descr, $prec $inserted = dbInsert($insert_data, 'processors'); echo '+'; - log_event('Processor added: type '.mres($type).' index '.mres($index).' descr '.mres($descr), $device, 'processor', $inserted); - } - else { + log_event('Processor added: type ' . mres($type) . ' index ' . mres($index) . ' descr ' . mres($descr), $device, 'processor', $inserted); + } else { echo '.'; $update_data = array( - 'processor_descr' => $descr, - 'processor_oid' => $oid, - 'processor_usage' => $current, + 'processor_descr' => $descr, + 'processor_oid' => $oid, + 'processor_usage' => $current, 'processor_precision' => $precision, ); dbUpdate($update_data, 'processors', '`device_id`=? AND `processor_index`=? AND `processor_type`=?', array($device['device_id'], $index, $type)); }//end if $valid[$type][$index] = 1; }//end if +} -}//end discover_processor() +//end discover_processor() - -function discover_mempool(&$valid, $device, $index, $type, $descr, $precision='1', $entPhysicalIndex=null, $hrDeviceIndex=null) { +function discover_mempool(&$valid, $device, $index, $type, $descr, $precision = '1', $entPhysicalIndex = null, $hrDeviceIndex = null) { global $config; d_echo("$device, $oid, $index, $type, $descr, $precision, $current, $entPhysicalIndex, $hrDeviceIndex\n"); @@ -599,15 +575,15 @@ function discover_mempool(&$valid, $device, $index, $type, $descr, $precision='1 if ($descr) { if (dbFetchCell('SELECT COUNT(mempool_id) FROM `mempools` WHERE `mempool_index` = ? AND `device_id` = ? AND `mempool_type` = ?', array($index, $device['device_id'], $type)) == '0') { $insert_data = array( - 'device_id' => $device['device_id'], - 'mempool_descr' => $descr, - 'mempool_index' => $index, - 'mempool_type' => $type, + 'device_id' => $device['device_id'], + 'mempool_descr' => $descr, + 'mempool_index' => $index, + 'mempool_type' => $type, 'mempool_precision' => $precision, - 'mempool_perc' => 0, - 'mempool_used' => 0, - 'mempool_free' => 0, - 'mempool_total' => 0, + 'mempool_perc' => 0, + 'mempool_used' => 0, + 'mempool_free' => 0, + 'mempool_total' => 0, ); if (!empty($entPhysicalIndex)) { @@ -620,9 +596,8 @@ function discover_mempool(&$valid, $device, $index, $type, $descr, $precision='1 $inserted = dbInsert($insert_data, 'mempools'); echo '+'; - log_event('Memory pool added: type '.mres($type).' index '.mres($index).' descr '.mres($descr), $device, 'mempool', $inserted); - } - else { + log_event('Memory pool added: type ' . mres($type) . ' index ' . mres($index) . ' descr ' . mres($descr), $device, 'mempool', $inserted); + } else { echo '.'; $update_data = array( 'mempool_descr' => $descr, @@ -640,11 +615,11 @@ function discover_mempool(&$valid, $device, $index, $type, $descr, $precision='1 }//end if $valid[$type][$index] = 1; }//end if +} -}//end discover_mempool() +//end discover_mempool() - -function discover_toner(&$valid, $device, $oid, $index, $type, $descr, $capacity_oid=null, $capacity=null, $current=null) { +function discover_toner(&$valid, $device, $oid, $index, $type, $descr, $capacity_oid = null, $capacity = null, $current = null) { global $config; d_echo("$oid, $index, $type, $descr, $capacity, $capacity_oid\n"); @@ -652,28 +627,26 @@ function discover_toner(&$valid, $device, $oid, $index, $type, $descr, $capacity if (dbFetchCell('SELECT COUNT(toner_id) FROM `toner` WHERE device_id = ? AND toner_type = ? AND `toner_index` = ? AND `toner_capacity_oid` =?', array($device['device_id'], $type, $index, $capacity_oid)) == '0') { $inserted = dbInsert(array('device_id' => $device['device_id'], 'toner_oid' => $oid, 'toner_capacity_oid' => $capacity_oid, 'toner_index' => $index, 'toner_type' => $type, 'toner_descr' => $descr, 'toner_capacity' => $capacity, 'toner_current' => $current), 'toner'); echo '+'; - log_event('Toner added: type '.mres($type).' index '.mres($index).' descr '.mres($descr), $device, 'toner', $inserted); - } - else { + log_event('Toner added: type ' . mres($type) . ' index ' . mres($index) . ' descr ' . mres($descr), $device, 'toner', $inserted); + } else { $toner_entry = dbFetchRow('SELECT * FROM `toner` WHERE `device_id` = ? AND `toner_type` = ? AND `toner_index` =?', array($device['device_id'], $type, $index)); if ($oid == $toner_entry['toner_oid'] && $descr == $toner_entry['toner_descr'] && $capacity == $toner_entry['toner_capacity'] && $capacity_oid == $toner_entry['toner_capacity_oid']) { echo '.'; - } - else { + } else { dbUpdate(array('toner_descr' => $descr, 'toner_oid' => $oid, 'toner_capacity_oid' => $capacity_oid, 'toner_capacity' => $capacity), 'toner', 'device_id=? AND toner_type=? AND `toner_index`=?', array($device['device_id'], $type, $index)); echo 'U'; } } $valid[$type][$index] = 1; +} -}//end discover_toner() +//end discover_toner() - -function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen, $ipv6_origin, $context_name='') { - global $device,$config; +function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen, $ipv6_origin, $context_name = '') { + global $device, $config; - $ipv6_network = Net_IPv6::getNetmask("$ipv6_address/$ipv6_prefixlen").'/'.$ipv6_prefixlen; + $ipv6_network = Net_IPv6::getNetmask("$ipv6_address/$ipv6_prefixlen") . '/' . $ipv6_prefixlen; $ipv6_compressed = Net_IPv6::compress($ipv6_address); if (Net_IPv6::getAddressType($ipv6_address) == NET_IPV6_LOCAL_LINK) { @@ -683,30 +656,32 @@ function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex)) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1') { $port_id = dbFetchCell('SELECT port_id FROM `ports` WHERE device_id = ? AND ifIndex = ?', array($device['device_id'], $ifIndex)); - if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ? AND `context_name` = ?', array($ipv6_network, $context_name)) < '1') { + + if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ?', array($ipv6_network)) < '1') { dbInsert(array('ipv6_network' => $ipv6_network, 'context_name' => $context_name), 'ipv6_networks'); echo 'N'; + } else { + //Update Context + dbUpdate(array('context_name' => $device['context_name']), 'ipv6_network', '`ipv6_network` = ?', array($ipv6_network)); + echo 'n'; } - // Below looks like a duplicate of the above FIXME - if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ? AND `context_name` = ?', array($ipv6_network, $context_name)) < '1') { - dbInsert(array('ipv6_network' => $ipv6_network, 'context_name' => $context_name), 'ipv6_networks'); - echo 'N'; - } $ipv6_network_id = dbFetchCell('SELECT `ipv6_network_id` FROM `ipv6_networks` WHERE `ipv6_network` = ? AND `context_name` = ?', array($ipv6_network, $context_name)); - if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = ? AND `ipv6_prefixlen` = ? AND `port_id` = ? AND `context_name` = ?', array($ipv6_address, $ipv6_prefixlen, $port_id)) == '0') { - dbInsert(array('ipv6_address' => $ipv6_address, 'ipv6_compressed' => $ipv6_compressed, 'ipv6_prefixlen' => $ipv6_prefixlen, 'ipv6_origin' => $ipv6_origin, 'ipv6_network_id' => $ipv6_network_id, 'port_id' => $port_id, 'context_name' => $context_name), 'ipv6_addresses'); + if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = ? AND `ipv6_prefixlen` = ? AND `port_id` = ?', array($ipv6_address, $ipv6_prefixlen, $port_id)) == '0') { + dbInsert(array('ipv6_address' => $ipv6_address, 'ipv6_compressed' => $ipv6_compressed, 'ipv6_prefixlen' => $ipv6_prefixlen, 'ipv6_origin' => $ipv6_origin, 'ipv6_network_id' => $ipv6_network_id, 'port_id' => $port_id, 'context_name' => $context_name), 'ipv6_addresses'); echo '+'; - } - else { + } else { + //Update Context + dbUpdate(array('context_name' => $device['context_name']), 'ipv6_address', '`ipv6_address` = ? AND `ipv6_prefixlen` = ? AND `port_id` = ?', array($ipv6_address, $ipv6_prefixlen, $port_id)); echo '.'; } - $full_address = "$ipv6_address/$ipv6_prefixlen"; - $valid_address = $full_address.'-'.$port_id; + $full_address = "$ipv6_address/$ipv6_prefixlen"; + $valid_address = $full_address . '-' . $port_id; $valid['ipv6'][$valid_address] = 1; }//end if +} -}//end discover_process_ipv6() +//end discover_process_ipv6() diff --git a/includes/discovery/ipv4-addresses.inc.php b/includes/discovery/ipv4-addresses.inc.php index f930b8fd3..441b82874 100644 --- a/includes/discovery/ipv4-addresses.inc.php +++ b/includes/discovery/ipv4-addresses.inc.php @@ -23,20 +23,27 @@ foreach ($vrfs_lite_cisco as $vrf) { if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex)) != '0' && $oid != '0.0.0.0' && $oid != 'ipAdEntIfIndex') { $port_id = dbFetchCell('SELECT `port_id` FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex)); - if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ? and `context_name` = ?', array($network, $device['context_name'])) < '1') { + if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network)) < '1') { dbInsert(array('ipv4_network' => $network,'context_name' => $device['context_name']), 'ipv4_networks'); // echo("Create Subnet $network\n"); echo 'S'; } + else { + //Update Context + dbUpdate(array('context_name' => $device['context_name']), 'ipv4_networks', '`ipv4_network` = ?', array($network) ); + echo 's'; + } - $ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ? and `context_name`= ?', array($network, $device['context_name'])); + $ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network)); - if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ? and `context_name`=?', array($oid, $cidr, $port_id, $device['context_name'])) == '0') { - dbInsert(array('ipv4_address' => $oid, 'ipv4_prefixlen' => $cidr, 'ipv4_network_id' => $ipv4_network_id, 'port_id' => $port_id,'context_name' => $device['context_name']), 'ipv4_addresses'); + if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ? ', array($oid, $cidr, $port_id)) == '0') { + dbInsert(array('ipv4_address' => $oid, 'ipv4_prefixlen' => $cidr, 'ipv4_network_id' => $ipv4_network_id, 'port_id' => $port_id, 'context_name' => $device['context_name']), 'ipv4_addresses'); // echo("Added $oid/$cidr to $port_id ( $hostname $ifIndex )\n $i_query\n"); echo '+'; } else { + //Update Context + dbUpdate(array('context_name' => $device['context_name']), 'ipv4_addresses', 'ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ?',array($oid, $cidr, $port_id) ); echo '.'; } @@ -48,7 +55,7 @@ foreach ($vrfs_lite_cisco as $vrf) { }//end if }//end foreach - $sql = "SELECT * FROM ipv4_addresses AS A, ports AS I WHERE I.device_id = '".$device['device_id']."' AND A.port_id = I.port_id AND a.context_name= '" . $device['context_name'] . "'"; + $sql = "SELECT * FROM ipv4_addresses AS A, ports AS I WHERE I.device_id = '".$device['device_id']."' AND A.port_id = I.port_id"; foreach (dbFetchRows($sql) as $row) { $full_address = $row['ipv4_address'].'/'.$row['ipv4_prefixlen'].'|'.$row['ifIndex']; diff --git a/includes/discovery/ipv6-addresses.inc.php b/includes/discovery/ipv6-addresses.inc.php index fb9864020..617c003e1 100644 --- a/includes/discovery/ipv6-addresses.inc.php +++ b/includes/discovery/ipv6-addresses.inc.php @@ -68,7 +68,7 @@ foreach ($vrfs_lite_cisco as $vrf) { } //end foreach } //end if - $sql = "SELECT * FROM ipv6_addresses AS A, ports AS I WHERE I.device_id = '".$device['device_id']."' AND A.port_id = I.port_id AND A.context_name= '" . $device['context_name'] . "'"; + $sql = "SELECT * FROM ipv6_addresses AS A, ports AS I WHERE I.device_id = '".$device['device_id']."' AND A.port_id = I.port_id'"; foreach (dbFetchRows($sql) as $row) { $full_address = $row['ipv6_address'].'/'.$row['ipv6_prefixlen'];