Projet

Général

Profil

Télécharger (33,1 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / rrd.inc @ be544b90

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

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

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

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

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

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

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

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

    
203
function enable_rrd_graphing() {
204
	global $config, $g, $altq_list_queues;
205

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

    
209
	$rrddbpath = "/var/db/rrd/";
210
	$rrdgraphpath = "/usr/local/www/rrd";
211

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

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

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

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

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

    
273
	/* read the shaper config */
274
	read_altq_config();
275

    
276
	if (isset ($config['rrd']['enable'])) {
277

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

    
284
		if ($g['booting']) {
285
			restore_rrd();
286
		}
287

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

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

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

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

    
335
				create_new_rrd($rrdcreate);
336
				unset($rrdcreate);
337
			}
338

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

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

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

    
368
				create_new_rrd($rrdcreate);
369
				unset($rrdcreate);
370
			}
371

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

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

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

    
397
					create_new_rrd($rrdcreate);
398
					unset($rrdcreate);
399
				}
400

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

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

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

    
422
					create_new_rrd($rrdcreate);
423
					unset($rrdcreate);
424
				}
425

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

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

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

    
483
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
484
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
485
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
486
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
487

    
488
					create_new_rrd($rrdcreate);
489
					unset($rrdcreate);
490
				}
491

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

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

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

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

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

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

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

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

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

    
587
		}
588
		$i++;
589

    
590
		/* System only statistics */
591
		$ifname = "system";
592

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

    
606
			create_new_rrd($rrdcreate);
607
			unset($rrdcreate);
608
		}
609

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

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

    
626
		/* End pf states statistics */
627

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

    
641
			create_new_rrd($rrdcreate);
642
			unset($rrdcreate);
643
		}
644

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

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

    
656
		/* End CPU statistics */
657

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

    
679
			create_new_rrd($rrdcreate);
680
			unset($rrdcreate);
681
		}
682

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

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

    
694
		/* End Memory statistics */
695

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

    
716
			create_new_rrd($rrdcreate);
717
			unset($rrdcreate);
718
		}
719

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

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

    
730
		/* End mbuf statistics */
731

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

    
753
				create_new_rrd($rrdcreate);
754
				unset($rrdcreate);
755
			}
756

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

    
762
		}
763
		/* End System statistics */
764

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

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

    
793
					create_new_rrd($rrdcreate);
794
					unset($rrdcreate);
795
				}
796

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

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

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

    
829
					create_new_rrd($rrdcreate);
830
					unset($rrdcreate);
831
				}
832

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

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

    
844
			}
845
		}
846

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

    
856
		unset($rrdupdatesh);
857

    
858
		/* kill off traffic collectors */
859
		kill_traffic_collector();
860

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

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

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

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

    
877
}
878

    
879
function kill_traffic_collector() {
880
	global $g;
881

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

    
885
?>
(46-46/66)