1
|
<?php
|
2
|
/*
|
3
|
* To change this template, choose Tools | Templates
|
4
|
* and open the template in the editor.
|
5
|
*
|
6
|
*/
|
7
|
|
8
|
/*
|
9
|
pfSense_BUILDER_BINARIES: /usr/local/bin/rate
|
10
|
pfSense_MODULE: trafficgraph
|
11
|
*/
|
12
|
|
13
|
require_once('guiconfig.inc');
|
14
|
require_once('interfaces.inc');
|
15
|
require_once('pfsense-utils.inc');
|
16
|
require_once('util.inc');
|
17
|
|
18
|
$listedIPs = "";
|
19
|
|
20
|
//get interface IP and break up into an array
|
21
|
$interface = $_GET['if'];
|
22
|
$real_interface = get_real_interface($interface);
|
23
|
if (!does_interface_exist($real_interface)) {
|
24
|
echo gettext("Wrong Interface");
|
25
|
return;
|
26
|
}
|
27
|
|
28
|
$intip = find_interface_ip($real_interface);
|
29
|
//get interface subnet
|
30
|
$netmask = find_interface_subnet($real_interface);
|
31
|
$intsubnet = gen_subnet($intip, $netmask) . "/$netmask";
|
32
|
|
33
|
// see if they want local, remote or all IPs returned
|
34
|
$filter = $_GET['filter'];
|
35
|
|
36
|
if ($filter == "")
|
37
|
$filter = "local";
|
38
|
|
39
|
if ($filter == "local") {
|
40
|
$ratesubnet = "-c " . $intsubnet;
|
41
|
} else {
|
42
|
// Tell the rate utility to consider the whole internet (0.0.0.0/0)
|
43
|
// and to consider local "l" traffic - i.e. traffic within the whole internet
|
44
|
// then we can filter the resulting output as we wish below.
|
45
|
$ratesubnet = "-lc 0.0.0.0/0";
|
46
|
}
|
47
|
|
48
|
//get the sort method
|
49
|
$sort = $_GET['sort'];
|
50
|
if ($sort == "out")
|
51
|
{$sort_method = "-T";}
|
52
|
else
|
53
|
{$sort_method = "-R";}
|
54
|
|
55
|
// get the desired format for displaying the host name or IP
|
56
|
$hostipformat = $_GET['hostipformat'];
|
57
|
$iplookup = array();
|
58
|
// If hostname display is requested and the DNS forwarder does not already have DHCP static names registered,
|
59
|
// then load the DHCP static mappings into an array keyed by IP address.
|
60
|
if (($hostipformat != "") && ((!isset($config['dnsmasq']['enable']) || !isset($config['dnsmasq']['regdhcpstatic']))
|
61
|
|| (!isset($config['unbound']['enable']) || !isset($config['unbound']['regdhcpstatic'])))) {
|
62
|
if (is_array($config['dhcpd'])) {
|
63
|
foreach ($config['dhcpd'] as $ifdata) {
|
64
|
if (is_array($ifdata['staticmap'])) {
|
65
|
foreach ($ifdata['staticmap'] as $hostent) {
|
66
|
if (($hostent['ipaddr'] != "") && ($hostent['hostname'] != "")) {
|
67
|
$iplookup[$hostent['ipaddr']] = $hostent['hostname'];
|
68
|
}
|
69
|
}
|
70
|
}
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
|
75
|
$_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);
|
76
|
|
77
|
$someinfo = false;
|
78
|
for ($x=2; $x<12; $x++){
|
79
|
|
80
|
$bandwidthinfo = $listedIPs[$x];
|
81
|
|
82
|
// echo $bandwidthinfo;
|
83
|
$emptyinfocounter = 1;
|
84
|
if ($bandwidthinfo != "") {
|
85
|
$infoarray = explode (":",$bandwidthinfo);
|
86
|
if (($filter == "all") ||
|
87
|
(($filter == "local") && (ip_in_subnet($infoarray[0], $intsubnet))) ||
|
88
|
(($filter == "remote") && (!ip_in_subnet($infoarray[0], $intsubnet)))) {
|
89
|
if ($hostipformat == "") {
|
90
|
$addrdata = $infoarray[0];
|
91
|
} else {
|
92
|
// $hostipformat is "hostname" or "fqdn"
|
93
|
$addrdata = gethostbyaddr($infoarray[0]);
|
94
|
if ($addrdata == $infoarray[0]) {
|
95
|
// gethostbyaddr() gave us back the IP address, so try the static mapping array
|
96
|
if ($iplookup[$infoarray[0]] != "")
|
97
|
$addrdata = $iplookup[$infoarray[0]];
|
98
|
} else {
|
99
|
if ($hostipformat == "hostname") {
|
100
|
// Only pass back the first part of the name, not the FQDN.
|
101
|
$name_array = explode(".", $addrdata);
|
102
|
$addrdata = $name_array[0];
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
//print host information;
|
107
|
echo $addrdata . ";" . $infoarray[1] . ";" . $infoarray[2] . "|";
|
108
|
|
109
|
//mark that we collected information
|
110
|
$someinfo = true;
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
unset($bandwidthinfo, $_grb);
|
115
|
unset($listedIPs);
|
116
|
|
117
|
//no bandwidth usage found
|
118
|
if ($someinfo == false)
|
119
|
echo gettext("no info");
|
120
|
|
121
|
?>
|