forked from sseshachala/observium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll-billing.php
executable file
·179 lines (151 loc) · 6.66 KB
/
poll-billing.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env php
<?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]));
// FIXME - implement cli switches, debugging, etc.
include("includes/defaults.inc.php");
include("config.php");
include("includes/definitions.inc.php");
include("includes/functions.php");
$options = getopt("d");
if (isset($options['d']))
{
echo("DEBUG!\n");
$debug = TRUE;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('log_errors', 1);
# ini_set('error_reporting', E_ALL ^ E_NOTICE);
} else {
$debug = FALSE;
# ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set('log_errors', 0);
# ini_set('error_reporting', 0);
}
$iter = "0";
echo("Starting Polling Session ... \n\n");
foreach (dbFetchRows("SELECT * FROM `bills`") as $bill)
{
echo("Bill : ".$bill['bill_name']."\n");
# replace old bill_gb with bill_quota (we're now storing bytes, not gigabytes)
if ($bill['bill_type'] == "quota" && !is_numeric($bill['bill_quota']))
{
$bill['bill_quota'] = $bill['bill_gb'] * $config['billing']['base'] * $config['billing']['base'];
dbUpdate(array('bill_quota' => $bill['bill_quota']), 'bills', '`bill_id` = ?', array($bill['bill_id']));
echo("Quota -> ".$bill['bill_quota']);
}
poll_bill($bill);
$iter++;
}
function poll_bill($bill)
{
$ports = 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['bill_id']));
if (isset($options['d'])) {
print_r($ports);
}
foreach ($ports as $port)
{
echo("\nPolling ".$port['ifDescr']." on ".$port['hostname']."\n");
$now = time();
if(is_numeric($port['bill_port_polled'])) { $period = $now - $port['bill_port_polled']; }
echo("time: ".$now."|".$port['bill_port_polled']." period:".$period."\n");
if($port['snmpver'] == "1") {
// SNMPv1 - Use non 64-bit counters
$oids = "IF-MIB::ifInOctets.".$port['ifIndex']." IF-MIB::ifOutOctets.".$port['ifIndex'];
$data = snmp_get_multi($port, $oids, "-OQUs", "IF-MIB");
$data = $data[$port['ifIndex']];
$data = array('in' => $data['ifInOctets'], 'out' => $data['ifOutOctets']);
} else {
// Not SNMPv1 - Use 64-bit counters
$oids = "IF-MIB::ifHCInOctets.".$port['ifIndex']." IF-MIB::ifHCOutOctets.".$port['ifIndex'];
$data = snmp_get_multi($port, $oids, "-OQUs", "IF-MIB");
$data = $data[$port['ifIndex']];
$data = array('in' => $data['ifHCInOctets'], 'out' => $data['ifHCOutOctets']);
// Fallback on 32-bit counters if 64-bit fails
if (empty($data['in']) && empty($data['out'])) {
$oids = "IF-MIB::ifInOctets.".$port['ifIndex']." IF-MIB::ifOutOctets.".$port['ifIndex'];
$data = snmp_get_multi($port, $oids, "-OQUs", "IF-MIB");
$data = $data[$port['ifIndex']];
$data = array('in' => $data['ifInOctets'], 'out' => $data['ifOutOctets']);
}
if (isset($options['d'])) {
print_r($data);
}
}
if (is_numeric($data['in']) && is_numeric($data['out']))
{
echo($period ."|".$port['bill_port_counter_in']."|".$port['bill_port_counter_out']."|".$data['in']."|".$port['bill_port_counter_in']."\n");
// The port returned counters
if(is_numeric($period) && is_numeric($port['bill_port_counter_in']) && is_numeric($port['bill_port_counter_out']) && $data['in'] >= $port['bill_port_counter_in'] && $data['out'] >= $port['bill_port_counter_out'])
{
// Counters are higher or equal to before, seems legit.
$in_delta = $data['in'] - $port['bill_port_counter_in'];
$out_delta = $data['out'] - $port['bill_port_counter_out'];
echo("Counters valid, delta generated.\n");
} elseif (is_numeric($period) && is_numeric($port['bill_port_counter_in']) && is_numeric($port['bill_port_counter_out'])) {
// Counters are lower, we must have wrapped. We'll take the measurement as the amount for this period.
$in_delta = $data['in'];
$out_delta = $data['out'];
echo("Counters wrapped, delta fudged.\n");
} else {
// First update. delta is zero, only insert counters.
echo("No existing counters.\n");
$in_delta = 0;
$out_delta = 0;
}
if($in_delta == $data['in'] || $in_delta == $data['in'])
{
// Deltas are equal to counters. Clearly fail.
echo("Deltas equal counters. Resetting.");
$in_delta = 0;
$out_delta = 0;
}
$update = array('bill_port_polled' => $now, 'bill_port_period' => $period,
'bill_port_counter_in' => $data['in'], 'bill_port_counter_out' => $data['out'],
'bill_port_delta_in' => $in_delta, 'bill_port_delta_out' => $out_delta,
);
dbUpdate($update, 'bill_ports', '`bill_id` = ? AND `port_id` = ?', array($port['bill_id'], $port['port_id']));
echo("data: in(N:".$data['in']."|P:".$port['bill_port_counter_in'].") delta:".$in_delta."\n");
echo(" out(N:".$data['out']."|P:".$port['bill_port_counter_out'].") delta:".$out_delta."\n");
} else {
// No counters were returned
// We don't need to update the database.
echo("No data.\n");
}
dbInsert(array('port_id' => $port['port_id'], 'timestamp' => $now, 'counter' => $data['in'], 'delta' => $in_delta), 'bill_port_in_data');
dbInsert(array('port_id' => $port['port_id'], 'timestamp' => $now, 'counter' => $data['out'], 'delta' => $out_delta), 'bill_port_out_data');
$bill_delta += $in_delta + $out_delta;
$bill_in_delta += $in_delta;
$bill_out_delta += $out_delta;
echo("deltas: i:".$bill_in_delta." o:".$bill_out_delta." t:".$bill_delta."\n");
}
echo("Bill processing...\n");
$now = time();
if(is_numeric($bill['bill_polled'])) { $period = $now - $bill['bill_polled']; }
echo("time: ".$now."|".$bill['bill_polled']." period:".$period."\n\n\n");
if ($period <= '0')
{
$bill_delta = '0';
$bill_in_delta = '0';
$bill_out_delta = '0';
}
dbUpdate(array('bill_polled' => $now), 'bills', '`bill_id` = ?', array($port['bill_id']));
if ($period < "0" || !is_numeric($period)) {
logfile("BILLING: negative period! id:".$bill['bill_id']." period:$period delta:$bill_delta in_delta:$bill_in_delta out_delta:$bill_out_delta");
} else {
dbInsert(array('bill_id' => $bill['bill_id'], 'timestamp' => array("NOW()"), 'period' => $period, 'delta' => $bill_delta, 'in_delta' => $bill_in_delta, 'out_delta' => $bill_out_delta), 'bill_data');
}
}
if ($argv[1]) { poll_bill($argv[1]); }
?>