Projet

Général

Profil

Télécharger (36,2 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / rrd.inc @ a88376d9

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
	$ntpd = "ntpd.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
	$ntpq = "/usr/local/sbin/ntpq";
237

    
238
	$rrdtrafficinterval = 60;
239
	$rrdwirelessinterval = 60;
240
	$rrdqueuesinterval = 60;
241
	$rrdqueuesdropinterval = 60;
242
	$rrdpacketsinterval = 60;
243
	$rrdstatesinterval = 60;
244
	$rrdspamdinterval = 60;
245
	$rrdlbpoolinterval = 60;
246
	$rrdprocinterval = 60;
247
	$rrdmeminterval = 60;
248
	$rrdmbufinterval = 60;
249
	$rrdcellularinterval = 60;
250
	$rrdvpninterval = 60;
251
	$rrdcaptiveportalinterval = 60;
252
	$rrdntpdinterval = 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
	$ntpdvalid = $rrdntpdinterval * 2;
269

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
588
		}
589
		$i++;
590

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

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

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

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

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

    
627
		/* End pf states statistics */
628

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

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

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

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

    
657
		/* End CPU statistics */
658

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

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

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

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

    
695
		/* End Memory statistics */
696

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

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

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

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

    
731
		/* End mbuf statistics */
732

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

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

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

    
763
		}
764
		/* End System statistics */
765

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

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

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

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

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

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

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

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

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

    
845
			}
846
		}
847
		/* End Captive Portal statistics */
848

    
849
		/* NTP, set up the ntpd rrd file */
850
		if (isset($config['ntpd']['statsgraph'])) {
851
			/* set up the ntpd rrd file */
852
			if (!file_exists("$rrddbpath$ntpd")) {
853
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
854
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:0:1000 ";
855
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
856
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
857
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
858
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:0:1000 ";
859
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
860
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
861
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
862
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
863
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
864
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
865
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
866
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
867
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
868
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
869
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
870
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
871
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
872

    
873
				create_new_rrd($rrdcreate);
874
				unset($rrdcreate);
875
			}
876

    
877
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
878
			if($g['booting']) {
879
			mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
880
			}
881

    
882
			/* the ntp stats gathering function. */
883
			$rrdupdatesh .= "\n";
884
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
885
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
886
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
887
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
888
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
889
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
890
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
891
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
892
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
893
			$rrdupdatesh .= "\n";
894

    
895
		}
896
		/* End NTP statistics */
897

    
898
		$rrdupdatesh .= "sleep 60\n";
899
		$rrdupdatesh .= "done\n";
900
		log_error(gettext("Creating rrd update script"));
901
		/* write the rrd update script */
902
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
903
		$fd = fopen("$updaterrdscript", "w");
904
		fwrite($fd, "$rrdupdatesh");
905
		fclose($fd);
906

    
907
		unset($rrdupdatesh);
908

    
909
		/* kill off traffic collectors */
910
		kill_traffic_collector();
911

    
912
		/* start traffic collector */
913
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
914

    
915
	} else {
916
		/* kill off traffic collectors */
917
		kill_traffic_collector();
918
	}
919

    
920
	$databases = glob("{$rrddbpath}/*.rrd");
921
	foreach($databases as $database) {
922
		chown($database, "nobody");
923
	}
924

    
925
	if($g['booting'])
926
		echo gettext("done.") . "\n";
927

    
928
}
929

    
930
# Create gateway quality RRD with settings suitable for pfSense graph set.
931
function create_gateway_quality_rrd($rrd_file) {
932
	global $g;
933
	$rrdinterval = 60;
934
	$valid = $rrdinterval * 2;
935
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
936

    
937
	/* GATEWAY QUALITY, set up the rrd file */
938
	if (!file_exists("$rrd_file")) {
939
		$rrdcreate = "$rrdtool create $rrd_file --step $rrdinterval ";
940
		$rrdcreate .= "DS:loss:GAUGE:$valid:0:100 ";
941
		$rrdcreate .= "DS:delay:GAUGE:$valid:0:100000 ";
942
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
943
		$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
944
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
945
		$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
946

    
947
		create_new_rrd($rrdcreate);
948
		unset($rrdcreate);
949
	}
950

    
951
	/* enter UNKNOWN values in the RRD so it knows we rebooted. */
952
	if($g['booting']) {
953
		mwexec("$rrdtool update $rrd_file N:U:U");
954
	}
955
	unset($rrdtool, $rrdinterval, $valid, $rrd_file);
956
}
957

    
958
function kill_traffic_collector() {
959
	global $g;
960

    
961
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
962
}
963

    
964
?>
(46-46/67)