Project

General

Profile

Download (36.5 KB) Statistics
| Branch: | Tag: | Revision:
1 b0a5b824 Seth Mos
<?php
2
/*
3 ac24dc24 Renato Botelho
 * rrd.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 c5d81585 Renato Botelho
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
7 81299b5c Renato Botelho
 * Copyright (c) 2010-2016 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
22 b0a5b824 Seth Mos
23
/* include all configuration functions */
24
25
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
26 d9acea75 Scott Ullrich
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
27 552f5a6a Renato Botelho
	unlink_if_exists($xmldumpfile);
28 1b25fc27 Seth Mos
29 873c1701 Renato Botelho
	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
30 b0a5b824 Seth Mos
	if ($dumpret <> 0) {
31
		$dumpout = implode(" ", $dumpout);
32 addc0439 Renato Botelho
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
33 b0a5b824 Seth Mos
	}
34
	return($dumpret);
35
}
36
37
function create_new_rrd($rrdcreatecmd) {
38
	$rrdcreateoutput = array();
39
	$rrdcreatereturn = 0;
40 2159494f Ermal
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
41 b0a5b824 Seth Mos
	if ($rrdcreatereturn <> 0) {
42
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
43 addc0439 Renato Botelho
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
44 b0a5b824 Seth Mos
	}
45 2159494f Ermal
	unset($rrdcreateoutput);
46 b0a5b824 Seth Mos
	return $rrdcreatereturn;
47
}
48
49
function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
50 61e047a5 Phil Davis
	if (!file_exists("/tmp/rrd_notice_sent.txt")) {
51 2159494f Ermal
		$_gb = exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
52
		@touch("/tmp/rrd_notice_sent.txt");
53 fdd20aba Scott Ullrich
	}
54 b0a5b824 Seth Mos
	$numrraold = count($rrdoldxml['rra']);
55
	$numrranew = count($rrdnewxml['rra']);
56 2159494f Ermal
	$numdsold = count($rrdoldxml['ds']);
57 b0a5b824 Seth Mos
	$numdsnew = count($rrdnewxml['ds']);
58 086cf944 Phil Davis
	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 5e5d5abc Renato Botelho
60 b0a5b824 Seth Mos
	/* add data sources not found in the old array from the new array */
61
	$i = 0;
62 61e047a5 Phil Davis
	foreach ($rrdnewxml['ds'] as $ds) {
63
		if (!is_array($rrdoldxml['ds'][$i])) {
64 b0a5b824 Seth Mos
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
65 d9e896fa Seth Mos
			/* 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 b0a5b824 Seth Mos
		}
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 61e047a5 Phil Davis
	foreach ($rrdnewxml['rra'] as $rra) {
78
		if (!is_array($rrdoldxml['rra'][$i])) {
79 b0a5b824 Seth Mos
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
80
		}
81
82
		$d = 0;
83
		/* process cdp_prep */
84
		$cdp_prep = $rra['cdp_prep'];
85 61e047a5 Phil Davis
		foreach ($cdp_prep['ds'] as $ds) {
86
			if (!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
87 b0a5b824 Seth Mos
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
88 d9e896fa Seth Mos
				$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 b0a5b824 Seth Mos
			}
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 ddc26847 Seth Mos
		$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 61e047a5 Phil Davis
		while ($r < $rowcountdiff) {
107 ddc26847 Seth Mos
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
108
			$r++;
109
		}
110
		$rows = $rowsempty + $rowsdata;
111 b0a5b824 Seth Mos
		/* now foreach the rows in the database */
112 61e047a5 Phil Davis
		foreach ($rows['row'] as $row) {
113
			if (!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
114 b0a5b824 Seth Mos
				$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 61e047a5 Phil Davis
			foreach ($row['v'] as $value) {
120
				if (empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
121
					if (isset($valid)) {
122 ed3ea464 Seth Mos
						$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 61e047a5 Phil Davis
					if ($value <> " NaN ") {
128 ed3ea464 Seth Mos
						$valid = true;
129
					} else {
130
						$valid = false;
131
					}
132 b0a5b824 Seth Mos
				}
133
				$m++;
134
			}
135
			$k++;
136
		}
137
		$i++;
138
	}
139
140
	$numrranew = count($rrdoldxml['rra']);
141
	$numdsnew = count($rrdoldxml['ds']);
142 addc0439 Renato Botelho
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
143 b0a5b824 Seth Mos
	return $rrdoldxml;
144
}
145
146 ea4bc46f Seth Mos
function enable_rrd_graphing() {
147
	global $config, $g, $altq_list_queues;
148
149 61e047a5 Phil Davis
	if (platform_booting()) {
150 9eda6186 Carlos Eduardo Ramos
		echo gettext("Generating RRD graphs...");
151 61e047a5 Phil Davis
	}
152 ea4bc46f Seth Mos
153 afb6ae0c Renato Botelho
	$rrddbpath = "{$g['vardb_path']}/rrd/";
154 ea4bc46f Seth Mos
	$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 3ed917c7 jim-p
	$mbuf = "-mbuf.rrd";
166 ec51a222 thompsa
	$cellular = "-cellular.rrd";
167 edd2d8b7 smos
	$vpnusers = "-vpnusers.rrd";
168 20413b72 Warren Baker
	$captiveportalconcurrent = "-concurrent.rrd";
169
	$captiveportalloggedin = "-loggedin.rrd";
170 1f3eff9b nagyrobi
	$ntpd = "ntpd.rrd";
171 89f3ec04 heper
	$dhcpd = "-dhcpd.rrd";
172 ea4bc46f Seth Mos
173 d9acea75 Scott Ullrich
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
174 ea4bc46f Seth Mos
	$netstat = "/usr/bin/netstat";
175
	$awk = "/usr/bin/awk";
176
	$tar = "/usr/bin/tar";
177
	$pfctl = "/sbin/pfctl";
178
	$sysctl = "/sbin/sysctl";
179 cb7d18d5 Renato Botelho
	$php = "/usr/local/bin/php-cgi";
180 e9e295f7 jim-p
	$cpustats = "/usr/local/sbin/cpustats";
181 ea4bc46f Seth Mos
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
182
	$ifconfig = "/sbin/ifconfig";
183 20413b72 Warren Baker
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
184 89f3ec04 heper
	$dhcpd_gather = "/usr/local/bin/dhcpd_gather_stats.php";
185 617f9edf nagyrobi
	$ntpq = "/usr/local/sbin/ntpq";
186 ea4bc46f Seth Mos
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 3ed917c7 jim-p
	$rrdmbufinterval = 60;
198 ec51a222 thompsa
	$rrdcellularinterval = 60;
199 55c08a96 smos
	$rrdvpninterval = 60;
200 20413b72 Warren Baker
	$rrdcaptiveportalinterval = 60;
201 1f3eff9b nagyrobi
	$rrdntpdinterval = 60;
202 89f3ec04 heper
	$rrddhcpdinterval = 60;
203 ea4bc46f Seth Mos
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 3ed917c7 jim-p
	$mbufvalid = $rrdmbufinterval * 2;
215 ec51a222 thompsa
	$cellularvalid = $rrdcellularinterval * 2;
216 edd2d8b7 smos
	$vpnvalid = $rrdvpninterval * 2;
217 20413b72 Warren Baker
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
218 1f3eff9b nagyrobi
	$ntpdvalid = $rrdntpdinterval * 2;
219 89f3ec04 heper
	$dhcpdvalid = $rrddhcpdinterval * 2;
220 4e322e2c Phil Davis
221 fa3b33a5 Renato Botelho
	/* Assume 2*10GigE for now */
222
	$downstream = 2500000000;
223
	$upstream = 2500000000;
224 ea4bc46f Seth Mos
225
	/* read the shaper config */
226
	read_altq_config();
227
228
	if (isset ($config['rrd']['enable'])) {
229
230
		/* create directory if needed */
231 3d83f02e jim-p
		if (!is_dir($rrddbpath)) {
232
			mkdir($rrddbpath, 0775);
233 ea4bc46f Seth Mos
		}
234 25cca20b jim-p
		chown($rrddbpath, "nobody");
235 ea4bc46f Seth Mos
236
		/* db update script */
237
		$rrdupdatesh = "#!/bin/sh\n";
238
		$rrdupdatesh .= "\n";
239 a555cc58 Seth Mos
		$rrdupdatesh .= "export TERM=dumb\n";
240 3d54aa10 Renato Botelho
		$rrdupdatesh .= "\n";
241
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
242
		$rrdupdatesh .= "\n";
243 ea4bc46f Seth Mos
		$rrdupdatesh .= "counter=1\n";
244
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
245
		$rrdupdatesh .= "do\n";
246
		$rrdupdatesh .= "";
247
248
		$i = 0;
249 4563d12f Seth Mos
		$ifdescrs = get_configured_interface_with_descr();
250 edd2d8b7 smos
		/* IPsec counters */
251 4563d12f Seth Mos
		$ifdescrs['ipsec'] = "IPsec";
252 edd2d8b7 smos
		/* OpenVPN server counters */
253 61e047a5 Phil Davis
		if (is_array($config['openvpn']['openvpn-server'])) {
254
			foreach ($config['openvpn']['openvpn-server'] as $server) {
255 55c08a96 smos
				$serverid = "ovpns" . $server['vpnid'];
256
				$ifdescrs[$serverid] = "{$server['description']}";
257
			}
258
		}
259 4563d12f Seth Mos
260 edd2d8b7 smos
		/* process all real and pseudo interfaces */
261 4563d12f Seth Mos
		foreach ($ifdescrs as $ifname => $ifdescr) {
262 65615d89 Seth Mos
			$temp = get_real_interface($ifname);
263 61e047a5 Phil Davis
			if ($temp <> "") {
264 65615d89 Seth Mos
				$realif = $temp;
265
			}
266 ea4bc46f Seth Mos
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 9bc8b6b6 Seth Mos
				$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 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
279
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
280
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
281 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
282 ea4bc46f Seth Mos
283
				create_new_rrd($rrdcreate);
284 cba9d7d9 Renato Botelho
				unset($rrdcreate);
285 ea4bc46f Seth Mos
			}
286
287
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
288 61e047a5 Phil Davis
			if (platform_booting()) {
289 9bc8b6b6 Seth Mos
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
290 ea4bc46f Seth Mos
			}
291
292
			$rrdupdatesh .= "\n";
293 9bc8b6b6 Seth Mos
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
294 a555cc58 Seth Mos
			$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 ea4bc46f Seth Mos
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 9bc8b6b6 Seth Mos
				$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 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
312
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
313
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
314 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
315 ea4bc46f Seth Mos
316
				create_new_rrd($rrdcreate);
317 cba9d7d9 Renato Botelho
				unset($rrdcreate);
318 ea4bc46f Seth Mos
			}
319
320
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
321 61e047a5 Phil Davis
			if (platform_booting()) {
322 9bc8b6b6 Seth Mos
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
323 ea4bc46f Seth Mos
			}
324
325
			$rrdupdatesh .= "\n";
326
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
327 a555cc58 Seth Mos
			$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 ea4bc46f Seth Mos
333
			/* WIRELESS, set up the rrd file */
334 61e047a5 Phil Davis
			if ($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
335 ea4bc46f Seth Mos
				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 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
341
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
342
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
343 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
344 5e5d5abc Renato Botelho
345 ea4bc46f Seth Mos
					create_new_rrd($rrdcreate);
346 cba9d7d9 Renato Botelho
					unset($rrdcreate);
347 ea4bc46f Seth Mos
				}
348
349
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
350 61e047a5 Phil Davis
				if (platform_booting()) {
351 e256e9d4 smos
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
352 ea4bc46f Seth Mos
				}
353
354
				$rrdupdatesh .= "\n";
355
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
356 e9e295f7 jim-p
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
357 488595df smos
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
358 ea4bc46f Seth Mos
			}
359
360 edd2d8b7 smos
			/* OpenVPN, set up the rrd file */
361 61e047a5 Phil Davis
			if (stristr($ifname, "ovpns")) {
362 edd2d8b7 smos
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
363
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
364
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
365 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
366
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
367
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
368 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
369 5e5d5abc Renato Botelho
370 edd2d8b7 smos
					create_new_rrd($rrdcreate);
371 cba9d7d9 Renato Botelho
					unset($rrdcreate);
372 edd2d8b7 smos
				}
373
374
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
375 61e047a5 Phil Davis
				if (platform_booting()) {
376 edd2d8b7 smos
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
377
				}
378 ea4bc46f Seth Mos
379 61e047a5 Phil Davis
				if (is_array($config['openvpn']['openvpn-server'])) {
380
					foreach ($config['openvpn']['openvpn-server'] as $server) {
381
						if ("ovpns{$server['vpnid']}" == $ifname) {
382 edd2d8b7 smos
							$port = $server['local_port'];
383 5f250a24 smos
							$vpnid = $server['vpnid'];
384 edd2d8b7 smos
						}
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 bdb6bd30 jim-p
				$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 e9e295f7 jim-p
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
397 edd2d8b7 smos
			}
398 ea4bc46f Seth Mos
399 edd2d8b7 smos
			/* 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 e8c516a0 Phil Davis
						break;
407 edd2d8b7 smos
					case "Mb":
408 e8c516a0 Phil Davis
						$factor = 1024 * 1024;
409
						break;
410 edd2d8b7 smos
					case "Kb":
411 e8c516a0 Phil Davis
						$factor = 1024;
412
						break;
413 edd2d8b7 smos
					case "b":
414
					default:
415 e8c516a0 Phil Davis
						$factor = 1;
416
						break;
417 edd2d8b7 smos
				}
418
				$qbandwidth = $altq->GetBandwidth() * $factor;
419 6c07db48 Phil Davis
				if ($qbandwidth <= 0) {
420 edd2d8b7 smos
					$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 ea4bc46f Seth Mos
					}
430
431 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
432
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
433
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
434 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
435 ea4bc46f Seth Mos
436 edd2d8b7 smos
					create_new_rrd($rrdcreate);
437 cba9d7d9 Renato Botelho
					unset($rrdcreate);
438 edd2d8b7 smos
				}
439 ea4bc46f Seth Mos
440 edd2d8b7 smos
				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 ea4bc46f Seth Mos
					}
447
448 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
449
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
450
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
451 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
452 edd2d8b7 smos
453
					create_new_rrd($rrdcreate);
454 cba9d7d9 Renato Botelho
					unset($rrdcreate);
455 edd2d8b7 smos
				}
456
457 61e047a5 Phil Davis
				if (platform_booting()) {
458 edd2d8b7 smos
					$rrdqcommand = "-t ";
459
					$rrducommand = "N";
460 857a4a79 jim-p
					$qi = 0;
461 edd2d8b7 smos
					foreach ($qlist as $qname => $q) {
462 61e047a5 Phil Davis
						if ($qi == 0) {
463 edd2d8b7 smos
							$rrdqcommand .= "{$qname}";
464
						} else {
465
							$rrdqcommand .= ":{$qname}";
466 ea4bc46f Seth Mos
						}
467 857a4a79 jim-p
						$qi++;
468 edd2d8b7 smos
						$rrducommand .= ":U";
469 ea4bc46f Seth Mos
					}
470 edd2d8b7 smos
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
471
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
472 ea4bc46f Seth Mos
				}
473 edd2d8b7 smos
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 5e589685 smos
509
			/* 3G interfaces */
510 61e047a5 Phil Davis
			if (preg_match("/ppp[0-9]+/i", $realif))	{
511 5e589685 smos
				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 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
517
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
518
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
519 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
520 5e589685 smos
					create_new_rrd($rrdcreate);
521 cba9d7d9 Renato Botelho
					unset($rrdcreate);
522 5e589685 smos
				}
523
524
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
525 61e047a5 Phil Davis
				if (platform_booting()) {
526 5e589685 smos
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
527
				}
528
529
				$rrdupdatesh .= "\n";
530
				$rrdupdatesh .= "# polling 3G\n";
531 2b095a33 smos
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
532
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
533 5e589685 smos
			}
534
535 ea4bc46f Seth Mos
		}
536
		$i++;
537
538
		/* System only statistics */
539
		$ifname = "system";
540
541 edd2d8b7 smos
		/* STATES, create pf states database */
542 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$states")) {
543 edd2d8b7 smos
			$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 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
550
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
551
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
552 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
553 edd2d8b7 smos
554
			create_new_rrd($rrdcreate);
555 cba9d7d9 Renato Botelho
			unset($rrdcreate);
556 edd2d8b7 smos
		}
557 ea4bc46f Seth Mos
558 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
559 61e047a5 Phil Davis
		if (platform_booting()) {
560 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
561
		}
562 ea4bc46f Seth Mos
563 edd2d8b7 smos
		/* 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 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$proc")) {
578 edd2d8b7 smos
			$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 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
585
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
586
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
587 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
588 edd2d8b7 smos
589
			create_new_rrd($rrdcreate);
590 cba9d7d9 Renato Botelho
			unset($rrdcreate);
591 edd2d8b7 smos
		}
592 ea4bc46f Seth Mos
593 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
594 61e047a5 Phil Davis
		if (platform_booting()) {
595 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
596
		}
597 ea4bc46f Seth Mos
598 edd2d8b7 smos
		/* the CPU stats gathering function. */
599 e9e295f7 jim-p
		$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 edd2d8b7 smos
604
		/* End CPU statistics */
605
606
		/* Memory, create Memory statistics database */
607 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$mem")) {
608 edd2d8b7 smos
			$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 492b1314 N0YB
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
615
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
616
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
617 3e2ecafe N0YB
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
618 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
619
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
620
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
621 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
622 492b1314 N0YB
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
623
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
624
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
625 3e2ecafe N0YB
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
626 edd2d8b7 smos
627
			create_new_rrd($rrdcreate);
628 cba9d7d9 Renato Botelho
			unset($rrdcreate);
629 edd2d8b7 smos
		}
630 ea4bc46f Seth Mos
631 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
632 61e047a5 Phil Davis
		if (platform_booting()) {
633 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
634
		}
635 ea4bc46f Seth Mos
636 edd2d8b7 smos
		/* the Memory stats gathering function. */
637 e9e295f7 jim-p
		$rrdupdatesh .= "MEM=`$sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_active_count vm.stats.vm.v_inactive_count vm.stats.vm.v_free_count vm.stats.vm.v_cache_count vm.stats.vm.v_wire_count | ";
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 5e5d5abc Renato Botelho
642 edd2d8b7 smos
		/* End Memory statistics */
643
644 3ed917c7 jim-p
		/* mbuf, create mbuf statistics database */
645 61e047a5 Phil Davis
		if (!file_exists("$rrddbpath$ifname$mbuf")) {
646 3ed917c7 jim-p
			$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 3e2ecafe N0YB
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
655 3ed917c7 jim-p
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
656
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
657
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
658 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
659 3ed917c7 jim-p
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
660
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
661
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
662 3e2ecafe N0YB
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
663 3ed917c7 jim-p
664
			create_new_rrd($rrdcreate);
665
			unset($rrdcreate);
666
		}
667
668
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
669 61e047a5 Phil Davis
		if (platform_booting()) {
670 06ff126e N0YB
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
671 3ed917c7 jim-p
		}
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 edd2d8b7 smos
		/* SPAMD, set up the spamd rrd file */
681
		if (isset($config['installedpackages']['spamdsettings']) &&
682 61e047a5 Phil Davis
		    $config['installedpackages']['spamdsettings']['config'][0]['enablerrd']) {
683 edd2d8b7 smos
			/* 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 492b1314 N0YB
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
689
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
690
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
691 3e2ecafe N0YB
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
692 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
693
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
694
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
695 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
696 492b1314 N0YB
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
697
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
698
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
699 3e2ecafe N0YB
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
700 ea4bc46f Seth Mos
701
				create_new_rrd($rrdcreate);
702 cba9d7d9 Renato Botelho
				unset($rrdcreate);
703 ea4bc46f Seth Mos
			}
704
705 edd2d8b7 smos
			$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 ea4bc46f Seth Mos
710 edd2d8b7 smos
		}
711 ea4bc46f Seth Mos
		/* End System statistics */
712
713 20413b72 Warren Baker
		/* Captive Portal statistics, set up the rrd file */
714 61e047a5 Phil Davis
		if (is_array($config['captiveportal'])) {
715 cba9d7d9 Renato Botelho
			foreach ($config['captiveportal'] as $cpkey => $cp) {
716 61e047a5 Phil Davis
				if (!isset($cp['enable'])) {
717 cba9d7d9 Renato Botelho
					continue;
718 61e047a5 Phil Davis
				}
719 cba9d7d9 Renato Botelho
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 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
726
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
727
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
728 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
729 492b1314 N0YB
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
730
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
731
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
732 3e2ecafe N0YB
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
733 492b1314 N0YB
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
734
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
735
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
736 3e2ecafe N0YB
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
737 492b1314 N0YB
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
738
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
739
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
740 3e2ecafe N0YB
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
741 20413b72 Warren Baker
742 cba9d7d9 Renato Botelho
					create_new_rrd($rrdcreate);
743
					unset($rrdcreate);
744
				}
745 20413b72 Warren Baker
746 cba9d7d9 Renato Botelho
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
747 61e047a5 Phil Davis
				if (platform_booting()) {
748 cba9d7d9 Renato Botelho
					mwexec("$rrdtool update $concurrent_filename N:U");
749
				}
750 5e5d5abc Renato Botelho
751 cba9d7d9 Renato Botelho
				/* the Captive Portal stats gathering function. */
752
				$rrdupdatesh .= "\n";
753
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
754 9e378421 jim-p
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
755 cba9d7d9 Renato Botelho
				$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 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
762
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
763
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
764 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
765 492b1314 N0YB
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
766
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
767
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
768 3e2ecafe N0YB
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
769 492b1314 N0YB
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
770
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
771
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
772 3e2ecafe N0YB
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
773 492b1314 N0YB
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
774
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
775
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
776 3e2ecafe N0YB
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
777 20413b72 Warren Baker
778 cba9d7d9 Renato Botelho
					create_new_rrd($rrdcreate);
779
					unset($rrdcreate);
780
				}
781 20413b72 Warren Baker
782 cba9d7d9 Renato Botelho
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
783 61e047a5 Phil Davis
				if (platform_booting()) {
784 cba9d7d9 Renato Botelho
					mwexec("$rrdtool update $loggedin_filename N:U");
785
				}
786 20413b72 Warren Baker
787 cba9d7d9 Renato Botelho
				/* the Captive Portal stats gathering function. */
788
				$rrdupdatesh .= "\n";
789
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
790 9e378421 jim-p
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
791 cba9d7d9 Renato Botelho
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
792 7e5f3edb Warren Baker
793 cba9d7d9 Renato Botelho
			}
794 20413b72 Warren Baker
		}
795 1f3eff9b nagyrobi
		/* 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 a88376d9 nagyrobi
			if (!file_exists("$rrddbpath$ntpd")) {
801 1f3eff9b nagyrobi
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
802 da8b271a David Wood
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:-1000:1000 ";
803 1f3eff9b nagyrobi
				$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 61e047a5 Phil Davis
			if (platform_booting()) {
827
				mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
828 1f3eff9b nagyrobi
			}
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 4e322e2c Phil Davis
846 89f3ec04 heper
		/* Start dhcpd statistics */
847 ec5b9e96 Chris Buechler
		if (is_array($config['dhcpd'])) {
848
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
849 4e322e2c Phil Davis
				if (isset($config['dhcpd'][$dhcpif]['statsgraph'])) {
850 ec5b9e96 Chris Buechler
					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 89f3ec04 heper
871 ec5b9e96 Chris Buechler
					/* enter UNKNOWN values in the RRD so it knows we rebooted. */
872
					if (platform_booting()) {
873 3401dbe2 heper
						mwexec("$rrdtool update $rrddbpath$dhcpif$dhcpd N:U:U:U");
874 ec5b9e96 Chris Buechler
					}
875 89f3ec04 heper
876 ec5b9e96 Chris Buechler
					$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 4e322e2c Phil Davis
881 ec5b9e96 Chris Buechler
				}
882 89f3ec04 heper
			}
883
		}
884
		/* END dhcpd statistics */
885 ec51a222 thompsa
886 c53acd0f Renato Botelho
		/* 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 73e3aa21 Renato Botelho
895 9f268ac0 Denny Page
	t=\$(/usr/bin/nc -U \$sock)
896 0e1eef54 Denny Page
	if [ -z "\$t" ]; then
897 73e3aa21 Renato Botelho
		continue
898
	fi
899
900 0e1eef54 Denny Page
	gw=\$(echo "\$t" | awk '{ print \$1 }')
901
	delay=\$(echo "\$t" | awk '{ print \$2 }')
902 0b3613ef Denny Page
	stddev=\$(echo "\$t" | awk '{ print \$3 }')
903 0e1eef54 Denny Page
	loss=\$(echo "\$t" | awk '{ print \$4 }')
904 c53acd0f Renato Botelho
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 4fa580c2 Renato Botelho
	else
911 515887c3 NOYB
		# Convert delay from microseconds to seconds
912 834b97f1 Renato Botelho
		delay=\$(echo "scale=7; \$delay / 1000 / 1000" | /usr/bin/bc)
913 c53acd0f Renato Botelho
	fi
914 0b3613ef Denny Page
	if echo "\$stddev" | grep -Eqv '^[0-9]+\$'; then
915
		stddev="U"
916
	else
917 515887c3 NOYB
		# Convert stddev from microseconds to seconds
918 0b3613ef Denny Page
		stddev=\$(echo "scale=7; \$stddev / 1000 / 1000" | /usr/bin/bc)
919
	fi
920 c53acd0f Renato Botelho
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 0b3613ef Denny Page
		DS:stddev:GAUGE:120:0:100000 \\
926 c53acd0f Renato Botelho
		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 0b3613ef Denny Page
		{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:U:U:U
932 c53acd0f Renato Botelho
	fi
933
934 0b3613ef Denny Page
	{$rrdtool} update {$rrddbpath}\$gw-quality.rrd -t loss:delay:stddev N:\$loss:\$delay:\$stddev
935 c53acd0f Renato Botelho
done
936
937
EOD;
938
		/* End gateway quality */
939
940 ea4bc46f Seth Mos
		$rrdupdatesh .= "sleep 60\n";
941
		$rrdupdatesh .= "done\n";
942 9eda6186 Carlos Eduardo Ramos
		log_error(gettext("Creating rrd update script"));
943 ea4bc46f Seth Mos
		/* 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 cba9d7d9 Renato Botelho
		unset($rrdupdatesh);
950
951 ea4bc46f Seth Mos
		/* 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 25cca20b jim-p
	$databases = glob("{$rrddbpath}/*.rrd");
963 61e047a5 Phil Davis
	foreach ($databases as $database) {
964 8e23a630 Chris Buechler
		if (file_exists($database)) {
965
			chown($database, "nobody");
966
		}
967 25cca20b jim-p
	}
968
969 61e047a5 Phil Davis
	if (platform_booting()) {
970 9eda6186 Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
971 61e047a5 Phil Davis
	}
972 5e5d5abc Renato Botelho
973 ea4bc46f Seth Mos
}
974
975 ab7658da Seth Mos
function kill_traffic_collector() {
976 3d54aa10 Renato Botelho
	global $g;
977
978
	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
979 ab7658da Seth Mos
}
980
981 9bc8b6b6 Seth Mos
?>