forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
billing-calculate.php
executable file
·158 lines (136 loc) · 6.64 KB
/
billing-calculate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env php
<?php
/**
* LibreNMS
*
* This file is part of LibreNMS.
*
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
use LibreNMS\Billing;
use LibreNMS\Config;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Number;
$init_modules = [];
require __DIR__ . '/includes/init.php';
$options = getopt('frd');
if (isset($options['r'])) {
echo "Clearing history table.\n";
DB::table('bill_history')->truncate();
}
Debug::set(isset($options['d']));
$scheduler = Config::get('schedule_type.billing');
if (! isset($options['f']) && $scheduler != 'legacy' && $scheduler != 'cron') {
if (Debug::isEnabled()) {
echo "Billing is not enabled for cron scheduling. Add the -f command ar
gument if you want to force this command to run.\n";
}
exit(0);
}
foreach (dbFetchRows('SELECT * FROM `bills` ORDER BY `bill_id`') as $bill) {
echo str_pad($bill['bill_id'] . ' ' . $bill['bill_name'], 30) . " \n";
$i = 0;
while ($i <= 24) {
unset($class);
unset($rate_data);
$day_data = Billing::getDates($bill['bill_day'], $i);
$datefrom = $day_data['0'];
$dateto = $day_data['1'];
$check = dbFetchRow('SELECT * FROM `bill_history` WHERE bill_id = ? AND bill_datefrom = ? AND bill_dateto = ? LIMIT 1', [$bill['bill_id'], $datefrom, $dateto]);
$period = Billing::getPeriod($bill['bill_id'], $datefrom, $dateto);
$date_updated = $check ? str_replace(['-', ':', ' '], '', $check['updated']) : 0;
// 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'];
if ($period['period'] > 0 && $dateto > $date_updated) {
$rate_data = Billing::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'];
$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, 0, 'bps');
$used_text = Number::formatSi($used, 2, 0, 'bps');
$overuse = ($used - $allowed);
$overuse = (($overuse <= 0) ? '0' : $overuse);
$percent = Number::calculatePercent($rate_data['rate_95th'], $bill['bill_cdr']);
} elseif ($bill['bill_type'] == 'quota') {
$type = 'Quota';
$allowed = $bill['bill_quota'];
$used = $rate_data['total_data'];
$allowed_text = Billing::formatBytes($allowed);
$used_text = Billing::formatBytes($used);
$overuse = ($used - $allowed);
$overuse = (($overuse <= 0) ? '0' : $overuse);
$percent = Number::calculatePercent($rate_data['total_data'], $bill['bill_quota']);
}
echo \Carbon\Carbon::parse($datefrom)->toDateTimeString() . ' to ' . \Carbon\Carbon::parse($dateto)->toDateTimeString() . ' ' . str_pad($type, 8) . ' ' . str_pad($allowed_text, 10) . ' ' . str_pad($used_text, 10) . ' ' . $percent . '%';
if ($i == '0') {
$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'],
'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()'],
];
dbUpdate($update, 'bills', '`bill_id` = ?', [$bill['bill_id']]);
echo ' Updated! ';
}
if (isset($check['bill_id']) && $check['bill_id'] == $bill['bill_id']) {
$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'],
'bill_peak_out' => $period['peak_out'],
'bill_peak_in' => $period['peak_in'],
'bill_used' => $used,
'bill_overuse' => $overuse,
'bill_percent' => $percent,
'updated' => ['NOW()'],
];
dbUpdate($update, 'bill_history', '`bill_hist_id` = ?', [$check['bill_hist_id']]);
echo ' Updated history! ';
} else {
$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'],
'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'],
];
dbInsert($update, 'bill_history');
echo ' Generated history! ';
} //end if
echo "\n\n";
} //end if
$i++;
} //end while
}//end foreach