-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll-billing.php
118 lines (94 loc) · 4.91 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
#!/usr/bin/php
<?php
$debug = "1";
include("includes/defaults.inc.php");
include("config.php");
include("includes/functions.php");
$iter = "0";
echo("Starting Polling Session ... \n\n");
$bill_query = mysql_query("select * from bills");
while ($bill_data = mysql_fetch_array($bill_query)) {
echo("Bill : ".$bill_data['bill_name']."\n");
CollectData($bill_data['bill_id']);
$iter++;
}
function CollectData($bill_id) {
$port_query = mysql_query("select * from bill_ports as P, ports as I, devices as D where P.bill_id='$bill_id' AND I.interface_id = P.port_id AND D.device_id = I.device_id");
while ($port_data = mysql_fetch_array($port_query)) {
unset($port_in_measurement);
unset($port_in_delta);
unset($last_port_in_measurement);
unset($last_port_in_delta);
unset($port_out_measurement);
unset($port_out_delta);
unset($last_port_out_measurement);
unset($last_port_out_delta);
$port_id = $port_data['port_id'];
$host = $port_data['hostname'];
$port = $port_data['port'];
echo("\nPolling ".$port_data['ifDescr']." on ".$port_data['hostname']."\n");
$port_in_measurement = trim(getValue($host, $port_data['community'], $port, $port_data['ifIndex'], "In"));
$port_out_measurement = trim(getValue($host, $port_data['community'], $port, $port_data['ifIndex'], "Out"));
echo("$port_in_measurement and $port_out_measurement \n");
$now = mysql_result(mysql_query("SELECT NOW()"), 0);
$last_data = getLastPortCounter($port_id,in);
if ($last_data[state] == "ok") {
$last_port_in_measurement = $last_data[counter];
$last_port_in_delta = $last_data[delta];
if ($port_in_measurement > $last_port_in_measurement) {
$port_in_delta = $port_in_measurement - $last_port_in_measurement;
} else {
$port_in_delta = $last_port_in_delta;
}
} else {
$port_in_delta = '0';
}
$pim = "INSERT INTO port_in_measurements (port_id,timestamp,counter,delta) VALUES ($port_id, '$now', $port_in_measurement, $port_in_delta) ";
#echo("$pim \n");
$pim_query = mysql_query($pim);
unset($last_data, $last_port_in_measurement, $last_port_in_delta);
$last_data = getLastPortCounter($port_id,out);
if ($last_data[state] == "ok") {
$last_port_out_measurement = $last_data[counter];
$last_port_out_delta = $last_data[delta];
if ($port_out_measurement > $last_port_out_measurement) {
$port_out_delta = $port_out_measurement - $last_port_out_measurement;
} else {
$port_out_delta = $last_port_out_delta;
}
} else {
$port_out_delta = '0';
}
$pom = "INSERT INTO port_out_measurements (port_id,timestamp,counter,delta) VALUES ($port_id, '$now', $port_out_measurement, $port_out_delta) ";
#echo("$pom \n");
$pom_query = mysql_query($pom);
unset($last_data, $last_port_in_measurement, $last_port_in_delta);
$delta = $delta + $port_in_delta + $port_out_delta;
$in_delta = $in_delta + $port_in_delta;
$out_delta = $out_delta + $port_out_delta;
unset($port_in_delta,$port_out_delta,$prev_delta,$prev_timestamp,$period);
}
$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];
$period = mysql_result(mysql_query("SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP('$prev_timestamp')"),0);
} else {
$prev_delta = '0';
$period = '0';
$prev_in_delta = '0';
$prev_out_delta = '0';
}
if( $delta < '0' ) {
$delta = $prev_delta;
$in_delta = $prev_in_delta;
$out_delta = $prev_out_delta;
}
$insert_string = "INSERT INTO bill_data (bill_id,timestamp,period,delta,in_delta,out_delta) VALUES ('$bill_id','$now','$period','$delta','$in_delta','$out_delta')";
#echo("$insert_string\n");
$insert_measurement = mysql_query($insert_string);
}
if ($argv[1]) { CollectData($argv[1]); }
?>