2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2008-03-09 21:13:27 +00:00
< ? php
2015-07-13 20:10:26 +02:00
/*
2013-10-28 12:01:36 -07:00
* Observium
2012-05-09 10:01:42 +00:00
*
2013-10-28 12:01:36 -07:00
* This file is part of Observium .
2012-05-09 10:01:42 +00:00
*
2013-10-28 12:01:36 -07:00
* @ package observium
* @ subpackage billing
* @ author Adam Armstrong < adama @ memetic . org >
* @ copyright ( C ) 2006 - 2012 Adam Armstrong
2012-05-09 10:01:42 +00:00
*/
2012-04-08 10:44:41 +00:00
chdir ( dirname ( $argv [ 0 ]));
2012-05-29 13:08:01 +00:00
// FIXME - implement cli switches, debugging, etc.
2015-07-13 20:10:26 +02:00
require 'includes/defaults.inc.php' ;
require 'config.php' ;
require 'includes/definitions.inc.php' ;
require 'includes/functions.php' ;
2012-05-29 13:08:01 +00:00
2015-07-13 20:10:26 +02:00
$iter = '0' ;
2008-03-09 21:13:27 +00:00
2016-08-22 10:32:05 -05:00
rrdtool_initialize ();
2011-10-01 14:54:06 +00:00
2016-03-02 18:38:39 +00:00
$poller_start = microtime ( true );
2015-07-13 20:10:26 +02:00
echo " Starting Polling Session ... \n \n " ;
2008-03-09 21:13:27 +00:00
2016-03-02 21:00:32 +00:00
// Wait for schema update, as running during update can break update
$dbVersion = dbFetchCell ( 'SELECT version FROM dbSchema' );
if ( $dbVersion < 107 ) {
logfile ( " BILLING: Cannot continue until dbSchema update to >= 107 is complete " );
exit ( 1 );
}
2015-07-13 20:10:26 +02:00
foreach ( dbFetchRows ( 'SELECT * FROM `bills`' ) as $bill_data ) {
echo 'Bill : ' . $bill_data [ 'bill_name' ] . " \n " ;
2011-12-06 22:17:37 +00:00
2015-07-13 20:10:26 +02:00
// replace old bill_gb with bill_quota (we're now storing bytes, not gigabytes)
if ( $bill_data [ 'bill_type' ] == 'quota' && ! is_numeric ( $bill_data [ 'bill_quota' ])) {
$bill_data [ 'bill_quota' ] = ( $bill_data [ 'bill_gb' ] * $config [ 'billing' ][ 'base' ] * $config [ 'billing' ][ 'base' ]);
dbUpdate ( array ( 'bill_quota' => $bill_data [ 'bill_quota' ]), 'bills' , '`bill_id` = ?' , array ( $bill_data [ 'bill_id' ]));
echo 'Quota -> ' . $bill_data [ 'bill_quota' ];
}
2011-12-06 22:17:37 +00:00
2015-07-13 20:10:26 +02:00
CollectData ( $bill_data [ 'bill_id' ]);
$iter ++ ;
2008-03-09 21:13:27 +00:00
}
2015-07-13 20:10:26 +02:00
2016-08-28 17:32:55 -05:00
function CollectData ( $bill_id )
{
2015-07-13 20:10:26 +02:00
$port_list = dbFetchRows ( 'SELECT * FROM `bill_ports` as P, `ports` as I, `devices` as D WHERE P.bill_id=? AND I.port_id = P.port_id AND D.device_id = I.device_id' , array ( $bill_id ));
2016-03-02 18:38:39 +00:00
2015-07-13 20:10:26 +02:00
$now = dbFetchCell ( 'SELECT NOW()' );
2016-03-02 18:38:39 +00:00
$delta = 0 ;
$in_delta = 0 ;
$out_delta = 0 ;
2015-07-13 20:10:26 +02:00
foreach ( $port_list as $port_data ) {
$port_id = $port_data [ 'port_id' ];
$host = $port_data [ 'hostname' ];
$port = $port_data [ 'port' ];
2016-03-02 18:38:39 +00:00
echo " Polling ${ port_data['ifName'] } ( ${ port_data['ifDescr'] } ) on ${ port_data['hostname'] } \n " ;
2015-07-13 20:10:26 +02:00
$port_data [ 'in_measurement' ] = getValue ( $port_data [ 'hostname' ], $port_data [ 'port' ], $port_data [ 'ifIndex' ], 'In' );
$port_data [ 'out_measurement' ] = getValue ( $port_data [ 'hostname' ], $port_data [ 'port' ], $port_data [ 'ifIndex' ], 'Out' );
2016-07-17 20:35:50 +01:00
$last_counters = getLastPortCounter ( $port_id , $bill_id );
2016-03-02 18:38:39 +00:00
if ( $last_counters [ 'state' ] == 'ok' ) {
$port_data [ 'last_in_measurement' ] = $last_counters [ in_counter ];
$port_data [ 'last_in_delta' ] = $last_counters [ in_delta ];
$port_data [ 'last_out_measurement' ] = $last_counters [ out_counter ];
$port_data [ 'last_out_delta' ] = $last_counters [ out_delta ];
2016-06-15 16:25:14 +00:00
$tmp_period = dbFetchCell ( " SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP(' " . mres ( $last_counters [ 'timestamp' ]) . " ') " );
2016-08-01 14:46:33 +02:00
if ( $port_data [ 'ifSpeed' ] > 0 && ( delta_to_bits ( $port_data [ 'in_measurement' ], $tmp_period ) - delta_to_bits ( $port_data [ 'last_in_measurement' ], $tmp_period )) > $port_data [ 'ifSpeed' ]) {
2016-06-15 16:25:14 +00:00
$port_data [ 'in_delta' ] = $port_data [ 'last_in_delta' ];
2016-08-28 17:32:55 -05:00
} elseif ( $port_data [ 'in_measurement' ] >= $port_data [ 'last_in_measurement' ]) {
2015-07-13 20:10:26 +02:00
$port_data [ 'in_delta' ] = ( $port_data [ 'in_measurement' ] - $port_data [ 'last_in_measurement' ]);
2016-08-28 17:32:55 -05:00
} else {
2015-07-13 20:10:26 +02:00
$port_data [ 'in_delta' ] = $port_data [ 'last_in_delta' ];
}
2016-03-02 18:38:39 +00:00
2016-08-01 14:46:33 +02:00
if ( $port_data [ 'ifSpeed' ] > 0 && ( delta_to_bits ( $port_data [ 'out_measurement' ], $tmp_period ) - delta_to_bits ( $port_data [ 'last_out_measurement' ], $tmp_period )) > $port_data [ 'ifSpeed' ]) {
2016-06-15 16:25:14 +00:00
$port_data [ 'out_delta' ] = $port_data [ 'last_out_delta' ];
2016-08-28 17:32:55 -05:00
} elseif ( $port_data [ 'out_measurement' ] >= $port_data [ 'last_out_measurement' ]) {
2015-07-13 20:10:26 +02:00
$port_data [ 'out_delta' ] = ( $port_data [ 'out_measurement' ] - $port_data [ 'last_out_measurement' ]);
2016-08-28 17:32:55 -05:00
} else {
2015-07-13 20:10:26 +02:00
$port_data [ 'out_delta' ] = $port_data [ 'last_out_delta' ];
}
2016-08-28 17:32:55 -05:00
} else {
2016-03-02 18:38:39 +00:00
$port_data [ 'in_delta' ] = '0' ;
2015-07-13 20:10:26 +02:00
$port_data [ 'out_delta' ] = '0' ;
}
2016-06-15 16:25:14 +00:00
2016-03-02 18:38:39 +00:00
$fields = array ( 'timestamp' => $now , 'in_counter' => $port_data [ 'in_measurement' ], 'out_counter' => $port_data [ 'out_measurement' ], 'in_delta' => $port_data [ 'in_delta' ], 'out_delta' => $port_data [ 'out_delta' ]);
2016-07-17 20:35:50 +01:00
if ( dbUpdate ( $fields , 'bill_port_counters' , " `port_id`=' " . mres ( $port_id ) . " ' AND `bill_id`=' $bill_id ' " ) == 0 ) {
$fields [ 'bill_id' ] = $bill_id ;
2016-03-02 18:38:39 +00:00
$fields [ 'port_id' ] = $port_id ;
dbInsert ( $fields , 'bill_port_counters' );
}
2015-07-13 20:10:26 +02:00
$delta = ( $delta + $port_data [ 'in_delta' ] + $port_data [ 'out_delta' ]);
$in_delta = ( $in_delta + $port_data [ 'in_delta' ]);
$out_delta = ( $out_delta + $port_data [ 'out_delta' ]);
} //end foreach
$last_data = getLastMeasurement ( $bill_id );
if ( $last_data [ state ] == 'ok' ) {
$prev_delta = $last_data [ delta ];
$prev_in_delta = $last_data [ in_delta ];
$prev_out_delta = $last_data [ out_delta ];
$prev_timestamp = $last_data [ timestamp ];
$period = dbFetchCell ( " SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP(' " . mres ( $prev_timestamp ) . " ') " );
2016-08-28 17:32:55 -05:00
} else {
2015-07-13 20:10:26 +02:00
$prev_delta = '0' ;
$period = '0' ;
$prev_in_delta = '0' ;
$prev_out_delta = '0' ;
2011-03-11 18:03:49 +00:00
}
2008-03-09 21:13:27 +00:00
2015-07-13 20:10:26 +02:00
if ( $delta < '0' ) {
$delta = $prev_delta ;
$in_delta = $prev_in_delta ;
$out_delta = $prev_out_delta ;
}
2016-04-21 11:37:46 +00:00
if ( ! empty ( $period ) && $period < '0' ) {
2015-07-13 20:10:26 +02:00
logfile ( " BILLING: negative period! id: $bill_id period: $period delta: $delta in_delta: $in_delta out_delta: $out_delta " );
2016-08-28 17:32:55 -05:00
} else {
2015-07-13 20:10:26 +02:00
dbInsert ( array ( 'bill_id' => $bill_id , 'timestamp' => $now , 'period' => $period , 'delta' => $delta , 'in_delta' => $in_delta , 'out_delta' => $out_delta ), 'bill_data' );
}
} //end CollectData()
2008-03-09 21:13:27 +00:00
2011-10-01 14:54:06 +00:00
2015-07-13 20:10:26 +02:00
if ( $argv [ 1 ]) {
CollectData ( $argv [ 1 ]);
}
2016-03-02 18:38:39 +00:00
$poller_end = microtime ( true );
$poller_run = ( $poller_end - $poller_start );
$poller_time = substr ( $poller_run , 0 , 5 );
dbInsert ( array ( 'type' => 'pollbill' , 'doing' => $doing , 'start' => $poller_start , 'duration' => $poller_time , 'devices' => 0 , 'poller' => $config [ 'distributed_poller_name' ] ), 'perf_times' );
if ( $poller_time > 300 ) {
logfile ( " BILLING: polling took longer than 5 minutes ( $poller_time seconds)! " );
}
echo " \n Completed in $poller_time sec \n " ;
2016-08-26 01:50:29 -05:00
rrdtool_close ();