2012-01-19 08:49:26 +00:00
< ? php
echo ( " VLANs: \n " );
2012-05-25 12:24:34 +00:00
// Pre-cache the existing state of VLANs for this device from the database
2012-05-02 15:04:31 +00:00
$vlans_db_raw = dbFetchRows ( " SELECT * FROM `vlans` WHERE `device_id` = ? " , array ( $device [ 'device_id' ]));
2012-05-02 22:02:30 +00:00
foreach ( $vlans_db_raw as $vlan_db )
2012-05-02 15:04:31 +00:00
{
$vlans_db [ $vlan_db [ 'vlan_domain' ]][ $vlan_db [ 'vlan_vlan' ]] = $vlan_db ;
}
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 ();
2012-01-19 08:49:26 +00:00
2012-05-02 15:12:04 +00:00
include ( " includes/discovery/vlans/q-bridge-mib.inc.php " );
include ( " 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.
2012-05-02 15:04:31 +00:00
foreach ( $device [ 'vlans' ] as $domain_id => $vlans )
2012-01-19 08:49:26 +00:00
{
2012-05-02 15:04:31 +00:00
foreach ( $vlans as $vlan_id => $vlan )
2012-01-19 08:49:26 +00:00
{
2012-05-25 12:24:34 +00:00
// Pull Tables for this VLAN
2012-01-19 08:49:26 +00:00
2012-05-02 15:04:31 +00:00
#/usr/bin/snmpbulkwalk -v2c -c kglk5g3l454@988 -OQUs -m BRIDGE-MIB -M /opt/observium/mibs/ udp:sw2.ahf:161 dot1dStpPortEntry
#/usr/bin/snmpbulkwalk -v2c -c kglk5g3l454@988 -OQUs -m BRIDGE-MIB -M /opt/observium/mibs/ udp:sw2.ahf:161 dot1dBasePortEntry
2012-01-19 08:49:26 +00:00
2012-05-25 12:24:34 +00:00
// FIXME - do this only when vlan type == ethernet?
if ( is_numeric ( $vlan_id ) && ( $vlan_id < 1002 || $vlan_id > 1105 )) // Ignore reserved VLAN IDs
2012-01-19 14:00:29 +00:00
{
2012-05-25 12:24:34 +00:00
if ( $device [ 'os_group' ] == " cisco " || $device [ 'os' ] == " ios " ) // This shit only seems to work on IOS
2012-05-02 15:04:31 +00:00
{
2012-05-15 13:45:26 +00:00
# Probably does not work with snmpv3. I have no real idea about what this code is really doing
2012-05-02 15:04:31 +00:00
$vlan_device = array_merge ( $device , array ( 'community' => $device [ 'community' ] . " @ " . $vlan_id ));
2014-04-28 20:10:36 +10:00
$vlan_data = snmpwalk_cache_oid ( $vlan_device , " dot1dStpPortEntry " , array (), " BRIDGE-MIB:Q-BRIDGE-MIB " );
$vlan_data = snmpwalk_cache_oid ( $vlan_device , " dot1dBasePortEntry " , $vlan_data , " BRIDGE-MIB:Q-BRIDGE-MIB " );
2012-05-02 15:04:31 +00:00
}
2012-01-19 08:49:26 +00:00
2012-05-02 15:04:31 +00:00
echo ( " VLAN $vlan_id \n " );
2012-01-19 08:49:26 +00:00
2012-05-02 15:04:31 +00:00
if ( $vlan_data )
2012-01-19 08:49:26 +00:00
{
2012-05-02 15:04:31 +00:00
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 " );
2012-01-19 08:49:26 +00:00
}
2012-05-02 15:04:31 +00:00
foreach ( $vlan_data as $vlan_port_id => $vlan_port )
{
$port = get_port_by_index_cache ( $device , $vlan_port [ 'dot1dBasePortIfIndex' ]);
echo ( str_pad ( $vlan_port_id , 10 ) . str_pad ( $vlan_port [ 'dot1dBasePortIfIndex' ], 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' ],
2012-05-16 13:25:50 +00:00
'port_id' => $port [ 'port_id' ],
2012-05-02 15:04:31 +00:00
'vlan' => $vlan_id );
2015-02-16 10:50:22 -07:00
$db_a [ 'baseport' ] = $vlan_port_id ;
$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 ;
2012-05-02 15:04:31 +00:00
2012-05-16 13:25:50 +00: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-02-16 10:50:22 -07:00
2012-05-02 15:04:31 +00:00
if ( $from_db [ 'port_vlan_id' ])
{
2015-02-26 21:51:28 +00:00
dbUpdate ( $db_a , 'ports_vlans' , " `port_vlan_id` = ? " , array ( $from_db [ 'port_vlan_id' ]));
2012-05-02 15:04:31 +00:00
echo ( " Updated " );
} else {
dbInsert ( array_merge ( $db_w , $db_a ), 'ports_vlans' );
echo ( " Inserted " );
}
echo ( " \n " );
}
2012-01-19 08:49:26 +00:00
}
}
}
2012-05-02 22:02:30 +00:00
foreach ( $vlans_db as $domain_id => $vlans )
2012-01-19 08:49:26 +00:00
{
2012-05-02 22:02:30 +00:00
foreach ( $vlans as $vlan_id => $vlan )
2012-01-19 08:49:26 +00:00
{
2012-05-02 22:02:30 +00:00
if ( empty ( $device [ 'vlans' ][ $domain_id ][ $vlan_id ]))
2012-05-02 15:04:31 +00:00
{
dbDelete ( 'vlans' , " `device_id` = ? AND vlan_domain = ? AND vlan_vlan = ? " , array ( $device [ 'device_id' ], $domain_id , $vlan_id ));
}
2012-01-19 08:49:26 +00:00
}
}
2012-05-02 15:04:31 +00:00
unset ( $device [ 'vlans' ]);
2012-01-19 08:49:26 +00:00
?>