Project

General

Profile

Download (35.3 KB) Statistics
| Branch: | Tag: | Revision:
1 b0a5b824 Seth Mos
<?php
2
/*
3 ac24dc24 Renato Botelho
 * rrd.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 c5d81585 Renato Botelho
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
7 38809d47 Renato Botelho do Couto
 * Copyright (c) 2010-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9 8f585441 Luiz Souza
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
10 ac24dc24 Renato Botelho
 * All rights reserved.
11
 *
12 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
17 ac24dc24 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23 ac24dc24 Renato Botelho
 */
24 b0a5b824 Seth Mos
25
/* include all configuration functions */
26
27
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
28 d9acea75 Scott Ullrich
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
29 552f5a6a Renato Botelho
	unlink_if_exists($xmldumpfile);
30 1b25fc27 Seth Mos
31 873c1701 Renato Botelho
	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
32 b0a5b824 Seth Mos
	if ($dumpret <> 0) {
33
		$dumpout = implode(" ", $dumpout);
34 addc0439 Renato Botelho
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
35 b0a5b824 Seth Mos
	}
36
	return($dumpret);
37
}
38
39
function create_new_rrd($rrdcreatecmd) {
40
	$rrdcreateoutput = array();
41
	$rrdcreatereturn = 0;
42 2159494f Ermal
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
43 b0a5b824 Seth Mos
	if ($rrdcreatereturn <> 0) {
44
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
45 addc0439 Renato Botelho
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
46 b0a5b824 Seth Mos
	}
47 2159494f Ermal
	unset($rrdcreateoutput);
48 b0a5b824 Seth Mos
	return $rrdcreatereturn;
49
}
50
51
function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
52 61e047a5 Phil Davis
	if (!file_exists("/tmp/rrd_notice_sent.txt")) {
53 2159494f Ermal
		$_gb = exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
54
		@touch("/tmp/rrd_notice_sent.txt");
55 fdd20aba Scott Ullrich
	}
56 b0a5b824 Seth Mos
	$numrraold = count($rrdoldxml['rra']);
57
	$numrranew = count($rrdnewxml['rra']);
58 2159494f Ermal
	$numdsold = count($rrdoldxml['ds']);
59 b0a5b824 Seth Mos
	$numdsnew = count($rrdnewxml['ds']);
60 086cf944 Phil Davis
	log_error(sprintf(gettext('Import RRD has %1$s DS values and %2$s RRA databases, new format RRD has %3$s DS values and %4$s RRA databases'), $numdsold, $numrraold, $numdsnew , $numrranew));
61 5e5d5abc Renato Botelho
62 b0a5b824 Seth Mos
	/* add data sources not found in the old array from the new array */
63
	$i = 0;
64 61e047a5 Phil Davis
	foreach ($rrdnewxml['ds'] as $ds) {
65
		if (!is_array($rrdoldxml['ds'][$i])) {
66 b0a5b824 Seth Mos
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
67 d9e896fa Seth Mos
			/* set unknown values to 0 */
68
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
69
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
70
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
71 b0a5b824 Seth Mos
		}
72
		$i++;
73
	}
74
75
	$i = 0;
76
	$rracountold = count($rrdoldxml['rra']);
77
	$rracountnew = count($rrdnewxml['rra']);
78
	/* process each RRA, which contain a database */
79 61e047a5 Phil Davis
	foreach ($rrdnewxml['rra'] as $rra) {
80
		if (!is_array($rrdoldxml['rra'][$i])) {
81 b0a5b824 Seth Mos
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
82
		}
83
84
		$d = 0;
85
		/* process cdp_prep */
86
		$cdp_prep = $rra['cdp_prep'];
87 61e047a5 Phil Davis
		foreach ($cdp_prep['ds'] as $ds) {
88
			if (!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
89 b0a5b824 Seth Mos
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
90 d9e896fa Seth Mos
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
91
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
92
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
93
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
94 b0a5b824 Seth Mos
			}
95
			$d++;
96
		}
97
98
		/* process database */
99
		$rows = $rra['database'];
100
		$k = 0;
101
		$rowcountold = count($rrdoldxml['rra'][$i]['database']['row']);
102
		$rowcountnew = count($rrdnewxml['rra'][$i]['database']['row']);
103 ddc26847 Seth Mos
		$rowcountdiff = $rowcountnew - $rowcountold;
104
		/* save old rows for a bit before we put the required empty rows before it */
105
		$rowsdata = $rows;
106
		$rowsempty = array();
107
		$r = 0;
108 61e047a5 Phil Davis
		while ($r < $rowcountdiff) {
109 ddc26847 Seth Mos
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
110
			$r++;
111
		}
112
		$rows = $rowsempty + $rowsdata;
113 b0a5b824 Seth Mos
		/* now foreach the rows in the database */
114 61e047a5 Phil Davis
		foreach ($rows['row'] as $row) {
115
			if (!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
116 b0a5b824 Seth Mos
				$rrdoldxml['rra'][$i]['database']['row'][$k] = $rrdnewxml['rra'][$i]['database']['row'][$k];
117
			}
118
			$m = 0;
119
			$vcountold = count($rrdoldxml['rra'][$i]['database']['row'][$k]['v']);
120
			$vcountnew = count($rrdnewxml['rra'][$i]['database']['row'][$k]['v']);
121 61e047a5 Phil Davis
			foreach ($row['v'] as $value) {
122
				if (empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
123
					if (isset($valid)) {
124 ed3ea464 Seth Mos
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = "0.0000000000e+00 ";
125
					} else {
126
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = $rrdnewxml['rra'][$i]['database']['row'][$k]['v'][$m];
127
					}
128
				} else {
129 61e047a5 Phil Davis
					if ($value <> " NaN ") {
130 ed3ea464 Seth Mos
						$valid = true;
131
					} else {
132
						$valid = false;
133
					}
134 b0a5b824 Seth Mos
				}
135
				$m++;
136
			}
137
			$k++;
138
		}
139
		$i++;
140
	}
141
142
	$numrranew = count($rrdoldxml['rra']);
143
	$numdsnew = count($rrdoldxml['ds']);
144 addc0439 Renato Botelho
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
145 b0a5b824 Seth Mos
	return $rrdoldxml;
146
}
147
148 ea4bc46f Seth Mos
function enable_rrd_graphing() {
149
	global $config, $g, $altq_list_queues;
150
151 61e047a5 Phil Davis
	if (platform_booting()) {
152 9eda6186 Carlos Eduardo Ramos
		echo gettext("Generating RRD graphs...");
153 61e047a5 Phil Davis
	}
154 ea4bc46f Seth Mos
155 afb6ae0c Renato Botelho
	$rrddbpath = "{$g['vardb_path']}/rrd/";
156 ea4bc46f Seth Mos
	$rrdgraphpath = "/usr/local/www/rrd";
157
158
	$traffic = "-traffic.rrd";
159
	$packets = "-packets.rrd";
160
	$states = "-states.rrd";
161
	$wireless = "-wireless.rrd";
162
	$queues = "-queues.rrd";
163
	$queuesdrop = "-queuedrops.rrd";
164
	$proc = "-processor.rrd";
165
	$mem = "-memory.rrd";
166 3ed917c7 jim-p
	$mbuf = "-mbuf.rrd";
167 ec51a222 thompsa
	$cellular = "-cellular.rrd";
168 edd2d8b7 smos
	$vpnusers = "-vpnusers.rrd";
169 20413b72 Warren Baker
	$captiveportalconcurrent = "-concurrent.rrd";
170
	$captiveportalloggedin = "-loggedin.rrd";
171 1f3eff9b nagyrobi
	$ntpd = "ntpd.rrd";
172 89f3ec04 heper
	$dhcpd = "-dhcpd.rrd";
173 ea4bc46f Seth Mos
174 d9acea75 Scott Ullrich
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
175 ea4bc46f Seth Mos
	$netstat = "/usr/bin/netstat";
176
	$awk = "/usr/bin/awk";
177
	$tar = "/usr/bin/tar";
178
	$pfctl = "/sbin/pfctl";
179
	$sysctl = "/sbin/sysctl";
180 cb7d18d5 Renato Botelho
	$php = "/usr/local/bin/php-cgi";
181 e9e295f7 jim-p
	$cpustats = "/usr/local/sbin/cpustats";
182 ea4bc46f Seth Mos
	$ifconfig = "/sbin/ifconfig";
183 20413b72 Warren Baker
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
184 89f3ec04 heper
	$dhcpd_gather = "/usr/local/bin/dhcpd_gather_stats.php";
185 617f9edf nagyrobi
	$ntpq = "/usr/local/sbin/ntpq";
186 ea4bc46f Seth Mos
187
	$rrdtrafficinterval = 60;
188
	$rrdwirelessinterval = 60;
189
	$rrdqueuesinterval = 60;
190
	$rrdqueuesdropinterval = 60;
191
	$rrdpacketsinterval = 60;
192
	$rrdstatesinterval = 60;
193
	$rrdlbpoolinterval = 60;
194
	$rrdprocinterval = 60;
195
	$rrdmeminterval = 60;
196 3ed917c7 jim-p
	$rrdmbufinterval = 60;
197 ec51a222 thompsa
	$rrdcellularinterval = 60;
198 55c08a96 smos
	$rrdvpninterval = 60;
199 20413b72 Warren Baker
	$rrdcaptiveportalinterval = 60;
200 1f3eff9b nagyrobi
	$rrdntpdinterval = 60;
201 89f3ec04 heper
	$rrddhcpdinterval = 60;
202 ea4bc46f Seth Mos
203
	$trafficvalid = $rrdtrafficinterval * 2;
204
	$wirelessvalid = $rrdwirelessinterval * 2;
205
	$queuesvalid = $rrdqueuesinterval * 2;
206
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
207
	$packetsvalid = $rrdpacketsinterval * 2;
208
	$statesvalid = $rrdstatesinterval*2;
209
	$lbpoolvalid = $rrdlbpoolinterval * 2;
210
	$procvalid = $rrdlbpoolinterval * 2;
211
	$memvalid = $rrdmeminterval * 2;
212 3ed917c7 jim-p
	$mbufvalid = $rrdmbufinterval * 2;
213 ec51a222 thompsa
	$cellularvalid = $rrdcellularinterval * 2;
214 edd2d8b7 smos
	$vpnvalid = $rrdvpninterval * 2;
215 20413b72 Warren Baker
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
216 1f3eff9b nagyrobi
	$ntpdvalid = $rrdntpdinterval * 2;
217 89f3ec04 heper
	$dhcpdvalid = $rrddhcpdinterval * 2;
218 4e322e2c Phil Davis
219 fa3b33a5 Renato Botelho
	/* Assume 2*10GigE for now */
220
	$downstream = 2500000000;
221
	$upstream = 2500000000;
222 ea4bc46f Seth Mos
223
	/* read the shaper config */
224
	read_altq_config();
225
226
	if (isset ($config['rrd']['enable'])) {
227
228
		/* create directory if needed */
229 3d83f02e jim-p
		if (!is_dir($rrddbpath)) {
230
			mkdir($rrddbpath, 0775);
231 ea4bc46f Seth Mos
		}
232 25cca20b jim-p
		chown($rrddbpath, "nobody");
233 ea4bc46f Seth Mos
234
		/* db update script */
235
		$rrdupdatesh = "#!/bin/sh\n";
236
		$rrdupdatesh .= "\n";
237 a555cc58 Seth Mos
		$rrdupdatesh .= "export TERM=dumb\n";
238 3d54aa10 Renato Botelho
		$rrdupdatesh .= "\n";
239
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
240
		$rrdupdatesh .= "\n";
241 ea4bc46f Seth Mos
		$rrdupdatesh .= "counter=1\n";
242
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
243
		$rrdupdatesh .= "do\n";
244
		$rrdupdatesh .= "";
245
246
		$i = 0;
247 4563d12f Seth Mos
		$ifdescrs = get_configured_interface_with_descr();
248 edd2d8b7 smos
		/* IPsec counters */
249 4563d12f Seth Mos
		$ifdescrs['ipsec'] = "IPsec";
250 edd2d8b7 smos
		/* OpenVPN server counters */
251 61e047a5 Phil Davis
		if (is_array($config['openvpn']['openvpn-server'])) {
252
			foreach ($config['openvpn']['openvpn-server'] as $server) {
253 55c08a96 smos
				$serverid = "ovpns" . $server['vpnid'];
254
				$ifdescrs[$serverid] = "{$server['description']}";
255
			}
256
		}
257 4563d12f Seth Mos
258 edd2d8b7 smos
		/* process all real and pseudo interfaces */
259 4563d12f Seth Mos
		foreach ($ifdescrs as $ifname => $ifdescr) {
260 65615d89 Seth Mos
			$temp = get_real_interface($ifname);
261 61e047a5 Phil Davis
			if ($temp <> "") {
262 65615d89 Seth Mos
				$realif = $temp;
263
			}
264 ea4bc46f Seth Mos
265
			/* TRAFFIC, set up the rrd file */
266
			if (!file_exists("$rrddbpath$ifname$traffic")) {
267
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
268
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
269
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
270
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
271
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
272 9bc8b6b6 Seth Mos
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
273
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
274
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
275
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
276 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
277
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
278
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
279 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
280 ea4bc46f Seth Mos
281
				create_new_rrd($rrdcreate);
282 cba9d7d9 Renato Botelho
				unset($rrdcreate);
283 ea4bc46f Seth Mos
			}
284
285
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
286 61e047a5 Phil Davis
			if (platform_booting()) {
287 9bc8b6b6 Seth Mos
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
288 ea4bc46f Seth Mos
			}
289
290
			$rrdupdatesh .= "\n";
291 9bc8b6b6 Seth Mos
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
292 a555cc58 Seth Mos
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
293
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
294
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
295
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
296
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
297 ea4bc46f Seth Mos
298
			/* PACKETS, set up the rrd file */
299
			if (!file_exists("$rrddbpath$ifname$packets")) {
300
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
301
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
302
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
303
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
304
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
305 9bc8b6b6 Seth Mos
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
306
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
307
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
308
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
309 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
310
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
311
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
312 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
313 ea4bc46f Seth Mos
314
				create_new_rrd($rrdcreate);
315 cba9d7d9 Renato Botelho
				unset($rrdcreate);
316 ea4bc46f Seth Mos
			}
317
318
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
319 61e047a5 Phil Davis
			if (platform_booting()) {
320 9bc8b6b6 Seth Mos
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
321 ea4bc46f Seth Mos
			}
322
323
			$rrdupdatesh .= "\n";
324
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
325 a555cc58 Seth Mos
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
326
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
327
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
328
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
329
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
330 ea4bc46f Seth Mos
331
			/* WIRELESS, set up the rrd file */
332 61e047a5 Phil Davis
			if ($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
333 ea4bc46f Seth Mos
				if (!file_exists("$rrddbpath$ifname$wireless")) {
334
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
335
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
336
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
337
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
338 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
339
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
340
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
341 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
342 5e5d5abc Renato Botelho
343 ea4bc46f Seth Mos
					create_new_rrd($rrdcreate);
344 cba9d7d9 Renato Botelho
					unset($rrdcreate);
345 ea4bc46f Seth Mos
				}
346
347
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
348 61e047a5 Phil Davis
				if (platform_booting()) {
349 e256e9d4 smos
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
350 ea4bc46f Seth Mos
				}
351
352
				$rrdupdatesh .= "\n";
353
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
354 e9e295f7 jim-p
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
355 488595df smos
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
356 ea4bc46f Seth Mos
			}
357
358 edd2d8b7 smos
			/* OpenVPN, set up the rrd file */
359 61e047a5 Phil Davis
			if (stristr($ifname, "ovpns")) {
360 edd2d8b7 smos
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
361
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
362
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
363 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
364
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
365
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
366 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
367 5e5d5abc Renato Botelho
368 edd2d8b7 smos
					create_new_rrd($rrdcreate);
369 cba9d7d9 Renato Botelho
					unset($rrdcreate);
370 edd2d8b7 smos
				}
371
372
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
373 61e047a5 Phil Davis
				if (platform_booting()) {
374 edd2d8b7 smos
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
375
				}
376 ea4bc46f Seth Mos
377 61e047a5 Phil Davis
				if (is_array($config['openvpn']['openvpn-server'])) {
378
					foreach ($config['openvpn']['openvpn-server'] as $server) {
379
						if ("ovpns{$server['vpnid']}" == $ifname) {
380 edd2d8b7 smos
							$port = $server['local_port'];
381 5f250a24 smos
							$vpnid = $server['vpnid'];
382 edd2d8b7 smos
						}
383
					}
384
				}
385
				$rrdupdatesh .= "\n";
386
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
387
				$rrdupdatesh .= "list_current_users() {\n";
388
				$rrdupdatesh .= " sleep 0.2\n";
389
				$rrdupdatesh .= " echo \"status 2\"\n";
390
				$rrdupdatesh .= " sleep 0.2\n";
391
				$rrdupdatesh .= " echo \"quit\"\n";
392
				$rrdupdatesh .= "}\n";
393 348c2af1 jim-p
				$rrdupdatesh .= "OVPN=`list_current_users | /usr/bin/nc -U {$g['openvpn_base']}/server{$vpnid}/sock | /usr/bin/awk -F\",\" '/^CLIENT_LIST/ {print \$2}' | /usr/bin/wc -l | /usr/bin/awk '{print $1}'`\n";
394 e9e295f7 jim-p
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
395 edd2d8b7 smos
			}
396 ea4bc46f Seth Mos
397 edd2d8b7 smos
			/* QUEUES, set up the queues databases */
398 55d340b8 Viktor G
			if (($altq_list_queues[$ifname]) && 
399
			    !empty($altq_list_queues[$ifname]->get_queue_list())) {
400 edd2d8b7 smos
				$altq =& $altq_list_queues[$ifname];
401
				/* NOTE: Is it worth as its own function?! */
402
				switch ($altq->GetBwscale()) {
403
					case "Gb":
404
						$factor = 1024 * 1024 * 1024;
405 e8c516a0 Phil Davis
						break;
406 edd2d8b7 smos
					case "Mb":
407 e8c516a0 Phil Davis
						$factor = 1024 * 1024;
408
						break;
409 edd2d8b7 smos
					case "Kb":
410 e8c516a0 Phil Davis
						$factor = 1024;
411
						break;
412 edd2d8b7 smos
					case "b":
413
					default:
414 e8c516a0 Phil Davis
						$factor = 1;
415
						break;
416 edd2d8b7 smos
				}
417 3c95346d jim-p
				$qbandwidth = (int) $altq->GetBandwidth() * $factor;
418 6c07db48 Phil Davis
				if ($qbandwidth <= 0) {
419 edd2d8b7 smos
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
420
				}
421
				$qlist =& $altq->get_queue_list($notused);
422
				if (!file_exists("$rrddbpath$ifname$queues")) {
423
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
424
					/* loop list of shaper queues */
425
					$q = 0;
426
					foreach ($qlist as $qname => $q) {
427
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
428 ea4bc46f Seth Mos
					}
429
430 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
431
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
432
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
433 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
434 ea4bc46f Seth Mos
435 edd2d8b7 smos
					create_new_rrd($rrdcreate);
436 cba9d7d9 Renato Botelho
					unset($rrdcreate);
437 edd2d8b7 smos
				}
438 ea4bc46f Seth Mos
439 edd2d8b7 smos
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
440
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
441
					/* loop list of shaper queues */
442
					$q = 0;
443
					foreach ($qlist as $qname => $q) {
444
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
445 ea4bc46f Seth Mos
					}
446
447 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
448
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
449
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
450 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
451 edd2d8b7 smos
452
					create_new_rrd($rrdcreate);
453 cba9d7d9 Renato Botelho
					unset($rrdcreate);
454 edd2d8b7 smos
				}
455
456 61e047a5 Phil Davis
				if (platform_booting()) {
457 edd2d8b7 smos
					$rrdqcommand = "-t ";
458
					$rrducommand = "N";
459 857a4a79 jim-p
					$qi = 0;
460 edd2d8b7 smos
					foreach ($qlist as $qname => $q) {
461 61e047a5 Phil Davis
						if ($qi == 0) {
462 edd2d8b7 smos
							$rrdqcommand .= "{$qname}";
463
						} else {
464
							$rrdqcommand .= ":{$qname}";
465 ea4bc46f Seth Mos
						}
466 857a4a79 jim-p
						$qi++;
467 edd2d8b7 smos
						$rrducommand .= ":U";
468 ea4bc46f Seth Mos
					}
469 edd2d8b7 smos
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
470
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
471 ea4bc46f Seth Mos
				}
472 edd2d8b7 smos
473
				/* awk function to gather shaper data */
474
				/* yes, it's special */
475
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
476
				$rrdupdatesh .= "{ ";
477
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
478
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
479
				$rrdupdatesh .= " q=1; ";
480
				$rrdupdatesh .= "} ";
481
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
482
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
483
				$rrdupdatesh .= " q=0; ";
484
				$rrdupdatesh .= "} ";
485
				$rrdupdatesh .= "} END { ";
486
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
487
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
488
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
489
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
490
491
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
492
				$rrdupdatesh .= "{ ";
493
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
494
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
495
				$rrdupdatesh .= " q=1; ";
496
				$rrdupdatesh .= "} ";
497
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
498
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
499
				$rrdupdatesh .= " q=0; ";
500
				$rrdupdatesh .= "} ";
501
				$rrdupdatesh .= "} END { ";
502
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
503
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
504
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
505
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
506
			}
507 5e589685 smos
508
			/* 3G interfaces */
509 61e047a5 Phil Davis
			if (preg_match("/ppp[0-9]+/i", $realif))	{
510 5e589685 smos
				if (!file_exists("$rrddbpath$ifname$cellular")) {
511
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
512
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
513
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
514
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
515 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
516
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
517
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
518 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
519 5e589685 smos
					create_new_rrd($rrdcreate);
520 cba9d7d9 Renato Botelho
					unset($rrdcreate);
521 5e589685 smos
				}
522
523
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
524 61e047a5 Phil Davis
				if (platform_booting()) {
525 5e589685 smos
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
526
				}
527
528
				$rrdupdatesh .= "\n";
529
				$rrdupdatesh .= "# polling 3G\n";
530 2b095a33 smos
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
531
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
532 5e589685 smos
			}
533
534 ea4bc46f Seth Mos
		}
535
		$i++;
536
537
		/* System only statistics */
538
		$ifname = "system";
539
540 edd2d8b7 smos
		/* STATES, create pf states database */
541 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$states")) {
542 edd2d8b7 smos
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
543
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
544
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
545
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
546
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
547
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
548 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
549
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
550
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
551 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
552 edd2d8b7 smos
553
			create_new_rrd($rrdcreate);
554 cba9d7d9 Renato Botelho
			unset($rrdcreate);
555 edd2d8b7 smos
		}
556 ea4bc46f Seth Mos
557 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
558 61e047a5 Phil Davis
		if (platform_booting()) {
559 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
560
		}
561 ea4bc46f Seth Mos
562 edd2d8b7 smos
		/* the pf states gathering function. */
563
		$rrdupdatesh .= "\n";
564
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
565
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
566
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
567
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
568
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
569
		$rrdupdatesh .= "srcip=\"` cat /tmp/pfctl_ss_out | egrep -v '<\\-.*?<\\-|\\->.*?\\->' | grep '\\->' | awk '{print \$3}' | awk -F: '{print \$1}' | sort -u|wc -l|sed 's/ //g' `\"\n";
570
		$rrdupdatesh .= "dstip=\"` cat /tmp/pfctl_ss_out | egrep -v '<\\-.*?<\\-|\\->.*?\\->' | grep '<\\-' | awk '{print \$3}' | awk -F: '{print \$1}' | sort -u|wc -l|sed 's/ //g' `\"\n";
571
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
572
573
		/* End pf states statistics */
574
575
		/* CPU, create CPU statistics database */
576 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$proc")) {
577 edd2d8b7 smos
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
578
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
579
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
580
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
581
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
582
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
583 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
584
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
585
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
586 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
587 edd2d8b7 smos
588
			create_new_rrd($rrdcreate);
589 cba9d7d9 Renato Botelho
			unset($rrdcreate);
590 edd2d8b7 smos
		}
591 ea4bc46f Seth Mos
592 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
593 61e047a5 Phil Davis
		if (platform_booting()) {
594 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
595
		}
596 ea4bc46f Seth Mos
597 edd2d8b7 smos
		/* the CPU stats gathering function. */
598 e9e295f7 jim-p
		$rrdupdatesh .= "CPU=`$cpustats | cut -f1-4 -d':'`\n";
599
		/* Using ps uxaH will count all processes including system threads. Top was undercounting. */
600
		$rrdupdatesh .= "PROCS=`ps uxaH | wc -l | awk '{print \$1;}'`\n";
601
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$proc N:\${CPU}:\${PROCS}\n";
602 edd2d8b7 smos
603
		/* End CPU statistics */
604
605
		/* Memory, create Memory statistics database */
606 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$mem")) {
607 edd2d8b7 smos
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
608
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
609
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
610
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
611
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
612
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
613 492b1314 N0YB
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
614
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
615
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
616 3e2ecafe N0YB
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
617 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
618
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
619
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
620 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
621 492b1314 N0YB
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
622
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
623
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
624 3e2ecafe N0YB
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
625 edd2d8b7 smos
626
			create_new_rrd($rrdcreate);
627 cba9d7d9 Renato Botelho
			unset($rrdcreate);
628 edd2d8b7 smos
		}
629 ea4bc46f Seth Mos
630 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
631 61e047a5 Phil Davis
		if (platform_booting()) {
632 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
633
		}
634 ea4bc46f Seth Mos
635 edd2d8b7 smos
		/* the Memory stats gathering function. */
636 3c44c845 Luiz Souza
		$rrdupdatesh .= "MEM=`$sysctl -qn vm.stats.vm.v_page_count vm.stats.vm.v_active_count vm.stats.vm.v_inactive_count vm.stats.vm.v_free_count vm.stats.vm.v_cache_count vm.stats.vm.v_wire_count | ";
637 e9e295f7 jim-p
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
638
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
639
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
640 5e5d5abc Renato Botelho
641 edd2d8b7 smos
		/* End Memory statistics */
642
643 3ed917c7 jim-p
		/* mbuf, create mbuf statistics database */
644 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$mbuf")) {
645 3ed917c7 jim-p
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
646
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
647
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
648
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
649
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
650
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
651
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
652
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
653 3e2ecafe N0YB
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
654 3ed917c7 jim-p
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
655
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
656
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
657 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
658 3ed917c7 jim-p
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
659
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
660
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
661 3e2ecafe N0YB
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
662 3ed917c7 jim-p
663
			create_new_rrd($rrdcreate);
664
			unset($rrdcreate);
665
		}
666
667
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
668 61e047a5 Phil Davis
		if (platform_booting()) {
669 06ff126e N0YB
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
670 3ed917c7 jim-p
		}
671
672
		/* the mbuf stats gathering function. */
673
		$rrdupdatesh .= "MBUF=`$netstat -m | ";
674
		$rrdupdatesh .= " $awk '/mbuf clusters in use/ { gsub(/\//, \":\", $1); print $1; }'`\n";
675
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mbuf N:\${MBUF}\n";
676
677
		/* End mbuf statistics */
678
679 20413b72 Warren Baker
		/* Captive Portal statistics, set up the rrd file */
680 61e047a5 Phil Davis
		if (is_array($config['captiveportal'])) {
681 cba9d7d9 Renato Botelho
			foreach ($config['captiveportal'] as $cpkey => $cp) {
682 61e047a5 Phil Davis
				if (!isset($cp['enable'])) {
683 cba9d7d9 Renato Botelho
					continue;
684 61e047a5 Phil Davis
				}
685 cba9d7d9 Renato Botelho
686
				$ifname= "captiveportal";
687
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
688
				if (!file_exists("$concurrent_filename")) {
689
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
690
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
691 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
692
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
693
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
694 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
695 492b1314 N0YB
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
696
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
697
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
698 3e2ecafe N0YB
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
699 492b1314 N0YB
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
700
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
701
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
702 3e2ecafe N0YB
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
703 492b1314 N0YB
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
704
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
705
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
706 3e2ecafe N0YB
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
707 20413b72 Warren Baker
708 cba9d7d9 Renato Botelho
					create_new_rrd($rrdcreate);
709
					unset($rrdcreate);
710
				}
711 20413b72 Warren Baker
712 cba9d7d9 Renato Botelho
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
713 61e047a5 Phil Davis
				if (platform_booting()) {
714 cba9d7d9 Renato Botelho
					mwexec("$rrdtool update $concurrent_filename N:U");
715
				}
716 5e5d5abc Renato Botelho
717 cba9d7d9 Renato Botelho
				/* the Captive Portal stats gathering function. */
718
				$rrdupdatesh .= "\n";
719
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
720 9e378421 jim-p
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
721 cba9d7d9 Renato Botelho
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
722
723
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
724
				if (!file_exists("$loggedin_filename")) {
725
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
726
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
727 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
728
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
729
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
730 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
731 492b1314 N0YB
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
732
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
733
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
734 3e2ecafe N0YB
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
735 492b1314 N0YB
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
736
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
737
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
738 3e2ecafe N0YB
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
739 492b1314 N0YB
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
740
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
741
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
742 3e2ecafe N0YB
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
743 20413b72 Warren Baker
744 cba9d7d9 Renato Botelho
					create_new_rrd($rrdcreate);
745
					unset($rrdcreate);
746
				}
747 20413b72 Warren Baker
748 cba9d7d9 Renato Botelho
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
749 61e047a5 Phil Davis
				if (platform_booting()) {
750 cba9d7d9 Renato Botelho
					mwexec("$rrdtool update $loggedin_filename N:U");
751
				}
752 20413b72 Warren Baker
753 cba9d7d9 Renato Botelho
				/* the Captive Portal stats gathering function. */
754
				$rrdupdatesh .= "\n";
755
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
756 9e378421 jim-p
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
757 cba9d7d9 Renato Botelho
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
758 7e5f3edb Warren Baker
759 cba9d7d9 Renato Botelho
			}
760 20413b72 Warren Baker
		}
761 1f3eff9b nagyrobi
		/* End Captive Portal statistics */
762
763
		/* NTP, set up the ntpd rrd file */
764 b5d5da0c Viktor Gurov
		if (isset($config['ntpd']['statsgraph']) && ($config['ntpd']['enable'] != 'disabled')) {
765 1f3eff9b nagyrobi
			/* set up the ntpd rrd file */
766 a88376d9 nagyrobi
			if (!file_exists("$rrddbpath$ntpd")) {
767 1f3eff9b nagyrobi
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
768 da8b271a David Wood
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:-1000:1000 ";
769 1f3eff9b nagyrobi
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
770
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
771
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
772 3ccfb627 Viktor G
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:-1000:1000 ";
773 1f3eff9b nagyrobi
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
774
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
775
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
776
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
777
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
778
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
779
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
780
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
781
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
782
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
783
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
784
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
785
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
786
787
				create_new_rrd($rrdcreate);
788
				unset($rrdcreate);
789
			}
790
791
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
792 61e047a5 Phil Davis
			if (platform_booting()) {
793
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
794 1f3eff9b nagyrobi
			}
795
796
			/* the ntp stats gathering function. */
797
			$rrdupdatesh .= "\n";
798
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
799
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
800
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
801
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
802
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
803
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
804
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
805
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
806
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
807
			$rrdupdatesh .= "\n";
808
809
		}
810
		/* End NTP statistics */
811 4e322e2c Phil Davis
812 89f3ec04 heper
		/* Start dhcpd statistics */
813 ec5b9e96 Chris Buechler
		if (is_array($config['dhcpd'])) {
814
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
815 4e322e2c Phil Davis
				if (isset($config['dhcpd'][$dhcpif]['statsgraph'])) {
816 ec5b9e96 Chris Buechler
					if (!file_exists("$rrddbpath$dhcpif$dhcpd")) {
817
						$rrdcreate = "$rrdtool create $rrddbpath$dhcpif$dhcpd --step $rrddhcpdinterval ";
818
						$rrdcreate .= "DS:leases:GAUGE:$dhcpdvalid:0:100000 ";
819
						$rrdcreate .= "DS:staticleases:GAUGE:$dhcpdvalid:0:100000 ";
820
						$rrdcreate .= "DS:dhcprange:GAUGE:$dhcpdvalid:0:100000 ";
821
						$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
822
						$rrdcreate .= "RRA:MIN:0.5:5:720 ";
823
						$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
824
						$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
825
						$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
826
						$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
827
						$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
828
						$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
829
						$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
830
						$rrdcreate .= "RRA:MAX:0.5:5:720 ";
831
						$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
832
						$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
833
						create_new_rrd($rrdcreate);
834
						unset($rrdcreate);
835
					}
836 89f3ec04 heper
837 ec5b9e96 Chris Buechler
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
838
					if (platform_booting()) {
839 3401dbe2 heper
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U");
840 ec5b9e96 Chris Buechler
					}
841 89f3ec04 heper
842 ec5b9e96 Chris Buechler
					$rrdupdatesh .= "\n";
843
					$rrdupdatesh .= "# polling leases for dhcp \n";
844
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
845
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
846 4e322e2c Phil Davis
847 ec5b9e96 Chris Buechler
				}
848 89f3ec04 heper
			}
849
		}
850
		/* END dhcpd statistics */
851 ec51a222 thompsa
852 c53acd0f Renato Botelho
		/* Start gateway quality */
853
		$rrdupdatesh .= <<<EOD
854
855
# Gateway quality graphs
856
for sock in {$g['varrun_path']}/dpinger_*.sock; do
857
	if [ ! -S "\$sock" ]; then
858
		continue
859
	fi
860 73e3aa21 Renato Botelho
861 9f268ac0 Denny Page
	t=\$(/usr/bin/nc -U \$sock)
862 0e1eef54 Denny Page
	if [ -z "\$t" ]; then
863 73e3aa21 Renato Botelho
		continue
864
	fi
865
866 0e1eef54 Denny Page
	gw=\$(echo "\$t" | awk '{ print \$1 }')
867
	delay=\$(echo "\$t" | awk '{ print \$2 }')
868 0b3613ef Denny Page
	stddev=\$(echo "\$t" | awk '{ print \$3 }')
869 0e1eef54 Denny Page
	loss=\$(echo "\$t" | awk '{ print \$4 }')
870 c53acd0f Renato Botelho
871
	if echo "\$loss" | grep -Eqv '^[0-9]+\$'; then
872
		loss="U"
873
	fi
874
	if echo "\$delay" | grep -Eqv '^[0-9]+\$'; then
875
		delay="U"
876 4fa580c2 Renato Botelho
	else
877 515887c3 NOYB
		# Convert delay from microseconds to seconds
878 834b97f1 Renato Botelho
		delay=\$(echo "scale=7; \$delay / 1000 / 1000" | /usr/bin/bc)
879 c53acd0f Renato Botelho
	fi
880 0b3613ef Denny Page
	if echo "\$stddev" | grep -Eqv '^[0-9]+\$'; then
881
		stddev="U"
882
	else
883 515887c3 NOYB
		# Convert stddev from microseconds to seconds
884 0b3613ef Denny Page
		stddev=\$(echo "scale=7; \$stddev / 1000 / 1000" | /usr/bin/bc)
885
	fi
886 c53acd0f Renato Botelho
887
	if [ ! -f {$rrddbpath}\$gw-quality.rrd ]; then
888
		{$rrdtool} create {$rrddbpath}\$gw-quality.rrd --step 60 \\
889
		DS:loss:GAUGE:120:0:100 \\
890
		DS:delay:GAUGE:120:0:100000 \\
891 0b3613ef Denny Page
		DS:stddev:GAUGE:120:0:100000 \\
892 c53acd0f Renato Botelho
		RRA:AVERAGE:0.5:1:1200 \\
893
		RRA:AVERAGE:0.5:5:720 \\
894
		RRA:AVERAGE:0.5:60:1860 \\
895
		RRA:AVERAGE:0.5:1440:2284
896
897 0b3613ef Denny Page
		{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:U:U:U
898 c53acd0f Renato Botelho
	fi
899
900 0b3613ef Denny Page
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:\$loss:\$delay:\$stddev
901 c53acd0f Renato Botelho
done
902
903
EOD;
904
		/* End gateway quality */
905
906 ea4bc46f Seth Mos
		$rrdupdatesh .= "sleep 60\n";
907
		$rrdupdatesh .= "done\n";
908 9eda6186 Carlos Eduardo Ramos
		log_error(gettext("Creating rrd update script"));
909 ea4bc46f Seth Mos
		/* write the rrd update script */
910
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
911
		$fd = fopen("$updaterrdscript", "w");
912
		fwrite($fd, "$rrdupdatesh");
913
		fclose($fd);
914
915 cba9d7d9 Renato Botelho
		unset($rrdupdatesh);
916
917 ea4bc46f Seth Mos
		/* kill off traffic collectors */
918
		kill_traffic_collector();
919
920
		/* start traffic collector */
921
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
922
923
	} else {
924
		/* kill off traffic collectors */
925
		kill_traffic_collector();
926
	}
927
928 25cca20b jim-p
	$databases = glob("{$rrddbpath}/*.rrd");
929 61e047a5 Phil Davis
	foreach ($databases as $database) {
930 8e23a630 Chris Buechler
		if (file_exists($database)) {
931
			chown($database, "nobody");
932
		}
933 25cca20b jim-p
	}
934
935 61e047a5 Phil Davis
	if (platform_booting()) {
936 9eda6186 Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
937 61e047a5 Phil Davis
	}
938 5e5d5abc Renato Botelho
939 ea4bc46f Seth Mos
}
940
941 ab7658da Seth Mos
function kill_traffic_collector() {
942 3d54aa10 Renato Botelho
	global $g;
943
944
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
945 ab7658da Seth Mos
}
946
947 9bc8b6b6 Seth Mos
?>