Project

General

Profile

Download (38.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * rrd.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
7
 * Copyright (c) 2010-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
/* include all configuration functions */
24

    
25
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
26
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
27
	unlink_if_exists($xmldumpfile);
28

    
29
	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
30
	if ($dumpret <> 0) {
31
		$dumpout = implode(" ", $dumpout);
32
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
33
	}
34
	return($dumpret);
35
}
36

    
37
function restore_rrd() {
38
	global $g, $config;
39

    
40
	$rrddbpath = "{$g['vardb_path']}/rrd/";
41
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
42

    
43
	$rrdrestore = "";
44
	$rrdreturn = "";
45
	if (file_exists("{$g['cf_conf_path']}/rrd.tgz") && isset($config['system']['use_mfs_tmpvar'])) {
46
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
47
			@unlink($xml_file);
48
		}
49
		unset($rrdrestore);
50
		$_gb = exec("cd /;LANG=C /usr/bin/tar -tf {$g['cf_conf_path']}/rrd.tgz", $rrdrestore, $rrdreturn);
51
		if ($rrdreturn != 0) {
52
			log_error(sprintf(gettext('RRD restore failed exited with %1$s, the error is: %2$s'), $rrdreturn, $rrdrestore));
53
			return;
54
		}
55
		foreach ($rrdrestore as $xml_file) {
56
			$rrd_file = '/' . substr($xml_file, 0, -4) . '.rrd';
57
			if (file_exists("{$rrd_file}")) {
58
				@unlink($rrd_file);
59
			}
60
			file_put_contents("{$g['tmp_path']}/rrd_restore", $xml_file);
61
			$_gb = exec("cd /;LANG=C /usr/bin/tar -xf {$g['cf_conf_path']}/rrd.tgz -T {$g['tmp_path']}/rrd_restore");
62
			if (!file_exists("/{$xml_file}")) {
63
				log_error(sprintf(gettext("Could not extract %s RRD xml file from archive!"), $xml_file));
64
				continue;
65
			}
66
			$_gb = exec("$rrdtool restore -f '/{$xml_file}' '{$rrd_file}'", $output, $status);
67
			if ($status) {
68
				log_error(sprintf(gettext("rrdtool restore -f '%1\$s' '%2\$s' failed returning %3\$s."), $xml_file, $rrd_file, $status));
69
				continue;
70
			}
71
			unset($output);
72
			@unlink("/{$xml_file}");
73
		}
74
		unset($rrdrestore);
75
		@unlink("{$g['tmp_path']}/rrd_restore");
76
		/* 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. */
77
		if (!isset($config['system']['use_mfs_tmpvar'])) {
78
			unlink_if_exists("{$g['cf_conf_path']}/rrd.tgz");
79
		}
80
		return true;
81
	}
82
	return false;
83
}
84

    
85
function create_new_rrd($rrdcreatecmd) {
86
	$rrdcreateoutput = array();
87
	$rrdcreatereturn = 0;
88
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
89
	if ($rrdcreatereturn <> 0) {
90
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
91
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
92
	}
93
	unset($rrdcreateoutput);
94
	return $rrdcreatereturn;
95
}
96

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

    
108
	/* add data sources not found in the old array from the new array */
109
	$i = 0;
110
	foreach ($rrdnewxml['ds'] as $ds) {
111
		if (!is_array($rrdoldxml['ds'][$i])) {
112
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
113
			/* set unknown values to 0 */
114
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
115
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
116
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
117
		}
118
		$i++;
119
	}
120

    
121
	$i = 0;
122
	$rracountold = count($rrdoldxml['rra']);
123
	$rracountnew = count($rrdnewxml['rra']);
124
	/* process each RRA, which contain a database */
125
	foreach ($rrdnewxml['rra'] as $rra) {
126
		if (!is_array($rrdoldxml['rra'][$i])) {
127
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
128
		}
129

    
130
		$d = 0;
131
		/* process cdp_prep */
132
		$cdp_prep = $rra['cdp_prep'];
133
		foreach ($cdp_prep['ds'] as $ds) {
134
			if (!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
135
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
136
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
137
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
138
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
139
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
140
			}
141
			$d++;
142
		}
143

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

    
188
	$numrranew = count($rrdoldxml['rra']);
189
	$numdsnew = count($rrdoldxml['ds']);
190
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
191
	return $rrdoldxml;
192
}
193

    
194
function enable_rrd_graphing() {
195
	global $config, $g, $altq_list_queues;
196

    
197
	if (platform_booting()) {
198
		echo gettext("Generating RRD graphs...");
199
	}
200

    
201
	$rrddbpath = "{$g['vardb_path']}/rrd/";
202
	$rrdgraphpath = "/usr/local/www/rrd";
203

    
204
	$traffic = "-traffic.rrd";
205
	$packets = "-packets.rrd";
206
	$states = "-states.rrd";
207
	$wireless = "-wireless.rrd";
208
	$queues = "-queues.rrd";
209
	$queuesdrop = "-queuedrops.rrd";
210
	$spamd = "-spamd.rrd";
211
	$proc = "-processor.rrd";
212
	$mem = "-memory.rrd";
213
	$mbuf = "-mbuf.rrd";
214
	$cellular = "-cellular.rrd";
215
	$vpnusers = "-vpnusers.rrd";
216
	$captiveportalconcurrent = "-concurrent.rrd";
217
	$captiveportalloggedin = "-loggedin.rrd";
218
	$ntpd = "ntpd.rrd";
219
	$dhcpd = "-dhcpd.rrd";
220

    
221
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
222
	$netstat = "/usr/bin/netstat";
223
	$awk = "/usr/bin/awk";
224
	$tar = "/usr/bin/tar";
225
	$pfctl = "/sbin/pfctl";
226
	$sysctl = "/sbin/sysctl";
227
	$php = "/usr/local/bin/php-cgi";
228
	$cpustats = "/usr/local/sbin/cpustats";
229
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
230
	$ifconfig = "/sbin/ifconfig";
231
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
232
	$dhcpd_gather = "/usr/local/bin/dhcpd_gather_stats.php";
233
	$ntpq = "/usr/local/sbin/ntpq";
234

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

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

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

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

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

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

    
284
		if (platform_booting()) {
285
			restore_rrd();
286
		}
287

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

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

    
312
		if (platform_booting()) {
313
			if (!is_dir($rrddbpath)) {
314
				mkdir($rrddbpath, 0775);
315
			}
316

    
317
			@chown($rrddbpath, "nobody");
318
		}
319

    
320
		/* process all real and pseudo interfaces */
321
		foreach ($ifdescrs as $ifname => $ifdescr) {
322
			$temp = get_real_interface($ifname);
323
			if ($temp <> "") {
324
				$realif = $temp;
325
			}
326

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

    
343
				create_new_rrd($rrdcreate);
344
				unset($rrdcreate);
345
			}
346

    
347
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
348
			if (platform_booting()) {
349
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
350
			}
351

    
352
			$rrdupdatesh .= "\n";
353
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
354
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
355
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
356
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
357
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
358
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
359

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

    
376
				create_new_rrd($rrdcreate);
377
				unset($rrdcreate);
378
			}
379

    
380
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
381
			if (platform_booting()) {
382
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
383
			}
384

    
385
			$rrdupdatesh .= "\n";
386
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
387
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
388
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
389
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
390
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
391
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
392

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

    
405
					create_new_rrd($rrdcreate);
406
					unset($rrdcreate);
407
				}
408

    
409
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
410
				if (platform_booting()) {
411
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
412
				}
413

    
414
				$rrdupdatesh .= "\n";
415
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
416
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
417
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
418
			}
419

    
420
			/* OpenVPN, set up the rrd file */
421
			if (stristr($ifname, "ovpns")) {
422
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
423
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
424
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
425
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
426
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
427
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
428
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
429

    
430
					create_new_rrd($rrdcreate);
431
					unset($rrdcreate);
432
				}
433

    
434
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
435
				if (platform_booting()) {
436
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
437
				}
438

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

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

    
491
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
492
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
493
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
494
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
495

    
496
					create_new_rrd($rrdcreate);
497
					unset($rrdcreate);
498
				}
499

    
500
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
501
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
502
					/* loop list of shaper queues */
503
					$q = 0;
504
					foreach ($qlist as $qname => $q) {
505
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
506
					}
507

    
508
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
509
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
510
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
511
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
512

    
513
					create_new_rrd($rrdcreate);
514
					unset($rrdcreate);
515
				}
516

    
517
				if (platform_booting()) {
518
					$rrdqcommand = "-t ";
519
					$rrducommand = "N";
520
					$qi = 0;
521
					foreach ($qlist as $qname => $q) {
522
						if ($qi == 0) {
523
							$rrdqcommand .= "{$qname}";
524
						} else {
525
							$rrdqcommand .= ":{$qname}";
526
						}
527
						$qi++;
528
						$rrducommand .= ":U";
529
					}
530
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
531
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
532
				}
533

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

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

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

    
584
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
585
				if (platform_booting()) {
586
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
587
				}
588

    
589
				$rrdupdatesh .= "\n";
590
				$rrdupdatesh .= "# polling 3G\n";
591
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
592
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
593
			}
594

    
595
		}
596
		$i++;
597

    
598
		/* System only statistics */
599
		$ifname = "system";
600

    
601
		/* STATES, create pf states database */
602
		if (!file_exists("$rrddbpath$ifname$states")) {
603
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
604
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
605
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
606
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
607
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
608
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
609
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
610
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
611
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
612
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
613

    
614
			create_new_rrd($rrdcreate);
615
			unset($rrdcreate);
616
		}
617

    
618
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
619
		if (platform_booting()) {
620
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
621
		}
622

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

    
634
		/* End pf states statistics */
635

    
636
		/* CPU, create CPU statistics database */
637
		if (!file_exists("$rrddbpath$ifname$proc")) {
638
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
639
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
640
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
641
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
642
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
643
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
644
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
645
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
646
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
647
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
648

    
649
			create_new_rrd($rrdcreate);
650
			unset($rrdcreate);
651
		}
652

    
653
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
654
		if (platform_booting()) {
655
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
656
		}
657

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

    
664
		/* End CPU statistics */
665

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

    
687
			create_new_rrd($rrdcreate);
688
			unset($rrdcreate);
689
		}
690

    
691
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
692
		if (platform_booting()) {
693
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
694
		}
695

    
696
		/* the Memory stats gathering function. */
697
		$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 | ";
698
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
699
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
700
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
701

    
702
		/* End Memory statistics */
703

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

    
724
			create_new_rrd($rrdcreate);
725
			unset($rrdcreate);
726
		}
727

    
728
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
729
		if (platform_booting()) {
730
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
731
		}
732

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

    
738
		/* End mbuf statistics */
739

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

    
761
				create_new_rrd($rrdcreate);
762
				unset($rrdcreate);
763
			}
764

    
765
			$rrdupdatesh .= "\n";
766
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
767
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
768
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
769

    
770
		}
771
		/* End System statistics */
772

    
773
		/* Captive Portal statistics, set up the rrd file */
774
		if (is_array($config['captiveportal'])) {
775
			foreach ($config['captiveportal'] as $cpkey => $cp) {
776
				if (!isset($cp['enable'])) {
777
					continue;
778
				}
779

    
780
				$ifname= "captiveportal";
781
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
782
				if (!file_exists("$concurrent_filename")) {
783
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
784
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
785
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
786
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
787
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
788
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
789
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
790
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
791
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
792
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
793
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
794
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
795
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
796
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
797
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
798
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
799
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
800
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
801

    
802
					create_new_rrd($rrdcreate);
803
					unset($rrdcreate);
804
				}
805

    
806
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
807
				if (platform_booting()) {
808
					mwexec("$rrdtool update $concurrent_filename N:U");
809
				}
810

    
811
				/* the Captive Portal stats gathering function. */
812
				$rrdupdatesh .= "\n";
813
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
814
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
815
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
816

    
817
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
818
				if (!file_exists("$loggedin_filename")) {
819
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
820
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
821
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
822
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
823
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
824
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
825
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
826
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
827
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
828
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
829
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
830
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
831
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
832
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
833
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
834
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
835
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
836
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
837

    
838
					create_new_rrd($rrdcreate);
839
					unset($rrdcreate);
840
				}
841

    
842
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
843
				if (platform_booting()) {
844
					mwexec("$rrdtool update $loggedin_filename N:U");
845
				}
846

    
847
				/* the Captive Portal stats gathering function. */
848
				$rrdupdatesh .= "\n";
849
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
850
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
851
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
852

    
853
			}
854
		}
855
		/* End Captive Portal statistics */
856

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

    
881
				create_new_rrd($rrdcreate);
882
				unset($rrdcreate);
883
			}
884

    
885
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
886
			if (platform_booting()) {
887
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
888
			}
889

    
890
			/* the ntp stats gathering function. */
891
			$rrdupdatesh .= "\n";
892
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
893
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
894
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
895
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
896
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
897
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
898
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
899
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
900
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
901
			$rrdupdatesh .= "\n";
902

    
903
		}
904
		/* End NTP statistics */
905

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

    
931
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
932
					if (platform_booting()) {
933
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U");
934
					}
935

    
936
					$rrdupdatesh .= "\n";
937
					$rrdupdatesh .= "# polling leases for dhcp \n";
938
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
939
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
940

    
941
				}
942
			}
943
		}
944
		/* END dhcpd statistics */
945

    
946
		/* Start gateway quality */
947
		$rrdupdatesh .= <<<EOD
948

    
949
# Gateway quality graphs
950
for sock in {$g['varrun_path']}/dpinger_*.sock; do
951
	if [ ! -S "\$sock" ]; then
952
		continue
953
	fi
954

    
955
	t=\$(/usr/bin/nc -U \$sock)
956
	if [ -z "\$t" ]; then
957
		continue
958
	fi
959

    
960
	gw=\$(echo "\$t" | awk '{ print \$1 }')
961
	delay=\$(echo "\$t" | awk '{ print \$2 }')
962
	stddev=\$(echo "\$t" | awk '{ print \$3 }')
963
	loss=\$(echo "\$t" | awk '{ print \$4 }')
964

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

    
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
		DS:stddev: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:stddev N:U:U:U
992
	fi
993

    
994
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:\$loss:\$delay:\$stddev
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
		if (file_exists($database)) {
1025
			chown($database, "nobody");
1026
		}
1027
	}
1028

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

    
1033
}
1034

    
1035
function kill_traffic_collector() {
1036
	global $g;
1037

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

    
1041
?>
(36-36/51)