diff --git a/LibreNMS/Billing.php b/LibreNMS/Billing.php new file mode 100644 index 0000000000..77cabed3a2 --- /dev/null +++ b/LibreNMS/Billing.php @@ -0,0 +1,473 @@ + $dayofmonth) { + // Billing day is past, so it is next month + $date_end = date_create($year . '-' . $month . '-' . $dayofmonth); + $date_start = date_create($year . '-' . $month . '-' . $dayofmonth); + date_add($date_end, date_interval_create_from_date_string('1 month')); + } else { + // Billing day will happen this month, therefore started last month + $date_end = date_create($year . '-' . $month . '-' . $dayofmonth); + $date_start = date_create($year . '-' . $month . '-' . $dayofmonth); + date_sub($date_start, date_interval_create_from_date_string('1 month')); + } + + if ($months > 0) { + date_sub($date_start, date_interval_create_from_date_string($months . ' month')); + date_sub($date_end, date_interval_create_from_date_string($months . ' month')); + } + + // date_sub($date_start, date_interval_create_from_date_string('1 month')); + date_sub($date_end, date_interval_create_from_date_string('1 day')); + + $date_from = date_format($date_start, 'Ymd') . '000000'; + $date_to = date_format($date_end, 'Ymd') . '235959'; + + date_sub($date_start, date_interval_create_from_date_string('1 month')); + date_sub($date_end, date_interval_create_from_date_string('1 month')); + + $last_from = date_format($date_start, 'Ymd') . '000000'; + $last_to = date_format($date_end, 'Ymd') . '235959'; + + $return = []; + $return['0'] = $date_from; + $return['1'] = $date_to; + $return['2'] = $last_from; + $return['3'] = $last_to; + + return $return; + } + + public static function getPredictedUsage($bill_day, $cur_used): float|int + { + $tmp = self::getDates($bill_day, 0); + $start = new DateTime($tmp[0], new DateTimeZone(date_default_timezone_get())); + $end = new DateTime($tmp[1], new DateTimeZone(date_default_timezone_get())); + $now = new DateTime(date('Y-m-d'), new DateTimeZone(date_default_timezone_get())); + $total = $end->diff($start)->format('%a'); + $since = $now->diff($start)->format('%a'); + + return $cur_used / $since * $total; + } + + public static function getValue($host, $port, $id, $inout): int + { + $oid = 'IF-MIB::ifHC' . $inout . 'Octets.' . $id; + $device = dbFetchRow('SELECT * from `devices` WHERE `hostname` = ? LIMIT 1', [$host]); + $value = snmp_get($device, $oid, '-Oqv'); + + if (! is_numeric($value)) { + $oid = 'IF-MIB::if' . $inout . 'Octets.' . $id; + $value = snmp_get($device, $oid, '-Oqv'); + } + + return (int) $value; + } + + public static function getLastPortCounter($port_id, $bill_id): array + { + $return = []; + $row = dbFetchRow('SELECT timestamp, in_counter, in_delta, out_counter, out_delta FROM bill_port_counters WHERE `port_id` = ? AND `bill_id` = ?', [$port_id, $bill_id]); + if (! is_null($row)) { + $return['timestamp'] = $row['timestamp']; + $return['in_counter'] = $row['in_counter']; + $return['in_delta'] = $row['in_delta']; + $return['out_counter'] = $row['out_counter']; + $return['out_delta'] = $row['out_delta']; + $return['state'] = 'ok'; + } else { + $return['state'] = 'failed'; + } + + return $return; + } + + public static function getLastMeasurement($bill_id): array + { + $return = []; + $row = dbFetchRow('SELECT timestamp,delta,in_delta,out_delta FROM bill_data WHERE bill_id = ? ORDER BY timestamp DESC LIMIT 1', [$bill_id]); + if (! is_null($row)) { + $return['delta'] = $row['delta']; + $return['in_delta'] = $row['in_delta']; + $return['out_delta'] = $row['out_delta']; + $return['timestamp'] = $row['timestamp']; + $return['state'] = 'ok'; + } else { + $return['state'] = 'failed'; + } + + return $return; + } + + private static function get95thagg($bill_id, $datefrom, $dateto): float + { + $mq_sql = 'SELECT count(delta) FROM bill_data WHERE bill_id = ?'; + $mq_sql .= ' AND timestamp > ? AND timestamp <= ?'; + $measurements = dbFetchCell($mq_sql, [$bill_id, $datefrom, $dateto]); + $measurement_95th = (round($measurements / 100 * 95) - 1); + + $q_95_sql = 'SELECT (delta / period * 8) AS rate FROM bill_data WHERE bill_id = ?'; + $q_95_sql .= ' AND timestamp > ? AND timestamp <= ? ORDER BY rate ASC'; + $a_95th = dbFetchColumn($q_95_sql, [$bill_id, $datefrom, $dateto]); + $m_95th = $a_95th[$measurement_95th]; + + return round($m_95th, 2); + } + + private static function get95thIn($bill_id, $datefrom, $dateto): float + { + $mq_sql = 'SELECT count(delta) FROM bill_data WHERE bill_id = ?'; + $mq_sql .= ' AND timestamp > ? AND timestamp <= ?'; + $measurements = dbFetchCell($mq_sql, [$bill_id, $datefrom, $dateto]); + $measurement_95th = (round($measurements / 100 * 95) - 1); + + $q_95_sql = 'SELECT (in_delta / period * 8) AS rate FROM bill_data WHERE bill_id = ?'; + $q_95_sql .= ' AND timestamp > ? AND timestamp <= ? ORDER BY rate ASC'; + $a_95th = dbFetchColumn($q_95_sql, [$bill_id, $datefrom, $dateto]); + $m_95th = $a_95th[$measurement_95th]; + + return round($m_95th, 2); + } + + private static function get95thout($bill_id, $datefrom, $dateto): float + { + $mq_sql = 'SELECT count(delta) FROM bill_data WHERE bill_id = ?'; + $mq_sql .= ' AND timestamp > ? AND timestamp <= ?'; + $measurements = dbFetchCell($mq_sql, [$bill_id, $datefrom, $dateto]); + $measurement_95th = (round($measurements / 100 * 95) - 1); + + $q_95_sql = 'SELECT (out_delta / period * 8) AS rate FROM bill_data WHERE bill_id = ?'; + $q_95_sql .= ' AND timestamp > ? AND timestamp <= ? ORDER BY rate ASC'; + $a_95th = dbFetchColumn($q_95_sql, [$bill_id, $datefrom, $dateto]); + $m_95th = $a_95th[$measurement_95th]; + + return round($m_95th, 2); + } + + public static function getRates($bill_id, $datefrom, $dateto, $dir_95th): array + { + $data = []; + + $sum_data = self::getSum($bill_id, $datefrom, $dateto); + $mtot = $sum_data['total']; + $mtot_in = $sum_data['inbound']; + $mtot_out = $sum_data['outbound']; + $ptot = $sum_data['period']; + + $data['rate_95th_in'] = self::get95thIn($bill_id, $datefrom, $dateto); + $data['rate_95th_out'] = self::get95thout($bill_id, $datefrom, $dateto); + + if ($dir_95th == 'agg') { + $data['rate_95th'] = self::get95thagg($bill_id, $datefrom, $dateto); + $data['dir_95th'] = 'agg'; + } else { + if ($data['rate_95th_out'] > $data['rate_95th_in']) { + $data['rate_95th'] = $data['rate_95th_out']; + $data['dir_95th'] = 'out'; + } else { + $data['rate_95th'] = $data['rate_95th_in']; + $data['dir_95th'] = 'in'; + } + } + + $data['total_data'] = $mtot; + $data['total_data_in'] = $mtot_in; + $data['total_data_out'] = $mtot_out; + $data['rate_average'] = ! empty($ptot) ? ($mtot / $ptot * 8) : 0; + $data['rate_average_in'] = ! empty($ptot) ? ($mtot_in / $ptot * 8) : 0; + $data['rate_average_out'] = ! empty($ptot) ? ($mtot_out / $ptot * 8) : 0; + + return $data; + } + + private static function getSum($bill_id, $datefrom, $dateto) + { + $sum = dbFetchRow('SELECT SUM(period) as period, SUM(delta) as total, SUM(in_delta) as inbound, SUM(out_delta) as outbound FROM bill_data WHERE bill_id = ? AND timestamp > ? AND timestamp <= ?', [$bill_id, $datefrom, $dateto]); + + return $sum; + } + + public static function getPeriod($bill_id, $datefrom, $dateto): array + { + $ptot = dbFetchRow('SELECT SUM(period) as `period`, MAX(in_delta) as `peak_in`, MAX(out_delta) as `peak_out` FROM bill_data WHERE bill_id = ? AND timestamp > ? AND timestamp <= ?', [$bill_id, $datefrom, $dateto]); + + return $ptot; + } + + public static function getHistoryBitsGraphData($bill_id, $bill_hist_id, $reducefactor): ?array + { + $histrow = dbFetchRow('SELECT UNIX_TIMESTAMP(bill_datefrom) as `from`, UNIX_TIMESTAMP(bill_dateto) AS `to`, rate_95th, rate_average, bill_type FROM bill_history WHERE bill_id = ? AND bill_hist_id = ?', [$bill_id, $bill_hist_id]); + + if (is_null($histrow)) { + return null; + } + + $graph_data = self::getBitsGraphData($bill_id, $histrow['from'], $histrow['to'], $reducefactor); + + // Overwrite the rate data with the historical version + $graph_data['rate_95th'] = $histrow['rate_95th']; + $graph_data['rate_average'] = $histrow['rate_average']; + $graph_data['bill_type'] = $histrow['bill_type']; + + return $graph_data; + } + + public static function getBitsGraphData($bill_id, $from, $to, $reducefactor): array + { + $i = '0'; + $iter = 0; + $first = null; + $last = null; + $iter_in = 0; + $iter_out = 0; + $iter_period = 0; + $max_in = 0; + $max_out = 0; + $tot_in = 0; + $tot_out = 0; + $tot_period = 0; + $in_delta = null; + $out_delta = null; + $period = null; + $in_data = []; + $out_data = []; + $tot_data = []; + $ticks = []; + + if (! isset($reducefactor) || ! is_numeric($reducefactor) || $reducefactor < 1) { + // Auto calculate reduce factor + $expectedpoints = ceil(($to - $from) / 300); + $desiredpoints = 400; + $reducefactor = max(1, floor($expectedpoints / $desiredpoints)); + } + + $bill_data = dbFetchRow('SELECT * from `bills` WHERE `bill_id`= ? LIMIT 1', [$bill_id]); + + foreach (dbFetch('SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bill_data WHERE bill_id = ? AND `timestamp` >= FROM_UNIXTIME( ? ) AND `timestamp` <= FROM_UNIXTIME( ? ) ORDER BY timestamp ASC', [$bill_id, $from, $to]) as $row) { + $timestamp = $row['formatted_date']; + if (! $first) { + $first = $timestamp; + } + + $period = $row['period']; + $in_delta = $row['in_delta'] * 8; + $out_delta = $row['out_delta'] * 8; + $last = $timestamp; + + $iter_in += $in_delta; + $iter_out += $out_delta; + $iter_period += $period; + + if ($period > 0) { + $max_in = max($max_in, $in_delta / $period); + $max_out = max($max_out, $out_delta / $period); + $tot_in += $in_delta; + $tot_out += $out_delta; + $tot_period += $period; + + if (++$iter >= $reducefactor) { + $out_data[$i] = round($iter_out / $iter_period, 2); + $in_data[$i] = round($iter_in / $iter_period, 2); + $tot_data[$i] = ($out_data[$i] + $in_data[$i]); + $ticks[$i] = $timestamp; + $i++; + $iter = 0; + $iter_out = 0; + $iter_in = 0; + $iter_period = 0; + } + } + }//end foreach + + if (! empty($iter_in)) { // Write last element + $out_data[$i] = round($iter_out / $iter_period, 2); + $in_data[$i] = round($iter_in / $iter_period, 2); + $tot_data[$i] = ($out_data[$i] + $in_data[$i]); + $ticks[$i] = $timestamp ?? time(); + $i++; + } + $result = [ + 'from' => $from, + 'to' => $to, + 'first' => $first, + 'last' => $last, + + 'in_data' => $in_data, + 'out_data' => $out_data, + 'tot_data' => $tot_data, + 'ticks' => $ticks, + + 'rate_95th' => $bill_data['rate_95th'], + 'rate_average' => $bill_data['rate_average'], + 'bill_type' => $bill_data['bill_type'], + ]; + + if ($period) { + $result['max_in'] = $max_in; + $result['max_out'] = $max_out; + $result['ave_in'] = $tot_in / $tot_period; + $result['ave_out'] = $tot_out / $tot_period; + $result['last_in'] = $in_delta / $period; + $result['last_out'] = $out_delta / $period; + } + + return $result; + } + + public static function getHistoricTransferGraphData($bill_id): array + { + $i = '0'; + + $in_data = []; + $out_data = []; + $tot_data = []; + $allow_data = []; + $ave_data = []; + $overuse_data = []; + $ticklabels = []; + $allowed_val = null; + + foreach (dbFetchRows('SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` DESC LIMIT 12', [$bill_id]) as $data) { + $datefrom = date('Y-m-d', strtotime($data['bill_datefrom'])); + $dateto = date('Y-m-d', strtotime($data['bill_dateto'])); + $datelabel = $datefrom . ' - ' . $dateto; + + array_push($ticklabels, $datelabel); + array_push($in_data, $data['traf_in']); + array_push($out_data, $data['traf_out']); + array_push($tot_data, $data['traf_total']); + array_push($allow_data, $allowed_val = ($data['bill_type'] == 'Quota' ? $data['bill_allowed'] : 0)); + array_push($overuse_data, $data['bill_type'] == 'Quota' ? $data['bill_overuse'] : 0); + $i++; + }//end foreach + + if ($i < 12) { + $y = (12 - $i); + for ($x = 0; $x < $y; $x++) { + $allowed = (($x == '0') ? $allowed_val : '0'); + array_push($in_data, '0'); + array_push($out_data, '0'); + array_push($tot_data, '0'); + array_push($allow_data, $allowed); + array_push($overuse_data, '0'); + array_push($ticklabels, ''); + } + } + + $graph_name = 'Historical bandwidth over the last 12 billing periods'; + + return [ + 'graph_name' => $graph_name, + 'in_data' => $in_data, + 'out_data' => $out_data, + 'tot_data' => $tot_data, + 'allow_data' => $allow_data, + 'ave_data' => $ave_data, + 'overuse_data' => $overuse_data, + 'ticklabels' => $ticklabels, + ]; + } + + public static function getBandwidthGraphData($bill_id, $bill_hist_id, $from, $to, $imgtype): ?array + { + if (is_numeric($bill_hist_id)) { + $histrow = dbFetchRow('SELECT UNIX_TIMESTAMP(bill_datefrom) as `from`, UNIX_TIMESTAMP(bill_dateto) AS `to`, rate_95th, rate_average FROM bill_history WHERE bill_id = ? AND bill_hist_id = ?', [$bill_id, $bill_hist_id]); + + if (is_null($histrow)) { + return null; + } + $from = $histrow['from']; + $to = $histrow['to']; + } else { + if (! is_numeric($from) || ! is_numeric($to)) { + throw new \Exception('Must supply from and to if bill_hist_id is not supplied'); + } + } + + $in_data = []; + $out_data = []; + $tot_data = []; + $allow_data = []; + $ave_data = []; + $overuse_data = []; + $ticklabels = []; + + $data = []; + $average = 0; + if ($imgtype == 'day') { + foreach (dbFetch('SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY DATE(timestamp) ORDER BY timestamp ASC', [$bill_id, $from, $to]) as $data) { + array_push($ticklabels, date('Y-m-d', $data['timestamp'])); + array_push($in_data, isset($data['traf_in']) ? $data['traf_in'] : 0); + array_push($out_data, isset($data['traf_out']) ? $data['traf_out'] : 0); + array_push($tot_data, isset($data['traf_total']) ? $data['traf_total'] : 0); + $average += $data['traf_total']; + } + + $ave_count = count($tot_data); + + // Add empty items for the days not yet passed + $days = (date('j', $to - $from) - $ave_count - 1); + for ($x = 0; $x < $days; $x++) { + array_push($ticklabels, ''); + array_push($in_data, 0); + array_push($out_data, 0); + array_push($tot_data, 0); + } + } elseif ($imgtype == 'hour') { + foreach (dbFetch('SELECT DISTINCT HOUR(timestamp) as hour, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY HOUR(timestamp) ORDER BY HOUR(timestamp) ASC', [$bill_id, $from, $to]) as $data) { + array_push($ticklabels, sprintf('%02d', $data['hour']) . ':00'); + array_push($in_data, isset($data['traf_in']) ? $data['traf_in'] : 0); + array_push($out_data, isset($data['traf_out']) ? $data['traf_out'] : 0); + array_push($tot_data, isset($data['traf_total']) ? $data['traf_total'] : 0); + $average += $data['traf_total']; + } + + $ave_count = count($tot_data); + } else { + exit("Unknown graph type $imgtype"); + }//end if + + $average = ($average / $ave_count); + $tot_data_size = count($tot_data); + for ($x = 0; $x <= $tot_data_size; $x++) { + array_push($ave_data, $average); + } + + $graph_name = date('M j g:ia', $from) . ' - ' . date('M j g:ia', $to); + + return [ + 'graph_name' => $graph_name, + 'in_data' => $in_data, + 'out_data' => $out_data, + 'tot_data' => $tot_data, + 'allow_data' => $allow_data, + 'ave_data' => $ave_data, + 'overuse_data' => $overuse_data, + 'ticklabels' => $ticklabels, + ]; + } +} diff --git a/billing-calculate.php b/billing-calculate.php index dd6f024d0d..f02c940c21 100755 --- a/billing-calculate.php +++ b/billing-calculate.php @@ -9,6 +9,7 @@ * @copyright (C) 2006 - 2012 Adam Armstrong */ +use LibreNMS\Billing; use LibreNMS\Util\Number; $init_modules = []; @@ -28,14 +29,14 @@ foreach (dbFetchRows('SELECT * FROM `bills` ORDER BY `bill_id`') as $bill) { while ($i <= 24) { unset($class); unset($rate_data); - $day_data = getDates($bill['bill_day'], $i); + $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 = getPeriod($bill['bill_id'], $datefrom, $dateto); + $period = Billing::getPeriod($bill['bill_id'], $datefrom, $dateto); $date_updated = str_replace('-', '', str_replace(':', '', str_replace(' ', '', $check['updated']))); @@ -43,7 +44,7 @@ foreach (dbFetchRows('SELECT * FROM `bills` ORDER BY `bill_id`') as $bill) { $dir_95th = $bill['dir_95th']; if ($period['period'] > 0 && $dateto > $date_updated) { - $rate_data = getRates($bill['bill_id'], $datefrom, $dateto, $dir_95th); + $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']; @@ -62,8 +63,8 @@ foreach (dbFetchRows('SELECT * FROM `bills` ORDER BY `bill_id`') as $bill) { $type = 'Quota'; $allowed = $bill['bill_quota']; $used = $rate_data['total_data']; - $allowed_text = format_bytes_billing($allowed); - $used_text = format_bytes_billing($used); + $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']); diff --git a/includes/billing.php b/includes/billing.php deleted file mode 100644 index 6f62434d26..0000000000 --- a/includes/billing.php +++ /dev/null @@ -1,474 +0,0 @@ - $dayofmonth) { - // Billing day is past, so it is next month - $date_end = date_create($year . '-' . $month . '-' . $dayofmonth); - $date_start = date_create($year . '-' . $month . '-' . $dayofmonth); - date_add($date_end, date_interval_create_from_date_string('1 month')); - } else { - // Billing day will happen this month, therefore started last month - $date_end = date_create($year . '-' . $month . '-' . $dayofmonth); - $date_start = date_create($year . '-' . $month . '-' . $dayofmonth); - date_sub($date_start, date_interval_create_from_date_string('1 month')); - } - - if ($months > 0) { - date_sub($date_start, date_interval_create_from_date_string($months . ' month')); - date_sub($date_end, date_interval_create_from_date_string($months . ' month')); - } - - // date_sub($date_start, date_interval_create_from_date_string('1 month')); - date_sub($date_end, date_interval_create_from_date_string('1 day')); - - $date_from = date_format($date_start, 'Ymd') . '000000'; - $date_to = date_format($date_end, 'Ymd') . '235959'; - - date_sub($date_start, date_interval_create_from_date_string('1 month')); - date_sub($date_end, date_interval_create_from_date_string('1 month')); - - $last_from = date_format($date_start, 'Ymd') . '000000'; - $last_to = date_format($date_end, 'Ymd') . '235959'; - - $return = []; - $return['0'] = $date_from; - $return['1'] = $date_to; - $return['2'] = $last_from; - $return['3'] = $last_to; - - return $return; -}//end getDates() - -function getPredictedUsage($bill_day, $cur_used) -{ - $tmp = getDates($bill_day, 0); - $start = new DateTime($tmp[0], new DateTimeZone(date_default_timezone_get())); - $end = new DateTime($tmp[1], new DateTimeZone(date_default_timezone_get())); - $now = new DateTime(date('Y-m-d'), new DateTimeZone(date_default_timezone_get())); - $total = $end->diff($start)->format('%a'); - $since = $now->diff($start)->format('%a'); - - return $cur_used / $since * $total; -} - -function getValue($host, $port, $id, $inout) -{ - $oid = 'IF-MIB::ifHC' . $inout . 'Octets.' . $id; - $device = dbFetchRow('SELECT * from `devices` WHERE `hostname` = ? LIMIT 1', [$host]); - $value = snmp_get($device, $oid, '-Oqv'); - - if (! is_numeric($value)) { - $oid = 'IF-MIB::if' . $inout . 'Octets.' . $id; - $value = snmp_get($device, $oid, '-Oqv'); - } - - return $value; -}//end getValue() - -function getLastPortCounter($port_id, $bill_id) -{ - $return = []; - $row = dbFetchRow('SELECT timestamp, in_counter, in_delta, out_counter, out_delta FROM bill_port_counters WHERE `port_id` = ? AND `bill_id` = ?', [$port_id, $bill_id]); - if (! is_null($row)) { - $return['timestamp'] = $row['timestamp']; - $return['in_counter'] = $row['in_counter']; - $return['in_delta'] = $row['in_delta']; - $return['out_counter'] = $row['out_counter']; - $return['out_delta'] = $row['out_delta']; - $return['state'] = 'ok'; - } else { - $return['state'] = 'failed'; - } - - return $return; -}//end getLastPortCounter() - -function getLastMeasurement($bill_id) -{ - $return = []; - $row = dbFetchRow('SELECT timestamp,delta,in_delta,out_delta FROM bill_data WHERE bill_id = ? ORDER BY timestamp DESC LIMIT 1', [$bill_id]); - if (! is_null($row)) { - $return['delta'] = $row['delta']; - $return['in_delta'] = $row['in_delta']; - $return['out_delta'] = $row['out_delta']; - $return['timestamp'] = $row['timestamp']; - $return['state'] = 'ok'; - } else { - $return['state'] = 'failed'; - } - - return $return; -}//end getLastMeasurement() - -function get95thagg($bill_id, $datefrom, $dateto) -{ - $mq_sql = 'SELECT count(delta) FROM bill_data WHERE bill_id = ?'; - $mq_sql .= ' AND timestamp > ? AND timestamp <= ?'; - $measurements = dbFetchCell($mq_sql, [$bill_id, $datefrom, $dateto]); - $measurement_95th = (round($measurements / 100 * 95) - 1); - - $q_95_sql = 'SELECT (delta / period * 8) AS rate FROM bill_data WHERE bill_id = ?'; - $q_95_sql .= ' AND timestamp > ? AND timestamp <= ? ORDER BY rate ASC'; - $a_95th = dbFetchColumn($q_95_sql, [$bill_id, $datefrom, $dateto]); - $m_95th = $a_95th[$measurement_95th]; - - return round($m_95th, 2); -}//end get95thagg() - -function get95thIn($bill_id, $datefrom, $dateto) -{ - $mq_sql = 'SELECT count(delta) FROM bill_data WHERE bill_id = ?'; - $mq_sql .= ' AND timestamp > ? AND timestamp <= ?'; - $measurements = dbFetchCell($mq_sql, [$bill_id, $datefrom, $dateto]); - $measurement_95th = (round($measurements / 100 * 95) - 1); - - $q_95_sql = 'SELECT (in_delta / period * 8) AS rate FROM bill_data WHERE bill_id = ?'; - $q_95_sql .= ' AND timestamp > ? AND timestamp <= ? ORDER BY rate ASC'; - $a_95th = dbFetchColumn($q_95_sql, [$bill_id, $datefrom, $dateto]); - $m_95th = $a_95th[$measurement_95th]; - - return round($m_95th, 2); -}//end get95thIn() - -function get95thout($bill_id, $datefrom, $dateto) -{ - $mq_sql = 'SELECT count(delta) FROM bill_data WHERE bill_id = ?'; - $mq_sql .= ' AND timestamp > ? AND timestamp <= ?'; - $measurements = dbFetchCell($mq_sql, [$bill_id, $datefrom, $dateto]); - $measurement_95th = (round($measurements / 100 * 95) - 1); - - $q_95_sql = 'SELECT (out_delta / period * 8) AS rate FROM bill_data WHERE bill_id = ?'; - $q_95_sql .= ' AND timestamp > ? AND timestamp <= ? ORDER BY rate ASC'; - $a_95th = dbFetchColumn($q_95_sql, [$bill_id, $datefrom, $dateto]); - $m_95th = $a_95th[$measurement_95th]; - - return round($m_95th, 2); -}//end get95thout() - -function getRates($bill_id, $datefrom, $dateto, $dir_95th) -{ - $data = []; - - $sum_data = getSum($bill_id, $datefrom, $dateto); - $mtot = $sum_data['total']; - $mtot_in = $sum_data['inbound']; - $mtot_out = $sum_data['outbound']; - $ptot = $sum_data['period']; - - $data['rate_95th_in'] = get95thIn($bill_id, $datefrom, $dateto); - $data['rate_95th_out'] = get95thout($bill_id, $datefrom, $dateto); - - if ($dir_95th == 'agg') { - $data['rate_95th'] = get95thagg($bill_id, $datefrom, $dateto); - $data['dir_95th'] = 'agg'; - } else { - if ($data['rate_95th_out'] > $data['rate_95th_in']) { - $data['rate_95th'] = $data['rate_95th_out']; - $data['dir_95th'] = 'out'; - } else { - $data['rate_95th'] = $data['rate_95th_in']; - $data['dir_95th'] = 'in'; - } - } - - $data['total_data'] = $mtot; - $data['total_data_in'] = $mtot_in; - $data['total_data_out'] = $mtot_out; - $data['rate_average'] = ! empty($ptot) ? ($mtot / $ptot * 8) : 0; - $data['rate_average_in'] = ! empty($ptot) ? ($mtot_in / $ptot * 8) : 0; - $data['rate_average_out'] = ! empty($ptot) ? ($mtot_out / $ptot * 8) : 0; - - // print_r($data); - return $data; -}//end getRates() - -function getTotal($bill_id, $datefrom, $dateto) -{ - $mtot = dbFetchCell('SELECT SUM(delta) FROM bill_data WHERE bill_id = ? AND timestamp > ? AND timestamp <= ?', [$bill_id, $datefrom, $dateto]); - - return $mtot; -}//end getTotal() - -function getSum($bill_id, $datefrom, $dateto) -{ - $sum = dbFetchRow('SELECT SUM(period) as period, SUM(delta) as total, SUM(in_delta) as inbound, SUM(out_delta) as outbound FROM bill_data WHERE bill_id = ? AND timestamp > ? AND timestamp <= ?', [$bill_id, $datefrom, $dateto]); - - return $sum; -}//end getSum() - -function getPeriod($bill_id, $datefrom, $dateto) -{ - $ptot = dbFetchRow('SELECT SUM(period) as `period`, MAX(in_delta) as `peak_in`, MAX(out_delta) as `peak_out` FROM bill_data WHERE bill_id = ? AND timestamp > ? AND timestamp <= ?', [$bill_id, $datefrom, $dateto]); - - return $ptot; -}//end getPeriod() - -function getBillingHistoryBitsGraphData($bill_id, $bill_hist_id, $reducefactor) -{ - $histrow = dbFetchRow('SELECT UNIX_TIMESTAMP(bill_datefrom) as `from`, UNIX_TIMESTAMP(bill_dateto) AS `to`, rate_95th, rate_average, bill_type FROM bill_history WHERE bill_id = ? AND bill_hist_id = ?', [$bill_id, $bill_hist_id]); - - if (is_null($histrow)) { - return null; - } - - $graph_data = getBillingBitsGraphData($bill_id, $histrow['from'], $histrow['to'], $reducefactor); - - // Overwrite the rate data with the historical version - $graph_data['rate_95th'] = $histrow['rate_95th']; - $graph_data['rate_average'] = $histrow['rate_average']; - $graph_data['bill_type'] = $histrow['bill_type']; - - return $graph_data; -} - -function getBillingBitsGraphData($bill_id, $from, $to, $reducefactor) -{ - $i = '0'; - $iter = 0; - $first = null; - $last = null; - $iter_in = 0; - $iter_out = 0; - $iter_period = 0; - $max_in = 0; - $max_out = 0; - $tot_in = 0; - $tot_out = 0; - $tot_period = 0; - $in_delta = null; - $out_delta = null; - $period = null; - $in_data = []; - $out_data = []; - $tot_data = []; - $ticks = []; - - if (! isset($reducefactor) || ! is_numeric($reducefactor) || $reducefactor < 1) { - // Auto calculate reduce factor - $expectedpoints = ceil(($to - $from) / 300); - $desiredpoints = 400; - $reducefactor = max(1, floor($expectedpoints / $desiredpoints)); - } - - $bill_data = dbFetchRow('SELECT * from `bills` WHERE `bill_id`= ? LIMIT 1', [$bill_id]); - - foreach (dbFetch('SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bill_data WHERE bill_id = ? AND `timestamp` >= FROM_UNIXTIME( ? ) AND `timestamp` <= FROM_UNIXTIME( ? ) ORDER BY timestamp ASC', [$bill_id, $from, $to]) as $row) { - $timestamp = $row['formatted_date']; - if (! $first) { - $first = $timestamp; - } - - $period = $row['period']; - $in_delta = $row['in_delta'] * 8; - $out_delta = $row['out_delta'] * 8; - $last = $timestamp; - - $iter_in += $in_delta; - $iter_out += $out_delta; - $iter_period += $period; - - if ($period > 0) { - $max_in = max($max_in, $in_delta / $period); - $max_out = max($max_out, $out_delta / $period); - $tot_in += $in_delta; - $tot_out += $out_delta; - $tot_period += $period; - - if (++$iter >= $reducefactor) { - $out_data[$i] = round($iter_out / $iter_period, 2); - $in_data[$i] = round($iter_in / $iter_period, 2); - $tot_data[$i] = ($out_data[$i] + $in_data[$i]); - $ticks[$i] = $timestamp; - $i++; - $iter = 0; - unset($iter_out, $iter_in, $iter_period); - } - } - }//end foreach - - if (! empty($iter_in)) { // Write last element - $out_data[$i] = round($iter_out / $iter_period, 2); - $in_data[$i] = round($iter_in / $iter_period, 2); - $tot_data[$i] = ($out_data[$i] + $in_data[$i]); - $ticks[$i] = $timestamp; - $i++; - } - $result = [ - 'from' => $from, - 'to' => $to, - 'first' => $first, - 'last' => $last, - - 'in_data' => $in_data, - 'out_data' => $out_data, - 'tot_data' => $tot_data, - 'ticks' => $ticks, - - 'rate_95th' => $bill_data['rate_95th'], - 'rate_average' => $bill_data['rate_average'], - 'bill_type' => $bill_data['bill_type'], - ]; - - if ($period) { - $result['max_in'] = $max_in; - $result['max_out'] = $max_out; - $result['ave_in'] = $tot_in / $tot_period; - $result['ave_out'] = $tot_out / $tot_period; - $result['last_in'] = $in_delta / $period; - $result['last_out'] = $out_delta / $period; - } - - return $result; -}//end getBillingBitsGraphData - -function getHistoricTransferGraphData($bill_id) -{ - $i = '0'; - - $in_data = []; - $out_data = []; - $tot_data = []; - $allow_data = []; - $ave_data = []; - $overuse_data = []; - $ticklabels = []; - $allowed_val = null; - - foreach (dbFetchRows('SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` DESC LIMIT 12', [$bill_id]) as $data) { - $datefrom = date('Y-m-d', strtotime($data['bill_datefrom'])); - $dateto = date('Y-m-d', strtotime($data['bill_dateto'])); - $datelabel = $datefrom . ' - ' . $dateto; - - array_push($ticklabels, $datelabel); - array_push($in_data, $data['traf_in']); - array_push($out_data, $data['traf_out']); - array_push($tot_data, $data['traf_total']); - array_push($allow_data, $allowed_val = ($data['bill_type'] == 'Quota' ? $data['bill_allowed'] : 0)); - array_push($overuse_data, $data['bill_type'] == 'Quota' ? $data['bill_overuse'] : 0); - $i++; - }//end foreach - - if ($i < 12) { - $y = (12 - $i); - for ($x = 0; $x < $y; $x++) { - $allowed = (($x == '0') ? $allowed_val : '0'); - array_push($in_data, '0'); - array_push($out_data, '0'); - array_push($tot_data, '0'); - array_push($allow_data, $allowed); - array_push($overuse_data, '0'); - array_push($ticklabels, ''); - } - } - - $graph_name = 'Historical bandwidth over the last 12 billing periods'; - - return [ - 'graph_name' => $graph_name, - 'in_data' => $in_data, - 'out_data' => $out_data, - 'tot_data' => $tot_data, - 'allow_data' => $allow_data, - 'ave_data' => $ave_data, - 'overuse_data' => $overuse_data, - 'ticklabels' => $ticklabels, - ]; -} - -function getBillingBandwidthGraphData($bill_id, $bill_hist_id, $from, $to, $imgtype) -{ - if (is_numeric($bill_hist_id)) { - $histrow = dbFetchRow('SELECT UNIX_TIMESTAMP(bill_datefrom) as `from`, UNIX_TIMESTAMP(bill_dateto) AS `to`, rate_95th, rate_average FROM bill_history WHERE bill_id = ? AND bill_hist_id = ?', [$bill_id, $bill_hist_id]); - - if (is_null($histrow)) { - return null; - } - $from = $histrow['from']; - $to = $histrow['to']; - } else { - if (! is_numeric($from) || ! is_numeric($to)) { - exit('Must supply from and to if bill_hist_id is not supplied'); - } - } - - $in_data = []; - $out_data = []; - $tot_data = []; - $allow_data = []; - $ave_data = []; - $overuse_data = []; - $ticklabels = []; - - $data = []; - $average = 0; - if ($imgtype == 'day') { - foreach (dbFetch('SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY DATE(timestamp) ORDER BY timestamp ASC', [$bill_id, $from, $to]) as $data) { - array_push($ticklabels, date('Y-m-d', $data['timestamp'])); - array_push($in_data, isset($data['traf_in']) ? $data['traf_in'] : 0); - array_push($out_data, isset($data['traf_out']) ? $data['traf_out'] : 0); - array_push($tot_data, isset($data['traf_total']) ? $data['traf_total'] : 0); - $average += $data['traf_total']; - } - - $ave_count = count($tot_data); - - // Add empty items for the days not yet passed - $days = (date('j', date($to - $from)) - $ave_count - 1); - for ($x = 0; $x < $days; $x++) { - array_push($ticklabels, ''); - array_push($in_data, 0); - array_push($out_data, 0); - array_push($tot_data, 0); - } - } elseif ($imgtype == 'hour') { - foreach (dbFetch('SELECT DISTINCT HOUR(timestamp) as hour, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY HOUR(timestamp) ORDER BY HOUR(timestamp) ASC', [$bill_id, $from, $to]) as $data) { - array_push($ticklabels, sprintf('%02d', $data['hour']) . ':00'); - array_push($in_data, isset($data['traf_in']) ? $data['traf_in'] : 0); - array_push($out_data, isset($data['traf_out']) ? $data['traf_out'] : 0); - array_push($tot_data, isset($data['traf_total']) ? $data['traf_total'] : 0); - $average += $data['traf_total']; - } - - $ave_count = count($tot_data); - } else { - exit("Unknown graph type $imgtype"); - }//end if - - $average = ($average / $ave_count); - $tot_data_size = count($tot_data); - for ($x = 0; $x <= $tot_data_size; $x++) { - array_push($ave_data, $average); - } - - $graph_name = date('M j g:ia', $from) . ' - ' . date('M j g:ia', $to); - - return [ - 'graph_name' => $graph_name, - 'in_data' => $in_data, - 'out_data' => $out_data, - 'tot_data' => $tot_data, - 'allow_data' => $allow_data, - 'ave_data' => $ave_data, - 'overuse_data' => $overuse_data, - 'ticklabels' => $ticklabels, - ]; -} -//end getBillingBandwidthGraphData diff --git a/includes/html/api_functions.inc.php b/includes/html/api_functions.inc.php index 3a7e679a1c..6e4e16a3c0 100644 --- a/includes/html/api_functions.inc.php +++ b/includes/html/api_functions.inc.php @@ -34,6 +34,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; use LibreNMS\Alerting\QueryBuilderParser; +use LibreNMS\Billing; use LibreNMS\Config; use LibreNMS\Exceptions\InvalidIpException; use LibreNMS\Exceptions\InvalidTableColumnException; @@ -1582,15 +1583,15 @@ function list_bills(Illuminate\Http\Request $request) $overuse = $rate_data['rate_95th'] - $bill['bill_cdr']; $overuse = (($overuse <= 0) ? '-' : Number::formatSi($overuse, 2, 3, '')); } elseif (strtolower($bill['bill_type']) == 'quota') { - $allowed = format_bytes_billing($bill['bill_quota']); - $used = format_bytes_billing($rate_data['total_data']); + $allowed = Billing::formatBytes($bill['bill_quota']); + $used = Billing::formatBytes($rate_data['total_data']); if ($bill['bill_quota'] > 0) { $percent = Number::calculatePercent($rate_data['total_data'], $bill['bill_quota']); } else { $percent = '-'; } $overuse = $rate_data['total_data'] - $bill['bill_quota']; - $overuse = (($overuse <= 0) ? '-' : format_bytes_billing($overuse)); + $overuse = (($overuse <= 0) ? '-' : Billing::formatBytes($overuse)); } $bill['allowed'] = $allowed; $bill['used'] = $used; @@ -1634,9 +1635,9 @@ function get_bill_graphdata(Illuminate\Http\Request $request) $to = $request->get('to', time()); $reducefactor = $request->get('reducefactor'); - $graph_data = getBillingBitsGraphData($bill_id, $from, $to, $reducefactor); + $graph_data = Billing::getBitsGraphData($bill_id, $from, $to, $reducefactor); } elseif ($graph_type == 'monthly') { - $graph_data = getHistoricTransferGraphData($bill_id); + Billing::getHistoricTransferGraphData($bill_id); } if (! isset($graph_data)) { @@ -1703,11 +1704,11 @@ function get_bill_history_graphdata(Illuminate\Http\Request $request) case 'bits': $reducefactor = $request->get('reducefactor'); - $graph_data = getBillingHistoryBitsGraphData($bill_id, $bill_hist_id, $reducefactor); + $graph_data = Billing::getHistoryBitsGraphData($bill_id, $bill_hist_id, $reducefactor); break; case 'day': case 'hour': - $graph_data = getBillingBandwidthGraphData($bill_id, $bill_hist_id, null, null, $graph_type); + $graph_data = Billing::getBandwidthGraphData($bill_id, $bill_hist_id, null, null, $graph_type); break; } diff --git a/includes/html/graphs/bill/bits.inc.php b/includes/html/graphs/bill/bits.inc.php index ea755f2c8c..cbe95f6869 100644 --- a/includes/html/graphs/bill/bits.inc.php +++ b/includes/html/graphs/bill/bits.inc.php @@ -1,11 +1,12 @@ SetLegend('Traffic total'); $barplot_tot->SetColor('darkgray'); $barplot_tot->SetFillColor('lightgray@0.4'); $barplot_tot->value->Show(); -$barplot_tot->value->SetFormatCallback('format_bytes_billing_short'); +$barplot_tot->value->SetFormatCallback('\LibreNMS\Billing::formatBytesShort'); $barplot_in = new BarPlot($graph_data['in_data']); $barplot_in->SetLegend('Traffic In'); diff --git a/includes/html/graphs/bill/historictransfer.inc.php b/includes/html/graphs/bill/historictransfer.inc.php index 750cd89635..f170c3fbd6 100644 --- a/includes/html/graphs/bill/historictransfer.inc.php +++ b/includes/html/graphs/bill/historictransfer.inc.php @@ -4,14 +4,15 @@ use Amenadiel\JpGraph\Graph\Graph; use Amenadiel\JpGraph\Plot\BarPlot; use Amenadiel\JpGraph\Plot\GroupBarPlot; use Amenadiel\JpGraph\Plot\LinePlot; +use LibreNMS\Billing; use LibreNMS\Util\Number; if (is_numeric($vars['bill_hist_id'])) { - $graph_data = getBillingBandwidthGraphData($vars['id'], $vars['bill_hist_id'], null, null, $vars['imgtype']); + $graph_data = Billing::getBandwidthGraphData($vars['id'], $vars['bill_hist_id'], null, null, $vars['imgtype']); } elseif (is_numeric($vars['from'])) { - $graph_data = getBillingBandwidthGraphData($vars['id'], null, $vars['from'], $vars['to'], $vars['imgtype']); + $graph_data = Billing::getBandwidthGraphData($vars['id'], null, $vars['from'], $vars['to'], $vars['imgtype']); } else { - $graph_data = getHistoricTransferGraphData($vars['id']); + Billing::getHistoricTransferGraphData($vars['id']); $vars['imgtype'] = 'historical'; } @@ -75,7 +76,7 @@ $barplot_tot->SetLegend('Traffic total'); $barplot_tot->SetColor('darkgray'); $barplot_tot->SetFillColor('lightgray@0.4'); $barplot_tot->value->Show(); -$barplot_tot->value->SetFormatCallback('format_bytes_billing_short'); +$barplot_tot->value->SetFormatCallback('\LibreNMS\Billing::formatBytesShort'); $barplot_in = new BarPlot($graph_data['in_data']); $barplot_in->SetLegend('Traffic In'); diff --git a/includes/html/pages/bill.inc.php b/includes/html/pages/bill.inc.php index 1505dd28aa..09a39da4e4 100644 --- a/includes/html/pages/bill.inc.php +++ b/includes/html/pages/bill.inc.php @@ -1,5 +1,6 @@ - of + of - Average rate @@ -174,7 +175,7 @@ if (bill_permitted($bill_id)) { + echo 'Predicted usage: ' . Billing::formatBytes(Billing::getPredictedUsage($bill_data['bill_day'], $bill_data['total_data'])); ?> + echo 'Predicted usage: ' . Number::formatSi(Billing::getPredictedUsage($bill_data['bill_day'], $bill_data['rate_95th']), 2, 3, '') . 'bps'; ?> 'bill', 'bill_id' => $bill['bill_id']]); $used95th = Number::formatSi($bill['rate_95th'], 2, 3, '') . 'bps'; @@ -118,20 +119,20 @@ foreach (dbFetchRows($sql, $param) as $bill) { $rate_95th = "$rate_95th"; } elseif (strtolower($bill['bill_type']) == 'quota') { $type = 'Quota'; - $allowed = format_bytes_billing($bill['bill_allowed']); + $allowed = Billing::formatBytes($bill['bill_allowed']); if (! empty($prev)) { - $in = format_bytes_billing($bill['traf_in']); - $out = format_bytes_billing($bill['traf_out']); + $in = Billing::formatBytes($bill['traf_in']); + $out = Billing::formatBytes($bill['traf_out']); } else { - $in = format_bytes_billing($bill['total_data_in']); - $out = format_bytes_billing($bill['total_data_out']); + $in = Billing::formatBytes($bill['total_data_in']); + $out = Billing::formatBytes($bill['total_data_out']); } if (! $prev) { $percent = Number::calculatePercent($bill['total_data'], $bill['bill_allowed']); $overuse = ($bill['total_data'] - $bill['bill_allowed']); } - $overuse_formatted = format_bytes_billing($overuse); + $overuse_formatted = Billing::formatBytes($overuse); $used = $total_data; $tmp_used = $bill['total_data']; $total_data = "$total_data"; @@ -152,9 +153,9 @@ foreach (dbFetchRows($sql, $param) as $bill) { "'> Edit "; } if (strtolower($bill['bill_type']) == 'cdr') { - $predicted = Number::formatSi(getPredictedUsage($bill['bill_day'], $tmp_used), 2, 3, '') . 'bps'; + $predicted = Number::formatSi(Billing::getPredictedUsage($bill['bill_day'], $tmp_used), 2, 3, '') . 'bps'; } elseif (strtolower($bill['bill_type']) == 'quota') { - $predicted = format_bytes_billing(getPredictedUsage($bill['bill_day'], $tmp_used)); + $predicted = Billing::formatBytes(Billing::getPredictedUsage($bill['bill_day'], $tmp_used)); } $response[] = [ diff --git a/includes/init.php b/includes/init.php index 0a5ec8e2b2..7a2ce073b2 100644 --- a/includes/init.php +++ b/includes/init.php @@ -60,7 +60,6 @@ if (! function_exists('module_selected')) { require_once $install_dir . '/includes/common.php'; require_once $install_dir . '/includes/dbFacile.php'; require_once $install_dir . '/includes/datastore.inc.php'; -require_once $install_dir . '/includes/billing.php'; require_once $install_dir . '/includes/syslog.php'; require_once $install_dir . '/includes/snmp.inc.php'; require_once $install_dir . '/includes/services.inc.php'; diff --git a/phpstan-baseline-deprecated.neon b/phpstan-baseline-deprecated.neon index 5c993498b5..1efb685947 100644 --- a/phpstan-baseline-deprecated.neon +++ b/phpstan-baseline-deprecated.neon @@ -350,7 +350,7 @@ parameters: Please use Eloquent instead; https\\://laravel\\.com/docs/eloquent$# """ count: 3 - path: includes/billing.php + path: LibreNMS/Billing.php - message: """ @@ -358,7 +358,7 @@ parameters: Please use Eloquent instead; https\\://laravel\\.com/docs/eloquent$# """ count: 4 - path: includes/billing.php + path: LibreNMS/Billing.php - message: """ @@ -366,7 +366,7 @@ parameters: Please use Eloquent instead; https\\://laravel\\.com/docs/eloquent$# """ count: 3 - path: includes/billing.php + path: LibreNMS/Billing.php - message: """ @@ -374,7 +374,7 @@ parameters: Please use Eloquent instead; https\\://laravel\\.com/docs/eloquent$# """ count: 8 - path: includes/billing.php + path: LibreNMS/Billing.php - message: """ @@ -382,7 +382,7 @@ parameters: Please use Eloquent instead; https\\://laravel\\.com/docs/eloquent$# """ count: 1 - path: includes/billing.php + path: LibreNMS/Billing.php - message: """ diff --git a/poll-billing.php b/poll-billing.php index 4041b5373a..98a014b99b 100755 --- a/poll-billing.php +++ b/poll-billing.php @@ -11,6 +11,7 @@ * @copyright (C) 2006 - 2012 Adam Armstrong */ +use LibreNMS\Billing; use LibreNMS\Data\Store\Datastore; use LibreNMS\Util\Debug; @@ -53,10 +54,10 @@ foreach ($query->get(['bill_id', 'bill_name']) as $bill) { echo " Polling ${port_data['ifName']} (${port_data['ifDescr']}) on ${port_data['hostname']}\n"; - $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'); + $port_data['in_measurement'] = Billing::getValue($port_data['hostname'], $port_data['port'], $port_data['ifIndex'], 'In'); + $port_data['out_measurement'] = Billing::getValue($port_data['hostname'], $port_data['port'], $port_data['ifIndex'], 'Out'); - $last_counters = getLastPortCounter($port_id, $bill_id); + $last_counters = Billing::getLastPortCounter($port_id, $bill_id); if ($last_counters['state'] == 'ok') { $port_data['last_in_measurement'] = $last_counters['in_counter']; $port_data['last_in_delta'] = $last_counters['in_delta']; @@ -113,7 +114,7 @@ foreach ($query->get(['bill_id', 'bill_name']) as $bill) { $out_delta = ($out_delta + $port_data['out_delta']); }//end foreach - $last_data = getLastMeasurement($bill_id); + $last_data = Billing::getLastMeasurement($bill_id); if ($last_data['state'] == 'ok') { $prev_delta = $last_data['delta'];