Project

General

Profile

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