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
/*
2016-09-08 14:12:23 +01:00
* LibreNMS
2012-05-09 10:01:42 +00:00
*
2016-09-08 14:12:23 +01:00
* This file is part of LibreNMS .
2012-05-09 10:01:42 +00:00
*
2016-09-08 14:12:23 +01:00
* @ package LibreNMS
2013-10-28 12:01:36 -07:00
* @ subpackage billing
* @ copyright ( C ) 2006 - 2012 Adam Armstrong
2012-05-09 10:01:42 +00:00
*/
2020-03-16 09:17:58 -05:00
use LibreNMS\Data\Store\Datastore ;
2020-09-21 14:54:51 +02:00
$init_modules = [];
2016-11-21 14:12:59 -06:00
require __DIR__ . '/includes/init.php' ;
2012-05-29 13:08:01 +00:00
2018-08-25 15:57:08 -05:00
if ( isset ( $argv [ 1 ]) && is_numeric ( $argv [ 1 ])) {
// allow old cli style
$options = [ 'b' => $argv [ 1 ]];
} else {
$options = getopt ( 'db:' );
}
2011-10-01 14:54:06 +00:00
2018-08-25 15:57:08 -05:00
set_debug ( isset ( $options [ 'd' ]));
2020-03-16 09:17:58 -05:00
Datastore :: init ();
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
2018-08-25 15:57:08 -05:00
if ( get_db_schema () < 107 ) {
2020-09-21 15:59:34 +02:00
logfile ( 'BILLING: Cannot continue until the database schema update to >= 107 is complete' );
2016-03-02 21:00:32 +00:00
exit ( 1 );
}
2018-08-25 15:57:08 -05:00
$poller_start = microtime ( true );
echo " Starting Polling Session ... \n \n " ;
2011-12-06 22:17:37 +00:00
2018-08-25 15:57:08 -05:00
$query = \LibreNMS\DB\Eloquent :: DB () -> table ( 'bills' );
2011-12-06 22:17:37 +00:00
2018-08-25 15:57:08 -05:00
if ( isset ( $options [ 'b' ])) {
$query -> where ( 'bill_id' , $options [ 'b' ]);
2008-03-09 21:13:27 +00:00
}
2018-08-25 15:57:08 -05:00
foreach ( $query -> get ([ 'bill_id' , 'bill_name' ]) as $bill ) {
2020-09-21 14:54:51 +02:00
echo 'Bill : ' . $bill -> bill_name . " \n " ;
2018-08-25 15:57:08 -05:00
$bill_id = $bill -> bill_id ;
2015-07-13 20:10:26 +02:00
2020-09-21 14:54:51 +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' , [ $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' ];
2020-09-21 14:54:51 +02:00
$host = $port_data [ 'hostname' ];
$port = $port_data [ 'port' ];
2015-07-13 20:10:26 +02:00
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
2020-09-21 14:54:51 +02:00
$port_data [ 'in_measurement' ] = getValue ( $port_data [ 'hostname' ], $port_data [ 'port' ], $port_data [ 'ifIndex' ], 'In' );
2015-07-13 20:10:26 +02:00
$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' ) {
2020-09-21 14:54:51 +02:00
$port_data [ 'last_in_measurement' ] = $last_counters [ 'in_counter' ];
$port_data [ 'last_in_delta' ] = $last_counters [ 'in_delta' ];
2018-07-13 17:08:00 -05:00
$port_data [ 'last_out_measurement' ] = $last_counters [ 'out_counter' ];
2020-09-21 14:54:51 +02:00
$port_data [ 'last_out_delta' ] = $last_counters [ 'out_delta' ];
2016-03-02 18:38:39 +00:00
2020-09-21 14:54:51 +02:00
$tmp_period = dbFetchCell ( " SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP(' " . mres ( $last_counters [ 'timestamp' ]) . " ') " );
2016-06-15 16:25:14 +00:00
2020-09-21 14:54:51 +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' ];
}
2018-07-13 17:08:00 -05:00
2020-09-21 14:54:51 +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' ;
}
2020-01-24 22:44:55 +11:00
//////////////////////////////////CountersValidation$DB-Update
echo " \n DB SNMP counters received. \n " ;
2020-09-21 15:59:34 +02:00
echo ' in_measurement: ' , $port_data [ 'in_measurement' ], ' out_measurement: ' , $port_data [ 'out_measurement' ], " \n " ;
echo ' The data types are --> in_measurement:' . gettype ( $port_data [ 'in_measurement' ]) . ' and out_measurement: ' . gettype ( $port_data [ 'out_measurement' ]) . " \n " ;
2020-01-24 22:44:55 +11:00
//For debugging
2020-09-21 14:54:51 +02:00
logfile ( " \n **** $now : " . $bill -> bill_name . " \n DB SNMP counters received. " );
2020-09-21 15:59:34 +02:00
logfile ( 'in_measurement: ' . $port_data [ 'in_measurement' ] . ' out_measurement: ' . $port_data [ 'out_measurement' ] . " \n The data types are. in_measurement: " . gettype ( $port_data [ 'in_measurement' ]) . ' and out_measurement: ' . gettype ( $port_data [ 'out_measurement' ]));
logfile ( 'IN_delta: ' . $port_data [ 'in_delta' ] . ' OUT_delta: ' . $port_data [ 'out_delta' ] . " \n Last_IN_delta: " . $port_data [ 'last_in_delta' ] . ' last_OUT_delta: ' . $port_data [ 'last_out_delta' ]);
2020-01-24 22:44:55 +11:00
if ( is_numeric ( $port_data [ 'in_measurement' ]) && is_numeric ( $port_data [ 'out_measurement' ])) {
echo " Nice, valid counters 'in/out_measurement', lets use them \n " ;
logfile ( " Nice, valid counters 'in/out_measurement', lets use them " );
// NOTE: casting to string for mysqli bug (fixed by mysqlnd)
2020-09-21 14:54:51 +02:00
$fields = [ 'timestamp' => $now , 'in_counter' => ( string ) set_numeric ( $port_data [ 'in_measurement' ]), 'out_counter' => ( string ) set_numeric ( $port_data [ 'out_measurement' ]), 'in_delta' => ( string ) set_numeric ( $port_data [ 'in_delta' ]), 'out_delta' => ( string ) set_numeric ( $port_data [ 'out_delta' ])];
2020-01-24 22:44:55 +11:00
if ( dbUpdate ( $fields , 'bill_port_counters' , " `port_id`=' " . mres ( $port_id ) . " ' AND `bill_id`=' $bill_id ' " ) == 0 ) {
$fields [ 'bill_id' ] = $bill_id ;
$fields [ 'port_id' ] = $port_id ;
dbInsert ( $fields , 'bill_port_counters' );
}
} else {
echo " WATCH out! - Wrong counters. Table 'bill_port_counters' not updated \n " ;
logfile ( " WATCH out! - Wrong counters. Table 'bill_port_counters' not updated " );
2016-03-02 18:38:39 +00:00
}
2020-01-24 22:44:55 +11:00
////////////////////////////////EndCountersValidation&DB-Update
2020-09-21 14:54:51 +02:00
$delta = ( $delta + $port_data [ 'in_delta' ] + $port_data [ 'out_delta' ]);
$in_delta = ( $in_delta + $port_data [ 'in_delta' ]);
2015-07-13 20:10:26 +02:00
$out_delta = ( $out_delta + $port_data [ 'out_delta' ]);
} //end foreach
$last_data = getLastMeasurement ( $bill_id );
2018-07-13 17:08:00 -05:00
if ( $last_data [ 'state' ] == 'ok' ) {
2020-09-21 14:54:51 +02:00
$prev_delta = $last_data [ 'delta' ];
$prev_in_delta = $last_data [ 'in_delta' ];
2018-07-13 17:08:00 -05:00
$prev_out_delta = $last_data [ 'out_delta' ];
$prev_timestamp = $last_data [ 'timestamp' ];
2020-09-21 14:54:51 +02:00
$period = dbFetchCell ( " SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP(' " . mres ( $prev_timestamp ) . " ') " );
2016-08-28 17:32:55 -05:00
} else {
2020-09-21 14:54:51 +02:00
$prev_delta = '0' ;
$period = '0' ;
$prev_in_delta = '0' ;
2015-07-13 20:10:26 +02:00
$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' ) {
2020-09-21 14:54:51 +02:00
$delta = $prev_delta ;
$in_delta = $prev_in_delta ;
2015-07-13 20:10:26 +02:00
$out_delta = $prev_out_delta ;
}
2020-09-21 14:54:51 +02: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 {
2018-08-20 18:00:02 -05:00
// NOTE: casting to string for mysqli bug (fixed by mysqlnd)
2020-09-21 14:54:51 +02:00
dbInsert ([ 'bill_id' => $bill_id , 'timestamp' => $now , 'period' => $period , 'delta' => ( string ) $delta , 'in_delta' => ( string ) $in_delta , 'out_delta' => ( string ) $out_delta ], 'bill_data' );
2015-07-13 20:10:26 +02:00
}
} //end CollectData()
2008-03-09 21:13:27 +00:00
2020-09-21 14:54:51 +02:00
$poller_end = microtime ( true );
$poller_run = ( $poller_end - $poller_start );
2016-03-02 18:38:39 +00:00
$poller_time = substr ( $poller_run , 0 , 5 );
2019-06-23 00:29:12 -05:00
dbInsert ([
'type' => 'pollbill' ,
'doing' => $doing ,
'start' => $poller_start ,
'duration' => $poller_time ,
'devices' => 0 ,
2020-09-21 14:54:51 +02:00
'poller' => \LibreNMS\Config :: get ( 'distributed_poller_name' ),
2019-06-23 00:29:12 -05:00
], 'perf_times' );
2016-03-02 18:38:39 +00:00
if ( $poller_time > 300 ) {
logfile ( " BILLING: polling took longer than 5 minutes ( $poller_time seconds)! " );
}
echo " \n Completed in $poller_time sec \n " ;
2020-03-16 09:17:58 -05:00
Datastore :: terminate ();