2016-03-16 17:17:16 +01:00
< ? php
/* Copyright ( C ) 2014 Nicolas Armando < nicearma @ yahoo . com > and Mathieu Millet < htam - net @ github . net >
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />. */
//This file is a litle diferent, because the route depend of the vrf, not of the context,
//like the others, so if i use the context, you will have the same information n time the context how have the same VRF
global $debug ;
2016-04-21 09:17:20 +02:00
$ids = array ();
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
// For the moment only will be cisco and the version 3
if ( $device [ 'os_group' ] == " cisco " ) {
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
//RFC1213-MIB
$mib = " RFC1213-MIB " ;
//IpRouteEntry
$vrfs_lite_cisco = array ();
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
if ( key_exists ( 'vrf_lite_cisco' , $device ) && ( count ( $device [ 'vrf_lite_cisco' ]) != 0 )) {
//i will only get one context of vrf, read the begin of this file
foreach ( $device [ 'vrf_lite_cisco' ] as $vrf_lite ) {
if ( ! key_exists ( $vrf_lite [ 'vrf_name' ], $vrfs_lite_cisco )) {
$vrfs_lite_cisco [ $vrf_lite [ 'vrf_name' ]] = $vrf_lite ;
2016-03-16 17:17:16 +01:00
}
}
2016-04-21 09:17:20 +02:00
}
else {
$vrfs_lite_cisco = array ( array ( 'context_name' => null ));
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
$tableRoute = array ();
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( $vrfs_lite_cisco as $vrf_lite ) {
$device [ 'context_name' ] = $vrf_lite [ 'context_name' ];
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
////////////////ipRouteDest//////////////////
$oid = '.1.3.6.1.2.1.4.21.1.1' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteDest ) {
list ( $ip , $value ) = explode ( " " , $ipRouteDest );
$tableRoute [ $ip ][ 'ipRouteDest' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
/////////////////ipRouteIfIndex//////////////
$oid = '.1.3.6.1.2.1.4.21.1.2' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteIfIndex ) {
list ( $ip , $value ) = explode ( " " , $ipRouteIfIndex );
$tableRoute [ $ip ][ 'ipRouteIfIndex' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
///////////////ipRouteMetric1///////////////
$oid = '.1.3.6.1.2.1.4.21.1.3' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteMetric ) {
list ( $ip , $value ) = explode ( " " , $ipRouteMetric );
$tableRoute [ $ip ][ 'ipRouteMetric' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
////////////ipRouteNextHop//////////////////
$oid = '.1.3.6.1.2.1.4.21.1.7' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteNextHop ) {
list ( $ip , $value ) = explode ( " " , $ipRouteNextHop );
$tableRoute [ $ip ][ 'ipRouteNextHop' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
////////////ipRouteType/////////////////////
$oid = '.1.3.6.1.2.1.4.21.1.8' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteType ) {
list ( $ip , $value ) = explode ( " " , $ipRouteType );
$tableRoute [ $ip ][ 'ipRouteType' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
///////////ipRouteProto//////////////////////
$oid = '.1.3.6.1.2.1.4.21.1.9' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteProto ) {
list ( $ip , $value ) = explode ( " " , $ipRouteProto );
$tableRoute [ $ip ][ 'ipRouteProto' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
/*
///////////ipRouteAge//////////////////////
$oid = '.1.3.6.1.2.1.4.21.1.10' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn " , $mib , NULL );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteAge ) {
2016-03-16 17:17:16 +01:00
list ( $ip , $value ) = explode ( " " , $ipRouteAge );
$tableRoute [ $ip ][ 'ipRouteAge' ] = $value ;
2016-04-21 09:17:20 +02:00
} */
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
///////////ipRouteMask//////////////////////
$oid = '.1.3.6.1.2.1.4.21.1.11' ;
$resultHelp = snmp_walk ( $device , $oid , " -Osqn -Ln " , $mib , NULL );
$resultHelp = trim ( $resultHelp );
$resultHelp = str_replace ( " $oid . " , " " , $resultHelp );
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( explode ( " \n " , $resultHelp ) as $ipRouteMask ) {
list ( $ip , $value ) = explode ( " " , $ipRouteMask );
$tableRoute [ $ip ][ 'ipRouteMask' ] = $value ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
if ( $debug ) {
echo 'Table routage' ;
var_dump ( $tableRoute );
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
foreach ( $tableRoute as $ipRoute ) {
if ( empty ( $ipRoute [ 'ipRouteDest' ]) || $ipRoute [ 'ipRouteDest' ] == '' ) {
continue ;
}
2016-03-16 17:17:16 +01:00
2016-04-21 09:17:20 +02:00
$oldRouteRow = dbFetchRow ( 'select * from route where device_id = ? AND ipRouteDest = ? AND context_name = ?' , array ( $device [ 'device_id' ], $ipRoute [ 'ipRouteDest' ], $device [ 'context_name' ]));
if ( ! empty ( $oldRouteRow )) {
unset ( $oldRouteRow [ 'discoveredAt' ]);
$changeRoute = array ();
foreach ( $ipRoute as $key => $value ) {
if ( $oldRouteRow [ $key ] != $value ) {
$changeRoute [ $key ] = $value ;
2016-03-16 17:17:16 +01:00
}
}
2016-04-21 09:17:20 +02:00
if ( ! empty ( $changeRoute )) {
dbUpdate ( $changeRoute , 'route' , 'device_id = ? and ipRouteDest = ? and context_name = ?' , array ( $device [ 'device_id' ], $ipRoute [ 'ipRouteDest' ], $device [ 'context_name' ]));
}
} else {
$toInsert = array_merge ( $ipRoute , array ( 'device_id' => $device [ 'device_id' ], 'context_name' => $device [ 'context_name' ], 'discoveredAt' => time ()));
dbInsert ( $toInsert , 'route' );
2016-03-16 17:17:16 +01:00
}
}
2016-04-21 09:17:20 +02:00
// unset($tableRoute);
2016-03-16 17:17:16 +01:00
}
2016-04-21 09:17:20 +02:00
unset ( $vrfs_lite_cisco );
2016-03-16 17:17:16 +01:00
}