Project

General

Profile

Download (36.6 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-2019 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
	$spamd = "-spamd.rrd";
165
	$proc = "-processor.rrd";
166
	$mem = "-memory.rrd";
167
	$mbuf = "-mbuf.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
	$tar = "/usr/bin/tar";
179
	$pfctl = "/sbin/pfctl";
180
	$sysctl = "/sbin/sysctl";
181
	$php = "/usr/local/bin/php-cgi";
182
	$cpustats = "/usr/local/sbin/cpustats";
183
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
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
	$rrdspamdinterval = 60;
196
	$rrdlbpoolinterval = 60;
197
	$rrdprocinterval = 60;
198
	$rrdmeminterval = 60;
199
	$rrdmbufinterval = 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
	$spamdvalid = $rrdspamdinterval * 2;
213
	$lbpoolvalid = $rrdlbpoolinterval * 2;
214
	$procvalid = $rrdlbpoolinterval * 2;
215
	$memvalid = $rrdmeminterval * 2;
216
	$mbufvalid = $rrdmbufinterval * 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
		if (is_array($config['openvpn']['openvpn-server'])) {
256
			foreach ($config['openvpn']['openvpn-server'] as $server) {
257
				$serverid = "ovpns" . $server['vpnid'];
258
				$ifdescrs[$serverid] = "{$server['description']}";
259
			}
260
		}
261

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
381
				if (is_array($config['openvpn']['openvpn-server'])) {
382
					foreach ($config['openvpn']['openvpn-server'] as $server) {
383
						if ("ovpns{$server['vpnid']}" == $ifname) {
384
							$port = $server['local_port'];
385
							$vpnid = $server['vpnid'];
386
						}
387
					}
388
				}
389
				$rrdupdatesh .= "\n";
390
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
391
				$rrdupdatesh .= "list_current_users() {\n";
392
				$rrdupdatesh .= " sleep 0.2\n";
393
				$rrdupdatesh .= " echo \"status 2\"\n";
394
				$rrdupdatesh .= " sleep 0.2\n";
395
				$rrdupdatesh .= " echo \"quit\"\n";
396
				$rrdupdatesh .= "}\n";
397
				$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";
398
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
399
			}
400

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

    
433
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
434
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
435
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
436
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
437

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

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

    
450
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
451
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
452
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
453
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
454

    
455
					create_new_rrd($rrdcreate);
456
					unset($rrdcreate);
457
				}
458

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

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

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

    
511
			/* 3G interfaces */
512
			if (preg_match("/ppp[0-9]+/i", $realif))	{
513
				if (!file_exists("$rrddbpath$ifname$cellular")) {
514
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
515
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
516
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
517
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
518
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
519
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
520
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
521
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
522
					create_new_rrd($rrdcreate);
523
					unset($rrdcreate);
524
				}
525

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

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

    
537
		}
538
		$i++;
539

    
540
		/* System only statistics */
541
		$ifname = "system";
542

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

    
556
			create_new_rrd($rrdcreate);
557
			unset($rrdcreate);
558
		}
559

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

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

    
576
		/* End pf states statistics */
577

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

    
591
			create_new_rrd($rrdcreate);
592
			unset($rrdcreate);
593
		}
594

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

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

    
606
		/* End CPU statistics */
607

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

    
629
			create_new_rrd($rrdcreate);
630
			unset($rrdcreate);
631
		}
632

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

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

    
644
		/* End Memory statistics */
645

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

    
666
			create_new_rrd($rrdcreate);
667
			unset($rrdcreate);
668
		}
669

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

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

    
680
		/* End mbuf statistics */
681

    
682
		/* SPAMD, set up the spamd rrd file */
683
		if (isset($config['installedpackages']['spamdsettings']) &&
684
		    $config['installedpackages']['spamdsettings']['config'][0]['enablerrd']) {
685
			/* set up the spamd rrd file */
686
			if (!file_exists("$rrddbpath$ifname$spamd")) {
687
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
688
				$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
689
				$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
690
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
691
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
692
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
693
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
694
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
695
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
696
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
697
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
698
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
699
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
700
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
701
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
702

    
703
				create_new_rrd($rrdcreate);
704
				unset($rrdcreate);
705
			}
706

    
707
			$rrdupdatesh .= "\n";
708
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
709
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
710
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
711

    
712
		}
713
		/* End System statistics */
714

    
715
		/* Captive Portal statistics, set up the rrd file */
716
		if (is_array($config['captiveportal'])) {
717
			foreach ($config['captiveportal'] as $cpkey => $cp) {
718
				if (!isset($cp['enable'])) {
719
					continue;
720
				}
721

    
722
				$ifname= "captiveportal";
723
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
724
				if (!file_exists("$concurrent_filename")) {
725
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
726
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
727
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
728
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
729
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
730
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
731
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
732
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
733
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
734
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
735
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
736
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
737
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
738
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
739
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
740
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
741
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
742
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
743

    
744
					create_new_rrd($rrdcreate);
745
					unset($rrdcreate);
746
				}
747

    
748
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
749
				if (platform_booting()) {
750
					mwexec("$rrdtool update $concurrent_filename N:U");
751
				}
752

    
753
				/* the Captive Portal stats gathering function. */
754
				$rrdupdatesh .= "\n";
755
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
756
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
757
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
758

    
759
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
760
				if (!file_exists("$loggedin_filename")) {
761
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
762
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
763
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
764
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
765
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
766
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
767
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
768
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
769
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
770
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
771
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
772
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
773
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
774
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
775
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
776
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
777
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
778
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
779

    
780
					create_new_rrd($rrdcreate);
781
					unset($rrdcreate);
782
				}
783

    
784
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
785
				if (platform_booting()) {
786
					mwexec("$rrdtool update $loggedin_filename N:U");
787
				}
788

    
789
				/* the Captive Portal stats gathering function. */
790
				$rrdupdatesh .= "\n";
791
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
792
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
793
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
794

    
795
			}
796
		}
797
		/* End Captive Portal statistics */
798

    
799
		/* NTP, set up the ntpd rrd file */
800
		if (isset($config['ntpd']['statsgraph'])) {
801
			/* set up the ntpd rrd file */
802
			if (!file_exists("$rrddbpath$ntpd")) {
803
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
804
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:-1000:1000 ";
805
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
806
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
807
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
808
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:0:1000 ";
809
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
810
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
811
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
812
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
813
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
814
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
815
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
816
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
817
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
818
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
819
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
820
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
821
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
822

    
823
				create_new_rrd($rrdcreate);
824
				unset($rrdcreate);
825
			}
826

    
827
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
828
			if (platform_booting()) {
829
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
830
			}
831

    
832
			/* the ntp stats gathering function. */
833
			$rrdupdatesh .= "\n";
834
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
835
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
836
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
837
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
838
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
839
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
840
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
841
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
842
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
843
			$rrdupdatesh .= "\n";
844

    
845
		}
846
		/* End NTP statistics */
847

    
848
		/* Start dhcpd statistics */
849
		if (is_array($config['dhcpd'])) {
850
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
851
				if (isset($config['dhcpd'][$dhcpif]['statsgraph'])) {
852
					if (!file_exists("$rrddbpath$dhcpif$dhcpd")) {
853
						$rrdcreate = "$rrdtool create $rrddbpath$dhcpif$dhcpd --step $rrddhcpdinterval ";
854
						$rrdcreate .= "DS:leases:GAUGE:$dhcpdvalid:0:100000 ";
855
						$rrdcreate .= "DS:staticleases:GAUGE:$dhcpdvalid:0:100000 ";
856
						$rrdcreate .= "DS:dhcprange:GAUGE:$dhcpdvalid:0:100000 ";
857
						$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
858
						$rrdcreate .= "RRA:MIN:0.5:5:720 ";
859
						$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
860
						$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
861
						$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
862
						$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
863
						$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
864
						$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
865
						$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
866
						$rrdcreate .= "RRA:MAX:0.5:5:720 ";
867
						$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
868
						$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
869
						create_new_rrd($rrdcreate);
870
						unset($rrdcreate);
871
					}
872

    
873
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
874
					if (platform_booting()) {
875
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U");
876
					}
877

    
878
					$rrdupdatesh .= "\n";
879
					$rrdupdatesh .= "# polling leases for dhcp \n";
880
					$rrdupdatesh .= "DHCP=`${php} -q ${dhcpd_gather} '${dhcpif}'`\n";
881
					$rrdupdatesh .= "$rrdtool update $rrddbpath$dhcpif$dhcpd \${DHCP}\n";
882

    
883
				}
884
			}
885
		}
886
		/* END dhcpd statistics */
887

    
888
		/* Start gateway quality */
889
		$rrdupdatesh .= <<<EOD
890

    
891
# Gateway quality graphs
892
for sock in {$g['varrun_path']}/dpinger_*.sock; do
893
	if [ ! -S "\$sock" ]; then
894
		continue
895
	fi
896

    
897
	t=\$(/usr/bin/nc -U \$sock)
898
	if [ -z "\$t" ]; then
899
		continue
900
	fi
901

    
902
	gw=\$(echo "\$t" | awk '{ print \$1 }')
903
	delay=\$(echo "\$t" | awk '{ print \$2 }')
904
	stddev=\$(echo "\$t" | awk '{ print \$3 }')
905
	loss=\$(echo "\$t" | awk '{ print \$4 }')
906

    
907
	if echo "\$loss" | grep -Eqv '^[0-9]+\$'; then
908
		loss="U"
909
	fi
910
	if echo "\$delay" | grep -Eqv '^[0-9]+\$'; then
911
		delay="U"
912
	else
913
		# Convert delay from microseconds to seconds
914
		delay=\$(echo "scale=7; \$delay / 1000 / 1000" | /usr/bin/bc)
915
	fi
916
	if echo "\$stddev" | grep -Eqv '^[0-9]+\$'; then
917
		stddev="U"
918
	else
919
		# Convert stddev from microseconds to seconds
920
		stddev=\$(echo "scale=7; \$stddev / 1000 / 1000" | /usr/bin/bc)
921
	fi
922

    
923
	if [ ! -f {$rrddbpath}\$gw-quality.rrd ]; then
924
		{$rrdtool} create {$rrddbpath}\$gw-quality.rrd --step 60 \\
925
		DS:loss:GAUGE:120:0:100 \\
926
		DS:delay:GAUGE:120:0:100000 \\
927
		DS:stddev:GAUGE:120:0:100000 \\
928
		RRA:AVERAGE:0.5:1:1200 \\
929
		RRA:AVERAGE:0.5:5:720 \\
930
		RRA:AVERAGE:0.5:60:1860 \\
931
		RRA:AVERAGE:0.5:1440:2284
932

    
933
		{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:U:U:U
934
	fi
935

    
936
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:\$loss:\$delay:\$stddev
937
done
938

    
939
EOD;
940
		/* End gateway quality */
941

    
942
		$rrdupdatesh .= "sleep 60\n";
943
		$rrdupdatesh .= "done\n";
944
		log_error(gettext("Creating rrd update script"));
945
		/* write the rrd update script */
946
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
947
		$fd = fopen("$updaterrdscript", "w");
948
		fwrite($fd, "$rrdupdatesh");
949
		fclose($fd);
950

    
951
		unset($rrdupdatesh);
952

    
953
		/* kill off traffic collectors */
954
		kill_traffic_collector();
955

    
956
		/* start traffic collector */
957
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
958

    
959
	} else {
960
		/* kill off traffic collectors */
961
		kill_traffic_collector();
962
	}
963

    
964
	$databases = glob("{$rrddbpath}/*.rrd");
965
	foreach ($databases as $database) {
966
		if (file_exists($database)) {
967
			chown($database, "nobody");
968
		}
969
	}
970

    
971
	if (platform_booting()) {
972
		echo gettext("done.") . "\n";
973
	}
974

    
975
}
976

    
977
function kill_traffic_collector() {
978
	global $g;
979

    
980
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
981
}
982

    
983
?>
(43-43/59)