2011-11-02 18:26:57 +00:00
#!/usr/bin/env php
< ? php
2012-05-09 10:01:42 +00:00
/**
2016-09-08 13:12:23 +00:00
* LibreNMS
2012-05-09 10:01:42 +00:00
*
2016-09-08 13:12:23 +00:00
* This file is part of LibreNMS .
2012-05-09 10:01:42 +00:00
*
2013-10-28 19:01:36 +00:00
* @ copyright ( C ) 2006 - 2012 Adam Armstrong
2012-05-09 10:01:42 +00:00
*/
2021-03-28 22:25:30 +00:00
2023-04-22 00:52:47 +00:00
use LibreNMS\Billing ;
2021-03-28 22:25:30 +00:00
use LibreNMS\Util\Number ;
2016-11-21 20:12:59 +00:00
$init_modules = [];
require __DIR__ . '/includes/init.php' ;
2015-07-13 18:10:26 +00:00
2011-11-05 14:55:08 +00:00
$options = getopt ( 'r' );
2015-07-13 18:10:26 +00:00
2011-12-12 16:10:49 +00:00
if ( isset ( $options [ 'r' ])) {
2011-12-06 22:17:37 +00:00
echo " Clearing history table. \n " ;
2011-12-06 22:26:47 +00:00
dbQuery ( 'TRUNCATE TABLE `bill_history`' );
2011-12-06 22:17:37 +00:00
}
2011-11-02 18:26:57 +00:00
2011-12-12 16:10:49 +00:00
foreach ( dbFetchRows ( 'SELECT * FROM `bills` ORDER BY `bill_id`' ) as $bill ) {
2011-12-06 22:17:37 +00:00
echo str_pad ( $bill [ 'bill_id' ] . ' ' . $bill [ 'bill_name' ], 30 ) . " \n " ;
2015-07-13 18:10:26 +00:00
$i = 0 ;
2011-12-07 18:33:27 +00:00
while ( $i <= 24 ) {
2011-12-06 22:17:37 +00:00
unset ( $class );
unset ( $rate_data );
2023-04-22 00:52:47 +00:00
$day_data = Billing :: getDates ( $bill [ 'bill_day' ], $i );
2015-07-13 18:10:26 +00:00
2011-12-06 22:17:37 +00:00
$datefrom = $day_data [ '0' ];
$dateto = $day_data [ '1' ];
2015-07-13 18:10:26 +00:00
2011-11-02 18:26:57 +00:00
$check = dbFetchRow ( 'SELECT * FROM `bill_history` WHERE bill_id = ? AND bill_datefrom = ? AND bill_dateto = ? LIMIT 1' , [ $bill [ 'bill_id' ], $datefrom , $dateto ]);
2015-07-13 18:10:26 +00:00
2023-04-22 00:52:47 +00:00
$period = Billing :: getPeriod ( $bill [ 'bill_id' ], $datefrom , $dateto );
2015-07-13 18:10:26 +00:00
2011-12-06 22:17:37 +00:00
$date_updated = str_replace ( '-' , '' , str_replace ( ':' , '' , str_replace ( ' ' , '' , $check [ 'updated' ])));
2015-07-13 18:10:26 +00:00
2019-05-28 02:44:02 +00:00
// Send the current dir_95th to the getRates function so it knows to aggregate or return the max in/out value and highest direction
$dir_95th = $bill [ 'dir_95th' ];
2021-07-26 15:37:52 +00:00
if ( $period [ 'period' ] > 0 && $dateto > $date_updated ) {
2023-04-22 00:52:47 +00:00
$rate_data = Billing :: getRates ( $bill [ 'bill_id' ], $datefrom , $dateto , $dir_95th );
2011-11-02 18:26:57 +00:00
$rate_95th = $rate_data [ 'rate_95th' ];
$dir_95th = $rate_data [ 'dir_95th' ];
$total_data = $rate_data [ 'total_data' ];
$rate_average = $rate_data [ 'rate_average' ];
2015-07-13 18:10:26 +00:00
2011-11-02 18:26:57 +00:00
if ( $bill [ 'bill_type' ] == 'cdr' ) {
$type = 'CDR' ;
2011-12-06 22:17:37 +00:00
$allowed = $bill [ 'bill_cdr' ];
$used = $rate_data [ 'rate_95th' ];
2021-03-28 22:25:30 +00:00
$allowed_text = Number :: formatSi ( $allowed , 2 , 3 , 'bps' );
$used_text = Number :: formatSi ( $used , 2 , 3 , 'bps' );
2011-12-07 14:18:46 +00:00
$overuse = ( $used - $allowed );
$overuse = (( $overuse <= 0 ) ? '0' : $overuse );
2022-08-24 19:01:54 +00:00
$percent = Number :: calculatePercent ( $rate_data [ 'rate_95th' ], $bill [ 'bill_cdr' ]);
2016-08-28 22:32:55 +00:00
} elseif ( $bill [ 'bill_type' ] == 'quota' ) {
2011-11-02 18:26:57 +00:00
$type = 'Quota' ;
2011-12-06 22:17:37 +00:00
$allowed = $bill [ 'bill_quota' ];
$used = $rate_data [ 'total_data' ];
2023-04-22 00:52:47 +00:00
$allowed_text = Billing :: formatBytes ( $allowed );
$used_text = Billing :: formatBytes ( $used );
2011-12-07 14:18:46 +00:00
$overuse = ( $used - $allowed );
$overuse = (( $overuse <= 0 ) ? '0' : $overuse );
2022-08-24 19:01:54 +00:00
$percent = Number :: calculatePercent ( $rate_data [ 'total_data' ], $bill [ 'bill_quota' ]);
2015-07-13 18:10:26 +00:00
}
2011-12-12 12:16:57 +00:00
echo strftime ( '%x @ %X' , strtotime ( $datefrom )) . ' to ' . strftime ( '%x @ %X' , strtotime ( $dateto )) . ' ' . str_pad ( $type , 8 ) . ' ' . str_pad ( $allowed_text , 10 ) . ' ' . str_pad ( $used_text , 10 ) . ' ' . $percent . '%' ;
2015-07-13 18:10:26 +00:00
2012-04-06 15:19:43 +00:00
if ( $i == '0' ) {
2011-12-06 22:17:37 +00:00
$update = [
'rate_95th' => $rate_data [ 'rate_95th' ],
2011-11-05 14:55:08 +00:00
'rate_95th_in' => $rate_data [ 'rate_95th_in' ],
'rate_95th_out' => $rate_data [ 'rate_95th_out' ],
'dir_95th' => $rate_data [ 'dir_95th' ],
'total_data' => $rate_data [ 'total_data' ],
'total_data_in' => $rate_data [ 'total_data_in' ],
'total_data_out' => $rate_data [ 'total_data_out' ],
'rate_average' => $rate_data [ 'rate_average' ],
'rate_average_in' => $rate_data [ 'rate_average_in' ],
'rate_average_out' => $rate_data [ 'rate_average_out' ],
'bill_last_calc' => [ 'NOW()' ],
2015-07-13 18:10:26 +00:00
];
2011-12-06 22:17:37 +00:00
dbUpdate ( $update , 'bills' , '`bill_id` = ?' , [ $bill [ 'bill_id' ]]);
echo ' Updated! ' ;
2015-07-13 18:10:26 +00:00
}
2012-04-06 15:19:43 +00:00
if ( $check [ 'bill_id' ] == $bill [ 'bill_id' ]) {
2011-12-06 22:17:37 +00:00
$update = [
'rate_95th' => $rate_data [ 'rate_95th' ],
'rate_95th_in' => $rate_data [ 'rate_95th_in' ],
'rate_95th_out' => $rate_data [ 'rate_95th_out' ],
'dir_95th' => $rate_data [ 'dir_95th' ],
'rate_average' => $rate_data [ 'rate_average' ],
'rate_average_in' => $rate_data [ 'rate_average_in' ],
'rate_average_out' => $rate_data [ 'rate_average_out' ],
'traf_total' => $rate_data [ 'total_data' ],
'traf_in' => $rate_data [ 'total_data_in' ],
'traf_out' => $rate_data [ 'total_data_out' ],
2021-07-26 15:37:52 +00:00
'bill_peak_out' => $period [ 'peak_out' ],
'bill_peak_in' => $period [ 'peak_in' ],
2011-12-06 22:17:37 +00:00
'bill_used' => $used ,
'bill_overuse' => $overuse ,
'bill_percent' => $percent ,
'updated' => [ 'NOW()' ],
2015-07-13 18:10:26 +00:00
];
2011-12-12 16:10:49 +00:00
dbUpdate ( $update , 'bill_history' , '`bill_hist_id` = ?' , [ $check [ 'bill_hist_id' ]]);
2011-12-06 22:17:37 +00:00
echo ' Updated history! ' ;
2016-08-28 22:32:55 +00:00
} else {
2011-12-06 22:17:37 +00:00
$update = [
'rate_95th' => $rate_data [ 'rate_95th' ],
2011-11-05 14:55:08 +00:00
'rate_95th_in' => $rate_data [ 'rate_95th_in' ],
'rate_95th_out' => $rate_data [ 'rate_95th_out' ],
'dir_95th' => $rate_data [ 'dir_95th' ],
2011-11-05 15:00:09 +00:00
'rate_average' => $rate_data [ 'rate_average' ],
'rate_average_in' => $rate_data [ 'rate_average_in' ],
'rate_average_out' => $rate_data [ 'rate_average_out' ],
2011-11-05 14:55:08 +00:00
'traf_total' => $rate_data [ 'total_data' ],
'traf_in' => $rate_data [ 'total_data_in' ],
'traf_out' => $rate_data [ 'total_data_out' ],
2011-12-06 22:17:37 +00:00
'bill_datefrom' => $datefrom ,
'bill_dateto' => $dateto ,
2011-11-05 14:55:08 +00:00
'bill_type' => $type ,
'bill_allowed' => $allowed ,
'bill_used' => $used ,
'bill_overuse' => $overuse ,
'bill_percent' => $percent ,
'bill_id' => $bill [ 'bill_id' ],
2015-07-13 18:10:26 +00:00
];
2011-12-12 16:10:49 +00:00
dbInsert ( $update , 'bill_history' );
2011-12-06 22:17:37 +00:00
echo ' Generated history! ' ;
2021-07-26 15:37:52 +00:00
} //end if
2011-12-06 22:17:37 +00:00
echo " \n \n " ;
2021-07-26 15:37:52 +00:00
} //end if
2015-07-13 18:10:26 +00:00
$i ++ ;
2021-07-26 15:37:52 +00:00
} //end while
2015-07-13 18:10:26 +00:00
} //end foreach