2009-10-28 13:49:37 +00:00
< ? php
2017-02-23 22:45:50 +00:00
use LibreNMS\RRD\RrdDefinition ;
2012-04-09 10:53:55 +00:00
if ( $device [ 'os_group' ] == 'cisco' ) {
2012-05-16 13:25:50 +00:00
$acc_rows = dbFetchRows ( 'SELECT *, A.poll_time AS poll_time FROM `mac_accounting` as A, `ports` AS I where A.port_id = I.port_id AND I.device_id = ?' , [ $device [ 'device_id' ]]);
2015-07-13 20:10:26 +02:00
2017-12-02 17:18:45 -06:00
if ( ! empty ( $acc_rows )) {
$cip_oids = [
'cipMacHCSwitchedBytes' ,
'cipMacHCSwitchedPkts' ,
];
$cip_array = [];
2019-05-21 03:56:19 +02:00
foreach ( array_merge ( $cip_oids , [ 'cipMacSwitchedBytes' , 'cipMacSwitchedPkts' ]) as $oid ) {
2017-12-02 17:18:45 -06:00
echo " $oid " ;
$cip_array = snmpwalk_cache_cip ( $device , $oid , $cip_array , 'CISCO-IP-STAT-MIB' );
}
2019-05-21 03:56:19 +02:00
// Normalize cip_array
$cip_array = array_map ( function ( $entries ) {
return array_map ( function ( $entry ) {
$new_entry = [];
2020-09-21 15:43:38 +02:00
2019-05-21 03:56:19 +02:00
foreach ([ 'Bytes' , 'Pkts' ] as $unit ) {
$returned_oid = ( array_key_exists ( 'cipMacHCSwitched' . $unit , $entry )) ? 'cipMacHCSwitched' : 'cipMacSwitched' ;
$new_value = [];
2020-09-21 15:43:38 +02:00
2019-05-21 03:56:19 +02:00
foreach ( $entry [ $returned_oid . $unit ] as $key => $value ) {
$new_value [ $key ] = intval ( $value );
}
2020-09-21 15:43:38 +02:00
2019-05-21 03:56:19 +02:00
$new_entry [ 'cipMacHCSwitched' . $unit ] = $new_value ;
}
2020-09-21 15:43:38 +02:00
2019-05-21 03:56:19 +02:00
return $new_entry ;
}, $entries );
}, $cip_array );
2017-12-02 17:18:45 -06:00
$polled = time ();
$mac_entries = 0 ;
foreach ( $acc_rows as $acc ) {
$device_id = $acc [ 'device_id' ];
$ifIndex = $acc [ 'ifIndex' ];
$mac = $acc [ 'mac' ];
$polled_period = ( $polled - $acc [ 'poll_time' ]);
if ( $cip_array [ $ifIndex ][ $mac ]) {
$acc [ 'update' ][ 'poll_time' ] = $polled ;
$acc [ 'update' ][ 'poll_prev' ] = $acc [ 'poll_time' ];
$acc [ 'update' ][ 'poll_period' ] = $polled_period ;
$mac_entries ++ ;
2022-10-25 19:27:28 +02:00
$b_in = $cip_array [ $ifIndex ][ $mac ][ 'cipMacHCSwitchedBytes' ][ 'input' ] ? ? null ;
$b_out = $cip_array [ $ifIndex ][ $mac ][ 'cipMacHCSwitchedBytes' ][ 'output' ] ? ? null ;
$p_in = $cip_array [ $ifIndex ][ $mac ][ 'cipMacHCSwitchedPkts' ][ 'input' ] ? ? null ;
$p_out = $cip_array [ $ifIndex ][ $mac ][ 'cipMacHCSwitchedPkts' ][ 'output' ] ? ? null ;
2017-12-02 17:18:45 -06:00
$this_ma = & $cip_array [ $ifIndex ][ $mac ];
// Update metrics
foreach ( $cip_oids as $oid ) {
foreach ([ 'input' , 'output' ] as $dir ) {
$oid_dir = $oid . '_' . $dir ;
2022-10-25 19:27:28 +02:00
$acc [ 'update' ][ $oid_dir ] = $this_ma [ $oid ][ $dir ] ? ? null ;
2017-12-02 17:18:45 -06:00
$acc [ 'update' ][ $oid_dir . '_prev' ] = $acc [ $oid_dir ];
$oid_prev = $oid_dir . '_prev' ;
2022-10-25 19:27:28 +02:00
if ( isset ( $this_ma [ $oid ][ $dir ])) {
2017-12-02 17:18:45 -06:00
$oid_diff = ( $this_ma [ $oid ][ $dir ] - $acc [ $oid_dir ]);
$oid_rate = ( $oid_diff / $polled_period );
$acc [ 'update' ][ $oid_dir . '_rate' ] = $oid_rate ;
$acc [ 'update' ][ $oid_dir . '_delta' ] = $oid_diff ;
d_echo ( " \n $oid_dir ( $oid_diff B) $oid_rate Bps $polled_period secs \n " );
}
2015-07-13 20:10:26 +02:00
}
}
2012-04-09 15:30:45 +00:00
2022-10-25 19:27:28 +02:00
d_echo ( " \n Device id: " . $acc [ 'device_id' ] . ' -> ' . $acc [ 'ifDescr' ] . " $mac -> $b_in : $b_out : $p_in : $p_out " );
2017-12-02 17:18:45 -06:00
$rrd_name = [ 'cip' , $ifIndex , $mac ];
2019-05-21 03:56:19 +02:00
$rrd_def = RrdDefinition :: make ()
2017-12-02 17:18:45 -06:00
-> addDataset ( 'IN' , 'COUNTER' , 0 , 12500000000 )
-> addDataset ( 'OUT' , 'COUNTER' , 0 , 12500000000 )
-> addDataset ( 'PIN' , 'COUNTER' , 0 , 12500000000 )
-> addDataset ( 'POUT' , 'COUNTER' , 0 , 12500000000 );
// FIXME - use memcached to make sure these values don't go backwards?
$fields = [
'IN' => $b_in ,
'OUT' => $b_out ,
'PIN' => $p_in ,
'POUT' => $p_out ,
];
$tags = compact ( 'ifIndex' , 'mac' , 'rrd_name' , 'rrd_def' );
data_update ( $device , 'cip' , $tags , $fields );
if ( $acc [ 'update' ]) {
// Do Updates
dbUpdate ( $acc [ 'update' ], 'mac_accounting' , '`ma_id` = ?' , [ $acc [ 'ma_id' ]]);
} //end if
} //end if
} //end foreach
unset ( $cip_array );
if ( $mac_entries ) {
echo " $mac_entries MAC accounting entries \n " ;
}
}
2015-07-13 20:10:26 +02:00
echo " \n " ;
} //end if
2017-02-08 04:54:30 +00:00
unset (
$cip_oids ,
$oid ,
$polled ,
$mac_entries ,
$acc_rows
);