mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Replace port_in/out_measurements with latest values only
This commit is contained in:
@@ -79,27 +79,25 @@ function getValue($host, $port, $id, $inout) {
|
|||||||
|
|
||||||
}//end getValue()
|
}//end getValue()
|
||||||
|
|
||||||
|
function getLastPortCounter($port_id) {
|
||||||
function getLastPortCounter($port_id, $inout) {
|
|
||||||
$return = array();
|
$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)) {
|
if (!is_null($row)) {
|
||||||
$return[counter] = $row['counter'];
|
$return[in_counter] = $row['in_counter'];
|
||||||
$return[delta] = $row['delta'];
|
$return[in_delta] = $row['in_delta'];
|
||||||
$return[state] = 'ok';
|
$return[out_counter] = $row['out_counter'];
|
||||||
|
$return[out_delta] = $row['out_delta'];
|
||||||
|
$return[state] = 'ok';
|
||||||
|
} else {
|
||||||
|
$return[state] = 'failed';
|
||||||
}
|
}
|
||||||
else {
|
return $return;
|
||||||
$return[state] = 'failed';
|
|
||||||
}
|
|
||||||
|
|
||||||
return ($return);
|
|
||||||
|
|
||||||
}//end getLastPortCounter()
|
}//end getLastPortCounter()
|
||||||
|
|
||||||
|
|
||||||
function getLastMeasurement($bill_id) {
|
function getLastMeasurement($bill_id) {
|
||||||
$return = array();
|
$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)) {
|
if (!is_null($row)) {
|
||||||
$return[delta] = $row['delta'];
|
$return[delta] = $row['delta'];
|
||||||
$return[delta_in] = $row['delta_in'];
|
$return[delta_in] = $row['delta_in'];
|
||||||
|
@@ -24,6 +24,7 @@ $iter = '0';
|
|||||||
|
|
||||||
rrdtool_pipe_open($rrd_process, $rrd_pipes);
|
rrdtool_pipe_open($rrd_process, $rrd_pipes);
|
||||||
|
|
||||||
|
$poller_start = microtime(true);
|
||||||
echo "Starting Polling Session ... \n\n";
|
echo "Starting Polling Session ... \n\n";
|
||||||
|
|
||||||
foreach (dbFetchRows('SELECT * FROM `bills`') as $bill_data) {
|
foreach (dbFetchRows('SELECT * FROM `bills`') as $bill_data) {
|
||||||
@@ -43,39 +44,35 @@ foreach (dbFetchRows('SELECT * FROM `bills`') as $bill_data) {
|
|||||||
|
|
||||||
function CollectData($bill_id) {
|
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));
|
$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()');
|
$now = dbFetchCell('SELECT NOW()');
|
||||||
|
$delta = 0;
|
||||||
|
$in_delta = 0;
|
||||||
|
$out_delta = 0;
|
||||||
foreach ($port_list as $port_data) {
|
foreach ($port_list as $port_data) {
|
||||||
$port_id = $port_data['port_id'];
|
$port_id = $port_data['port_id'];
|
||||||
$host = $port_data['hostname'];
|
$host = $port_data['hostname'];
|
||||||
$port = $port_data['port'];
|
$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['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['out_measurement'] = getValue($port_data['hostname'], $port_data['port'], $port_data['ifIndex'], 'Out');
|
||||||
|
|
||||||
$last_data = getLastPortCounter($port_id, in);
|
$last_counters = getLastPortCounter($port_id);
|
||||||
if ($last_data['state'] == 'ok') {
|
if ($last_counters['state'] == 'ok') {
|
||||||
$port_data['last_in_measurement'] = $last_data[counter];
|
$port_data['last_in_measurement'] = $last_counters[in_counter];
|
||||||
$port_data['last_in_delta'] = $last_data[delta];
|
$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']) {
|
if ($port_data['in_measurement'] >= $port_data['last_in_measurement']) {
|
||||||
$port_data['in_delta'] = ($port_data['in_measurement'] - $port_data['last_in_measurement']);
|
$port_data['in_delta'] = ($port_data['in_measurement'] - $port_data['last_in_measurement']);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$port_data['in_delta'] = $port_data['last_in_delta'];
|
$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']) {
|
if ($port_data['out_measurement'] >= $port_data['last_out_measurement']) {
|
||||||
$port_data['out_delta'] = ($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 {
|
else {
|
||||||
|
$port_data['in_delta'] = '0';
|
||||||
$port_data['out_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']);
|
$delta = ($delta + $port_data['in_delta'] + $port_data['out_delta']);
|
||||||
$in_delta = ($in_delta + $port_data['in_delta']);
|
$in_delta = ($in_delta + $port_data['in_delta']);
|
||||||
@@ -130,4 +132,14 @@ if ($argv[1]) {
|
|||||||
CollectData($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);
|
rrdtool_pipe_close($rrd_process, $rrd_pipes);
|
||||||
|
4
sql-schema/107.sql
Normal file
4
sql-schema/107.sql
Normal file
@@ -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;
|
Reference in New Issue
Block a user