librenms/billing-calculate.php

143 lines
6.2 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/**
* LibreNMS
*
* This file is part of LibreNMS.
*
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
use LibreNMS\Util\Number;
$init_modules = [];
require __DIR__ . '/includes/init.php';
2015-07-13 18:10:26 +00:00
$options = getopt('r');
if (isset($options['r'])) {
echo "Clearing history table.\n";
dbQuery('TRUNCATE TABLE `bill_history`');
}
2015-07-13 18:10:26 +00:00
foreach (dbFetchRows('SELECT * FROM `bills` ORDER BY `bill_id`') as $bill) {
echo str_pad($bill['bill_id'] . ' ' . $bill['bill_name'], 30) . " \n";
2015-07-13 18:10:26 +00:00
$i = 0;
while ($i <= 24) {
unset($class);
unset($rate_data);
$day_data = getDates($bill['bill_day'], $i);
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
2015-07-13 18:10:26 +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
$period = getPeriod($bill['bill_id'], $datefrom, $dateto);
$date_updated = str_replace('-', '', str_replace(':', '', str_replace(' ', '', $check['updated'])));
// 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'];
2015-07-13 18:10:26 +00:00
if ($period > 0 && $dateto > $date_updated) {
$rate_data = getRates($bill['bill_id'], $datefrom, $dateto, $dir_95th);
$rate_95th = $rate_data['rate_95th'];
$dir_95th = $rate_data['dir_95th'];
$total_data = $rate_data['total_data'];
2015-07-13 18:10:26 +00:00
$rate_average = $rate_data['rate_average'];
if ($bill['bill_type'] == 'cdr') {
$type = 'CDR';
$allowed = $bill['bill_cdr'];
$used = $rate_data['rate_95th'];
$allowed_text = Number::formatSi($allowed, 2, 3, 'bps');
$used_text = Number::formatSi($used, 2, 3, 'bps');
$overuse = ($used - $allowed);
$overuse = (($overuse <= 0) ? '0' : $overuse);
$percent = round((($rate_data['rate_95th'] / $bill['bill_cdr']) * 100), 2);
} elseif ($bill['bill_type'] == 'quota') {
$type = 'Quota';
$allowed = $bill['bill_quota'];
$used = $rate_data['total_data'];
2015-07-13 18:10:26 +00:00
$allowed_text = format_bytes_billing($allowed);
$used_text = format_bytes_billing($used);
$overuse = ($used - $allowed);
$overuse = (($overuse <= 0) ? '0' : $overuse);
$percent = round((($rate_data['total_data'] / $bill['bill_quota']) * 100), 2);
2015-07-13 18:10:26 +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
if ($i == '0') {
$update = [
2015-07-13 18:10:26 +00:00
'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'],
'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
dbUpdate($update, 'bills', '`bill_id` = ?', [$bill['bill_id']]);
2015-07-13 18:10:26 +00:00
echo ' Updated! ';
}
if ($check['bill_id'] == $bill['bill_id']) {
$update = [
2015-07-13 18:10:26 +00:00
'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'],
'bill_used' => $used,
'bill_overuse' => $overuse,
'bill_percent' => $percent,
'updated' => ['NOW()'],
];
2015-07-13 18:10:26 +00:00
dbUpdate($update, 'bill_history', '`bill_hist_id` = ?', [$check['bill_hist_id']]);
2015-07-13 18:10:26 +00:00
echo ' Updated history! ';
} else {
$update = [
2015-07-13 18:10:26 +00:00
'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'],
'bill_datefrom' => $datefrom,
'bill_dateto' => $dateto,
'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
dbInsert($update, 'bill_history');
echo ' Generated history! ';
}//end if
echo "\n\n";
}//end if
$i++;
}//end while
}//end foreach