Project

General

Profile

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

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

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

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

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

    
28
  */
29

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

    
35
/* include all configuration functions */
36

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
600
		}
601
		$i++;
602

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

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

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

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

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

    
639
		/* End pf states statistics */
640

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

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

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

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

    
669
		/* End CPU statistics */
670

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

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

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

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

    
707
		/* End Memory statistics */
708

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

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

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

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

    
743
		/* End mbuf statistics */
744

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

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

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

    
775
		}
776
		/* End System statistics */
777

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

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

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

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

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

    
821
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
822
				if (!file_exists("$loggedin_filename")) {
823
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
824
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
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:MIN:0.5:1:1200 ";
830
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
831
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
832
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
833
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
834
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
835
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
836
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
837
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
838
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
839
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
840
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
841

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

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

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

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

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

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

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

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

    
907
		}
908
		/* End NTP statistics */
909

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

    
919
		unset($rrdupdatesh);
920

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

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

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

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

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

    
940
}
941

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

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

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

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

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

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

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

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

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

    
982
?>
(47-47/68)