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
	if(file_exists($xmldumpfile))
40
		mwexec("rm {$xmldumpfile}");
41

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
490
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
491
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
492
					/* loop list of shaper queues */
493
					$q = 0;
494
					foreach ($qlist as $qname => $q) {
495
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
496
					}
497

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

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

    
507
				if($g['booting']) {
508
					$rrdqcommand = "-t ";
509
					$rrducommand = "N";
510
					$qi = 0;
511
					foreach ($qlist as $qname => $q) {
512
						if($qi == 0) {
513
							$rrdqcommand .= "{$qname}";
514
						} else {
515
							$rrdqcommand .= ":{$qname}";
516
						}
517
						$qi++;
518
						$rrducommand .= ":U";
519
					}
520
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
521
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
522
				}
523

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

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

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

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

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

    
585
		}
586
		$i++;
587

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

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

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

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

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

    
624
		/* End pf states statistics */
625

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

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

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

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

    
654
		/* End CPU statistics */
655

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

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

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

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

    
692
		/* End Memory statistics */
693

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

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

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

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

    
728
		/* End mbuf statistics */
729

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

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

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

    
760
		}
761
		/* End System statistics */
762

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

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

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

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

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

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

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

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

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

    
842
			}
843
		}
844

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

    
854
		unset($rrdupdatesh);
855

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

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

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

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

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

    
875
}
876

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

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

    
883
?>
(46-46/66)