Project

General

Profile

Download (32.9 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
		$_gb = exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/rrd.tgz 2>&1", $rrdrestore, $rrdreturn);
62
		$rrdrestore = implode(" ", $rrdrestore);
63
		if($rrdreturn != 0) {
64
			log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
65
		}
66
		unset($rrdrestore);
67
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
68
			$rrd_file = preg_replace('/\.xml$/', ".rrd", $xml_file);
69
			if (file_exists("{$rrd_file}")) {
70
				@unlink($rrd_file);
71
			}
72
			$output = array();
73
			$status = null;
74
			$_gb = exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
75
			if ($status) {
76
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
77
				continue;
78
			}
79
			unset($output);
80
			@unlink($xml_file);
81
		}
82
		/* 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. */
83
		if (($g['platform'] == "pfSense") && !isset($config['system']['use_mfs_tmpvar'])) {
84
			unlink_if_exists("{$g['cf_conf_path']}/rrd.tgz");
85
		}
86
		return true;
87
	}
88
	return false;
89
}
90

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

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

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

    
127
	$i = 0;
128
	$rracountold = count($rrdoldxml['rra']);
129
	$rracountnew = count($rrdnewxml['rra']);
130
	/* process each RRA, which contain a database */
131
	foreach($rrdnewxml['rra'] as $rra) {
132
		if(!is_array($rrdoldxml['rra'][$i])) {
133
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
134
		}
135

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

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

    
194
	$numrranew = count($rrdoldxml['rra']);
195
	$numdsnew = count($rrdoldxml['ds']);
196
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
197
	return $rrdoldxml;
198
}
199

    
200
function enable_rrd_graphing() {
201
	global $config, $g, $altq_list_queues;
202

    
203
	if($g['booting'])
204
		echo gettext("Generating RRD graphs...");
205

    
206
	$rrddbpath = "/var/db/rrd/";
207
	$rrdgraphpath = "/usr/local/www/rrd";
208

    
209
	$traffic = "-traffic.rrd";
210
	$packets = "-packets.rrd";
211
	$states = "-states.rrd";
212
	$wireless = "-wireless.rrd";
213
	$queues = "-queues.rrd";
214
	$queuesdrop = "-queuedrops.rrd";
215
	$spamd = "-spamd.rrd";
216
	$proc = "-processor.rrd";
217
	$mem = "-memory.rrd";
218
	$mbuf = "-mbuf.rrd";
219
	$cellular = "-cellular.rrd";
220
	$vpnusers = "-vpnusers.rrd";
221
	$captiveportalconcurrent = "-concurrent.rrd";
222
	$captiveportalloggedin = "-loggedin.rrd";
223

    
224
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
225
	$netstat = "/usr/bin/netstat";
226
	$awk = "/usr/bin/awk";
227
	$tar = "/usr/bin/tar";
228
	$pfctl = "/sbin/pfctl";
229
	$sysctl = "/sbin/sysctl";
230
	$php = "/usr/local/bin/php";
231
	$cpustats = "/usr/local/sbin/cpustats";
232
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
233
	$ifconfig = "/sbin/ifconfig";
234
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
235

    
236
	$rrdtrafficinterval = 60;
237
	$rrdwirelessinterval = 60;
238
	$rrdqueuesinterval = 60;
239
	$rrdqueuesdropinterval = 60;
240
	$rrdpacketsinterval = 60;
241
	$rrdstatesinterval = 60;
242
	$rrdspamdinterval = 60;
243
	$rrdlbpoolinterval = 60;
244
	$rrdprocinterval = 60;
245
	$rrdmeminterval = 60;
246
	$rrdmbufinterval = 60;
247
	$rrdcellularinterval = 60;
248
	$rrdvpninterval = 60;
249
	$rrdcaptiveportalinterval = 60;
250

    
251
	$trafficvalid = $rrdtrafficinterval * 2;
252
	$wirelessvalid = $rrdwirelessinterval * 2;
253
	$queuesvalid = $rrdqueuesinterval * 2;
254
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
255
	$packetsvalid = $rrdpacketsinterval * 2;
256
	$statesvalid = $rrdstatesinterval*2;
257
	$spamdvalid = $rrdspamdinterval * 2;
258
	$lbpoolvalid = $rrdlbpoolinterval * 2;
259
	$procvalid = $rrdlbpoolinterval * 2;
260
	$memvalid = $rrdmeminterval * 2;
261
	$mbufvalid = $rrdmbufinterval * 2;
262
	$cellularvalid = $rrdcellularinterval * 2;
263
	$vpnvalid = $rrdvpninterval * 2;
264
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
265

    
266
	/* Assume 2*10GigE for now */
267
	$downstream = 2500000000;
268
	$upstream = 2500000000;
269

    
270
	/* read the shaper config */
271
	read_altq_config();
272

    
273
	if (isset ($config['rrd']['enable'])) {
274

    
275
		/* create directory if needed */
276
		if (!is_dir($rrddbpath)) {
277
			mkdir($rrddbpath, 0775);
278
		}
279
		chown($rrddbpath, "nobody");
280

    
281
		if ($g['booting']) {
282
			restore_rrd();
283
		}
284

    
285
		/* db update script */
286
		$rrdupdatesh = "#!/bin/sh\n";
287
		$rrdupdatesh .= "\n";
288
		$rrdupdatesh .= "export TERM=dumb\n";
289
		$rrdupdatesh .= "\n";
290
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
291
		$rrdupdatesh .= "\n";
292
		$rrdupdatesh .= "counter=1\n";
293
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
294
		$rrdupdatesh .= "do\n";
295
		$rrdupdatesh .= "";
296

    
297
		$i = 0;
298
		$ifdescrs = get_configured_interface_with_descr();
299
		/* IPsec counters */
300
		$ifdescrs['ipsec'] = "IPsec";
301
		/* OpenVPN server counters */
302
		if(is_array($config['openvpn']['openvpn-server'])) {
303
			foreach($config['openvpn']['openvpn-server'] as $server) {
304
				$serverid = "ovpns" . $server['vpnid'];
305
				$ifdescrs[$serverid] = "{$server['description']}";
306
			}
307
		}
308

    
309
		/* process all real and pseudo interfaces */
310
		foreach ($ifdescrs as $ifname => $ifdescr) {
311
			$temp = get_real_interface($ifname);
312
			if($temp <> "") {
313
				$realif = $temp;
314
			}
315

    
316
			/* TRAFFIC, set up the rrd file */
317
			if (!file_exists("$rrddbpath$ifname$traffic")) {
318
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
319
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
320
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
321
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
322
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
323
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
324
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
325
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
326
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
327
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
328
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
329
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
330
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
331

    
332
				create_new_rrd($rrdcreate);
333
				unset($rrdcreate);
334
			}
335

    
336
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
337
			if($g['booting']) {
338
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
339
			}
340

    
341
			$rrdupdatesh .= "\n";
342
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
343
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
344
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
345
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
346
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
347
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
348

    
349
			/* PACKETS, set up the rrd file */
350
			if (!file_exists("$rrddbpath$ifname$packets")) {
351
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
352
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
353
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
354
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
355
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
356
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
357
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
358
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
359
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
360
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
361
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
362
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
363
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
364

    
365
				create_new_rrd($rrdcreate);
366
				unset($rrdcreate);
367
			}
368

    
369
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
370
			if($g['booting']) {
371
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
372
			}
373

    
374
			$rrdupdatesh .= "\n";
375
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
376
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
377
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
378
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
379
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
380
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
381

    
382
			/* WIRELESS, set up the rrd file */
383
			if($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
384
				if (!file_exists("$rrddbpath$ifname$wireless")) {
385
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
386
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
387
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
388
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
389
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
390
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
391
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
392
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
393

    
394
					create_new_rrd($rrdcreate);
395
					unset($rrdcreate);
396
				}
397

    
398
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
399
				if($g['booting']) {
400
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
401
				}
402

    
403
				$rrdupdatesh .= "\n";
404
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
405
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
406
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
407
			}
408

    
409
			/* OpenVPN, set up the rrd file */
410
			if(stristr($ifname, "ovpns")) {
411
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
412
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
413
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
414
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
415
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
416
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
417
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
418

    
419
					create_new_rrd($rrdcreate);
420
					unset($rrdcreate);
421
				}
422

    
423
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
424
				if($g['booting']) {
425
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
426
				}
427

    
428
				if(is_array($config['openvpn']['openvpn-server'])) {
429
					foreach($config['openvpn']['openvpn-server'] as $server) {
430
						if("ovpns{$server['vpnid']}" == $ifname) {
431
							$port = $server['local_port'];
432
							$vpnid = $server['vpnid'];
433
						}
434
					}
435
				}
436
				$rrdupdatesh .= "\n";
437
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
438
				$rrdupdatesh .= "list_current_users() {\n";
439
				$rrdupdatesh .= " sleep 0.2\n";
440
				$rrdupdatesh .= " echo \"status 2\"\n";
441
				$rrdupdatesh .= " sleep 0.2\n";
442
				$rrdupdatesh .= " echo \"quit\"\n";
443
				$rrdupdatesh .= "}\n";
444
				$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";
445
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
446
			}
447

    
448
			/* QUEUES, set up the queues databases */
449
			if ($altq_list_queues[$ifname]) {
450
				$altq =& $altq_list_queues[$ifname];
451
				/* NOTE: Is it worth as its own function?! */
452
				switch ($altq->GetBwscale()) {
453
					case "Gb":
454
						$factor = 1024 * 1024 * 1024;
455
							break;
456
					case "Mb":
457
							$factor = 1024 * 1024;
458
							break;
459
					case "Kb":
460
							$factor = 1024;
461
							break;
462
					case "b":
463
					default:
464
							$factor = 1;
465
							break;
466
				}
467
				$qbandwidth = $altq->GetBandwidth() * $factor;
468
				if ($qbandwidth <=0) {
469
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
470
				}
471
				$qlist =& $altq->get_queue_list($notused);
472
				if (!file_exists("$rrddbpath$ifname$queues")) {
473
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
474
					/* loop list of shaper queues */
475
					$q = 0;
476
					foreach ($qlist as $qname => $q) {
477
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
478
					}
479

    
480
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
481
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
482
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
483
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
484

    
485
					create_new_rrd($rrdcreate);
486
					unset($rrdcreate);
487
				}
488

    
489
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
490
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
491
					/* loop list of shaper queues */
492
					$q = 0;
493
					foreach ($qlist as $qname => $q) {
494
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid: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($g['booting']) {
507
					$rrdqcommand = "-t ";
508
					$rrducommand = "N";
509
					$qi = 0;
510
					foreach ($qlist as $qname => $q) {
511
						if($qi == 0) {
512
							$rrdqcommand .= "{$qname}";
513
						} else {
514
							$rrdqcommand .= ":{$qname}";
515
						}
516
						$qi++;
517
						$rrducommand .= ":U";
518
					}
519
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
520
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
521
				}
522

    
523
				/* awk function to gather shaper data */
524
				/* yes, it's special */
525
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
526
				$rrdupdatesh .= "{ ";
527
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
528
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
529
				$rrdupdatesh .= " q=1; ";
530
				$rrdupdatesh .= "} ";
531
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
532
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
533
				$rrdupdatesh .= " q=0; ";
534
				$rrdupdatesh .= "} ";
535
				$rrdupdatesh .= "} END { ";
536
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
537
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
538
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
539
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
540

    
541
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
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 \":\" \$8 ; ";
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

    
558
			/* 3G interfaces */
559
			if(preg_match("/ppp[0-9]+/i", $realif))	{
560
				if (!file_exists("$rrddbpath$ifname$cellular")) {
561
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
562
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
563
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
564
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
565
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
566
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
567
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
568
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
569
					create_new_rrd($rrdcreate);
570
					unset($rrdcreate);
571
				}
572

    
573
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
574
				if($g['booting']) {
575
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
576
				}
577

    
578
				$rrdupdatesh .= "\n";
579
				$rrdupdatesh .= "# polling 3G\n";
580
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
581
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
582
			}
583

    
584
		}
585
		$i++;
586

    
587
		/* System only statistics */
588
		$ifname = "system";
589

    
590
		/* STATES, create pf states database */
591
		if(! file_exists("$rrddbpath$ifname$states")) {
592
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
593
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
594
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
595
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
596
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
597
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
598
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
599
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
600
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
601
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
602

    
603
			create_new_rrd($rrdcreate);
604
			unset($rrdcreate);
605
		}
606

    
607
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
608
		if($g['booting']) {
609
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
610
		}
611

    
612
		/* the pf states gathering function. */
613
		$rrdupdatesh .= "\n";
614
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
615
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
616
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
617
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
618
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
619
		$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";
620
		$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";
621
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
622

    
623
		/* End pf states statistics */
624

    
625
		/* CPU, create CPU statistics database */
626
		if(! file_exists("$rrddbpath$ifname$proc")) {
627
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
628
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
629
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
630
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
631
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
632
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
633
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
634
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
635
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
636
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
637

    
638
			create_new_rrd($rrdcreate);
639
			unset($rrdcreate);
640
		}
641

    
642
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
643
		if($g['booting']) {
644
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
645
		}
646

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

    
653
		/* End CPU statistics */
654

    
655
		/* Memory, create Memory statistics database */
656
		if(! file_exists("$rrddbpath$ifname$mem")) {
657
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
658
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
659
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
660
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
661
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
662
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
663
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
664
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
665
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
666
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
667
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
668
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
669
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
670
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
671
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
672
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
673
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
674
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
675

    
676
			create_new_rrd($rrdcreate);
677
			unset($rrdcreate);
678
		}
679

    
680
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
681
		if($g['booting']) {
682
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
683
		}
684

    
685
		/* the Memory stats gathering function. */
686
		$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 | ";
687
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
688
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
689
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
690

    
691
		/* End Memory statistics */
692

    
693
		/* mbuf, create mbuf statistics database */
694
		if(! file_exists("$rrddbpath$ifname$mbuf")) {
695
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
696
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
697
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
698
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
699
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
700
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
701
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
702
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
703
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
704
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
705
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
706
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
707
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
708
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
709
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
710
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
711
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
712

    
713
			create_new_rrd($rrdcreate);
714
			unset($rrdcreate);
715
		}
716

    
717
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
718
		if($g['booting']) {
719
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
720
		}
721

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

    
727
		/* End mbuf statistics */
728

    
729
		/* SPAMD, set up the spamd rrd file */
730
		if (isset($config['installedpackages']['spamdsettings']) &&
731
			 $config['installedpackages']['spamdsettings']['config'][0]['enablerrd']) {
732
			/* set up the spamd rrd file */
733
			if (!file_exists("$rrddbpath$ifname$spamd")) {
734
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
735
				$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
736
				$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
737
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
738
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
739
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
740
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
741
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
742
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
743
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
744
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
745
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
746
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
747
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
748
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
749

    
750
				create_new_rrd($rrdcreate);
751
				unset($rrdcreate);
752
			}
753

    
754
			$rrdupdatesh .= "\n";
755
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
756
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
757
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
758

    
759
		}
760
		/* End System statistics */
761

    
762
		/* Captive Portal statistics, set up the rrd file */
763
		if(is_array($config['captiveportal'])) {
764
			foreach ($config['captiveportal'] as $cpkey => $cp) {
765
				if (!isset($cp['enable']))
766
					continue;
767

    
768
				$ifname= "captiveportal";
769
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
770
				if (!file_exists("$concurrent_filename")) {
771
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
772
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
773
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
774
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
775
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
776
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
777
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
778
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
779
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
780
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
781
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
782
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
783
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
784
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
785
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
786
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
787
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
788
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
789

    
790
					create_new_rrd($rrdcreate);
791
					unset($rrdcreate);
792
				}
793

    
794
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
795
				if($g['booting']) {
796
					mwexec("$rrdtool update $concurrent_filename N:U");
797
				}
798

    
799
				/* the Captive Portal stats gathering function. */
800
				$rrdupdatesh .= "\n";
801
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
802
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
803
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
804

    
805
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
806
				if (!file_exists("$loggedin_filename")) {
807
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
808
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
809
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
810
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
811
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
812
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
813
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
814
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
815
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
816
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
817
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
818
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
819
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
820
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
821
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
822
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
823
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
824
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
825

    
826
					create_new_rrd($rrdcreate);
827
					unset($rrdcreate);
828
				}
829

    
830
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
831
				if($g['booting']) {
832
					mwexec("$rrdtool update $loggedin_filename N:U");
833
				}
834

    
835
				/* the Captive Portal stats gathering function. */
836
				$rrdupdatesh .= "\n";
837
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
838
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
839
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
840

    
841
			}
842
		}
843

    
844
		$rrdupdatesh .= "sleep 60\n";
845
		$rrdupdatesh .= "done\n";
846
		log_error(gettext("Creating rrd update script"));
847
		/* write the rrd update script */
848
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
849
		$fd = fopen("$updaterrdscript", "w");
850
		fwrite($fd, "$rrdupdatesh");
851
		fclose($fd);
852

    
853
		unset($rrdupdatesh);
854

    
855
		/* kill off traffic collectors */
856
		kill_traffic_collector();
857

    
858
		/* start traffic collector */
859
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
860

    
861
	} else {
862
		/* kill off traffic collectors */
863
		kill_traffic_collector();
864
	}
865

    
866
	$databases = glob("{$rrddbpath}/*.rrd");
867
	foreach($databases as $database) {
868
		chown($database, "nobody");
869
	}
870

    
871
	if($g['booting'])
872
		echo gettext("done.") . "\n";
873

    
874
}
875

    
876
function kill_traffic_collector() {
877
	global $g;
878

    
879
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
880
}
881

    
882
?>
(46-46/67)