Project

General

Profile

Download (3.54 KB) Statistics
| Branch: | Tag: | Revision:
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

    
40
if ($filter == "local") {
41
	$ratesubnet = "-c " . $intsubnet;
42
} else {
43
	// Tell the rate utility to consider the whole internet (0.0.0.0/0)
44
	// and to consider local "l" traffic - i.e. traffic within the whole internet
45
	// then we can filter the resulting output as we wish below.
46
	$ratesubnet = "-lc 0.0.0.0/0";
47
}
48

    
49
//get the sort method
50
$sort = $_GET['sort'];
51
if ($sort == "out") {
52
	$sort_method = "-T";
53
} else {
54
	$sort_method = "-R";
55
}
56

    
57
// get the desired format for displaying the host name or IP
58
$hostipformat = $_GET['hostipformat'];
59
$iplookup = array();
60
// If hostname display is requested and the DNS forwarder does not already have DHCP static names registered,
61
// then load the DHCP static mappings into an array keyed by IP address.
62
if (($hostipformat != "") && ((!isset($config['dnsmasq']['enable']) || !isset($config['dnsmasq']['regdhcpstatic'])) ||
63
    (!isset($config['unbound']['enable']) || !isset($config['unbound']['regdhcpstatic'])))) {
64
	if (is_array($config['dhcpd'])) {
65
		foreach ($config['dhcpd'] as $ifdata) {
66
			if (is_array($ifdata['staticmap'])) {
67
				foreach ($ifdata['staticmap'] as $hostent) {
68
					if (($hostent['ipaddr'] != "") && ($hostent['hostname'] != "")) {
69
						$iplookup[$hostent['ipaddr']] = $hostent['hostname'];
70
					}
71
				}
72
			}
73
		}
74
	}
75
}
76

    
77
$_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);
78

    
79
$someinfo = false;
80
for ($x=2; $x<12; $x++) {
81

    
82
	$bandwidthinfo = $listedIPs[$x];
83

    
84
	// echo $bandwidthinfo;
85
	$emptyinfocounter = 1;
86
	if ($bandwidthinfo != "") {
87
		$infoarray = explode (":",$bandwidthinfo);
88
		if (($filter == "all") ||
89
		    (($filter == "local") && (ip_in_subnet($infoarray[0], $intsubnet))) ||
90
		    (($filter == "remote") && (!ip_in_subnet($infoarray[0], $intsubnet)))) {
91
			if ($hostipformat == "") {
92
				$addrdata = $infoarray[0];
93
			} else {
94
				// $hostipformat is "hostname" or "fqdn"
95
				$addrdata = gethostbyaddr($infoarray[0]);
96
				if ($addrdata == $infoarray[0]) {
97
					// gethostbyaddr() gave us back the IP address, so try the static mapping array
98
					if ($iplookup[$infoarray[0]] != "") {
99
						$addrdata = $iplookup[$infoarray[0]];
100
					}
101
				} else {
102
					if ($hostipformat == "hostname") {
103
						// Only pass back the first part of the name, not the FQDN.
104
						$name_array = explode(".", $addrdata);
105
						$addrdata = $name_array[0];
106
					}
107
				}
108
			}
109
			//print host information;
110
			echo $addrdata . ";" . $infoarray[1] . ";" . $infoarray[2] . "|";
111

    
112
			//mark that we collected information
113
			$someinfo = true;
114
		}
115
	}
116
}
117
unset($bandwidthinfo, $_grb);
118
unset($listedIPs);
119

    
120
//no bandwidth usage found
121
if ($someinfo == false) {
122
	echo gettext("no info");
123
}
124
?>
(2-2/256)