2009-09-07 11:07:59 +00:00
< ? php
2008-03-15 17:58:15 +00:00
2012-05-06 18:38:11 +00:00
global $debug ;
2010-07-27 20:49:31 +00:00
if ( $config [ 'enable_bgp' ])
{
2012-05-25 12:24:34 +00:00
// Discover BGP peers
2008-03-15 17:58:15 +00:00
2008-03-22 23:09:35 +00:00
echo ( " BGP Sessions : " );
2008-03-15 17:58:15 +00:00
2011-03-08 17:28:24 +00:00
$bgpLocalAs = trim ( snmp_walk ( $device , " .1.3.6.1.2.1.15.2 " , " -Oqvn " , " BGP4-MIB " , $config [ 'mibdir' ]));
if ( is_numeric ( $bgpLocalAs ))
2010-01-09 22:34:18 +00:00
{
2009-11-12 17:51:36 +00:00
echo ( " AS $bgpLocalAs " );
2008-03-15 17:58:15 +00:00
2011-03-08 17:28:24 +00:00
if ( $bgpLocalAs != $device [ 'bgpLocalAs' ])
2010-01-09 22:34:18 +00:00
{
2014-01-13 17:43:58 +00:00
dbUpdate ( array ( 'bgpLocalAs' => $bgpLocalAs ), 'devices' , 'device_id=?' , array ( $device [ 'device_id' ]));
echo ( " Updated AS " );
2010-01-09 22:34:18 +00:00
}
2008-03-15 17:58:15 +00:00
2011-03-08 17:28:24 +00:00
$peers_data = snmp_walk ( $device , " BGP4-MIB::bgpPeerRemoteAs " , " -Oq " , " BGP4-MIB " , $config [ 'mibdir' ]);
if ( $debug ) { echo ( " Peers : $peers_data \n " ); }
$peers = trim ( str_replace ( " BGP4-MIB::bgpPeerRemoteAs. " , " " , $peers_data ));
2010-02-04 16:33:24 +00:00
2011-03-08 17:28:24 +00:00
foreach ( explode ( " \n " , $peers ) as $peer )
2010-01-09 22:34:18 +00:00
{
2011-04-11 09:22:12 +00:00
list ( $peer_ip , $peer_as ) = explode ( " " , $peer );
2011-03-08 17:28:24 +00:00
if ( $peer && $peer_ip != " 0.0.0.0 " )
2010-01-09 22:34:18 +00:00
{
2010-11-23 14:33:02 +00:00
if ( $debug ) { echo ( " Found peer $peer_ip (AS $peer_as ) \n " ); }
$peerlist [] = array ( 'ip' => $peer_ip , 'as' => $peer_as );
2011-03-08 17:28:24 +00:00
}
} # Foreach
2009-12-31 19:06:05 +00:00
if ( $device [ 'os' ] == " junos " )
2010-01-09 22:34:18 +00:00
{
2012-05-25 12:24:34 +00:00
// Juniper BGP4-V2 MIB
2011-03-08 17:28:24 +00:00
2012-05-25 12:24:34 +00:00
// FIXME: needs a big cleanup! also see below.
2011-03-08 17:28:24 +00:00
2012-05-25 12:24:34 +00:00
// FIXME: is .0.ipv6 the only possible value here?
2011-03-08 17:28:24 +00:00
$result = snmp_walk ( $device , " jnxBgpM2PeerRemoteAs.0.ipv6 " , " -Onq " , " BGP4-V2-MIB-JUNIPER " , $config [ 'install_dir' ] . " /mibs/junos " );
2011-03-15 11:24:35 +00:00
$peers = trim ( str_replace ( " .1.3.6.1.4.1.2636.5.1.1.2.1.1.1.13.0. " , " " , $result ));
2011-03-08 17:28:24 +00:00
foreach ( explode ( " \n " , $peers ) as $peer )
2010-01-09 22:34:18 +00:00
{
2011-04-11 09:22:12 +00:00
list ( $peer_ip_snmp , $peer_as ) = explode ( " " , $peer );
2010-01-09 22:34:18 +00:00
# Magic! Basically, takes SNMP form and finds peer IPs from the walk OIDs.
2010-01-16 23:37:26 +00:00
$peer_ip = Net_IPv6 :: compress ( snmp2ipv6 ( implode ( '.' , array_slice ( explode ( '.' , $peer_ip_snmp ), count ( explode ( '.' , $peer_ip_snmp )) - 16 ))));
2011-03-08 17:28:24 +00:00
if ( $peer )
2010-01-09 22:34:18 +00:00
{
2010-11-20 14:04:07 +00:00
if ( $debug ) echo ( " Found peer $peer_ip (AS $peer_as ) \n " );
2011-03-08 17:28:24 +00:00
$peerlist [] = array ( 'ip' => $peer_ip , 'as' => $peer_as );
2009-12-31 19:06:05 +00:00
}
2011-03-08 17:28:24 +00:00
} # Foreach
2010-01-09 22:34:18 +00:00
} # OS junos
2011-03-08 17:28:24 +00:00
} else {
2009-08-07 11:53:10 +00:00
echo ( " No BGP on host " );
2011-03-15 11:24:35 +00:00
if ( $device [ 'bgpLocalAs' ])
{
2014-01-13 17:43:58 +00:00
dbUpdate ( array ( 'bgpLocalAs' => 'NULL' ), 'devices' , 'device_id=?' , array ( $device [ 'device_id' ]));
echo ( " (Removed ASN) " );
2009-08-07 11:53:10 +00:00
} # End if
} # End if
2008-03-15 17:58:15 +00:00
2012-05-25 12:24:34 +00:00
// Process disovered peers
2010-01-09 22:34:18 +00:00
2011-03-08 17:28:24 +00:00
if ( isset ( $peerlist ))
2010-01-09 22:34:18 +00:00
{
2011-03-08 17:28:24 +00:00
foreach ( $peerlist as $peer )
2010-01-09 23:28:06 +00:00
{
2011-03-08 17:28:24 +00:00
$astext = get_astext ( $peer [ 'as' ]);
2010-01-09 22:34:18 +00:00
2014-01-13 17:43:58 +00:00
if ( dbFetchCell ( " SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? " , array ( $device [ 'device_id' ], $peer [ 'ip' ])) < '1' )
2011-03-08 17:28:24 +00:00
{
2014-01-13 17:43:58 +00:00
$add = dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'bgpPeerIdentifier' => $peer [ 'ip' ], 'bgpPeerRemoteAs' => $peer [ 'as' ]), 'bgpPeers' );
2011-03-08 17:28:24 +00:00
echo ( " + " );
} else {
2014-01-13 17:43:58 +00:00
$update = dbUpdate ( array ( 'bgpPeerRemoteAs' => $peer [ 'as' ], 'astext' => mres ( $astext )), 'bgpPeers' , 'device_id=? AND bgpPeerIdentifier=?' , array ( $device [ 'device_id' ], $peer [ 'ip' ]));
2011-03-08 17:28:24 +00:00
echo ( " . " );
}
2012-01-12 11:16:38 +00:00
if ( $device [ 'os_group' ] == " cisco " || $device [ 'os' ] == " junos " )
2010-11-23 14:33:02 +00:00
{
2011-03-08 17:28:24 +00:00
2012-01-12 11:16:38 +00:00
if ( $device [ 'os_group' ] == " cisco " )
2010-11-23 14:33:02 +00:00
{
2012-05-25 12:24:34 +00:00
// Get afi/safi and populate cbgp on cisco ios (xe/xr)
2011-03-08 17:28:24 +00:00
unset ( $af_list );
2011-03-15 11:24:35 +00:00
2011-03-08 17:34:17 +00:00
$af_data = snmp_walk ( $device , " cbgpPeerAddrFamilyName. " . $peer [ 'ip' ], " -OsQ " , " CISCO-BGP4-MIB " , $config [ 'mibdir' ]);
2011-03-08 17:28:24 +00:00
if ( $debug ) { echo ( " afi data :: $af_data \n " ); }
2011-03-15 11:24:35 +00:00
2011-03-08 17:28:24 +00:00
$afs = trim ( str_replace ( " cbgpPeerAddrFamilyName. " . $peer [ 'ip' ] . " . " , " " , $af_data ));
foreach ( explode ( " \n " , $afs ) as $af )
2010-11-23 14:33:02 +00:00
{
2011-03-08 17:28:24 +00:00
if ( $debug ) { echo ( " AFISAFI = $af\n " ); }
list ( $afisafi , $text ) = explode ( " = " , $af );
list ( $afi , $safi ) = explode ( " . " , $afisafi );
if ( $afi && $safi )
2010-11-23 14:33:02 +00:00
{
2011-03-08 17:28:24 +00:00
$af_list [ $afi ][ $safi ] = 1 ;
2014-01-13 17:43:58 +00:00
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 )
2011-03-08 17:28:24 +00:00
{
2014-01-13 17:43:58 +00:00
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'bgpPeerIdentifier' => $peer [ 'ip' ], 'afi' => $afi , 'safi' => $safi ), 'bgpPeers_cbgp' );
2011-03-08 17:28:24 +00:00
}
2010-11-23 14:33:02 +00:00
}
}
2012-01-12 11:16:38 +00:00
} # os_group=cisco
2010-11-23 14:33:02 +00:00
2011-03-08 17:28:24 +00:00
if ( $device [ 'os' ] == " junos " )
2010-01-09 22:34:18 +00:00
{
2011-03-08 17:28:24 +00:00
$safis [ 1 ] = " unicast " ;
$safis [ 2 ] = " multicast " ;
2012-05-06 18:38:11 +00:00
if ( ! isset ( $j_peerIndexes ))
2010-11-23 14:33:02 +00:00
{
2011-03-08 17:28:24 +00:00
$j_bgp = snmpwalk_cache_multi_oid ( $device , " jnxBgpM2PeerTable " , $jbgp , " BGP4-V2-MIB-JUNIPER " , $config [ 'install_dir' ] . " /mibs/junos " );
2012-05-06 18:38:11 +00:00
2011-03-08 17:28:24 +00:00
foreach ( $j_bgp as $index => $entry )
2010-11-23 14:33:02 +00:00
{
2011-03-08 17:28:24 +00:00
switch ( $entry [ 'jnxBgpM2PeerRemoteAddrType' ])
{
case 'ipv4' :
$ip = long2ip ( hexdec ( $entry [ 'jnxBgpM2PeerRemoteAddr' ]));
2012-05-06 18:38:11 +00:00
if ( $debug ) { echo ( " peerindex for ipv4 $ip is " . $entry [ 'jnxBgpM2PeerIndex' ] . " \n " ); }
2011-03-08 17:28:24 +00:00
$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 = Net_IPv6 :: compress ( $ip6 );
2012-05-06 18:38:11 +00:00
if ( $debug ) { echo ( " peerindex for ipv6 $ip6 is " . $entry [ 'jnxBgpM2PeerIndex' ] . " \n " ); }
2011-03-08 17:28:24 +00:00
$j_peerIndexes [ $ip6 ] = $entry [ 'jnxBgpM2PeerIndex' ];
break ;
default :
echo ( " HALP? Don't know RemoteAddrType " . $entry [ 'jnxBgpM2PeerRemoteAddrType' ] . " ! \n " );
break ;
}
2010-11-23 14:33:02 +00:00
}
}
2011-03-08 17:28:24 +00:00
if ( ! isset ( $j_afisafi ))
2010-11-23 14:33:02 +00:00
{
2011-03-08 17:28:24 +00:00
$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 ;
}
2010-11-23 14:33:02 +00:00
}
2012-05-07 16:23:43 +00:00
2011-03-08 17:28:24 +00:00
foreach ( $j_afisafi [ $j_peerIndexes [ $peer [ 'ip' ]]] as $afisafi )
2010-01-09 23:28:06 +00:00
{
2011-03-08 17:28:24 +00:00
list ( $afi , $safi ) = explode ( '.' , $afisafi ); $safi = $safis [ $safi ];
$af_list [ $afi ][ $safi ] = 1 ;
2014-01-13 17:43:58 +00:00
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 )
2011-03-08 17:28:24 +00:00
{
2014-01-13 17:43:58 +00:00
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'bgpPeerIdentifier' => $peer [ 'ip' ], 'afi' => $afi , 'safi' => $safi ), 'bgpPeers_cbgp' );
2011-03-08 17:28:24 +00:00
}
2010-01-09 23:28:06 +00:00
}
2011-03-08 17:28:24 +00:00
} # os=junos
2014-01-13 17:43:58 +00:00
$af_query = " SELECT * FROM bgpPeers_cbgp WHERE `device_id` = ' " . $device [ 'device_id' ] . " ' AND bgpPeerIdentifier = ' " . $peer [ 'ip' ] . " ' " ;
foreach ( dbFetchRows ( $af_query ) as $entry )
2010-01-09 22:34:18 +00:00
{
2011-03-08 17:28:24 +00:00
$afi = $entry [ 'afi' ];
$safi = $entry [ 'safi' ];
if ( ! $af_list [ $afi ][ $safi ])
{
2014-01-13 17:43:58 +00:00
dbDelete ( 'bgpPeers_cbgp' , '`device_id` = ? AND `bgpPeerIdentifier` = ?, afi=?, safi=?' , array ( $device [ 'device_id' ], $peer [ 'ip' ], $afi , $safi ));
2011-03-08 17:28:24 +00:00
}
} # AF list
2012-01-12 11:16:38 +00:00
} # os=cisco|junos
2011-03-08 17:28:24 +00:00
} # Foreach
unset ( $j_afisafi );
unset ( $j_prefixes );
unset ( $j_bgp );
unset ( $j_peerIndexes );
} # isset
2012-05-25 12:24:34 +00:00
// Delete removed peers
2011-03-08 17:28:24 +00:00
$sql = " SELECT * FROM bgpPeers AS B, devices AS D WHERE B.device_id = D.device_id AND D.device_id = ' " . $device [ 'device_id' ] . " ' " ;
2014-01-13 17:43:58 +00:00
foreach ( dbFetchRows ( $sql ) as $entry )
2011-03-08 17:28:24 +00:00
{
unset ( $exists );
$i = 0 ;
while ( $i < count ( $peerlist ) && ! isset ( $exists ))
{
if ( $peerlist [ $i ][ 'ip' ] == $entry [ 'bgpPeerIdentifier' ]) { $exists = 1 ; }
$i ++ ;
}
if ( ! isset ( $exists ))
{
2014-01-13 17:43:58 +00:00
dbDelete ( 'bgpPeers' , '`bgpPeer_id` = ?' , array ( $entry [ 'bgpPeer_id' ]));
dbDelete ( 'bgpPeers_cbgp' , '`bgpPeer_id` = ?' , array ( $entry [ 'bgpPeer_id' ]));
2011-03-08 17:28:24 +00:00
echo ( " - " );
}
}
2008-03-15 17:58:15 +00:00
2011-03-08 17:28:24 +00:00
unset ( $peerlist );
2010-01-09 23:28:06 +00:00
2011-03-08 17:28:24 +00:00
echo ( " \n " );
2010-07-27 20:49:31 +00:00
}
2008-03-15 17:58:15 +00:00
2012-01-12 11:16:38 +00:00
?>