Project

General

Profile

Download (36.9 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
			file_put_contents("{$g['tmp_path']}/rrd_restore", $xml_file);
73
			$_gb = exec("cd /;LANG=C /usr/bin/tar -xf {$g['cf_conf_path']}/rrd.tgz -T {$g['tmp_path']}/rrd_restore");
74
			if (!file_exists("/{$xml_file}")) {
75
				log_error("Could not extract {$xml_file} RRD xml file from archive!");
76
				continue;
77
			}
78
			$_gb = exec("$rrdtool restore -f '/{$xml_file}' '{$rrd_file}'", $output, $status);
79
			if ($status) {
80
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
81
				continue;
82
			}
83
			unset($output);
84
			@unlink("/{$xml_file}");
85
		}
86
		unset($rrdrestore);
87
		@unlink("{$g['tmp_path']}/rrd_restore");
88
		/* 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. */
89
		if (($g['platform'] == "pfSense") && !isset($config['system']['use_mfs_tmpvar'])) {
90
			unlink_if_exists("{$g['cf_conf_path']}/rrd.tgz");
91
		}
92
		return true;
93
	}
94
	return false;
95
}
96

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

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

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

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

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

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

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

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

    
209
	if(platform_booting())
210
		echo gettext("Generating RRD graphs...");
211

    
212
	$rrddbpath = "/var/db/rrd/";
213
	$rrdgraphpath = "/usr/local/www/rrd";
214

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

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

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

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

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

    
280
	/* read the shaper config */
281
	read_altq_config();
282

    
283
	if (isset ($config['rrd']['enable'])) {
284

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

    
291
		if (platform_booting()) {
292
			restore_rrd();
293
		}
294

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

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

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

    
323
			@chown("{$g['vardb_path']}/rrd", "nobody");
324
		}
325

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

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

    
349
				create_new_rrd($rrdcreate);
350
				unset($rrdcreate);
351
			}
352

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

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

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

    
382
				create_new_rrd($rrdcreate);
383
				unset($rrdcreate);
384
			}
385

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

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

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

    
411
					create_new_rrd($rrdcreate);
412
					unset($rrdcreate);
413
				}
414

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

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

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

    
436
					create_new_rrd($rrdcreate);
437
					unset($rrdcreate);
438
				}
439

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

    
445
				if(is_array($config['openvpn']['openvpn-server'])) {
446
					foreach($config['openvpn']['openvpn-server'] as $server) {
447
						if("ovpns{$server['vpnid']}" == $ifname) {
448
							$port = $server['local_port'];
449
							$vpnid = $server['vpnid'];
450
						}
451
					}
452
				}
453
				$rrdupdatesh .= "\n";
454
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
455
				$rrdupdatesh .= "list_current_users() {\n";
456
				$rrdupdatesh .= " sleep 0.2\n";
457
				$rrdupdatesh .= " echo \"status 2\"\n";
458
				$rrdupdatesh .= " sleep 0.2\n";
459
				$rrdupdatesh .= " echo \"quit\"\n";
460
				$rrdupdatesh .= "}\n";
461
				$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";
462
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
463
			}
464

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

    
497
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
498
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
499
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
500
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
501

    
502
					create_new_rrd($rrdcreate);
503
					unset($rrdcreate);
504
				}
505

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

    
514
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
515
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
516
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
517
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
518

    
519
					create_new_rrd($rrdcreate);
520
					unset($rrdcreate);
521
				}
522

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

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

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

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

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

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

    
601
		}
602
		$i++;
603

    
604
		/* System only statistics */
605
		$ifname = "system";
606

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

    
620
			create_new_rrd($rrdcreate);
621
			unset($rrdcreate);
622
		}
623

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

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

    
640
		/* End pf states statistics */
641

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

    
655
			create_new_rrd($rrdcreate);
656
			unset($rrdcreate);
657
		}
658

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

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

    
670
		/* End CPU statistics */
671

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

    
693
			create_new_rrd($rrdcreate);
694
			unset($rrdcreate);
695
		}
696

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

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

    
708
		/* End Memory statistics */
709

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

    
730
			create_new_rrd($rrdcreate);
731
			unset($rrdcreate);
732
		}
733

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

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

    
744
		/* End mbuf statistics */
745

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

    
767
				create_new_rrd($rrdcreate);
768
				unset($rrdcreate);
769
			}
770

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

    
776
		}
777
		/* End System statistics */
778

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

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

    
807
					create_new_rrd($rrdcreate);
808
					unset($rrdcreate);
809
				}
810

    
811
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
812
				if(platform_booting()) {
813
					mwexec("$rrdtool update $concurrent_filename N:U");
814
				}
815

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

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

    
843
					create_new_rrd($rrdcreate);
844
					unset($rrdcreate);
845
				}
846

    
847
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
848
				if(platform_booting()) {
849
					mwexec("$rrdtool update $loggedin_filename N:U");
850
				}
851

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

    
858
			}
859
		}
860
		/* End Captive Portal statistics */
861

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

    
886
				create_new_rrd($rrdcreate);
887
				unset($rrdcreate);
888
			}
889

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

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

    
908
		}
909
		/* End NTP statistics */
910

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

    
920
		unset($rrdupdatesh);
921

    
922
		/* kill off traffic collectors */
923
		kill_traffic_collector();
924

    
925
		/* start traffic collector */
926
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
927

    
928
	} else {
929
		/* kill off traffic collectors */
930
		kill_traffic_collector();
931
	}
932

    
933
	$databases = glob("{$rrddbpath}/*.rrd");
934
	foreach($databases as $database) {
935
		chown($database, "nobody");
936
	}
937

    
938
	if(platform_booting())
939
		echo gettext("done.") . "\n";
940

    
941
}
942

    
943
# Create gateway quality RRD with settings suitable for pfSense graph set.
944
function create_gateway_quality_rrd($rrd_file) {
945
	global $g;
946

    
947
	$rrdinterval = 60;
948
	$valid = $rrdinterval * 2;
949
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
950

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

    
961
		create_new_rrd($rrdcreate);
962
		unset($rrdcreate);
963
	}
964

    
965
	/* enter UNKNOWN values in the RRD so it knows we rebooted. */
966
	if(platform_booting()) {
967
		if (!is_dir("{$g['vardb_path']}/rrd"))
968
			mkdir("{$g['vardb_path']}/rrd", 0775);
969

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

    
972
		mwexec("$rrdtool update $rrd_file N:U:U");
973
	}
974
	unset($rrdtool, $rrdinterval, $valid, $rrd_file);
975
}
976

    
977
function kill_traffic_collector() {
978
	global $g;
979

    
980
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
981
}
982

    
983
?>
(47-47/68)