Project

General

Profile

Download (39.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-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2023 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_get('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
		$nat_match_pattern = '\(([0-9a-f:.]|\[|\])+\) (\->|<\-)';
570
		$rrdupdatesh .= "\n";
571
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
572
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
573
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
574
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v '{$nat_match_pattern}' | wc -l|sed 's/ //g'`\"\n";
575
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '{$nat_match_pattern}' | wc -l|sed 's/ //g' `\"\n";
576
		$rrdupdatesh .= "srcip=\"` cat /tmp/pfctl_ss_out | egrep -v '{$nat_match_pattern}' | grep '\\->' | awk '{print \$3}' | awk -F: '{print \$1}' | sort -u|wc -l|sed 's/ //g' `\"\n";
577
		$rrdupdatesh .= "dstip=\"` cat /tmp/pfctl_ss_out | egrep -v '{$nat_match_pattern}' | grep '<\\-' | awk '{print \$3}' | awk -F: '{print \$1}' | sort -u|wc -l|sed 's/ //g' `\"\n";
578
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
579

    
580
		/* End pf states statistics */
581

    
582
		/* CPU, create CPU statistics database */
583
		if (!file_exists("$rrddbpath$ifname$proc")) {
584
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
585
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
586
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
587
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
588
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
589
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
590
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
591
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
592
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
593
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
594

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

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

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

    
610
		/* End CPU statistics */
611

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

    
636
			create_new_rrd($rrdcreate);
637
			unset($rrdcreate);
638
		}
639

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

    
645
		/* Memory stats gathering function. */
646
		$rrdupdatesh .= "MEM=`$sysctl -qn ";
647
		/* Total pages */
648
		$rrdupdatesh .= "vm.stats.vm.v_page_count ";
649
		/* Active pages  */
650
		$rrdupdatesh .= "vm.stats.vm.v_active_count ";
651
		/* Inactive pages */
652
		$rrdupdatesh .= "vm.stats.vm.v_inactive_count ";
653
		/* Free pages  */
654
		$rrdupdatesh .= "vm.stats.vm.v_free_count ";
655
		if (is_module_loaded('zfs.ko')) {
656
			/* If the system is using ZFS, use ARC size as "cache" (value is bytes, not pages) */
657
			$rrdupdatesh .= "kstat.zfs.misc.arcstats.size ";
658
		} else {
659
			/* If the system is using UFS, use dirhash memory as "cache" (value is bytes, not pages) */
660
			$rrdupdatesh .= "vfs.ufs.dirhash_mem ";
661
		}
662
		/* Wired pages */
663
		$rrdupdatesh .= "vm.stats.vm.v_wire_count ";
664
		/* User wired pages */
665
		$rrdupdatesh .= "vm.stats.vm.v_user_wire_count ";
666
		/* Laundy pages */
667
		$rrdupdatesh .= "vm.stats.vm.v_laundry_count ";
668
		/* UFS Buffer space (value is in bytes, not pages) */
669
		$rrdupdatesh .= "vfs.bufspace ";
670
		/* Page size for calculations */
671
		$rrdupdatesh .= "hw.pagesize ";
672
		$rrdupdatesh .= " | ";
673
		/* Start the AWK code */
674
		$rrdupdatesh .= " $awk '{";
675
		/* Assign sysctl output to named variables */
676
		$rrdupdatesh .= "getline active;getline inactive;getline free;getline cache;getline wire;getline userwire;getline laundry;getline buffers;getline pagesize;";
677
		/* Divide cache and buffer values by page size so all values are in the same units */
678
		$rrdupdatesh .= "cache=(cache/pagesize);buffers=(buffers/pagesize);";
679
		/* Start of RRD-friendly output */
680
		$rrdupdatesh .= "printf ";
681
		/* Calculate values as a percentage of total pages */
682
		/* Active pages */
683
		$rrdupdatesh .= "((active/$0) * 100)";
684
		/* Inactive pages */
685
		$rrdupdatesh .= "\":\"((inactive/$0) * 100)";
686
		/* Free pages */
687
		$rrdupdatesh .= "\":\"((free/$0) * 100)";
688
		/* Cache (previously converted to pages) */
689
		$rrdupdatesh .= "\":\"((cache/$0) * 100)";
690
		/* Wired pages less cache and buffer pages (which are included in wired pages) */
691
		$rrdupdatesh .= "\":\"((wire - (cache + buffers))/$0 * 100)";
692
		/* User wired pages */
693
		$rrdupdatesh .= "\":\"((userwire/$0) * 100)";
694
		/* Laundry pages */
695
		$rrdupdatesh .= "\":\"((laundry/$0) * 100)";
696
		/* UFS Buffer space (previously converted to pages) */
697
		$rrdupdatesh .= "\":\"((buffers/$0) * 100)";
698
		/* Finish the AWK code */
699
		$rrdupdatesh .= "}'`\n";
700
		/* Update the database with new values */
701
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
702

    
703
		/* End Memory statistics */
704

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

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

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

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

    
739
		/* End mbuf statistics */
740

    
741
		/* CPU temperature */
742

    
743
		$sensors_array = array();
744
		$unknown = "";
745
		$rrdupdateds = "";
746
		$rrdsensors = "";
747
		$_gb = exec("/sbin/sysctl -q hw.acpi.thermal dev.cpu dev.t5nex dev.armada_thermal dev.cordbuc dev.pchtherm | /usr/bin/grep temperature | /usr/bin/cut -d':' -f1", $sensors_array);
748

    
749
		if (!empty($sensors_array)) {
750
			foreach ($sensors_array as $sensor) {
751
				preg_match("/^(.+)\.(.+\..+)\.temperature/", $sensor, $sensor_match);
752
				$sensor_name = str_replace(".", "_", $sensor_match[2]);
753
				$rrdcreateds .= "DS:$sensor_name:GAUGE:$sensorsvalid:0:100 ";
754
				$unknown .= ":U";
755
				$rrddsname = strtoupper($sensor_name);
756
				$rrdupdateds .= "$rrddsname=`$sysctl -qn $sensor | $sed 's/C//'`\n";
757
				$rrdsensors .= ":\${$rrddsname}";
758
			}
759
			if (!file_exists("$rrddbpath$ifname$sensors")) {
760
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$sensors --step $rrdsensorsinterval ";
761
				$rrdcreate .= $rrdcreateds;
762
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
763
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
764
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
765
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
766
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
767
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
768
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
769
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
770
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
771
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
772
				$rrdcreate .= "RRA:MAX:0.5:1440:2284";
773

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

    
778
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
779
			if (platform_booting()) {
780
				mwexec("$rrdtool update $rrddbpath$ifname$sensors N$unknown");
781
			}
782

    
783
			/* the CPU temp gathering function. */
784

    
785
			$rrdupdatesh .= $rrdupdateds;
786
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$sensors N$rrdsensors\n";
787
		}
788

    
789
		/* End CPU temperature */
790

    
791
		/* Captive Portal statistics, set up the rrd file */
792
		if (is_array($config['captiveportal'])) {
793
			foreach ($config['captiveportal'] as $cpkey => $cp) {
794
				if (!isset($cp['enable'])) {
795
					continue;
796
				}
797

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

    
820
					create_new_rrd($rrdcreate);
821
					unset($rrdcreate);
822
				}
823

    
824
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
825
				if (platform_booting()) {
826
					mwexec("$rrdtool update $concurrent_filename N:U");
827
				}
828

    
829
				/* the Captive Portal stats gathering function. */
830
				$rrdupdatesh .= "\n";
831
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
832
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
833
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
834

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

    
856
					create_new_rrd($rrdcreate);
857
					unset($rrdcreate);
858
				}
859

    
860
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
861
				if (platform_booting()) {
862
					mwexec("$rrdtool update $loggedin_filename N:U");
863
				}
864

    
865
				/* the Captive Portal stats gathering function. */
866
				$rrdupdatesh .= "\n";
867
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
868
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
869
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
870

    
871
			}
872
		}
873
		/* End Captive Portal statistics */
874

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

    
899
				create_new_rrd($rrdcreate);
900
				unset($rrdcreate);
901
			}
902

    
903
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
904
			if (platform_booting()) {
905
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
906
			}
907

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

    
921
		}
922
		/* End NTP statistics */
923

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

    
952
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
953
					if (platform_booting()) {
954
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U");
955
					}
956

    
957
					$rrdupdatesh .= "\n";
958
					$rrdupdatesh .= "# polling leases for dhcp \n";
959
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
960
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
961

    
962
				}
963
			}
964
		}
965
		/* END dhcpd statistics */
966

    
967
		/* Start gateway quality */
968
		$rrdupdatesh .= <<<EOD
969

    
970
# Gateway quality graphs
971
for sock in {$g['varrun_path']}/dpinger_*.sock; do
972
	if [ ! -S "\$sock" ]; then
973
		continue
974
	fi
975

    
976
	t=\$(/usr/bin/nc -U \$sock)
977
	if [ -z "\$t" ]; then
978
		continue
979
	fi
980

    
981
	gw=\$(echo "\$t" | awk '{ print \$1 }')
982
	delay=\$(echo "\$t" | awk '{ print \$2 }')
983
	stddev=\$(echo "\$t" | awk '{ print \$3 }')
984
	loss=\$(echo "\$t" | awk '{ print \$4 }')
985

    
986
	if echo "\$loss" | grep -Eqv '^[0-9]+\$'; then
987
		loss="U"
988
	fi
989
	if echo "\$delay" | grep -Eqv '^[0-9]+\$'; then
990
		delay="U"
991
	else
992
		# Convert delay from microseconds to seconds
993
		delay=\$(echo "scale=7; \$delay / 1000 / 1000" | /usr/bin/bc)
994
	fi
995
	if echo "\$stddev" | grep -Eqv '^[0-9]+\$'; then
996
		stddev="U"
997
	else
998
		# Convert stddev from microseconds to seconds
999
		stddev=\$(echo "scale=7; \$stddev / 1000 / 1000" | /usr/bin/bc)
1000
	fi
1001

    
1002
	if [ ! -f {$rrddbpath}\$gw-quality.rrd ]; then
1003
		{$rrdtool} create {$rrddbpath}\$gw-quality.rrd --step 60 \\
1004
		DS:loss:GAUGE:120:0:100 \\
1005
		DS:delay:GAUGE:120:0:100000 \\
1006
		DS:stddev:GAUGE:120:0:100000 \\
1007
		RRA:AVERAGE:0.5:1:1200 \\
1008
		RRA:AVERAGE:0.5:5:720 \\
1009
		RRA:AVERAGE:0.5:60:1860 \\
1010
		RRA:AVERAGE:0.5:1440:2284
1011

    
1012
		{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:U:U:U
1013
	fi
1014

    
1015
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:\$loss:\$delay:\$stddev
1016
done
1017

    
1018
EOD;
1019
		/* End gateway quality */
1020

    
1021
		$rrdupdatesh .= "sleep 60\n";
1022
		$rrdupdatesh .= "done\n";
1023
		log_error(gettext("Creating rrd update script"));
1024
		/* write the rrd update script */
1025
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
1026
		$fd = fopen("$updaterrdscript", "w");
1027
		fwrite($fd, "$rrdupdatesh");
1028
		fclose($fd);
1029

    
1030
		unset($rrdupdatesh);
1031

    
1032
		/* kill off traffic collectors */
1033
		kill_traffic_collector();
1034

    
1035
		/* start traffic collector */
1036
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
1037

    
1038
	} else {
1039
		/* kill off traffic collectors */
1040
		kill_traffic_collector();
1041
	}
1042

    
1043
	$databases = glob("{$rrddbpath}/*.rrd");
1044
	foreach ($databases as $database) {
1045
		if (file_exists($database)) {
1046
			chown($database, "nobody");
1047
		}
1048
	}
1049

    
1050
	if (platform_booting()) {
1051
		echo gettext("done.") . "\n";
1052
	}
1053

    
1054
}
1055

    
1056
function kill_traffic_collector() {
1057
	global $g;
1058

    
1059
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
1060
}
1061

    
1062
?>
(45-45/61)