Project

General

Profile

Download (38.6 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
/* include all configuration functions */
32

    
33
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
34
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
35
	unlink_if_exists($xmldumpfile);
36

    
37
	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
38
	if ($dumpret <> 0) {
39
		$dumpout = implode(" ", $dumpout);
40
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
41
	}
42
	return($dumpret);
43
}
44

    
45
function restore_rrd() {
46
	global $g, $config;
47

    
48
	$rrddbpath = "{$g['vardb_path']}/rrd/";
49
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
50

    
51
	$rrdrestore = "";
52
	$rrdreturn = "";
53
	if (file_exists("{$g['cf_conf_path']}/rrd.tgz") && (isset($config['system']['use_mfs_tmpvar']) || $g['platform'] != $g['product_name'])) {
54
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
55
			@unlink($xml_file);
56
		}
57
		unset($rrdrestore);
58
		$_gb = exec("cd /;LANG=C /usr/bin/tar -tf {$g['cf_conf_path']}/rrd.tgz", $rrdrestore, $rrdreturn);
59
		if ($rrdreturn != 0) {
60
			log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
61
			return;
62
		}
63
		foreach ($rrdrestore as $xml_file) {
64
			$rrd_file = '/' . substr($xml_file, 0, -4) . '.rrd';
65
			if (file_exists("{$rrd_file}")) {
66
				@unlink($rrd_file);
67
			}
68
			file_put_contents("{$g['tmp_path']}/rrd_restore", $xml_file);
69
			$_gb = exec("cd /;LANG=C /usr/bin/tar -xf {$g['cf_conf_path']}/rrd.tgz -T {$g['tmp_path']}/rrd_restore");
70
			if (!file_exists("/{$xml_file}")) {
71
				log_error("Could not extract {$xml_file} RRD xml file from archive!");
72
				continue;
73
			}
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
		unset($rrdrestore);
83
		@unlink("{$g['tmp_path']}/rrd_restore");
84
		/* 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. */
85
		if (($g['platform'] == $g['product_name']) && !isset($config['system']['use_mfs_tmpvar'])) {
86
			unlink_if_exists("{$g['cf_conf_path']}/rrd.tgz");
87
		}
88
		return true;
89
	}
90
	return false;
91
}
92

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

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

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

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

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

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

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

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

    
205
	if (platform_booting()) {
206
		echo gettext("Generating RRD graphs...");
207
	}
208

    
209
	$rrddbpath = "{$g['vardb_path']}/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
	$ntpd = "ntpd.rrd";
227
	$dhcpd = "-dhcpd.rrd";
228

    
229
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
230
	$netstat = "/usr/bin/netstat";
231
	$awk = "/usr/bin/awk";
232
	$tar = "/usr/bin/tar";
233
	$pfctl = "/sbin/pfctl";
234
	$sysctl = "/sbin/sysctl";
235
	$php = "/usr/local/bin/php-cgi";
236
	$cpustats = "/usr/local/sbin/cpustats";
237
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
238
	$ifconfig = "/sbin/ifconfig";
239
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
240
	$dhcpd_gather = "/usr/local/bin/dhcpd_gather_stats.php";
241
	$ntpq = "/usr/local/sbin/ntpq";
242

    
243
	$rrdtrafficinterval = 60;
244
	$rrdwirelessinterval = 60;
245
	$rrdqueuesinterval = 60;
246
	$rrdqueuesdropinterval = 60;
247
	$rrdpacketsinterval = 60;
248
	$rrdstatesinterval = 60;
249
	$rrdspamdinterval = 60;
250
	$rrdlbpoolinterval = 60;
251
	$rrdprocinterval = 60;
252
	$rrdmeminterval = 60;
253
	$rrdmbufinterval = 60;
254
	$rrdcellularinterval = 60;
255
	$rrdvpninterval = 60;
256
	$rrdcaptiveportalinterval = 60;
257
	$rrdntpdinterval = 60;
258
	$rrddhcpdinterval = 60;
259

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

    
277
	/* Assume 2*10GigE for now */
278
	$downstream = 2500000000;
279
	$upstream = 2500000000;
280

    
281
	/* read the shaper config */
282
	read_altq_config();
283

    
284
	if (isset ($config['rrd']['enable'])) {
285

    
286
		/* create directory if needed */
287
		if (!is_dir($rrddbpath)) {
288
			mkdir($rrddbpath, 0775);
289
		}
290
		chown($rrddbpath, "nobody");
291

    
292
		if (platform_booting()) {
293
			restore_rrd();
294
		}
295

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

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

    
320
		if (platform_booting()) {
321
			if (!is_dir($rrddbpath)) {
322
				mkdir($rrddbpath, 0775);
323
			}
324

    
325
			@chown($rrddbpath, "nobody");
326
		}
327

    
328
		/* process all real and pseudo interfaces */
329
		foreach ($ifdescrs as $ifname => $ifdescr) {
330
			$temp = get_real_interface($ifname);
331
			if ($temp <> "") {
332
				$realif = $temp;
333
			}
334

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

    
351
				create_new_rrd($rrdcreate);
352
				unset($rrdcreate);
353
			}
354

    
355
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
356
			if (platform_booting()) {
357
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
358
			}
359

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

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

    
384
				create_new_rrd($rrdcreate);
385
				unset($rrdcreate);
386
			}
387

    
388
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
389
			if (platform_booting()) {
390
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
391
			}
392

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

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

    
413
					create_new_rrd($rrdcreate);
414
					unset($rrdcreate);
415
				}
416

    
417
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
418
				if (platform_booting()) {
419
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
420
				}
421

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

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

    
438
					create_new_rrd($rrdcreate);
439
					unset($rrdcreate);
440
				}
441

    
442
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
443
				if (platform_booting()) {
444
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
445
				}
446

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

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

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

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

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

    
516
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
517
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
518
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
519
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
520

    
521
					create_new_rrd($rrdcreate);
522
					unset($rrdcreate);
523
				}
524

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

    
542
				/* awk function to gather shaper data */
543
				/* yes, it's special */
544
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
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 \":\" \$5 ; ";
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
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
561
				$rrdupdatesh .= "{ ";
562
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
563
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
564
				$rrdupdatesh .= " q=1; ";
565
				$rrdupdatesh .= "} ";
566
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
567
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
568
				$rrdupdatesh .= " q=0; ";
569
				$rrdupdatesh .= "} ";
570
				$rrdupdatesh .= "} END { ";
571
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
572
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
573
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
574
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
575
			}
576

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

    
592
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
593
				if (platform_booting()) {
594
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
595
				}
596

    
597
				$rrdupdatesh .= "\n";
598
				$rrdupdatesh .= "# polling 3G\n";
599
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
600
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
601
			}
602

    
603
		}
604
		$i++;
605

    
606
		/* System only statistics */
607
		$ifname = "system";
608

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

    
622
			create_new_rrd($rrdcreate);
623
			unset($rrdcreate);
624
		}
625

    
626
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
627
		if (platform_booting()) {
628
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
629
		}
630

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

    
642
		/* End pf states statistics */
643

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

    
657
			create_new_rrd($rrdcreate);
658
			unset($rrdcreate);
659
		}
660

    
661
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
662
		if (platform_booting()) {
663
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
664
		}
665

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

    
672
		/* End CPU statistics */
673

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

    
695
			create_new_rrd($rrdcreate);
696
			unset($rrdcreate);
697
		}
698

    
699
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
700
		if (platform_booting()) {
701
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
702
		}
703

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

    
710
		/* End Memory statistics */
711

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

    
732
			create_new_rrd($rrdcreate);
733
			unset($rrdcreate);
734
		}
735

    
736
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
737
		if (platform_booting()) {
738
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
739
		}
740

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

    
746
		/* End mbuf statistics */
747

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

    
769
				create_new_rrd($rrdcreate);
770
				unset($rrdcreate);
771
			}
772

    
773
			$rrdupdatesh .= "\n";
774
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
775
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
776
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
777

    
778
		}
779
		/* End System statistics */
780

    
781
		/* Captive Portal statistics, set up the rrd file */
782
		if (is_array($config['captiveportal'])) {
783
			foreach ($config['captiveportal'] as $cpkey => $cp) {
784
				if (!isset($cp['enable'])) {
785
					continue;
786
				}
787

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

    
810
					create_new_rrd($rrdcreate);
811
					unset($rrdcreate);
812
				}
813

    
814
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
815
				if (platform_booting()) {
816
					mwexec("$rrdtool update $concurrent_filename N:U");
817
				}
818

    
819
				/* the Captive Portal stats gathering function. */
820
				$rrdupdatesh .= "\n";
821
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
822
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
823
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
824

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

    
846
					create_new_rrd($rrdcreate);
847
					unset($rrdcreate);
848
				}
849

    
850
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
851
				if (platform_booting()) {
852
					mwexec("$rrdtool update $loggedin_filename N:U");
853
				}
854

    
855
				/* the Captive Portal stats gathering function. */
856
				$rrdupdatesh .= "\n";
857
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
858
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
859
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
860

    
861
			}
862
		}
863
		/* End Captive Portal statistics */
864

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

    
889
				create_new_rrd($rrdcreate);
890
				unset($rrdcreate);
891
			}
892

    
893
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
894
			if (platform_booting()) {
895
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
896
			}
897

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

    
911
		}
912
		/* End NTP statistics */
913

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

    
939
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
940
					if (platform_booting()) {
941
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U:U:U:U:U:U");
942
					}
943

    
944
					$rrdupdatesh .= "\n";
945
					$rrdupdatesh .= "# polling leases for dhcp \n";
946
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
947
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
948

    
949
				}
950
			}
951
		}
952
		/* END dhcpd statistics */
953

    
954
		/* Start gateway quality */
955
		$rrdupdatesh .= <<<EOD
956

    
957
# Gateway quality graphs
958
for sock in {$g['varrun_path']}/dpinger_*.sock; do
959
	if [ ! -S "\$sock" ]; then
960
		continue
961
	fi
962

    
963
	gw=\$(/usr/bin/nc -U \$sock | awk '{ print \$1 }')
964

    
965
	if [ -z "\$gw" ]; then
966
		continue
967
	fi
968

    
969
	delay=\$(/usr/bin/nc -U \$sock | awk '{ print \$2 }')
970
	loss=\$(/usr/bin/nc -U \$sock | awk '{ print \$4 }')
971

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

    
982
	if [ ! -f {$rrddbpath}\$gw-quality.rrd ]; then
983
		{$rrdtool} create {$rrddbpath}\$gw-quality.rrd --step 60 \\
984
		DS:loss:GAUGE:120:0:100 \\
985
		DS:delay:GAUGE:120:0:100000 \\
986
		RRA:AVERAGE:0.5:1:1200 \\
987
		RRA:AVERAGE:0.5:5:720 \\
988
		RRA:AVERAGE:0.5:60:1860 \\
989
		RRA:AVERAGE:0.5:1440:2284
990

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

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

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

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

    
1009
		unset($rrdupdatesh);
1010

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

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

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

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

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

    
1031
}
1032

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

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

    
1039
?>
(46-46/65)