Project

General

Profile

Download (37.2 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-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
/* include all configuration functions */
26

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

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

    
39
function create_new_rrd($rrdcreatecmd) {
40
	$rrdcreateoutput = array();
41
	$rrdcreatereturn = 0;
42
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
43
	if ($rrdcreatereturn <> 0) {
44
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
45
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
46
	}
47
	unset($rrdcreateoutput);
48
	return $rrdcreatereturn;
49
}
50

    
51
function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
52
	if (!file_exists("/tmp/rrd_notice_sent.txt")) {
53
		$_gb = exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
54
		@touch("/tmp/rrd_notice_sent.txt");
55
	}
56
	$numrraold = count($rrdoldxml['rra']);
57
	$numrranew = count($rrdnewxml['rra']);
58
	$numdsold = count($rrdoldxml['ds']);
59
	$numdsnew = count($rrdnewxml['ds']);
60
	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));
61

    
62
	/* add data sources not found in the old array from the new array */
63
	$i = 0;
64
	foreach ($rrdnewxml['ds'] as $ds) {
65
		if (!is_array($rrdoldxml['ds'][$i])) {
66
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
67
			/* set unknown values to 0 */
68
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
69
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
70
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
71
		}
72
		$i++;
73
	}
74

    
75
	$i = 0;
76
	$rracountold = count($rrdoldxml['rra']);
77
	$rracountnew = count($rrdnewxml['rra']);
78
	/* process each RRA, which contain a database */
79
	foreach ($rrdnewxml['rra'] as $rra) {
80
		if (!is_array($rrdoldxml['rra'][$i])) {
81
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
82
		}
83

    
84
		$d = 0;
85
		/* process cdp_prep */
86
		$cdp_prep = $rra['cdp_prep'];
87
		foreach ($cdp_prep['ds'] as $ds) {
88
			if (!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
89
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
90
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
91
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
92
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
93
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
94
			}
95
			$d++;
96
		}
97

    
98
		/* process database */
99
		$rows = $rra['database'];
100
		$k = 0;
101
		$rowcountold = count($rrdoldxml['rra'][$i]['database']['row']);
102
		$rowcountnew = count($rrdnewxml['rra'][$i]['database']['row']);
103
		$rowcountdiff = $rowcountnew - $rowcountold;
104
		/* save old rows for a bit before we put the required empty rows before it */
105
		$rowsdata = $rows;
106
		$rowsempty = array();
107
		$r = 0;
108
		while ($r < $rowcountdiff) {
109
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
110
			$r++;
111
		}
112
		$rows = $rowsempty + $rowsdata;
113
		/* now foreach the rows in the database */
114
		foreach ($rows['row'] as $row) {
115
			if (!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
116
				$rrdoldxml['rra'][$i]['database']['row'][$k] = $rrdnewxml['rra'][$i]['database']['row'][$k];
117
			}
118
			$m = 0;
119
			$vcountold = count($rrdoldxml['rra'][$i]['database']['row'][$k]['v']);
120
			$vcountnew = count($rrdnewxml['rra'][$i]['database']['row'][$k]['v']);
121
			foreach ($row['v'] as $value) {
122
				if (empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
123
					if (isset($valid)) {
124
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = "0.0000000000e+00 ";
125
					} else {
126
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = $rrdnewxml['rra'][$i]['database']['row'][$k]['v'][$m];
127
					}
128
				} else {
129
					if ($value <> " NaN ") {
130
						$valid = true;
131
					} else {
132
						$valid = false;
133
					}
134
				}
135
				$m++;
136
			}
137
			$k++;
138
		}
139
		$i++;
140
	}
141

    
142
	$numrranew = count($rrdoldxml['rra']);
143
	$numdsnew = count($rrdoldxml['ds']);
144
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
145
	return $rrdoldxml;
146
}
147

    
148
function enable_rrd_graphing() {
149
	global $config, $g, $altq_list_queues;
150

    
151
	if (platform_booting()) {
152
		echo gettext("Generating RRD graphs...");
153
	}
154

    
155
	$rrddbpath = "{$g['vardb_path']}/rrd/";
156
	$rrdgraphpath = "/usr/local/www/rrd";
157

    
158
	$traffic = "-traffic.rrd";
159
	$packets = "-packets.rrd";
160
	$states = "-states.rrd";
161
	$wireless = "-wireless.rrd";
162
	$queues = "-queues.rrd";
163
	$queuesdrop = "-queuedrops.rrd";
164
	$proc = "-processor.rrd";
165
	$mem = "-memory.rrd";
166
	$mbuf = "-mbuf.rrd";
167
	$sensors = "-sensors.rrd";
168
	$cellular = "-cellular.rrd";
169
	$vpnusers = "-vpnusers.rrd";
170
	$captiveportalconcurrent = "-concurrent.rrd";
171
	$captiveportalloggedin = "-loggedin.rrd";
172
	$ntpd = "ntpd.rrd";
173
	$dhcpd = "-dhcpd.rrd";
174

    
175
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
176
	$netstat = "/usr/bin/netstat";
177
	$awk = "/usr/bin/awk";
178
	$sed = "/usr/bin/sed";
179
	$tar = "/usr/bin/tar";
180
	$pfctl = "/sbin/pfctl";
181
	$sysctl = "/sbin/sysctl";
182
	$php = "/usr/local/bin/php-cgi";
183
	$cpustats = "/usr/local/sbin/cpustats";
184
	$ifconfig = "/sbin/ifconfig";
185
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
186
	$dhcpd_gather = "/usr/local/bin/dhcpd_gather_stats.php";
187
	$ntpq = "/usr/local/sbin/ntpq";
188

    
189
	$rrdtrafficinterval = 60;
190
	$rrdwirelessinterval = 60;
191
	$rrdqueuesinterval = 60;
192
	$rrdqueuesdropinterval = 60;
193
	$rrdpacketsinterval = 60;
194
	$rrdstatesinterval = 60;
195
	$rrdlbpoolinterval = 60;
196
	$rrdprocinterval = 60;
197
	$rrdmeminterval = 60;
198
	$rrdmbufinterval = 60;
199
	$rrdsensorsinterval = 60;
200
	$rrdcellularinterval = 60;
201
	$rrdvpninterval = 60;
202
	$rrdcaptiveportalinterval = 60;
203
	$rrdntpdinterval = 60;
204
	$rrddhcpdinterval = 60;
205

    
206
	$trafficvalid = $rrdtrafficinterval * 2;
207
	$wirelessvalid = $rrdwirelessinterval * 2;
208
	$queuesvalid = $rrdqueuesinterval * 2;
209
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
210
	$packetsvalid = $rrdpacketsinterval * 2;
211
	$statesvalid = $rrdstatesinterval*2;
212
	$lbpoolvalid = $rrdlbpoolinterval * 2;
213
	$procvalid = $rrdlbpoolinterval * 2;
214
	$memvalid = $rrdmeminterval * 2;
215
	$mbufvalid = $rrdmbufinterval * 2;
216
	$sensorsvalid = $rrdsensorsinterval * 2;
217
	$cellularvalid = $rrdcellularinterval * 2;
218
	$vpnvalid = $rrdvpninterval * 2;
219
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
220
	$ntpdvalid = $rrdntpdinterval * 2;
221
	$dhcpdvalid = $rrddhcpdinterval * 2;
222

    
223
	/* Assume 2*10GigE for now */
224
	$downstream = 2500000000;
225
	$upstream = 2500000000;
226

    
227
	/* read the shaper config */
228
	read_altq_config();
229

    
230
	if (isset ($config['rrd']['enable'])) {
231

    
232
		/* create directory if needed */
233
		if (!is_dir($rrddbpath)) {
234
			mkdir($rrddbpath, 0775);
235
		}
236
		chown($rrddbpath, "nobody");
237

    
238
		/* db update script */
239
		$rrdupdatesh = "#!/bin/sh\n";
240
		$rrdupdatesh .= "\n";
241
		$rrdupdatesh .= "export TERM=dumb\n";
242
		$rrdupdatesh .= "\n";
243
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
244
		$rrdupdatesh .= "\n";
245
		$rrdupdatesh .= "counter=1\n";
246
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
247
		$rrdupdatesh .= "do\n";
248
		$rrdupdatesh .= "";
249

    
250
		$i = 0;
251
		$ifdescrs = get_configured_interface_with_descr();
252
		/* IPsec counters */
253
		$ifdescrs['ipsec'] = "IPsec";
254
		/* OpenVPN server counters */
255
		$ovpncfg = config_get_path('openvpn/openvpn-server');
256
		if (is_array($ovpncfg)) {
257
			foreach ($ovpncfg as $server) {
258
				$serverid = "ovpns" . $server['vpnid'];
259
				$ifdescrs[$serverid] = "{$server['description']}";
260
			}
261
		}
262

    
263
		/* process all real and pseudo interfaces */
264
		foreach ($ifdescrs as $ifname => $ifdescr) {
265
			$temp = get_real_interface($ifname);
266
			if ($temp <> "") {
267
				$realif = $temp;
268
			}
269

    
270
			/* TRAFFIC, set up the rrd file */
271
			if (!file_exists("$rrddbpath$ifname$traffic")) {
272
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
273
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
274
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
275
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
276
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
277
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
278
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
279
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
280
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
281
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
282
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
283
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
284
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
285

    
286
				create_new_rrd($rrdcreate);
287
				unset($rrdcreate);
288
			}
289

    
290
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
291
			if (platform_booting()) {
292
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
293
			}
294

    
295
			$rrdupdatesh .= "\n";
296
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
297
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
298
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
299
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
300
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
301
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
302

    
303
			/* PACKETS, set up the rrd file */
304
			if (!file_exists("$rrddbpath$ifname$packets")) {
305
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
306
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
307
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
308
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
309
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
310
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
311
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
312
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
313
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
314
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
315
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
316
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
317
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
318

    
319
				create_new_rrd($rrdcreate);
320
				unset($rrdcreate);
321
			}
322

    
323
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
324
			if (platform_booting()) {
325
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
326
			}
327

    
328
			$rrdupdatesh .= "\n";
329
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
330
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
331
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
332
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
333
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
334
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
335

    
336
			/* WIRELESS, set up the rrd file */
337
			if ($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
338
				if (!file_exists("$rrddbpath$ifname$wireless")) {
339
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
340
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
341
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
342
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
343
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
344
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
345
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
346
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
347

    
348
					create_new_rrd($rrdcreate);
349
					unset($rrdcreate);
350
				}
351

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

    
357
				$rrdupdatesh .= "\n";
358
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
359
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
360
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
361
			}
362

    
363
			/* OpenVPN, set up the rrd file */
364
			if (stristr($ifname, "ovpns")) {
365
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
366
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
367
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
368
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
369
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
370
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
371
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
372

    
373
					create_new_rrd($rrdcreate);
374
					unset($rrdcreate);
375
				}
376

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

    
382
				$ovpncfg = config_get_path('openvpn/openvpn-server');
383
				if (is_array($ovpncfg)) {
384
					foreach ($ovpncfg as $server) {
385
						if ("ovpns{$server['vpnid']}" == $ifname) {
386
							$port = $server['local_port'];
387
							$vpnid = $server['vpnid'];
388
						}
389
					}
390
				}
391
				$rrdupdatesh .= "\n";
392
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
393
				$rrdupdatesh .= "list_current_users() {\n";
394
				$rrdupdatesh .= " sleep 0.2\n";
395
				$rrdupdatesh .= " echo \"status 2\"\n";
396
				$rrdupdatesh .= " sleep 0.2\n";
397
				$rrdupdatesh .= " echo \"quit\"\n";
398
				$rrdupdatesh .= "}\n";
399
				$rrdupdatesh .= "OVPN=`list_current_users | /usr/bin/nc -U {$g['openvpn_base']}/server{$vpnid}/sock | /usr/bin/awk -F\",\" '/^CLIENT_LIST/ {print \$2}' | /usr/bin/wc -l | /usr/bin/awk '{print $1}'`\n";
400
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
401
			}
402

    
403
			/* QUEUES, set up the queues databases */
404
			if (($altq_list_queues[$ifname]) && 
405
			    !empty($altq_list_queues[$ifname]->get_queue_list())) {
406
				$altq =& $altq_list_queues[$ifname];
407
				/* NOTE: Is it worth as its own function?! */
408
				switch ($altq->GetBwscale()) {
409
					case "Gb":
410
						$factor = 1024 * 1024 * 1024;
411
						break;
412
					case "Mb":
413
						$factor = 1024 * 1024;
414
						break;
415
					case "Kb":
416
						$factor = 1024;
417
						break;
418
					case "b":
419
					default:
420
						$factor = 1;
421
						break;
422
				}
423
				$qbandwidth = (int) $altq->GetBandwidth() * $factor;
424
				if ($qbandwidth <= 0) {
425
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
426
				}
427
				$qlist =& $altq->get_queue_list($notused);
428
				if (!file_exists("$rrddbpath$ifname$queues")) {
429
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
430
					/* loop list of shaper queues */
431
					$q = 0;
432
					foreach ($qlist as $qname => $q) {
433
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
434
					}
435

    
436
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
437
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
438
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
439
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
440

    
441
					create_new_rrd($rrdcreate);
442
					unset($rrdcreate);
443
				}
444

    
445
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
446
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
447
					/* loop list of shaper queues */
448
					$q = 0;
449
					foreach ($qlist as $qname => $q) {
450
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
451
					}
452

    
453
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
454
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
455
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
456
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
457

    
458
					create_new_rrd($rrdcreate);
459
					unset($rrdcreate);
460
				}
461

    
462
				if (platform_booting()) {
463
					$rrdqcommand = "-t ";
464
					$rrducommand = "N";
465
					$qi = 0;
466
					foreach ($qlist as $qname => $q) {
467
						if ($qi == 0) {
468
							$rrdqcommand .= "{$qname}";
469
						} else {
470
							$rrdqcommand .= ":{$qname}";
471
						}
472
						$qi++;
473
						$rrducommand .= ":U";
474
					}
475
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
476
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
477
				}
478

    
479
				/* awk function to gather shaper data */
480
				/* yes, it's special */
481
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
482
				$rrdupdatesh .= "{ ";
483
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
484
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
485
				$rrdupdatesh .= " q=1; ";
486
				$rrdupdatesh .= "} ";
487
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
488
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
489
				$rrdupdatesh .= " q=0; ";
490
				$rrdupdatesh .= "} ";
491
				$rrdupdatesh .= "} END { ";
492
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
493
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
494
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
495
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
496

    
497
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
498
				$rrdupdatesh .= "{ ";
499
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
500
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
501
				$rrdupdatesh .= " q=1; ";
502
				$rrdupdatesh .= "} ";
503
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
504
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
505
				$rrdupdatesh .= " q=0; ";
506
				$rrdupdatesh .= "} ";
507
				$rrdupdatesh .= "} END { ";
508
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
509
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
510
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
511
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
512
			}
513

    
514
			/* 3G interfaces */
515
			if (preg_match("/ppp[0-9]+/i", $realif))	{
516
				if (!file_exists("$rrddbpath$ifname$cellular")) {
517
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
518
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
519
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
520
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
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
					create_new_rrd($rrdcreate);
526
					unset($rrdcreate);
527
				}
528

    
529
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
530
				if (platform_booting()) {
531
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
532
				}
533

    
534
				$rrdupdatesh .= "\n";
535
				$rrdupdatesh .= "# polling 3G\n";
536
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
537
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
538
			}
539

    
540
		}
541
		$i++;
542

    
543
		/* System only statistics */
544
		$ifname = "system";
545

    
546
		/* STATES, create pf states database */
547
		if (!file_exists("$rrddbpath$ifname$states")) {
548
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
549
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
550
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
551
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
552
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
553
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
554
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
555
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
556
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
557
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
558

    
559
			create_new_rrd($rrdcreate);
560
			unset($rrdcreate);
561
		}
562

    
563
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
564
		if (platform_booting()) {
565
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
566
		}
567

    
568
		/* the pf states gathering function. */
569
		$rrdupdatesh .= "\n";
570
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
571
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
572
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
573
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
574
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
575
		$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";
576
		$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";
577
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
578

    
579
		/* End pf states statistics */
580

    
581
		/* CPU, create CPU statistics database */
582
		if (!file_exists("$rrddbpath$ifname$proc")) {
583
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
584
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
585
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
586
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
587
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
588
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
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

    
594
			create_new_rrd($rrdcreate);
595
			unset($rrdcreate);
596
		}
597

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

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

    
609
		/* End CPU statistics */
610

    
611
		/* Memory, create Memory statistics database */
612
		if (!file_exists("$rrddbpath$ifname$mem")) {
613
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
614
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
615
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
616
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
617
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
618
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
619
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
620
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
621
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
622
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
623
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
624
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
625
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
626
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
627
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
628
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
629
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
630
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
631

    
632
			create_new_rrd($rrdcreate);
633
			unset($rrdcreate);
634
		}
635

    
636
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
637
		if (platform_booting()) {
638
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
639
		}
640

    
641
		/* the Memory stats gathering function. */
642
		$rrdupdatesh .= "MEM=`$sysctl -qn 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 | ";
643
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
644
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
645
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
646

    
647
		/* End Memory statistics */
648

    
649
		/* mbuf, create mbuf statistics database */
650
		if (!file_exists("$rrddbpath$ifname$mbuf")) {
651
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
652
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
653
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
654
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
655
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
656
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
657
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
658
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
659
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
660
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
661
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
662
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
663
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
664
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
665
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
666
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
667
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
668

    
669
			create_new_rrd($rrdcreate);
670
			unset($rrdcreate);
671
		}
672

    
673
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
674
		if (platform_booting()) {
675
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
676
		}
677

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

    
683
		/* End mbuf statistics */
684

    
685
		/* CPU temperature */
686

    
687
		$sensors_array = array();
688
		$unknown = "";
689
		$rrdupdateds = "";
690
		$rrdsensors = "";
691
		$_gb = exec("/sbin/sysctl -q hw.acpi.thermal dev.cpu dev.t5nex dev.armada_thermal dev.cordbuc | /usr/bin/grep temperature | /usr/bin/cut -d':' -f1", $sensors_array);
692

    
693
		if (!empty($sensors_array)) {
694
			foreach ($sensors_array as $sensor) {
695
				preg_match("/^(.+)\.(.+\..+)\.temperature/", $sensor, $sensor_match);
696
				$sensor_name = str_replace(".", "_", $sensor_match[2]);
697
				$rrdcreateds .= "DS:$sensor_name:GAUGE:$sensorsvalid:0:100 ";
698
				$unknown .= ":U";
699
				$rrddsname = strtoupper($sensor_name);
700
				$rrdupdateds .= "$rrddsname=`$sysctl -qn $sensor | $sed 's/C//'`\n";
701
				$rrdsensors .= ":\${$rrddsname}";
702
			}
703
			if (!file_exists("$rrddbpath$ifname$sensors")) {
704
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$sensors --step $rrdsensorsinterval ";
705
				$rrdcreate .= $rrdcreateds;
706
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
707
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
708
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
709
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
710
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
711
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
712
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
713
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
714
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
715
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
716
				$rrdcreate .= "RRA:MAX:0.5:1440:2284";
717

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

    
722
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
723
			if (platform_booting()) {
724
				mwexec("$rrdtool update $rrddbpath$ifname$sensors N$unknown");
725
			}
726

    
727
			/* the CPU temp gathering function. */
728

    
729
			$rrdupdatesh .= $rrdupdateds;
730
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$sensors N$rrdsensors\n";
731
		}
732

    
733
		/* End CPU temperature */
734

    
735
		/* Captive Portal statistics, set up the rrd file */
736
		if (is_array($config['captiveportal'])) {
737
			foreach ($config['captiveportal'] as $cpkey => $cp) {
738
				if (!isset($cp['enable'])) {
739
					continue;
740
				}
741

    
742
				$ifname= "captiveportal";
743
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
744
				if (!file_exists("$concurrent_filename")) {
745
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
746
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
747
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
748
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
749
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
750
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
751
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
752
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
753
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
754
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
755
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
756
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
757
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
758
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
759
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
760
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
761
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
762
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
763

    
764
					create_new_rrd($rrdcreate);
765
					unset($rrdcreate);
766
				}
767

    
768
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
769
				if (platform_booting()) {
770
					mwexec("$rrdtool update $concurrent_filename N:U");
771
				}
772

    
773
				/* the Captive Portal stats gathering function. */
774
				$rrdupdatesh .= "\n";
775
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
776
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
777
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
778

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

    
800
					create_new_rrd($rrdcreate);
801
					unset($rrdcreate);
802
				}
803

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

    
809
				/* the Captive Portal stats gathering function. */
810
				$rrdupdatesh .= "\n";
811
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
812
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
813
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
814

    
815
			}
816
		}
817
		/* End Captive Portal statistics */
818

    
819
		/* NTP, set up the ntpd rrd file */
820
		if (isset($config['ntpd']['statsgraph']) && ($config['ntpd']['enable'] != 'disabled')) {
821
			/* set up the ntpd rrd file */
822
			if (!file_exists("$rrddbpath$ntpd")) {
823
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
824
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:-1000:1000 ";
825
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
826
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
827
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
828
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:-1000:1000 ";
829
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
830
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
831
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
832
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
833
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
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:MAX:0.5:1:1200 ";
839
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
840
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
841
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
842

    
843
				create_new_rrd($rrdcreate);
844
				unset($rrdcreate);
845
			}
846

    
847
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
848
			if (platform_booting()) {
849
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
850
			}
851

    
852
			/* the ntp stats gathering function. */
853
			$rrdupdatesh .= "\n";
854
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
855
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
856
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
857
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
858
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
859
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
860
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
861
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
862
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
863
			$rrdupdatesh .= "\n";
864

    
865
		}
866
		/* End NTP statistics */
867

    
868
		/* Start dhcpd statistics */
869
		if (is_array($config['dhcpd'])) {
870
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
871
				if (isset($config['dhcpd'][$dhcpif]['statsgraph'])) {
872
					if (!file_exists("$rrddbpath$dhcpif$dhcpd")) {
873
						$rrdcreate = "$rrdtool create $rrddbpath$dhcpif$dhcpd --step $rrddhcpdinterval ";
874
						$rrdcreate .= "DS:leases:GAUGE:$dhcpdvalid:0:100000 ";
875
						$rrdcreate .= "DS:staticleases:GAUGE:$dhcpdvalid:0:100000 ";
876
						$rrdcreate .= "DS:dhcprange:GAUGE:$dhcpdvalid:0:100000 ";
877
						$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
878
						$rrdcreate .= "RRA:MIN:0.5:5:720 ";
879
						$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
880
						$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
881
						$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
882
						$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
883
						$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
884
						$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
885
						$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
886
						$rrdcreate .= "RRA:MAX:0.5:5:720 ";
887
						$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
888
						$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
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$dhcpif$dhcpd N:U:U:U");
896
					}
897

    
898
					$rrdupdatesh .= "\n";
899
					$rrdupdatesh .= "# polling leases for dhcp \n";
900
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
901
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
902

    
903
				}
904
			}
905
		}
906
		/* END dhcpd statistics */
907

    
908
		/* Start gateway quality */
909
		$rrdupdatesh .= <<<EOD
910

    
911
# Gateway quality graphs
912
for sock in {$g['varrun_path']}/dpinger_*.sock; do
913
	if [ ! -S "\$sock" ]; then
914
		continue
915
	fi
916

    
917
	t=\$(/usr/bin/nc -U \$sock)
918
	if [ -z "\$t" ]; then
919
		continue
920
	fi
921

    
922
	gw=\$(echo "\$t" | awk '{ print \$1 }')
923
	delay=\$(echo "\$t" | awk '{ print \$2 }')
924
	stddev=\$(echo "\$t" | awk '{ print \$3 }')
925
	loss=\$(echo "\$t" | awk '{ print \$4 }')
926

    
927
	if echo "\$loss" | grep -Eqv '^[0-9]+\$'; then
928
		loss="U"
929
	fi
930
	if echo "\$delay" | grep -Eqv '^[0-9]+\$'; then
931
		delay="U"
932
	else
933
		# Convert delay from microseconds to seconds
934
		delay=\$(echo "scale=7; \$delay / 1000 / 1000" | /usr/bin/bc)
935
	fi
936
	if echo "\$stddev" | grep -Eqv '^[0-9]+\$'; then
937
		stddev="U"
938
	else
939
		# Convert stddev from microseconds to seconds
940
		stddev=\$(echo "scale=7; \$stddev / 1000 / 1000" | /usr/bin/bc)
941
	fi
942

    
943
	if [ ! -f {$rrddbpath}\$gw-quality.rrd ]; then
944
		{$rrdtool} create {$rrddbpath}\$gw-quality.rrd --step 60 \\
945
		DS:loss:GAUGE:120:0:100 \\
946
		DS:delay:GAUGE:120:0:100000 \\
947
		DS:stddev:GAUGE:120:0:100000 \\
948
		RRA:AVERAGE:0.5:1:1200 \\
949
		RRA:AVERAGE:0.5:5:720 \\
950
		RRA:AVERAGE:0.5:60:1860 \\
951
		RRA:AVERAGE:0.5:1440:2284
952

    
953
		{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:U:U:U
954
	fi
955

    
956
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:\$loss:\$delay:\$stddev
957
done
958

    
959
EOD;
960
		/* End gateway quality */
961

    
962
		$rrdupdatesh .= "sleep 60\n";
963
		$rrdupdatesh .= "done\n";
964
		log_error(gettext("Creating rrd update script"));
965
		/* write the rrd update script */
966
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
967
		$fd = fopen("$updaterrdscript", "w");
968
		fwrite($fd, "$rrdupdatesh");
969
		fclose($fd);
970

    
971
		unset($rrdupdatesh);
972

    
973
		/* kill off traffic collectors */
974
		kill_traffic_collector();
975

    
976
		/* start traffic collector */
977
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
978

    
979
	} else {
980
		/* kill off traffic collectors */
981
		kill_traffic_collector();
982
	}
983

    
984
	$databases = glob("{$rrddbpath}/*.rrd");
985
	foreach ($databases as $database) {
986
		if (file_exists($database)) {
987
			chown($database, "nobody");
988
		}
989
	}
990

    
991
	if (platform_booting()) {
992
		echo gettext("done.") . "\n";
993
	}
994

    
995
}
996

    
997
function kill_traffic_collector() {
998
	global $g;
999

    
1000
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
1001
}
1002

    
1003
?>
(45-45/62)