Project

General

Profile

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

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

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

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

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

    
29
  */
30

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

    
36
/* include all configuration functions */
37

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

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

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

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

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

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

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

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

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

    
207
function enable_rrd_graphing() {
208
	global $config, $g, $altq_list_queues;
209

    
210
	if (platform_booting()) {
211
		echo gettext("Generating RRD graphs...");
212
	}
213

    
214
	$rrddbpath = "{$g['vardb_path']}/rrd/";
215
	$rrdgraphpath = "/usr/local/www/rrd";
216

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

    
234
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
235
	$netstat = "/usr/bin/netstat";
236
	$awk = "/usr/bin/awk";
237
	$tar = "/usr/bin/tar";
238
	$pfctl = "/sbin/pfctl";
239
	$sysctl = "/sbin/sysctl";
240
	$php = "/usr/local/bin/php-cgi";
241
	$cpustats = "/usr/local/sbin/cpustats";
242
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
243
	$ifconfig = "/sbin/ifconfig";
244
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
245
	$dhcpd_gather = "/usr/local/bin/dhcpd_gather_stats.php";
246
	$ntpq = "/usr/local/sbin/ntpq";
247

    
248
	$rrdtrafficinterval = 60;
249
	$rrdwirelessinterval = 60;
250
	$rrdqueuesinterval = 60;
251
	$rrdqueuesdropinterval = 60;
252
	$rrdpacketsinterval = 60;
253
	$rrdstatesinterval = 60;
254
	$rrdspamdinterval = 60;
255
	$rrdlbpoolinterval = 60;
256
	$rrdprocinterval = 60;
257
	$rrdmeminterval = 60;
258
	$rrdmbufinterval = 60;
259
	$rrdcellularinterval = 60;
260
	$rrdvpninterval = 60;
261
	$rrdcaptiveportalinterval = 60;
262
	$rrdntpdinterval = 60;
263
	$rrddhcpdinterval = 60;
264

    
265
	$trafficvalid = $rrdtrafficinterval * 2;
266
	$wirelessvalid = $rrdwirelessinterval * 2;
267
	$queuesvalid = $rrdqueuesinterval * 2;
268
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
269
	$packetsvalid = $rrdpacketsinterval * 2;
270
	$statesvalid = $rrdstatesinterval*2;
271
	$spamdvalid = $rrdspamdinterval * 2;
272
	$lbpoolvalid = $rrdlbpoolinterval * 2;
273
	$procvalid = $rrdlbpoolinterval * 2;
274
	$memvalid = $rrdmeminterval * 2;
275
	$mbufvalid = $rrdmbufinterval * 2;
276
	$cellularvalid = $rrdcellularinterval * 2;
277
	$vpnvalid = $rrdvpninterval * 2;
278
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
279
	$ntpdvalid = $rrdntpdinterval * 2;
280
	$dhcpdvalid = $rrddhcpdinterval * 2;
281

    
282
	/* Assume 2*10GigE for now */
283
	$downstream = 2500000000;
284
	$upstream = 2500000000;
285

    
286
	/* read the shaper config */
287
	read_altq_config();
288

    
289
	if (isset ($config['rrd']['enable'])) {
290

    
291
		/* create directory if needed */
292
		if (!is_dir($rrddbpath)) {
293
			mkdir($rrddbpath, 0775);
294
		}
295
		chown($rrddbpath, "nobody");
296

    
297
		if (platform_booting()) {
298
			restore_rrd();
299
		}
300

    
301
		/* db update script */
302
		$rrdupdatesh = "#!/bin/sh\n";
303
		$rrdupdatesh .= "\n";
304
		$rrdupdatesh .= "export TERM=dumb\n";
305
		$rrdupdatesh .= "\n";
306
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
307
		$rrdupdatesh .= "\n";
308
		$rrdupdatesh .= "counter=1\n";
309
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
310
		$rrdupdatesh .= "do\n";
311
		$rrdupdatesh .= "";
312

    
313
		$i = 0;
314
		$ifdescrs = get_configured_interface_with_descr();
315
		/* IPsec counters */
316
		$ifdescrs['ipsec'] = "IPsec";
317
		/* OpenVPN server counters */
318
		if (is_array($config['openvpn']['openvpn-server'])) {
319
			foreach ($config['openvpn']['openvpn-server'] as $server) {
320
				$serverid = "ovpns" . $server['vpnid'];
321
				$ifdescrs[$serverid] = "{$server['description']}";
322
			}
323
		}
324

    
325
		if (platform_booting()) {
326
			if (!is_dir($rrddbpath)) {
327
				mkdir($rrddbpath, 0775);
328
			}
329

    
330
			@chown($rrddbpath, "nobody");
331
		}
332

    
333
		/* process all real and pseudo interfaces */
334
		foreach ($ifdescrs as $ifname => $ifdescr) {
335
			$temp = get_real_interface($ifname);
336
			if ($temp <> "") {
337
				$realif = $temp;
338
			}
339

    
340
			/* TRAFFIC, set up the rrd file */
341
			if (!file_exists("$rrddbpath$ifname$traffic")) {
342
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
343
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
344
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
345
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
346
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
347
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
348
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
349
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
350
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
351
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
352
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
353
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
354
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
355

    
356
				create_new_rrd($rrdcreate);
357
				unset($rrdcreate);
358
			}
359

    
360
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
361
			if (platform_booting()) {
362
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
363
			}
364

    
365
			$rrdupdatesh .= "\n";
366
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
367
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
368
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
369
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
370
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
371
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
372

    
373
			/* PACKETS, set up the rrd file */
374
			if (!file_exists("$rrddbpath$ifname$packets")) {
375
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
376
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
377
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
378
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
379
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
380
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
381
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
382
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
383
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
384
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
385
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
386
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
387
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
388

    
389
				create_new_rrd($rrdcreate);
390
				unset($rrdcreate);
391
			}
392

    
393
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
394
			if (platform_booting()) {
395
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
396
			}
397

    
398
			$rrdupdatesh .= "\n";
399
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
400
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
401
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
402
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
403
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
404
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
405

    
406
			/* WIRELESS, set up the rrd file */
407
			if ($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
408
				if (!file_exists("$rrddbpath$ifname$wireless")) {
409
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
410
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
411
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
412
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
413
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
414
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
415
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
416
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
417

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

    
422
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
423
				if (platform_booting()) {
424
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
425
				}
426

    
427
				$rrdupdatesh .= "\n";
428
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
429
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
430
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
431
			}
432

    
433
			/* OpenVPN, set up the rrd file */
434
			if (stristr($ifname, "ovpns")) {
435
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
436
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
437
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
438
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
439
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
440
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
441
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
442

    
443
					create_new_rrd($rrdcreate);
444
					unset($rrdcreate);
445
				}
446

    
447
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
448
				if (platform_booting()) {
449
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
450
				}
451

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

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

    
504
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
505
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
506
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
507
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
508

    
509
					create_new_rrd($rrdcreate);
510
					unset($rrdcreate);
511
				}
512

    
513
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
514
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
515
					/* loop list of shaper queues */
516
					$q = 0;
517
					foreach ($qlist as $qname => $q) {
518
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
519
					}
520

    
521
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
522
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
523
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
524
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
525

    
526
					create_new_rrd($rrdcreate);
527
					unset($rrdcreate);
528
				}
529

    
530
				if (platform_booting()) {
531
					$rrdqcommand = "-t ";
532
					$rrducommand = "N";
533
					$qi = 0;
534
					foreach ($qlist as $qname => $q) {
535
						if ($qi == 0) {
536
							$rrdqcommand .= "{$qname}";
537
						} else {
538
							$rrdqcommand .= ":{$qname}";
539
						}
540
						$qi++;
541
						$rrducommand .= ":U";
542
					}
543
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
544
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
545
				}
546

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

    
565
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
566
				$rrdupdatesh .= "{ ";
567
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
568
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
569
				$rrdupdatesh .= " q=1; ";
570
				$rrdupdatesh .= "} ";
571
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
572
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
573
				$rrdupdatesh .= " q=0; ";
574
				$rrdupdatesh .= "} ";
575
				$rrdupdatesh .= "} END { ";
576
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
577
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
578
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
579
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
580
			}
581

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

    
597
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
598
				if (platform_booting()) {
599
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
600
				}
601

    
602
				$rrdupdatesh .= "\n";
603
				$rrdupdatesh .= "# polling 3G\n";
604
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
605
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
606
			}
607

    
608
		}
609
		$i++;
610

    
611
		/* System only statistics */
612
		$ifname = "system";
613

    
614
		/* STATES, create pf states database */
615
		if (!file_exists("$rrddbpath$ifname$states")) {
616
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
617
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
618
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
619
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
620
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
621
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
622
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
623
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
624
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
625
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
626

    
627
			create_new_rrd($rrdcreate);
628
			unset($rrdcreate);
629
		}
630

    
631
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
632
		if (platform_booting()) {
633
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
634
		}
635

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

    
647
		/* End pf states statistics */
648

    
649
		/* CPU, create CPU statistics database */
650
		if (!file_exists("$rrddbpath$ifname$proc")) {
651
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
652
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
653
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
654
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
655
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
656
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
657
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
658
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
659
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
660
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
661

    
662
			create_new_rrd($rrdcreate);
663
			unset($rrdcreate);
664
		}
665

    
666
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
667
		if (platform_booting()) {
668
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
669
		}
670

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

    
677
		/* End CPU statistics */
678

    
679
		/* Memory, create Memory statistics database */
680
		if (!file_exists("$rrddbpath$ifname$mem")) {
681
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
682
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
683
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
684
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
685
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
686
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
687
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
688
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
689
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
690
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
691
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
692
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
693
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
694
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
695
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
696
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
697
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
698
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
699

    
700
			create_new_rrd($rrdcreate);
701
			unset($rrdcreate);
702
		}
703

    
704
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
705
		if (platform_booting()) {
706
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
707
		}
708

    
709
		/* the Memory stats gathering function. */
710
		$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 | ";
711
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
712
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
713
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
714

    
715
		/* End Memory statistics */
716

    
717
		/* mbuf, create mbuf statistics database */
718
		if (!file_exists("$rrddbpath$ifname$mbuf")) {
719
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
720
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
721
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
722
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
723
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
724
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
725
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
726
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
727
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
728
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
729
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
730
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
731
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
732
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
733
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
734
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
735
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
736

    
737
			create_new_rrd($rrdcreate);
738
			unset($rrdcreate);
739
		}
740

    
741
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
742
		if (platform_booting()) {
743
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
744
		}
745

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

    
751
		/* End mbuf statistics */
752

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

    
774
				create_new_rrd($rrdcreate);
775
				unset($rrdcreate);
776
			}
777

    
778
			$rrdupdatesh .= "\n";
779
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
780
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
781
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
782

    
783
		}
784
		/* End System statistics */
785

    
786
		/* Captive Portal statistics, set up the rrd file */
787
		if (is_array($config['captiveportal'])) {
788
			foreach ($config['captiveportal'] as $cpkey => $cp) {
789
				if (!isset($cp['enable'])) {
790
					continue;
791
				}
792

    
793
				$ifname= "captiveportal";
794
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
795
				if (!file_exists("$concurrent_filename")) {
796
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
797
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
798
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
799
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
800
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
801
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
802
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
803
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
804
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
805
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
806
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
807
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
808
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
809
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
810
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
811
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
812
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
813
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
814

    
815
					create_new_rrd($rrdcreate);
816
					unset($rrdcreate);
817
				}
818

    
819
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
820
				if (platform_booting()) {
821
					mwexec("$rrdtool update $concurrent_filename N:U");
822
				}
823

    
824
				/* the Captive Portal stats gathering function. */
825
				$rrdupdatesh .= "\n";
826
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
827
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
828
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
829

    
830
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
831
				if (!file_exists("$loggedin_filename")) {
832
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
833
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
834
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
835
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
836
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
837
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
838
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
839
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
840
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
841
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
842
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
843
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
844
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
845
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
846
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
847
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
848
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
849
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
850

    
851
					create_new_rrd($rrdcreate);
852
					unset($rrdcreate);
853
				}
854

    
855
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
856
				if (platform_booting()) {
857
					mwexec("$rrdtool update $loggedin_filename N:U");
858
				}
859

    
860
				/* the Captive Portal stats gathering function. */
861
				$rrdupdatesh .= "\n";
862
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
863
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
864
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
865

    
866
			}
867
		}
868
		/* End Captive Portal statistics */
869

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

    
894
				create_new_rrd($rrdcreate);
895
				unset($rrdcreate);
896
			}
897

    
898
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
899
			if (platform_booting()) {
900
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
901
			}
902

    
903
			/* the ntp stats gathering function. */
904
			$rrdupdatesh .= "\n";
905
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
906
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
907
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
908
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
909
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
910
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
911
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
912
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
913
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
914
			$rrdupdatesh .= "\n";
915

    
916
		}
917
		/* End NTP statistics */
918

    
919
		/* Start dhcpd statistics */
920
		if (is_array($config['dhcpd'])) {
921
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
922
				if (isset($config['dhcpd'][$dhcpif]['statsgraph'])) {
923
					if (!file_exists("$rrddbpath$dhcpif$dhcpd")) {
924
						$rrdcreate = "$rrdtool create $rrddbpath$dhcpif$dhcpd --step $rrddhcpdinterval ";
925
						$rrdcreate .= "DS:leases:GAUGE:$dhcpdvalid:0:100000 ";
926
						$rrdcreate .= "DS:staticleases:GAUGE:$dhcpdvalid:0:100000 ";
927
						$rrdcreate .= "DS:dhcprange:GAUGE:$dhcpdvalid:0:100000 ";
928
						$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
929
						$rrdcreate .= "RRA:MIN:0.5:5:720 ";
930
						$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
931
						$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
932
						$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
933
						$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
934
						$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
935
						$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
936
						$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
937
						$rrdcreate .= "RRA:MAX:0.5:5:720 ";
938
						$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
939
						$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
940
						create_new_rrd($rrdcreate);
941
						unset($rrdcreate);
942
					}
943

    
944
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
945
					if (platform_booting()) {
946
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U:U:U:U:U:U");
947
					}
948

    
949
					$rrdupdatesh .= "\n";
950
					$rrdupdatesh .= "# polling leases for dhcp \n";
951
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
952
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
953

    
954
				}
955
			}
956
		}
957
		/* END dhcpd statistics */
958

    
959
		/* Start gateway quality */
960
		$rrdupdatesh .= <<<EOD
961

    
962
# Gateway quality graphs
963
for sock in {$g['varrun_path']}/dpinger_*.sock; do
964
	if [ ! -S "\$sock" ]; then
965
		continue
966
	fi
967
	loss=\$(/usr/bin/nc -U \$sock | awk '{ print \$3 }')
968
	delay=\$(/usr/bin/nc -U \$sock | awk '{ print \$1 }')
969

    
970
	if echo "\$loss" | grep -Eqv '^[0-9]+\$'; then
971
		loss="U"
972
	fi
973
	if echo "\$delay" | grep -Eqv '^[0-9]+\$'; then
974
		delay="U"
975
	else
976
		# Convert delay to millisecond
977
		delay=\$(echo "scale=7; \$delay / 1000 / 1000" | /usr/bin/bc)
978
	fi
979

    
980
	gw=\$(echo \$sock | sed 's,^.*dpinger_,,; s,\.sock,,')
981
	if [ ! -f {$rrddbpath}\$gw-quality.rrd ]; then
982
		{$rrdtool} create {$rrddbpath}\$gw-quality.rrd --step 60 \\
983
		DS:loss:GAUGE:120:0:100 \\
984
		DS:delay:GAUGE:120:0:100000 \\
985
		RRA:AVERAGE:0.5:1:1200 \\
986
		RRA:AVERAGE:0.5:5:720 \\
987
		RRA:AVERAGE:0.5:60:1860 \\
988
		RRA:AVERAGE:0.5:1440:2284
989

    
990
		{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay N:U:U
991
	fi
992

    
993
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay N:\$loss:\$delay
994
done
995

    
996
EOD;
997
		/* End gateway quality */
998

    
999
		$rrdupdatesh .= "sleep 60\n";
1000
		$rrdupdatesh .= "done\n";
1001
		log_error(gettext("Creating rrd update script"));
1002
		/* write the rrd update script */
1003
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
1004
		$fd = fopen("$updaterrdscript", "w");
1005
		fwrite($fd, "$rrdupdatesh");
1006
		fclose($fd);
1007

    
1008
		unset($rrdupdatesh);
1009

    
1010
		/* kill off traffic collectors */
1011
		kill_traffic_collector();
1012

    
1013
		/* start traffic collector */
1014
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
1015

    
1016
	} else {
1017
		/* kill off traffic collectors */
1018
		kill_traffic_collector();
1019
	}
1020

    
1021
	$databases = glob("{$rrddbpath}/*.rrd");
1022
	foreach ($databases as $database) {
1023
		chown($database, "nobody");
1024
	}
1025

    
1026
	if (platform_booting()) {
1027
		echo gettext("done.") . "\n";
1028
	}
1029

    
1030
}
1031

    
1032
function kill_traffic_collector() {
1033
	global $g;
1034

    
1035
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
1036
}
1037

    
1038
?>
(46-46/65)