2009-08-12 00:07:29 +00:00
#!/usr/bin/php
2009-03-24 13:32:47 +00:00
< ? php
include ( " config.php " );
include ( " includes/functions.php " );
$mac_accounting_query = mysql_query ( " SELECT * FROM `mac_accounting` as A, `interfaces` AS I, `devices` AS D where A.interface_id = I.interface_id AND I.device_id = D.device_id AND D.status = '1' " );
while ( $acc = mysql_fetch_array ( $mac_accounting_query )) {
2009-05-03 22:32:01 +00:00
echo ( " Polling : " . $acc [ 'ip' ] . " " . $acc [ 'ifDescr' ] . " " . $acc [ 'mac' ] . " " . $acc [ 'hostname' ] . " " );
$mac = $acc [ 'mac' ];
$oid = hexdec ( substr ( $mac , 0 , 2 ));
$oid .= " . " . hexdec ( substr ( $mac , 2 , 2 ));
$oid .= " . " . hexdec ( substr ( $mac , 4 , 2 ));
$oid .= " . " . hexdec ( substr ( $mac , 6 , 2 ));
$oid .= " . " . hexdec ( substr ( $mac , 8 , 2 ));
$oid .= " . " . hexdec ( substr ( $mac , 10 , 2 ));
2009-03-24 13:32:47 +00:00
2009-04-23 21:13:56 +00:00
$snmp_cmd = $config [ 'snmpget' ] . " -m CISCO-IP-STAT-MIB -O Uqnv - " . $acc [ 'snmpver' ] . " -c " . $acc [ 'community' ] . " " . $acc [ 'hostname' ];
2009-05-03 22:32:01 +00:00
$snmp_cmd .= " cipMacSwitchedBytes. " . $acc [ 'ifIndex' ] . " .input. " . $oid ;
$snmp_cmd .= " cipMacSwitchedBytes. " . $acc [ 'ifIndex' ] . " .output. " . $oid ;
2009-03-24 13:32:47 +00:00
2009-05-03 22:32:01 +00:00
$snmp_output = trim ( shell_exec ( $snmp_cmd ));
$snmp_output = preg_replace ( " [a-zA-Z \ ] " , " " , $snmp_output );
2009-03-24 13:32:47 +00:00
list ( $in , $out ) = explode ( " \n " , $snmp_output );
$acc_rrd = $config [ 'rrd_dir' ] . " / " . $acc [ 'hostname' ] . " /mac-accounting " ;
2009-05-03 22:32:01 +00:00
2009-03-24 13:32:47 +00:00
if ( ! is_dir ( $acc_rrd )) { mkdir ( $acc_rrd ); echo ( " Created directory : $acc_rrd\n " ); }
2009-05-03 22:32:01 +00:00
$old_rrdfile = $acc_rrd . " / " . $acc [ 'ifIndex' ] . " - " . $acc [ 'ip' ] . " .rrd " ;
$rrdfile = $acc_rrd . " / " . $acc [ 'ifIndex' ] . " - " . $acc [ 'mac' ] . " .rrd " ;
if ( is_file ( $old_rrdfile ) && ! is_file ( $rrdfile )) { rename ( $old_rrdfile , $rrdfile ); echo ( " Moved $old_rrdfile -> $rrdfile \n " ); };
2009-03-24 13:32:47 +00:00
if ( ! is_file ( $rrdfile )) {
$woo = shell_exec ( $config [ 'rrdtool' ] . " create $rrdfile \
DS : IN : COUNTER : 600 : 0 : 12500000000 \
DS : OUT : COUNTER : 600 : 0 : 12500000000 \
RRA : AVERAGE : 0.5 : 1 : 600 \
RRA : AVERAGE : 0.5 : 6 : 700 \
RRA : AVERAGE : 0.5 : 24 : 775 \
RRA : AVERAGE : 0.5 : 288 : 797 \
RRA : MAX : 0.5 : 1 : 600 \
RRA : MAX : 0.5 : 6 : 700 \
RRA : MAX : 0.5 : 24 : 775 \
RRA : MAX : 0.5 : 288 : 797 " );
}
2009-05-03 22:32:01 +00:00
$woo = " N: " . ( $in + 0 ) . " : " . ( $out + 0 );
2009-03-24 13:32:47 +00:00
$ret = rrdtool_update ( " $rrdfile " , $woo );
2009-04-23 21:13:56 +00:00
$rates = interface_rates ( $rrdfile );
mysql_query ( " UPDATE `mac_accounting` SET bps_in = ' " . $rates [ 'in' ] . " ', bps_out = ' " . $rates [ 'out' ] . " ' WHERE ma_id= ' " . $acc [ 'ma_id' ] . " ' " );
echo ( formatRates ( $rates [ 'in' ]) . " in " . formatRates ( $rates [ 'out' ]) . " out \n " );
2009-03-24 13:32:47 +00:00
}
?>