Project

General

Profile

Bug #3334 » pfSense_status_graph.patch

Joshua Sign, 01/19/2019 12:24 AM

View differences:

src/etc/inc/util.inc
1962 1962
	return ("$num {$units[$i]}");
1963 1963
}
1964 1964

  
1965

  
1966
function unformat_number($formated_num) {
1967
    $num = strtoupper($formated_num);
1968
    
1969
    if ( strpos($num,"T") !== false ) {
1970
        $num = str_replace("T","",$num) * 1000 * 1000 * 1000 * 1000;
1971
    } else if ( strpos($num,"G") !== false ) {
1972
        $num = str_replace("G","",$num) * 1000 * 1000 * 1000;
1973
    } else if ( strpos($num,"M") !== false ) {
1974
        $num = str_replace("M","",$num) * 1000 * 1000;
1975
    } else if ( strpos($num,"K") !== false ) {
1976
        $num = str_replace("K","",$num) * 1000;
1977
    }
1978
    
1979
    return $num;
1980
}
1981

  
1965 1982
function update_filter_reload_status($text, $new=false) {
1966 1983
	global $g;
1967 1984

  
src/usr/local/bin/iftop_parse.awk
1
BEGIN {
2
	FS		= " "
3
	numlist = 0
4
	nblines = 15
5
}
6
{
7
	if ( numlist == 1 && $1 == "--------------------------------------------------------------------------------------------" ) {
8
		exit
9
	}
10

  
11
	if ( numlist == 0 && $1 == "--------------------------------------------------------------------------------------------" ) {
12
		numlist = 1
13
		next
14
	}
15

  
16
	if ( numlist == 1 ) {
17
		if ( $0 ~ "=>" && nblines > 0 ) {
18
			SENDER = $2
19
			STX = pfFormat($4)
20
			getline
21
			RECEIVER = $1
22
			RTX = pfFormat($3)
23
			printf "%s;%s;%s\n", SENDER, RTX, STX
24
			printf "%s;%s;%s\n", RECEIVER, STX, RTX
25
			nblines--
26
			if ( nblines < 1 ) {
27
				exit
28
			}
29
		}
30
		next
31
	}
32
}
33
END {
34
}
35

  
36
function pfFormat(str) {
37
 	sub("b","",str)
38
	return str
39
}
src/usr/local/bin/iftop_parser.sh
1
#!/bin/sh
2

  
3
# Usage
4
if [ $# -ne 1 -a $# -ne 2 ]; then
5
        echo "Usage : $0 iface"
6
        exit
7
fi
8

  
9
# files paths
10
pid_file=/var/run/iftop_${1}.pid
11
cache_file=/var/db/iftop_${1}.log
12
awk_script=/usr/local/bin/iftop_parse.awk
13

  
14
# Binaries paths
15
DATE=/bin/date
16
STAT=/usr/bin/stat
17
CUT=/usr/bin/cut
18
PS=/bin/ps
19
GREP=/usr/bin/grep
20
CAT=/bin/cat
21
RM=/bin/rm
22
IFTOP=/usr/local/sbin/iftop
23
AWK=/usr/bin/awk
24

  
25
# test if pid file exist
26
if [ -f $pid_file ]; then
27
        # check how old is the file
28
        curTS=`$DATE +%s`
29
        pidTS=`$STAT -r $pid_file | $CUT -d " " -f 10`
30
        # if more than 10 seconds,
31
        # it must be a dead pid file (process killed?)
32
        # or a stucked process that we should kill
33
        if [ $(( curTS - pidTS )) -gt 10 ]; then
34
                oldPID=`$CAT $pid_file`
35
                # test if pid still exist
36
                run=`$PS -p $oldPID | $GREP -F $0`
37
                if [ "$run" != "" ]; then
38
                        kill -9 $oldPID
39
                fi
40
                $RM $pid_file
41
                $RM $cache_file 2>> /dev/null
42
        else
43
                if [ -s $cache_file ]; then
44
                        $CAT $cache_file
45
                fi
46
        fi
47
else
48
        echo -n $$ > $pid_file
49
        $IFTOP -nNb -i $1 -s 1 -o 2s -t 2>> /dev/null | $AWK -f $awk_script > ${cache_file}.tmp
50
        $CAT ${cache_file}.tmp > $cache_file
51
        $CAT $cache_file
52
        $RM $pid_file
53
fi
src/usr/local/www/bandwidth_by_ip.php
109 109
	}
110 110
}
111 111

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

  
113
//get the mode
114
$mode = !empty($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
115
if ($mode == "iftop") {
116
    $current_ts = time();
117
    if ( file_exists("/var/run/iftop_{$real_interface}.pid") ) {
118
        $statPID = stat("/var/run/iftop_{$real_interface}.pid");
119
        $since = $current_ts - $statPID['mtime'];
120
        if ( $since < 5 && file_exists("/var/db/iftop_{$real_interface}.log") ) {
121
            $listedIPs=file("/var/db/iftop_{$real_interface}.log");
122
        } else {
123
            if ( isvalidpid("/var/run/iftop_{$real_interface}.pid") ) {
124
                killbypid("/var/run/iftop_{$real_interface}.pid");
125
            }
126
            unlink("/var/run/iftop_{$real_interface}.pid");
127
            $_grb = exec("/usr/local/bin/iftop_parser.sh {$real_interface} $current_ts", $listedIPs);
128
        }
129
    } else {
130
        // refresh iftop infos
131
        $_grb = exec("/usr/local/bin/iftop_parser.sh {$real_interface} $current_ts", $listedIPs);
132
    }
133
    
134
    // order and group by
135
    foreach ($listedIPs as $k => $line){
136
        if ($line != "") {
137
            $arrLine = explode (";", $line);
138
            $ip  = $arrLine[0];
139
            $in  = unformat_number($arrLine[1]);
140
            $out = unformat_number($arrLine[2]);
141
            if ( isset($arr_in[$ip]) ) {
142
                $arr_in[$ip]  += $in;
143
                $arr_out[$ip] += $out;
144
            } else {
145
                $arr_in[$ip]  = $in;
146
                $arr_out[$ip] = $out;
147
            }
148
        }
149
    }
150
    
151
    if ($sort == "out") {
152
        arsort($arr_out,SORT_NUMERIC);
153
        $arrIP = array_keys($arr_out);
154
    } else {
155
        arsort($arr_in,SORT_NUMERIC);
156
        $arrIP = array_keys($arr_in);
157
    }
158
    
159
    unset($listedIPs);
160
    $listedIPs[] = "";
161
    $listedIPs[] = "";
162
    foreach ($arrIP as $k => $ip) {
163
        $listedIPs[] = $ip.";".format_number($arr_in[$ip],2).";".format_number($arr_out[$ip],2);
164
    }
165

  
166
} else {
167
    $_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);
168
}
169

  
113 170

  
114 171
$someinfo = false;
115 172
for ($x=2; $x<12; $x++) {
......
119 176
	// echo $bandwidthinfo;
120 177
	$emptyinfocounter = 1;
121 178
	if ($bandwidthinfo != "") {
122
		$infoarray = explode (":", $bandwidthinfo);
179
		$infoarray = explode (";", $bandwidthinfo);
123 180
		if (($filter == "all") ||
124 181
		    (($filter == "local") && (ip_in_subnet($infoarray[0], $intsubnet))) ||
125 182
		    (($filter == "remote") && (!ip_in_subnet($infoarray[0], $intsubnet)))) {
......
159 216
if ($someinfo == false) {
160 217
	echo gettext("no info");
161 218
}
162
?>
219
?>
src/usr/local/www/status_graph.php
77 77
	$curbackgroundupdate = $_POST['backgroundupdate'];
78 78
	$curinvert = $_POST['invert'];
79 79
	$cursmoothing = $_POST['smoothfactor'];
80

  
80
	$curmode = $_POST['mode'];
81
	
81 82
	// Save data to config
82 83
	if (isset($_POST['save'])) {
83 84
		$pconfig = array();
......
88 89
		$pconfig["backgroundupdate"] = $curbackgroundupdate;
89 90
		$pconfig["smoothfactor"] = $cursmoothing;
90 91
		$pconfig["invert"] = $curinvert;
92
		$pconfig["mode"] = $curmode;
91 93
		$config["traffic_graphs"] = array();
92 94
		$config["traffic_graphs"] = $pconfig;
93 95
		write_config("Traffic Graphs settings updated");
......
102 104
		$curbackgroundupdate = $pconfig['backgroundupdate'];
103 105
		$cursmoothing = $pconfig['smoothfactor'];
104 106
		$curinvert = $pconfig['invert'];
107
		$curmode = $pconfig['mode'];;
105 108
	} else {
106 109
		// initialize when no config details are present
107 110
		if (empty($ifdescrs["wan"])) {
......
117 120
		$curbackgroundupdate = "";
118 121
		$cursmoothing = 0;
119 122
		$curinvert = "";
123
		$curmode = "";
120 124
	}
121 125
}
122 126

  
......
183 187
	)
184 188
))->setHelp('Display');
185 189

  
190
$group->add(new Form_Select(
191
    'mode',
192
    null,
193
    $curmode,
194
    array (
195
        'rate'		=> gettext('rate (standard)'),
196
        'iftop'	    => gettext('iftop (experimental)')
197
    )
198
))->setHelp('Mode');
199
    
186 200
$section->add($group);
187 201

  
188 202
$group2 = new Form_Group('Controls');
(4-4/9)