Project

General

Profile

Download (37 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	rrd.inc
5
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28

    
29
  */
30

    
31
/*
32
	pfSense_BUILDER_BINARIES:	/bin/rm	/usr/bin/nice	/usr/local/bin/rrdtool	/bin/cd
33
	pfSense_MODULE:	rrd
34
*/
35

    
36
/* include all configuration functions */
37

    
38
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
39
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
40
	unlink_if_exists($xmldumpfile);
41

    
42
	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
43
	if ($dumpret <> 0) {
44
		$dumpout = implode(" ", $dumpout);
45
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
46
	}
47
	return($dumpret);
48
}
49

    
50
function restore_rrd() {
51
	global $g, $config;
52

    
53
	$rrddbpath = "/var/db/rrd/";
54
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
55

    
56
	$rrdrestore = "";
57
	$rrdreturn = "";
58
	if (file_exists("{$g['cf_conf_path']}/rrd.tgz") && (isset($config['system']['use_mfs_tmpvar']) || $g['platform'] != "pfSense")) {
59
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
60
			@unlink($xml_file);
61
		}
62
		unset($rrdrestore);
63
		$_gb = exec("cd /;LANG=C /usr/bin/tar -tf {$g['cf_conf_path']}/rrd.tgz", $rrdrestore, $rrdreturn);
64
		if ($rrdreturn != 0) {
65
			log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
66
			return;
67
		}
68
		foreach ($rrdrestore as $xml_file) {
69
			$rrd_file = '/' . substr($xml_file, 0, -4) . '.rrd';
70
			if (file_exists("{$rrd_file}")) {
71
				@unlink($rrd_file);
72
			}
73
			file_put_contents("{$g['tmp_path']}/rrd_restore", $xml_file);
74
			$_gb = exec("cd /;LANG=C /usr/bin/tar -xf {$g['cf_conf_path']}/rrd.tgz -T {$g['tmp_path']}/rrd_restore");
75
			if (!file_exists("/{$xml_file}")) {
76
				log_error("Could not extract {$xml_file} RRD xml file from archive!");
77
				continue;
78
			}
79
			$_gb = exec("$rrdtool restore -f '/{$xml_file}' '{$rrd_file}'", $output, $status);
80
			if ($status) {
81
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
82
				continue;
83
			}
84
			unset($output);
85
			@unlink("/{$xml_file}");
86
		}
87
		unset($rrdrestore);
88
		@unlink("{$g['tmp_path']}/rrd_restore");
89
		/* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */
90
		if (($g['platform'] == "pfSense") && !isset($config['system']['use_mfs_tmpvar'])) {
91
			unlink_if_exists("{$g['cf_conf_path']}/rrd.tgz");
92
		}
93
		return true;
94
	}
95
	return false;
96
}
97

    
98
function create_new_rrd($rrdcreatecmd) {
99
	$rrdcreateoutput = array();
100
	$rrdcreatereturn = 0;
101
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
102
	if ($rrdcreatereturn <> 0) {
103
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
104
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
105
	}
106
	unset($rrdcreateoutput);
107
	return $rrdcreatereturn;
108
}
109

    
110
function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
111
	if (!file_exists("/tmp/rrd_notice_sent.txt")) {
112
		$_gb = exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
113
		@touch("/tmp/rrd_notice_sent.txt");
114
	}
115
	$numrraold = count($rrdoldxml['rra']);
116
	$numrranew = count($rrdnewxml['rra']);
117
	$numdsold = count($rrdoldxml['ds']);
118
	$numdsnew = count($rrdnewxml['ds']);
119
	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));
120

    
121
	/* add data sources not found in the old array from the new array */
122
	$i = 0;
123
	foreach ($rrdnewxml['ds'] as $ds) {
124
		if (!is_array($rrdoldxml['ds'][$i])) {
125
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
126
			/* set unknown values to 0 */
127
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
128
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
129
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
130
		}
131
		$i++;
132
	}
133

    
134
	$i = 0;
135
	$rracountold = count($rrdoldxml['rra']);
136
	$rracountnew = count($rrdnewxml['rra']);
137
	/* process each RRA, which contain a database */
138
	foreach ($rrdnewxml['rra'] as $rra) {
139
		if (!is_array($rrdoldxml['rra'][$i])) {
140
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
141
		}
142

    
143
		$d = 0;
144
		/* process cdp_prep */
145
		$cdp_prep = $rra['cdp_prep'];
146
		foreach ($cdp_prep['ds'] as $ds) {
147
			if (!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
148
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
149
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
150
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
151
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
152
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
153
			}
154
			$d++;
155
		}
156

    
157
		/* process database */
158
		$rows = $rra['database'];
159
		$k = 0;
160
		$rowcountold = count($rrdoldxml['rra'][$i]['database']['row']);
161
		$rowcountnew = count($rrdnewxml['rra'][$i]['database']['row']);
162
		$rowcountdiff = $rowcountnew - $rowcountold;
163
		/* save old rows for a bit before we put the required empty rows before it */
164
		$rowsdata = $rows;
165
		$rowsempty = array();
166
		$r = 0;
167
		while ($r < $rowcountdiff) {
168
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
169
			$r++;
170
		}
171
		$rows = $rowsempty + $rowsdata;
172
		/* now foreach the rows in the database */
173
		foreach ($rows['row'] as $row) {
174
			if (!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
175
				$rrdoldxml['rra'][$i]['database']['row'][$k] = $rrdnewxml['rra'][$i]['database']['row'][$k];
176
			}
177
			$m = 0;
178
			$vcountold = count($rrdoldxml['rra'][$i]['database']['row'][$k]['v']);
179
			$vcountnew = count($rrdnewxml['rra'][$i]['database']['row'][$k]['v']);
180
			foreach ($row['v'] as $value) {
181
				if (empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
182
					if (isset($valid)) {
183
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = "0.0000000000e+00 ";
184
					} else {
185
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = $rrdnewxml['rra'][$i]['database']['row'][$k]['v'][$m];
186
					}
187
				} else {
188
					if ($value <> " NaN ") {
189
						$valid = true;
190
					} else {
191
						$valid = false;
192
					}
193
				}
194
				$m++;
195
			}
196
			$k++;
197
		}
198
		$i++;
199
	}
200

    
201
	$numrranew = count($rrdoldxml['rra']);
202
	$numdsnew = count($rrdoldxml['ds']);
203
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
204
	return $rrdoldxml;
205
}
206

    
207
function enable_rrd_graphing() {
208
	global $config, $g, $altq_list_queues;
209

    
210
	if (platform_booting()) {
211
		echo gettext("Generating RRD graphs...");
212
	}
213

    
214
	$rrddbpath = "/var/db/rrd/";
215
	$rrdgraphpath = "/usr/local/www/rrd";
216

    
217
	$traffic = "-traffic.rrd";
218
	$packets = "-packets.rrd";
219
	$states = "-states.rrd";
220
	$wireless = "-wireless.rrd";
221
	$queues = "-queues.rrd";
222
	$queuesdrop = "-queuedrops.rrd";
223
	$spamd = "-spamd.rrd";
224
	$proc = "-processor.rrd";
225
	$mem = "-memory.rrd";
226
	$mbuf = "-mbuf.rrd";
227
	$cellular = "-cellular.rrd";
228
	$vpnusers = "-vpnusers.rrd";
229
	$captiveportalconcurrent = "-concurrent.rrd";
230
	$captiveportalloggedin = "-loggedin.rrd";
231
	$ntpd = "ntpd.rrd";
232

    
233
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
234
	$netstat = "/usr/bin/netstat";
235
	$awk = "/usr/bin/awk";
236
	$tar = "/usr/bin/tar";
237
	$pfctl = "/sbin/pfctl";
238
	$sysctl = "/sbin/sysctl";
239
	$php = "/usr/local/bin/php";
240
	$cpustats = "/usr/local/sbin/cpustats";
241
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
242
	$ifconfig = "/sbin/ifconfig";
243
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
244
	$ntpq = "/usr/local/sbin/ntpq";
245

    
246
	$rrdtrafficinterval = 60;
247
	$rrdwirelessinterval = 60;
248
	$rrdqueuesinterval = 60;
249
	$rrdqueuesdropinterval = 60;
250
	$rrdpacketsinterval = 60;
251
	$rrdstatesinterval = 60;
252
	$rrdspamdinterval = 60;
253
	$rrdlbpoolinterval = 60;
254
	$rrdprocinterval = 60;
255
	$rrdmeminterval = 60;
256
	$rrdmbufinterval = 60;
257
	$rrdcellularinterval = 60;
258
	$rrdvpninterval = 60;
259
	$rrdcaptiveportalinterval = 60;
260
	$rrdntpdinterval = 60;
261

    
262
	$trafficvalid = $rrdtrafficinterval * 2;
263
	$wirelessvalid = $rrdwirelessinterval * 2;
264
	$queuesvalid = $rrdqueuesinterval * 2;
265
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
266
	$packetsvalid = $rrdpacketsinterval * 2;
267
	$statesvalid = $rrdstatesinterval*2;
268
	$spamdvalid = $rrdspamdinterval * 2;
269
	$lbpoolvalid = $rrdlbpoolinterval * 2;
270
	$procvalid = $rrdlbpoolinterval * 2;
271
	$memvalid = $rrdmeminterval * 2;
272
	$mbufvalid = $rrdmbufinterval * 2;
273
	$cellularvalid = $rrdcellularinterval * 2;
274
	$vpnvalid = $rrdvpninterval * 2;
275
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
276
	$ntpdvalid = $rrdntpdinterval * 2;
277

    
278
	/* Assume 2*10GigE for now */
279
	$downstream = 2500000000;
280
	$upstream = 2500000000;
281

    
282
	/* read the shaper config */
283
	read_altq_config();
284

    
285
	if (isset ($config['rrd']['enable'])) {
286

    
287
		/* create directory if needed */
288
		if (!is_dir($rrddbpath)) {
289
			mkdir($rrddbpath, 0775);
290
		}
291
		chown($rrddbpath, "nobody");
292

    
293
		if (platform_booting()) {
294
			restore_rrd();
295
		}
296

    
297
		/* db update script */
298
		$rrdupdatesh = "#!/bin/sh\n";
299
		$rrdupdatesh .= "\n";
300
		$rrdupdatesh .= "export TERM=dumb\n";
301
		$rrdupdatesh .= "\n";
302
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
303
		$rrdupdatesh .= "\n";
304
		$rrdupdatesh .= "counter=1\n";
305
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
306
		$rrdupdatesh .= "do\n";
307
		$rrdupdatesh .= "";
308

    
309
		$i = 0;
310
		$ifdescrs = get_configured_interface_with_descr();
311
		/* IPsec counters */
312
		$ifdescrs['ipsec'] = "IPsec";
313
		/* OpenVPN server counters */
314
		if (is_array($config['openvpn']['openvpn-server'])) {
315
			foreach ($config['openvpn']['openvpn-server'] as $server) {
316
				$serverid = "ovpns" . $server['vpnid'];
317
				$ifdescrs[$serverid] = "{$server['description']}";
318
			}
319
		}
320

    
321
		if (platform_booting()) {
322
			if (!is_dir("{$g['vardb_path']}/rrd")) {
323
				mkdir("{$g['vardb_path']}/rrd", 0775);
324
			}
325

    
326
			@chown("{$g['vardb_path']}/rrd", "nobody");
327
		}
328

    
329
		/* process all real and pseudo interfaces */
330
		foreach ($ifdescrs as $ifname => $ifdescr) {
331
			$temp = get_real_interface($ifname);
332
			if ($temp <> "") {
333
				$realif = $temp;
334
			}
335

    
336
			/* TRAFFIC, set up the rrd file */
337
			if (!file_exists("$rrddbpath$ifname$traffic")) {
338
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
339
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
340
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
341
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
342
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
343
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
344
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
345
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
346
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
347
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
348
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
349
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
350
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
351

    
352
				create_new_rrd($rrdcreate);
353
				unset($rrdcreate);
354
			}
355

    
356
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
357
			if (platform_booting()) {
358
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
359
			}
360

    
361
			$rrdupdatesh .= "\n";
362
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
363
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
364
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
365
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
366
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
367
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
368

    
369
			/* PACKETS, set up the rrd file */
370
			if (!file_exists("$rrddbpath$ifname$packets")) {
371
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
372
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
373
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
374
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
375
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
376
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
377
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
378
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
379
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
380
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
381
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
382
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
383
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
384

    
385
				create_new_rrd($rrdcreate);
386
				unset($rrdcreate);
387
			}
388

    
389
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
390
			if (platform_booting()) {
391
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
392
			}
393

    
394
			$rrdupdatesh .= "\n";
395
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
396
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
397
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
398
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
399
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
400
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
401

    
402
			/* WIRELESS, set up the rrd file */
403
			if ($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
404
				if (!file_exists("$rrddbpath$ifname$wireless")) {
405
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
406
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
407
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
408
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
409
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
410
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
411
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
412
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
413

    
414
					create_new_rrd($rrdcreate);
415
					unset($rrdcreate);
416
				}
417

    
418
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
419
				if (platform_booting()) {
420
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
421
				}
422

    
423
				$rrdupdatesh .= "\n";
424
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
425
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
426
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
427
			}
428

    
429
			/* OpenVPN, set up the rrd file */
430
			if (stristr($ifname, "ovpns")) {
431
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
432
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
433
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
434
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
435
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
436
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
437
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
438

    
439
					create_new_rrd($rrdcreate);
440
					unset($rrdcreate);
441
				}
442

    
443
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
444
				if (platform_booting()) {
445
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
446
				}
447

    
448
				if (is_array($config['openvpn']['openvpn-server'])) {
449
					foreach ($config['openvpn']['openvpn-server'] as $server) {
450
						if ("ovpns{$server['vpnid']}" == $ifname) {
451
							$port = $server['local_port'];
452
							$vpnid = $server['vpnid'];
453
						}
454
					}
455
				}
456
				$rrdupdatesh .= "\n";
457
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
458
				$rrdupdatesh .= "list_current_users() {\n";
459
				$rrdupdatesh .= " sleep 0.2\n";
460
				$rrdupdatesh .= " echo \"status 2\"\n";
461
				$rrdupdatesh .= " sleep 0.2\n";
462
				$rrdupdatesh .= " echo \"quit\"\n";
463
				$rrdupdatesh .= "}\n";
464
				$rrdupdatesh .= "OVPN=`list_current_users | nc -U {$g['varetc_path']}/openvpn/server{$vpnid}.sock | awk -F\",\" '/^CLIENT_LIST/ {print \$2}' | wc -l | awk '{print $1}'`\n";
465
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
466
			}
467

    
468
			/* QUEUES, set up the queues databases */
469
			if ($altq_list_queues[$ifname]) {
470
				$altq =& $altq_list_queues[$ifname];
471
				/* NOTE: Is it worth as its own function?! */
472
				switch ($altq->GetBwscale()) {
473
					case "Gb":
474
						$factor = 1024 * 1024 * 1024;
475
							break;
476
					case "Mb":
477
							$factor = 1024 * 1024;
478
							break;
479
					case "Kb":
480
							$factor = 1024;
481
							break;
482
					case "b":
483
					default:
484
							$factor = 1;
485
							break;
486
				}
487
				$qbandwidth = $altq->GetBandwidth() * $factor;
488
				if ($qbandwidth <=0) {
489
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
490
				}
491
				$qlist =& $altq->get_queue_list($notused);
492
				if (!file_exists("$rrddbpath$ifname$queues")) {
493
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
494
					/* loop list of shaper queues */
495
					$q = 0;
496
					foreach ($qlist as $qname => $q) {
497
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
498
					}
499

    
500
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
501
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
502
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
503
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
504

    
505
					create_new_rrd($rrdcreate);
506
					unset($rrdcreate);
507
				}
508

    
509
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
510
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
511
					/* loop list of shaper queues */
512
					$q = 0;
513
					foreach ($qlist as $qname => $q) {
514
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
515
					}
516

    
517
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
518
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
519
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
520
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
521

    
522
					create_new_rrd($rrdcreate);
523
					unset($rrdcreate);
524
				}
525

    
526
				if (platform_booting()) {
527
					$rrdqcommand = "-t ";
528
					$rrducommand = "N";
529
					$qi = 0;
530
					foreach ($qlist as $qname => $q) {
531
						if ($qi == 0) {
532
							$rrdqcommand .= "{$qname}";
533
						} else {
534
							$rrdqcommand .= ":{$qname}";
535
						}
536
						$qi++;
537
						$rrducommand .= ":U";
538
					}
539
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
540
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
541
				}
542

    
543
				/* awk function to gather shaper data */
544
				/* yes, it's special */
545
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
546
				$rrdupdatesh .= "{ ";
547
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
548
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
549
				$rrdupdatesh .= " q=1; ";
550
				$rrdupdatesh .= "} ";
551
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
552
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
553
				$rrdupdatesh .= " q=0; ";
554
				$rrdupdatesh .= "} ";
555
				$rrdupdatesh .= "} END { ";
556
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
557
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
558
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
559
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
560

    
561
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
562
				$rrdupdatesh .= "{ ";
563
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
564
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
565
				$rrdupdatesh .= " q=1; ";
566
				$rrdupdatesh .= "} ";
567
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
568
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
569
				$rrdupdatesh .= " q=0; ";
570
				$rrdupdatesh .= "} ";
571
				$rrdupdatesh .= "} END { ";
572
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
573
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
574
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
575
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
576
			}
577

    
578
			/* 3G interfaces */
579
			if (preg_match("/ppp[0-9]+/i", $realif))	{
580
				if (!file_exists("$rrddbpath$ifname$cellular")) {
581
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
582
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
583
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
584
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
585
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
586
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
587
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
588
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
589
					create_new_rrd($rrdcreate);
590
					unset($rrdcreate);
591
				}
592

    
593
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
594
				if (platform_booting()) {
595
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
596
				}
597

    
598
				$rrdupdatesh .= "\n";
599
				$rrdupdatesh .= "# polling 3G\n";
600
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
601
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
602
			}
603

    
604
		}
605
		$i++;
606

    
607
		/* System only statistics */
608
		$ifname = "system";
609

    
610
		/* STATES, create pf states database */
611
		if (!file_exists("$rrddbpath$ifname$states")) {
612
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
613
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
614
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
615
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
616
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
617
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
618
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
619
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
620
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
621
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
622

    
623
			create_new_rrd($rrdcreate);
624
			unset($rrdcreate);
625
		}
626

    
627
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
628
		if (platform_booting()) {
629
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
630
		}
631

    
632
		/* the pf states gathering function. */
633
		$rrdupdatesh .= "\n";
634
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
635
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
636
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
637
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
638
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
639
		$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";
640
		$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";
641
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
642

    
643
		/* End pf states statistics */
644

    
645
		/* CPU, create CPU statistics database */
646
		if (!file_exists("$rrddbpath$ifname$proc")) {
647
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
648
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
649
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
650
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
651
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
652
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
653
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
654
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
655
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
656
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
657

    
658
			create_new_rrd($rrdcreate);
659
			unset($rrdcreate);
660
		}
661

    
662
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
663
		if (platform_booting()) {
664
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
665
		}
666

    
667
		/* the CPU stats gathering function. */
668
		$rrdupdatesh .= "CPU=`$cpustats | cut -f1-4 -d':'`\n";
669
		/* Using ps uxaH will count all processes including system threads. Top was undercounting. */
670
		$rrdupdatesh .= "PROCS=`ps uxaH | wc -l | awk '{print \$1;}'`\n";
671
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$proc N:\${CPU}:\${PROCS}\n";
672

    
673
		/* End CPU statistics */
674

    
675
		/* Memory, create Memory statistics database */
676
		if (!file_exists("$rrddbpath$ifname$mem")) {
677
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
678
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
679
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
680
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
681
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
682
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
683
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
684
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
685
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
686
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
687
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
688
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
689
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
690
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
691
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
692
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
693
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
694
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
695

    
696
			create_new_rrd($rrdcreate);
697
			unset($rrdcreate);
698
		}
699

    
700
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
701
		if (platform_booting()) {
702
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
703
		}
704

    
705
		/* the Memory stats gathering function. */
706
		$rrdupdatesh .= "MEM=`$sysctl -n 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 | ";
707
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
708
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
709
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
710

    
711
		/* End Memory statistics */
712

    
713
		/* mbuf, create mbuf statistics database */
714
		if (!file_exists("$rrddbpath$ifname$mbuf")) {
715
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
716
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
717
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
718
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
719
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
720
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
721
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
722
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
723
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
724
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
725
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
726
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
727
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
728
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
729
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
730
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
731
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
732

    
733
			create_new_rrd($rrdcreate);
734
			unset($rrdcreate);
735
		}
736

    
737
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
738
		if (platform_booting()) {
739
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
740
		}
741

    
742
		/* the mbuf stats gathering function. */
743
		$rrdupdatesh .= "MBUF=`$netstat -m | ";
744
		$rrdupdatesh .= " $awk '/mbuf clusters in use/ { gsub(/\//, \":\", $1); print $1; }'`\n";
745
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mbuf N:\${MBUF}\n";
746

    
747
		/* End mbuf statistics */
748

    
749
		/* SPAMD, set up the spamd rrd file */
750
		if (isset($config['installedpackages']['spamdsettings']) &&
751
		    $config['installedpackages']['spamdsettings']['config'][0]['enablerrd']) {
752
			/* set up the spamd rrd file */
753
			if (!file_exists("$rrddbpath$ifname$spamd")) {
754
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
755
				$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
756
				$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
757
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
758
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
759
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
760
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
761
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
762
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
763
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
764
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
765
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
766
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
767
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
768
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
769

    
770
				create_new_rrd($rrdcreate);
771
				unset($rrdcreate);
772
			}
773

    
774
			$rrdupdatesh .= "\n";
775
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
776
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
777
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
778

    
779
		}
780
		/* End System statistics */
781

    
782
		/* Captive Portal statistics, set up the rrd file */
783
		if (is_array($config['captiveportal'])) {
784
			foreach ($config['captiveportal'] as $cpkey => $cp) {
785
				if (!isset($cp['enable'])) {
786
					continue;
787
				}
788

    
789
				$ifname= "captiveportal";
790
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
791
				if (!file_exists("$concurrent_filename")) {
792
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
793
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
794
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
795
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
796
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
797
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
798
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
799
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
800
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
801
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
802
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
803
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
804
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
805
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
806
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
807
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
808
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
809
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
810

    
811
					create_new_rrd($rrdcreate);
812
					unset($rrdcreate);
813
				}
814

    
815
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
816
				if (platform_booting()) {
817
					mwexec("$rrdtool update $concurrent_filename N:U");
818
				}
819

    
820
				/* the Captive Portal stats gathering function. */
821
				$rrdupdatesh .= "\n";
822
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
823
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
824
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
825

    
826
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
827
				if (!file_exists("$loggedin_filename")) {
828
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
829
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
830
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
831
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
832
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
833
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
834
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
835
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
836
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
837
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
838
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
839
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
840
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
841
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
842
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
843
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
844
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
845
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
846

    
847
					create_new_rrd($rrdcreate);
848
					unset($rrdcreate);
849
				}
850

    
851
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
852
				if (platform_booting()) {
853
					mwexec("$rrdtool update $loggedin_filename N:U");
854
				}
855

    
856
				/* the Captive Portal stats gathering function. */
857
				$rrdupdatesh .= "\n";
858
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
859
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
860
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
861

    
862
			}
863
		}
864
		/* End Captive Portal statistics */
865

    
866
		/* NTP, set up the ntpd rrd file */
867
		if (isset($config['ntpd']['statsgraph'])) {
868
			/* set up the ntpd rrd file */
869
			if (!file_exists("$rrddbpath$ntpd")) {
870
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
871
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:0:1000 ";
872
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
873
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
874
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
875
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:0:1000 ";
876
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
877
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
878
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
879
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
880
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
881
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
882
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
883
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
884
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
885
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
886
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
887
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
888
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
889

    
890
				create_new_rrd($rrdcreate);
891
				unset($rrdcreate);
892
			}
893

    
894
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
895
			if (platform_booting()) {
896
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
897
			}
898

    
899
			/* the ntp stats gathering function. */
900
			$rrdupdatesh .= "\n";
901
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
902
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
903
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
904
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
905
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
906
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
907
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
908
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
909
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
910
			$rrdupdatesh .= "\n";
911

    
912
		}
913
		/* End NTP statistics */
914

    
915
		$rrdupdatesh .= "sleep 60\n";
916
		$rrdupdatesh .= "done\n";
917
		log_error(gettext("Creating rrd update script"));
918
		/* write the rrd update script */
919
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
920
		$fd = fopen("$updaterrdscript", "w");
921
		fwrite($fd, "$rrdupdatesh");
922
		fclose($fd);
923

    
924
		unset($rrdupdatesh);
925

    
926
		/* kill off traffic collectors */
927
		kill_traffic_collector();
928

    
929
		/* start traffic collector */
930
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
931

    
932
	} else {
933
		/* kill off traffic collectors */
934
		kill_traffic_collector();
935
	}
936

    
937
	$databases = glob("{$rrddbpath}/*.rrd");
938
	foreach ($databases as $database) {
939
		chown($database, "nobody");
940
	}
941

    
942
	if (platform_booting()) {
943
		echo gettext("done.") . "\n";
944
	}
945

    
946
}
947

    
948
# Create gateway quality RRD with settings suitable for pfSense graph set.
949
function create_gateway_quality_rrd($rrd_file) {
950
	global $g;
951

    
952
	$rrdinterval = 60;
953
	$valid = $rrdinterval * 2;
954
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
955

    
956
	/* GATEWAY QUALITY, set up the rrd file */
957
	if (!file_exists("$rrd_file")) {
958
		$rrdcreate = "$rrdtool create $rrd_file --step $rrdinterval ";
959
		$rrdcreate .= "DS:loss:GAUGE:$valid:0:100 ";
960
		$rrdcreate .= "DS:delay:GAUGE:$valid:0:100000 ";
961
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
962
		$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
963
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
964
		$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
965

    
966
		create_new_rrd($rrdcreate);
967
		unset($rrdcreate);
968
	}
969

    
970
	/* enter UNKNOWN values in the RRD so it knows we rebooted. */
971
	if (platform_booting()) {
972
		if (!is_dir("{$g['vardb_path']}/rrd")) {
973
			mkdir("{$g['vardb_path']}/rrd", 0775);
974
		}
975

    
976
		@chown("{$g['vardb_path']}/rrd", "nobody");
977

    
978
		mwexec("$rrdtool update $rrd_file N:U:U");
979
	}
980
	unset($rrdtool, $rrdinterval, $valid, $rrd_file);
981
}
982

    
983
function kill_traffic_collector() {
984
	global $g;
985

    
986
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
987
}
988

    
989
?>
(46-46/67)