Project

General

Profile

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

    
23
/* include all configuration functions */
24

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

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

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

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

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

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

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

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

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

    
146
function enable_rrd_graphing() {
147
	global $config, $g, $altq_list_queues;
148

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

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

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

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

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

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

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

    
225
	/* read the shaper config */
226
	read_altq_config();
227

    
228
	if (isset ($config['rrd']['enable'])) {
229

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

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

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

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

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

    
283
				create_new_rrd($rrdcreate);
284
				unset($rrdcreate);
285
			}
286

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

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

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

    
316
				create_new_rrd($rrdcreate);
317
				unset($rrdcreate);
318
			}
319

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

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

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

    
345
					create_new_rrd($rrdcreate);
346
					unset($rrdcreate);
347
				}
348

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

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

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

    
370
					create_new_rrd($rrdcreate);
371
					unset($rrdcreate);
372
				}
373

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

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

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

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

    
436
					create_new_rrd($rrdcreate);
437
					unset($rrdcreate);
438
				}
439

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

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

    
453
					create_new_rrd($rrdcreate);
454
					unset($rrdcreate);
455
				}
456

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

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

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

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

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

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

    
535
		}
536
		$i++;
537

    
538
		/* System only statistics */
539
		$ifname = "system";
540

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

    
554
			create_new_rrd($rrdcreate);
555
			unset($rrdcreate);
556
		}
557

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

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

    
574
		/* End pf states statistics */
575

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

    
589
			create_new_rrd($rrdcreate);
590
			unset($rrdcreate);
591
		}
592

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

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

    
604
		/* End CPU statistics */
605

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

    
627
			create_new_rrd($rrdcreate);
628
			unset($rrdcreate);
629
		}
630

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

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

    
642
		/* End Memory statistics */
643

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

    
664
			create_new_rrd($rrdcreate);
665
			unset($rrdcreate);
666
		}
667

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

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

    
678
		/* End mbuf statistics */
679

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

    
701
				create_new_rrd($rrdcreate);
702
				unset($rrdcreate);
703
			}
704

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

    
710
		}
711
		/* End System statistics */
712

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

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

    
742
					create_new_rrd($rrdcreate);
743
					unset($rrdcreate);
744
				}
745

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

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

    
757
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
758
				if (!file_exists("$loggedin_filename")) {
759
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
760
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
761
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
762
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
763
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
764
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
765
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
766
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
767
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
768
					$rrdcreate .= "RRA:MIN: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
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
774
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
775
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
776
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
777

    
778
					create_new_rrd($rrdcreate);
779
					unset($rrdcreate);
780
				}
781

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

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

    
793
			}
794
		}
795
		/* End Captive Portal statistics */
796

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

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

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

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

    
843
		}
844
		/* End NTP statistics */
845

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

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

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

    
881
				}
882
			}
883
		}
884
		/* END dhcpd statistics */
885

    
886
		/* Start gateway quality */
887
		$rrdupdatesh .= <<<EOD
888

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

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

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

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

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

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

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

    
937
EOD;
938
		/* End gateway quality */
939

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

    
949
		unset($rrdupdatesh);
950

    
951
		/* kill off traffic collectors */
952
		kill_traffic_collector();
953

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

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

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

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

    
973
}
974

    
975
function kill_traffic_collector() {
976
	global $g;
977

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

    
981
?>
(40-40/55)