1
|
<?php
|
2
|
/*
|
3
|
bandwidth_by_ip.php
|
4
|
*/
|
5
|
|
6
|
/*
|
7
|
pfSense_BUILDER_BINARIES: /usr/local/bin/rate
|
8
|
pfSense_MODULE: trafficgraph
|
9
|
*/
|
10
|
|
11
|
require_once('guiconfig.inc');
|
12
|
require_once('interfaces.inc');
|
13
|
require_once('pfsense-utils.inc');
|
14
|
require_once('util.inc');
|
15
|
|
16
|
$listedIPs = "";
|
17
|
|
18
|
//get interface IP and break up into an array
|
19
|
$interface = $_GET['if'];
|
20
|
$real_interface = get_real_interface($interface);
|
21
|
if (!does_interface_exist($real_interface)) {
|
22
|
echo gettext("Wrong Interface");
|
23
|
return;
|
24
|
}
|
25
|
|
26
|
$intip = find_interface_ip($real_interface);
|
27
|
//get interface subnet
|
28
|
$netmask = find_interface_subnet($real_interface);
|
29
|
$intsubnet = gen_subnet($intip, $netmask) . "/$netmask";
|
30
|
|
31
|
// see if they want local, remote or all IPs returned
|
32
|
$filter = $_GET['filter'];
|
33
|
|
34
|
if ($filter == "") {
|
35
|
$filter = "local";
|
36
|
}
|
37
|
|
38
|
if ($filter == "local") {
|
39
|
$ratesubnet = "-c " . $intsubnet;
|
40
|
} else {
|
41
|
// Tell the rate utility to consider the whole internet (0.0.0.0/0)
|
42
|
// and to consider local "l" traffic - i.e. traffic within the whole internet
|
43
|
// then we can filter the resulting output as we wish below.
|
44
|
$ratesubnet = "-lc 0.0.0.0/0";
|
45
|
}
|
46
|
|
47
|
//get the sort method
|
48
|
$sort = $_GET['sort'];
|
49
|
if ($sort == "out") {
|
50
|
$sort_method = "-T";
|
51
|
} else {
|
52
|
$sort_method = "-R";
|
53
|
}
|
54
|
|
55
|
// get the desired format for displaying the host name or IP
|
56
|
$hostipformat = $_GET['hostipformat'];
|
57
|
$iplookup = array();
|
58
|
// If hostname, description or FQDN is requested then load the locally-known IP address - host/description mappings into an array keyed by IP address.
|
59
|
if ($hostipformat != "") {
|
60
|
if (is_array($config['dhcpd'])) {
|
61
|
// Build an array of static-mapped DHCP entries keyed by IP address.
|
62
|
foreach ($config['dhcpd'] as $ifdata) {
|
63
|
if (is_array($ifdata['staticmap'])) {
|
64
|
foreach ($ifdata['staticmap'] as $hostent) {
|
65
|
if (($hostent['ipaddr'] != "") && ($hostent['hostname'] != "")) {
|
66
|
if ($hostipformat == "descr" && $hostent['descr'] != "") {
|
67
|
$iplookup[$hostent['ipaddr']] = $hostent['descr'];
|
68
|
} else {
|
69
|
$iplookup[$hostent['ipaddr']] = $hostent['hostname'];
|
70
|
if ($hostipformat == "fqdn") {
|
71
|
$iplookup[$hostent['ipaddr']] .= "." . $config['system']['domain'];
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
// Add any DNS host override data keyed by IP address.
|
80
|
foreach (array('dnsmasq', 'unbound') as $dns_type) {
|
81
|
if (isset($config[$dns_type]['enable'])) {
|
82
|
if (is_array($config[$dns_type]['hosts'])) {
|
83
|
foreach ($config[$dns_type]['hosts'] as $hostent) {
|
84
|
if (($hostent['ip'] != "") && ($hostent['host'] != "")) {
|
85
|
if ($hostipformat == "descr" && $hostent['descr'] != "") {
|
86
|
$iplookup[$hostent['ip']] = $hostent['descr'];
|
87
|
} else {
|
88
|
$iplookup[$hostent['ip']] = $hostent['host'];
|
89
|
if ($hostipformat == "fqdn") {
|
90
|
$iplookup[$hostent['ip']] .= "." . $hostent['domain'];
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
|
100
|
$_grb = exec("/usr/local/bin/rate -i {$real_interface} -nlq 1 -Aba 20 {$sort_method} {$ratesubnet} | tr \"|\" \" \" | awk '{ printf \"%s:%s:%s:%s:%s\\n\", $1, $2, $4, $6, $8 }'", $listedIPs);
|
101
|
|
102
|
$someinfo = false;
|
103
|
for ($x=2; $x<12; $x++) {
|
104
|
|
105
|
$bandwidthinfo = $listedIPs[$x];
|
106
|
|
107
|
// echo $bandwidthinfo;
|
108
|
$emptyinfocounter = 1;
|
109
|
if ($bandwidthinfo != "") {
|
110
|
$infoarray = explode (":", $bandwidthinfo);
|
111
|
if (($filter == "all") ||
|
112
|
(($filter == "local") && (ip_in_subnet($infoarray[0], $intsubnet))) ||
|
113
|
(($filter == "remote") && (!ip_in_subnet($infoarray[0], $intsubnet)))) {
|
114
|
if ($hostipformat == "") {
|
115
|
// pass back just the raw IP address
|
116
|
$addrdata = $infoarray[0];
|
117
|
} else {
|
118
|
// $hostipformat is one of "hostname", "descr" or "fqdn" - we want a text representation if we can get it.
|
119
|
if ($iplookup[$infoarray[0]] != "") {
|
120
|
// We have a local entry, so use it.
|
121
|
$addrdata = $iplookup[$infoarray[0]];
|
122
|
} else {
|
123
|
// Try to reverse lookup the IP address.
|
124
|
$addrdata = gethostbyaddr($infoarray[0]);
|
125
|
if ($addrdata != $infoarray[0]) {
|
126
|
// Reverse lookup returned something other than the IP address (FQDN, we hope!)
|
127
|
if ($hostipformat != "fqdn") {
|
128
|
// The user does not want the whole FQDN, so only pass back the first part of the name.
|
129
|
$name_array = explode(".", $addrdata);
|
130
|
$addrdata = $name_array[0];
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
//print host information;
|
136
|
echo $addrdata . ";" . $infoarray[1] . ";" . $infoarray[2] . "|";
|
137
|
|
138
|
//mark that we collected information
|
139
|
$someinfo = true;
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
unset($bandwidthinfo, $_grb);
|
144
|
unset($listedIPs);
|
145
|
|
146
|
//no bandwidth usage found
|
147
|
if ($someinfo == false) {
|
148
|
echo gettext("no info");
|
149
|
}
|
150
|
?>
|