From 19e9cda8d00bad938efdb23644488711473b7657 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 2 Mar 2016 18:38:39 +0000 Subject: [PATCH] Replace port_in/out_measurements with latest values only --- includes/billing.php | 24 ++++++++++----------- poll-billing.php | 50 +++++++++++++++++++++++++++----------------- sql-schema/107.sql | 4 ++++ 3 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 sql-schema/107.sql diff --git a/includes/billing.php b/includes/billing.php index cfdc657255..29e59183da 100644 --- a/includes/billing.php +++ b/includes/billing.php @@ -79,27 +79,25 @@ function getValue($host, $port, $id, $inout) { }//end getValue() - -function getLastPortCounter($port_id, $inout) { +function getLastPortCounter($port_id) { $return = array(); - $row = dbFetchRow('SELECT counter,delta FROM `port_'.mres($inout)."_measurements` WHERE `port_id`='".mres($port_id)."' ORDER BY timestamp DESC LIMIT 1"); + $row = dbFetchRow("SELECT in_counter, in_delta, out_counter, out_delta FROM bill_port_counters WHERE `port_id` = ?", array($port_id)); if (!is_null($row)) { - $return[counter] = $row['counter']; - $return[delta] = $row['delta']; - $return[state] = 'ok'; + $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'; } - else { - $return[state] = 'failed'; - } - - return ($return); - + return $return; }//end getLastPortCounter() function getLastMeasurement($bill_id) { $return = array(); - $row = dbFetchRow("SELECT timestamp,delta,in_delta,out_delta FROM bill_data WHERE bill_id='".mres($bill_id)."' ORDER BY timestamp DESC LIMIT 1"); + $row = dbFetchRow("SELECT timestamp,delta,in_delta,out_delta FROM bill_data WHERE bill_id = ? ORDER BY timestamp DESC LIMIT 1", array($port_id)); if (!is_null($row)) { $return[delta] = $row['delta']; $return[delta_in] = $row['delta_in']; diff --git a/poll-billing.php b/poll-billing.php index 5f2c278ab1..3bd1a46643 100755 --- a/poll-billing.php +++ b/poll-billing.php @@ -24,6 +24,7 @@ $iter = '0'; rrdtool_pipe_open($rrd_process, $rrd_pipes); +$poller_start = microtime(true); echo "Starting Polling Session ... \n\n"; foreach (dbFetchRows('SELECT * FROM `bills`') as $bill_data) { @@ -43,39 +44,35 @@ foreach (dbFetchRows('SELECT * FROM `bills`') as $bill_data) { function CollectData($bill_id) { $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', array($bill_id)); - print_r($port_list); + $now = dbFetchCell('SELECT NOW()'); + $delta = 0; + $in_delta = 0; + $out_delta = 0; foreach ($port_list as $port_data) { $port_id = $port_data['port_id']; $host = $port_data['hostname']; $port = $port_data['port']; - echo "\nPolling ".$port_data['ifDescr'].' on '.$port_data['hostname']."\n"; + 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'); - $last_data = getLastPortCounter($port_id, in); - if ($last_data['state'] == 'ok') { - $port_data['last_in_measurement'] = $last_data[counter]; - $port_data['last_in_delta'] = $last_data[delta]; + $last_counters = getLastPortCounter($port_id); + if ($last_counters['state'] == 'ok') { + $port_data['last_in_measurement'] = $last_counters[in_counter]; + $port_data['last_in_delta'] = $last_counters[in_delta]; + $port_data['last_out_measurement'] = $last_counters[out_counter]; + $port_data['last_out_delta'] = $last_counters[out_delta]; + if ($port_data['in_measurement'] >= $port_data['last_in_measurement']) { $port_data['in_delta'] = ($port_data['in_measurement'] - $port_data['last_in_measurement']); } else { $port_data['in_delta'] = $port_data['last_in_delta']; } - } - else { - $port_data['in_delta'] = '0'; - } - - dbInsert(array('port_id' => $port_id, 'timestamp' => $now, 'counter' => $port_data['in_measurement'], 'delta' => $port_data['in_delta']), 'port_in_measurements'); - - $last_data = getLastPortCounter($port_id, out); - if ($last_data[state] == 'ok') { - $port_data['last_out_measurement'] = $last_data[counter]; - $port_data['last_out_delta'] = $last_data[delta]; + if ($port_data['out_measurement'] >= $port_data['last_out_measurement']) { $port_data['out_delta'] = ($port_data['out_measurement'] - $port_data['last_out_measurement']); } @@ -84,10 +81,15 @@ function CollectData($bill_id) { } } else { + $port_data['in_delta'] = '0'; $port_data['out_delta'] = '0'; } - - dbInsert(array('port_id' => $port_id, 'timestamp' => $now, 'counter' => $port_data['out_measurement'], 'delta' => $port_data['out_delta']), 'port_out_measurements'); + + $fields = array('timestamp' => $now, 'in_counter' => $port_data['in_measurement'], 'out_counter' => $port_data['out_measurement'], 'in_delta' => $port_data['in_delta'], 'out_delta' => $port_data['out_delta']); + if (dbUpdate($fields, 'bill_port_counters', "`port_id`='" . mres($port_id) . "'") == 0) { + $fields['port_id'] = $port_id; + dbInsert($fields, 'bill_port_counters'); + } $delta = ($delta + $port_data['in_delta'] + $port_data['out_delta']); $in_delta = ($in_delta + $port_data['in_delta']); @@ -130,4 +132,14 @@ if ($argv[1]) { CollectData($argv[1]); } +$poller_end = microtime(true); +$poller_run = ($poller_end - $poller_start); +$poller_time = substr($poller_run, 0, 5); + +dbInsert(array('type' => 'pollbill', 'doing' => $doing, 'start' => $poller_start, 'duration' => $poller_time, 'devices' => 0, 'poller' => $config['distributed_poller_name'] ), 'perf_times'); +if ($poller_time > 300) { + logfile("BILLING: polling took longer than 5 minutes ($poller_time seconds)!"); +} +echo "\nCompleted in $poller_time sec\n"; + rrdtool_pipe_close($rrd_process, $rrd_pipes); diff --git a/sql-schema/107.sql b/sql-schema/107.sql new file mode 100644 index 0000000000..fd976f5499 --- /dev/null +++ b/sql-schema/107.sql @@ -0,0 +1,4 @@ +CREATE TABLE bill_port_counters(port_id int NOT NULL PRIMARY KEY, `timestamp` timestamp NOT NULL DEFAULT current_timestamp, in_counter bigint, in_delta bigint NOT NULL DEFAULT 0, out_counter bigint, out_delta bigint NOT NULL DEFAULT 0); +INSERT INTO bill_port_counters(port_id, timestamp, in_counter, out_counter) SELECT q.port_id, q.max_timestamp, max(i.counter), max(o.counter) FROM (SELECT port_id, MAX(`timestamp`) AS max_timestamp FROM port_in_measurements GROUP BY port_id) q INNER JOIN port_in_measurements i ON q.port_id = i.port_id AND q.max_timestamp = i.timestamp INNER JOIN port_out_measurements o ON q.port_id = o.port_id AND q.max_timestamp = o.timestamp GROUP BY q.port_id, q.max_timestamp; +DROP TABLE port_in_measurements; +DROP TABLE port_out_measurements; \ No newline at end of file