Files
librenms-librenms/poll-billing.php
T

139 lines
4.5 KiB
PHP
Raw Normal View History

2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2008-03-09 21:13:27 +00:00
<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage billing
* @author Adam Armstrong <adama@memetic.org>
* @copyright (C) 2006 - 2012 Adam Armstrong
*
*/
chdir(dirname($argv[0]));
2012-05-29 13:08:01 +00:00
// FIXME - implement cli switches, debugging, etc.
include("includes/defaults.inc.php");
2008-03-09 21:13:27 +00:00
include("config.php");
include("includes/definitions.inc.php");
2008-03-09 21:13:27 +00:00
include("includes/functions.php");
$iter = "0";
2011-10-01 14:54:06 +00:00
rrdtool_pipe_open($rrd_process, $rrd_pipes);
2008-03-09 21:13:27 +00:00
echo("Starting Polling Session ... \n\n");
2011-05-13 12:34:37 +00:00
foreach (dbFetchRows("SELECT * FROM `bills`") as $bill_data)
2011-03-11 18:03:49 +00:00
{
2011-03-23 09:54:56 +00:00
echo("Bill : ".$bill_data['bill_name']."\n");
2011-12-06 22:17:37 +00:00
# replace old bill_gb with bill_quota (we're now storing bytes, not gigabytes)
2012-04-05 16:17:41 +00:00
if ($bill_data['bill_type'] == "quota" && !is_numeric($bill_data['bill_quota']))
2011-12-06 22:17:37 +00:00
{
$bill_data['bill_quota'] = $bill_data['bill_gb'] * $config['billing']['base'] * $config['billing']['base'];
dbUpdate(array('bill_quota' => $bill_data['bill_quota']), 'bills', '`bill_id` = ?', array($bill_data['bill_id']));
echo("Quota -> ".$bill_data['bill_quota']);
}
2011-03-23 09:54:56 +00:00
CollectData($bill_data['bill_id']);
$iter++;
2008-03-09 21:13:27 +00:00
}
2011-03-11 18:03:49 +00:00
function CollectData($bill_id)
{
2012-05-29 13:08:01 +00:00
$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);
foreach ($port_list as $port_data)
2011-03-23 09:54:56 +00:00
{
$port_id = $port_data['port_id'];
$host = $port_data['hostname'];
$port = $port_data['port'];
2011-03-23 09:54:56 +00:00
echo("\nPolling ".$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");
2011-03-11 18:03:49 +00:00
2011-05-13 12:34:37 +00:00
$now = dbFetchCell("SELECT NOW()");
2011-03-23 09:54:56 +00:00
$last_data = getLastPortCounter($port_id,in);
if ($last_data['state'] == "ok")
2011-03-11 18:03:49 +00:00
{
$port_data['last_in_measurement'] = $last_data[counter];
$port_data['last_in_delta'] = $last_data[delta];
if ($port_data['in_measurement'] > $port_data['last_in_measurement'])
2011-03-23 09:54:56 +00:00
{
$port_data['in_delta'] = $port_data['in_measurement'] - $port_data['last_in_measurement'];
2011-03-23 09:54:56 +00:00
} else {
$port_data['in_delta'] = $port_data['last_in_delta'];
2011-03-23 09:54:56 +00:00
}
2011-03-11 18:03:49 +00:00
} else {
$port_data['in_delta'] = '0';
2011-03-11 18:03:49 +00:00
}
dbInsert(array('port_id' => $port_id, 'timestamp' => $now, 'counter' => $port_data['in_measurement'], 'delta' => $port_data['in_delta']), 'port_in_measurements');
2011-03-11 18:03:49 +00:00
2011-03-23 09:54:56 +00:00
$last_data = getLastPortCounter($port_id,out);
if ($last_data[state] == "ok")
2011-03-11 18:03:49 +00:00
{
$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'])
2011-03-23 09:54:56 +00:00
{
$port_data['out_delta'] = $port_data['out_measurement'] - $port_data['last_out_measurement'];
2011-03-23 09:54:56 +00:00
} else {
$port_data['out_delta'] = $port_data['last_out_delta'];
2011-03-23 09:54:56 +00:00
}
} else {
$port_data['out_delta'] = '0';
2011-03-11 18:03:49 +00:00
}
dbInsert(array('port_id' => $port_id, 'timestamp' => $now, 'counter' => $port_data['out_measurement'], 'delta' => $port_data['out_delta']), 'port_out_measurements');
2011-03-23 09:54:56 +00:00
$delta = $delta + $port_data['in_delta'] + $port_data['out_delta'];
$in_delta = $in_delta + $port_data['in_delta'];
$out_delta = $out_delta + $port_data['out_delta'];
2011-03-23 09:54:56 +00:00
}
$last_data = getLastMeasurement($bill_id);
if ($last_data[state] == "ok")
{
$prev_delta = $last_data[delta];
$prev_in_delta = $last_data[in_delta];
$prev_out_delta = $last_data[out_delta];
$prev_timestamp = $last_data[timestamp];
2011-05-13 12:34:37 +00:00
$period = dbFetchCell("SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP('".mres($prev_timestamp)."')");
2011-03-23 09:54:56 +00:00
} else {
$prev_delta = '0';
$period = '0';
$prev_in_delta = '0';
$prev_out_delta = '0';
}
if ($delta < '0')
2011-03-23 09:54:56 +00:00
{
$delta = $prev_delta;
$in_delta = $prev_in_delta;
$out_delta = $prev_out_delta;
}
2011-09-20 14:34:27 +00:00
if ($period < "0") {
2011-09-20 09:55:11 +00:00
logfile("BILLING: negative period! id:$bill_id period:$period delta:$delta in_delta:$in_delta out_delta:$out_delta");
} else {
dbInsert(array('bill_id' => $bill_id, 'timestamp' => $now, 'period' => $period, 'delta' => $delta, 'in_delta' => $in_delta, 'out_delta' => $out_delta), 'bill_data');
}
2008-03-09 21:13:27 +00:00
}
if ($argv[1]) { CollectData($argv[1]); }
2011-10-01 14:54:06 +00:00
rrdtool_pipe_close($rrd_process, $rrd_pipes);
2011-03-22 12:09:25 +00:00
?>