2012-01-19 08:49:26 +00:00
< ? php
2012-05-25 12:24:34 +00:00
// Pre-cache the existing state of VLANs for this device from the database
2018-04-24 21:20:21 -05:00
use LibreNMS\Config ;
2018-07-13 17:08:00 -05:00
$vlans_db = [];
2015-07-13 20:10:26 +02:00
$vlans_db_raw = dbFetchRows ( 'SELECT * FROM `vlans` WHERE `device_id` = ?' , array ( $device [ 'device_id' ]));
foreach ( $vlans_db_raw as $vlan_db ) {
$vlans_db [ $vlan_db [ 'vlan_domain' ]][ $vlan_db [ 'vlan_vlan' ]] = $vlan_db ;
2012-05-02 15:04:31 +00:00
}
2017-02-08 04:54:30 +00:00
unset (
$vlans_db_raw
);
2012-05-02 15:04:31 +00:00
2012-05-25 12:24:34 +00:00
// Create an empty array to record what VLANs we discover this session.
2012-05-02 15:04:31 +00:00
$device [ 'vlans' ] = array ();
2016-10-19 16:44:49 -05:00
$per_vlan_data = array (); // fill this with data for each vlan
$valid_vlan_port = array ();
// get a map of base port to ifIndex and the inverse
$base_to_index = array ();
$tmp_base_indexes = snmpwalk_cache_oid ( $device , 'dot1dBasePortIfIndex' , array (), 'BRIDGE-MIB' );
// flatten the array
foreach ( $tmp_base_indexes as $index => $array ) {
$base_to_index [ $index ] = $array [ 'dot1dBasePortIfIndex' ];
}
$index_to_base = array_flip ( $base_to_index );
2012-01-19 08:49:26 +00:00
2018-04-25 02:08:06 +01:00
if ( file_exists ( Config :: get ( 'install_dir' ) . " /includes/discovery/vlans/ { $device [ 'os' ] } .inc.php " )) {
include Config :: get ( 'install_dir' ) . " /includes/discovery/vlans/ { $device [ 'os' ] } .inc.php " ;
2017-08-22 20:52:01 +01:00
}
2017-08-28 20:24:43 +01:00
if ( empty ( $device [ 'vlans' ]) === true ) {
2017-05-13 23:06:14 +01:00
require 'includes/discovery/vlans/q-bridge-mib.inc.php' ;
require 'includes/discovery/vlans/cisco-vtp.inc.php' ;
}
2012-01-19 08:49:26 +00:00
2012-05-25 12:24:34 +00:00
// Fetch switchport <> VLAN relationships. This is DIRTY.
2015-07-13 20:10:26 +02:00
foreach ( $device [ 'vlans' ] as $domain_id => $vlans ) {
foreach ( $vlans as $vlan_id => $vlan ) {
2016-10-19 16:44:49 -05:00
// grab the populated data for this vlan
$vlan_data = $per_vlan_data [ $vlan_id ];
echo " VLAN $vlan_id \n " ;
if ( $vlan_data ) {
echo str_pad ( 'dot1d id' , 10 ) . str_pad ( 'ifIndex' , 10 ) . str_pad ( 'Port Name' , 25 ) . str_pad ( 'Priority' , 10 ) . str_pad ( 'State' , 15 ) . str_pad ( 'Cost' , 10 ) . " \n " ;
}
2018-07-13 17:08:00 -05:00
foreach (( array ) $vlan_data as $ifIndex => $vlan_port ) {
$port = get_port_by_index_cache ( $device [ 'device_id' ], $ifIndex );
2016-10-19 16:44:49 -05:00
echo str_pad ( $vlan_port_id , 10 ) . str_pad ( $ifIndex , 10 ) . str_pad ( $port [ 'ifDescr' ], 25 ) . str_pad ( $vlan_port [ 'dot1dStpPortPriority' ], 10 ) . str_pad ( $vlan_port [ 'dot1dStpPortState' ], 15 ) . str_pad ( $vlan_port [ 'dot1dStpPortPathCost' ], 10 );
$db_w = array (
'device_id' => $device [ 'device_id' ],
'port_id' => $port [ 'port_id' ],
'vlan' => $vlan_id ,
);
$db_a [ 'baseport' ] = $index_to_base [ $ifIndex ];
$db_a [ 'priority' ] = isset ( $vlan_port [ 'dot1dStpPortPriority' ]) ? $vlan_port [ 'dot1dStpPortPriority' ] : 0 ;
$db_a [ 'state' ] = isset ( $vlan_port [ 'dot1dStpPortState' ]) ? $vlan_port [ 'dot1dStpPortState' ] : 'unknown' ;
$db_a [ 'cost' ] = isset ( $vlan_port [ 'dot1dStpPortPathCost' ]) ? $vlan_port [ 'dot1dStpPortPathCost' ] : 0 ;
$db_a [ 'untagged' ] = isset ( $vlan_port [ 'untagged' ]) ? $vlan_port [ 'untagged' ] : 0 ;
2015-07-13 20:10:26 +02:00
2016-10-19 16:44:49 -05:00
$from_db = dbFetchRow ( 'SELECT * FROM `ports_vlans` WHERE device_id = ? AND port_id = ? AND `vlan` = ?' , array ( $device [ 'device_id' ], $port [ 'port_id' ], $vlan_id ));
2015-07-13 20:10:26 +02:00
2016-10-19 16:44:49 -05:00
if ( $from_db [ 'port_vlan_id' ]) {
$db_id = $from_db [ 'port_vlan_id' ];
dbUpdate ( $db_a , 'ports_vlans' , '`port_vlan_id` = ?' , array ( $db_id ));
echo 'Updated' ;
} else {
$db_id = dbInsert ( array_merge ( $db_w , $db_a ), 'ports_vlans' );
echo 'Inserted' ;
2015-07-13 20:10:26 +02:00
}
2017-04-15 14:08:49 -05:00
$valid_vlan_port [] = $db_id ;
2015-07-13 20:10:26 +02:00
2016-10-19 16:44:49 -05:00
echo PHP_EOL ;
} //end foreach
2015-07-13 20:10:26 +02:00
} //end foreach
} //end foreach
2016-10-19 16:44:49 -05:00
// remove non-existent vlans
2015-07-13 20:10:26 +02:00
foreach ( $vlans_db as $domain_id => $vlans ) {
foreach ( $vlans as $vlan_id => $vlan ) {
if ( empty ( $device [ 'vlans' ][ $domain_id ][ $vlan_id ])) {
dbDelete ( 'vlans' , '`device_id` = ? AND vlan_domain = ? AND vlan_vlan = ?' , array ( $device [ 'device_id' ], $domain_id , $vlan_id ));
2012-05-02 15:04:31 +00:00
}
}
2012-01-19 08:49:26 +00:00
}
2016-10-11 15:29:06 -06:00
// remove non-existent port-vlan mappings
2018-08-26 07:42:21 -05:00
if ( ! empty ( $valid_vlan_port )) {
$num = dbDelete ( 'ports_vlans' , '`device_id`=? AND `port_vlan_id` NOT IN ' . dbGenPlaceholders ( count ( $valid_vlan_port )), array_merge ([ $device [ 'device_id' ]], $valid_vlan_port ));
2017-01-07 17:32:38 +00:00
d_echo ( " Deleted $num vlan mappings \n " );
}
2016-10-11 15:29:06 -06:00
2012-05-02 15:04:31 +00:00
unset ( $device [ 'vlans' ]);
2016-10-19 16:44:49 -05:00
unset ( $base_to_index , $tmp_base_indexes , $index_to_base , $per_vlan_data , $valid_vlan_port , $num );