Project

General

Profile

Download (36.9 KB) Statistics
| Branch: | Tag: | Revision:
1 b0a5b824 Seth Mos
<?php
2
/* $Id$ */
3
/*
4 6317d31d Phil Davis
	rrd.inc
5
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>
6
	All rights reserved.
7
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28 b0a5b824 Seth Mos
29
  */
30
31 523855b0 Scott Ullrich
/*
32
	pfSense_BUILDER_BINARIES:	/bin/rm	/usr/bin/nice	/usr/local/bin/rrdtool	/bin/cd
33
	pfSense_MODULE:	rrd
34
*/
35
36 b0a5b824 Seth Mos
/* include all configuration functions */
37
38
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
39 d9acea75 Scott Ullrich
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
40 552f5a6a Renato Botelho
	unlink_if_exists($xmldumpfile);
41 1b25fc27 Seth Mos
42 873c1701 Renato Botelho
	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
43 b0a5b824 Seth Mos
	if ($dumpret <> 0) {
44
		$dumpout = implode(" ", $dumpout);
45 addc0439 Renato Botelho
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
46 b0a5b824 Seth Mos
	}
47
	return($dumpret);
48
}
49
50 8bdb6879 Darren Embry
function restore_rrd() {
51 dc21d4d5 Renato Botelho
	global $g, $config;
52 8bdb6879 Darren Embry
53
	$rrddbpath = "/var/db/rrd/";
54
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
55
56
	$rrdrestore = "";
57
	$rrdreturn = "";
58 05b7eef9 Chris Buechler
	if (file_exists("{$g['cf_conf_path']}/rrd.tgz") && (isset($config['system']['use_mfs_tmpvar']) || $g['platform'] != "pfSense")) {
59 8bdb6879 Darren Embry
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
60 2159494f Ermal
			@unlink($xml_file);
61 8bdb6879 Darren Embry
		}
62 7966b0df Ermal LUÇI
		unset($rrdrestore);
63
		$_gb = exec("cd /;LANG=C /usr/bin/tar -tf {$g['cf_conf_path']}/rrd.tgz", $rrdrestore, $rrdreturn);
64 8bdb6879 Darren Embry
		if($rrdreturn != 0) {
65
			log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
66 7966b0df Ermal LUÇI
			return;
67 8bdb6879 Darren Embry
		}
68 7966b0df Ermal LUÇI
		foreach ($rrdrestore as $xml_file) {
69
			$rrd_file = '/' . substr($xml_file, 0, -4) . '.rrd';
70
			if (file_exists("{$rrd_file}"))
71 2159494f Ermal
				@unlink($rrd_file);
72 7966b0df Ermal LUÇI
			file_put_contents("{$g['tmp_path']}/rrd_restore", $xml_file);
73
			$_gb = exec("cd /;LANG=C /usr/bin/tar -xf {$g['cf_conf_path']}/rrd.tgz -T {$g['tmp_path']}/rrd_restore");
74
			if (!file_exists("/{$xml_file}")) {
75
				log_error("Could not extract {$xml_file} RRD xml file from archive!");
76
				continue;
77 6a7b35ea Phil Davis
			}
78 7966b0df Ermal LUÇI
			$_gb = exec("$rrdtool restore -f '/{$xml_file}' '{$rrd_file}'", $output, $status);
79 5d51f00e Darren Embry
			if ($status) {
80
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
81
				continue;
82
			}
83 2159494f Ermal
			unset($output);
84 9c97e4b8 Phil Davis
			@unlink("/{$xml_file}");
85 8bdb6879 Darren Embry
		}
86 7b9dfd6b Ermal LUÇI
		unset($rrdrestore);
87
		@unlink("{$g['tmp_path']}/rrd_restore");
88 e92e83d4 jim-p
		/* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */
89
		if (($g['platform'] == "pfSense") && !isset($config['system']['use_mfs_tmpvar'])) {
90
			unlink_if_exists("{$g['cf_conf_path']}/rrd.tgz");
91
		}
92 8bdb6879 Darren Embry
		return true;
93
	}
94
	return false;
95
}
96
97 b0a5b824 Seth Mos
function create_new_rrd($rrdcreatecmd) {
98
	$rrdcreateoutput = array();
99
	$rrdcreatereturn = 0;
100 2159494f Ermal
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
101 b0a5b824 Seth Mos
	if ($rrdcreatereturn <> 0) {
102
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
103 addc0439 Renato Botelho
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
104 b0a5b824 Seth Mos
	}
105 2159494f Ermal
	unset($rrdcreateoutput);
106 b0a5b824 Seth Mos
	return $rrdcreatereturn;
107
}
108
109
function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
110 fdd20aba Scott Ullrich
	if(!file_exists("/tmp/rrd_notice_sent.txt")) {
111 2159494f Ermal
		$_gb = exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
112
		@touch("/tmp/rrd_notice_sent.txt");
113 fdd20aba Scott Ullrich
	}
114 b0a5b824 Seth Mos
	$numrraold = count($rrdoldxml['rra']);
115
	$numrranew = count($rrdnewxml['rra']);
116 2159494f Ermal
	$numdsold = count($rrdoldxml['ds']);
117 b0a5b824 Seth Mos
	$numdsnew = count($rrdnewxml['ds']);
118 addc0439 Renato Botelho
	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));
119 5e5d5abc Renato Botelho
120 b0a5b824 Seth Mos
	/* add data sources not found in the old array from the new array */
121
	$i = 0;
122
	foreach($rrdnewxml['ds'] as $ds) {
123
		if(!is_array($rrdoldxml['ds'][$i])) {
124
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
125 d9e896fa Seth Mos
			/* set unknown values to 0 */
126
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
127
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
128
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
129 b0a5b824 Seth Mos
		}
130
		$i++;
131
	}
132
133
	$i = 0;
134
	$rracountold = count($rrdoldxml['rra']);
135
	$rracountnew = count($rrdnewxml['rra']);
136
	/* process each RRA, which contain a database */
137
	foreach($rrdnewxml['rra'] as $rra) {
138
		if(!is_array($rrdoldxml['rra'][$i])) {
139
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
140
		}
141
142
		$d = 0;
143
		/* process cdp_prep */
144
		$cdp_prep = $rra['cdp_prep'];
145
		foreach($cdp_prep['ds'] as $ds) {
146
			if(!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
147
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
148 d9e896fa Seth Mos
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
149
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
150
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
151
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
152 b0a5b824 Seth Mos
			}
153
			$d++;
154
		}
155
156
		/* process database */
157
		$rows = $rra['database'];
158
		$k = 0;
159
		$rowcountold = count($rrdoldxml['rra'][$i]['database']['row']);
160
		$rowcountnew = count($rrdnewxml['rra'][$i]['database']['row']);
161 ddc26847 Seth Mos
		$rowcountdiff = $rowcountnew - $rowcountold;
162
		/* save old rows for a bit before we put the required empty rows before it */
163
		$rowsdata = $rows;
164
		$rowsempty = array();
165
		$r = 0;
166 ae4dce62 Seth Mos
		while($r < $rowcountdiff) {
167 ddc26847 Seth Mos
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
168
			$r++;
169
		}
170
		$rows = $rowsempty + $rowsdata;
171 b0a5b824 Seth Mos
		/* now foreach the rows in the database */
172
		foreach($rows['row'] as $row) {
173
			if(!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
174
				$rrdoldxml['rra'][$i]['database']['row'][$k] = $rrdnewxml['rra'][$i]['database']['row'][$k];
175
			}
176
			$m = 0;
177
			$vcountold = count($rrdoldxml['rra'][$i]['database']['row'][$k]['v']);
178
			$vcountnew = count($rrdnewxml['rra'][$i]['database']['row'][$k]['v']);
179
			foreach($row['v'] as $value) {
180
				if(empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
181 ed3ea464 Seth Mos
					if(isset($valid)) {
182
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = "0.0000000000e+00 ";
183
					} else {
184
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = $rrdnewxml['rra'][$i]['database']['row'][$k]['v'][$m];
185
					}
186
				} else {
187
					if($value <> " NaN ") {
188
						$valid = true;
189
					} else {
190
						$valid = false;
191
					}
192 b0a5b824 Seth Mos
				}
193
				$m++;
194
			}
195
			$k++;
196
		}
197
		$i++;
198
	}
199
200
	$numrranew = count($rrdoldxml['rra']);
201
	$numdsnew = count($rrdoldxml['ds']);
202 addc0439 Renato Botelho
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
203 b0a5b824 Seth Mos
	return $rrdoldxml;
204
}
205
206 ea4bc46f Seth Mos
function enable_rrd_graphing() {
207
	global $config, $g, $altq_list_queues;
208
209 285ef132 Ermal LUÇI
	if(platform_booting())
210 9eda6186 Carlos Eduardo Ramos
		echo gettext("Generating RRD graphs...");
211 ea4bc46f Seth Mos
212
	$rrddbpath = "/var/db/rrd/";
213
	$rrdgraphpath = "/usr/local/www/rrd";
214
215
	$traffic = "-traffic.rrd";
216
	$packets = "-packets.rrd";
217
	$states = "-states.rrd";
218
	$wireless = "-wireless.rrd";
219
	$queues = "-queues.rrd";
220
	$queuesdrop = "-queuedrops.rrd";
221
	$spamd = "-spamd.rrd";
222
	$proc = "-processor.rrd";
223
	$mem = "-memory.rrd";
224 3ed917c7 jim-p
	$mbuf = "-mbuf.rrd";
225 ec51a222 thompsa
	$cellular = "-cellular.rrd";
226 edd2d8b7 smos
	$vpnusers = "-vpnusers.rrd";
227 20413b72 Warren Baker
	$captiveportalconcurrent = "-concurrent.rrd";
228
	$captiveportalloggedin = "-loggedin.rrd";
229 1f3eff9b nagyrobi
	$ntpd = "ntpd.rrd";
230 ea4bc46f Seth Mos
231 d9acea75 Scott Ullrich
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
232 ea4bc46f Seth Mos
	$netstat = "/usr/bin/netstat";
233
	$awk = "/usr/bin/awk";
234
	$tar = "/usr/bin/tar";
235
	$pfctl = "/sbin/pfctl";
236
	$sysctl = "/sbin/sysctl";
237
	$php = "/usr/local/bin/php";
238 e9e295f7 jim-p
	$cpustats = "/usr/local/sbin/cpustats";
239 ea4bc46f Seth Mos
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
240
	$ifconfig = "/sbin/ifconfig";
241 20413b72 Warren Baker
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
242 617f9edf nagyrobi
	$ntpq = "/usr/local/sbin/ntpq";
243 ea4bc46f Seth Mos
244
	$rrdtrafficinterval = 60;
245
	$rrdwirelessinterval = 60;
246
	$rrdqueuesinterval = 60;
247
	$rrdqueuesdropinterval = 60;
248
	$rrdpacketsinterval = 60;
249
	$rrdstatesinterval = 60;
250
	$rrdspamdinterval = 60;
251
	$rrdlbpoolinterval = 60;
252
	$rrdprocinterval = 60;
253
	$rrdmeminterval = 60;
254 3ed917c7 jim-p
	$rrdmbufinterval = 60;
255 ec51a222 thompsa
	$rrdcellularinterval = 60;
256 55c08a96 smos
	$rrdvpninterval = 60;
257 20413b72 Warren Baker
	$rrdcaptiveportalinterval = 60;
258 1f3eff9b nagyrobi
	$rrdntpdinterval = 60;
259 ea4bc46f Seth Mos
260
	$trafficvalid = $rrdtrafficinterval * 2;
261
	$wirelessvalid = $rrdwirelessinterval * 2;
262
	$queuesvalid = $rrdqueuesinterval * 2;
263
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
264
	$packetsvalid = $rrdpacketsinterval * 2;
265
	$statesvalid = $rrdstatesinterval*2;
266
	$spamdvalid = $rrdspamdinterval * 2;
267
	$lbpoolvalid = $rrdlbpoolinterval * 2;
268
	$procvalid = $rrdlbpoolinterval * 2;
269
	$memvalid = $rrdmeminterval * 2;
270 3ed917c7 jim-p
	$mbufvalid = $rrdmbufinterval * 2;
271 ec51a222 thompsa
	$cellularvalid = $rrdcellularinterval * 2;
272 edd2d8b7 smos
	$vpnvalid = $rrdvpninterval * 2;
273 20413b72 Warren Baker
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
274 1f3eff9b nagyrobi
	$ntpdvalid = $rrdntpdinterval * 2;
275 ea4bc46f Seth Mos
276 fa3b33a5 Renato Botelho
	/* Assume 2*10GigE for now */
277
	$downstream = 2500000000;
278
	$upstream = 2500000000;
279 ea4bc46f Seth Mos
280
	/* read the shaper config */
281
	read_altq_config();
282
283
	if (isset ($config['rrd']['enable'])) {
284
285
		/* create directory if needed */
286 3d83f02e jim-p
		if (!is_dir($rrddbpath)) {
287
			mkdir($rrddbpath, 0775);
288 ea4bc46f Seth Mos
		}
289 25cca20b jim-p
		chown($rrddbpath, "nobody");
290 ea4bc46f Seth Mos
291 285ef132 Ermal LUÇI
		if (platform_booting()) {
292 e92e83d4 jim-p
			restore_rrd();
293 ea4bc46f Seth Mos
		}
294
295
		/* db update script */
296
		$rrdupdatesh = "#!/bin/sh\n";
297
		$rrdupdatesh .= "\n";
298 a555cc58 Seth Mos
		$rrdupdatesh .= "export TERM=dumb\n";
299 3d54aa10 Renato Botelho
		$rrdupdatesh .= "\n";
300
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
301
		$rrdupdatesh .= "\n";
302 ea4bc46f Seth Mos
		$rrdupdatesh .= "counter=1\n";
303
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
304
		$rrdupdatesh .= "do\n";
305
		$rrdupdatesh .= "";
306
307
		$i = 0;
308 4563d12f Seth Mos
		$ifdescrs = get_configured_interface_with_descr();
309 edd2d8b7 smos
		/* IPsec counters */
310 4563d12f Seth Mos
		$ifdescrs['ipsec'] = "IPsec";
311 edd2d8b7 smos
		/* OpenVPN server counters */
312 55c08a96 smos
		if(is_array($config['openvpn']['openvpn-server'])) {
313
			foreach($config['openvpn']['openvpn-server'] as $server) {
314
				$serverid = "ovpns" . $server['vpnid'];
315
				$ifdescrs[$serverid] = "{$server['description']}";
316
			}
317
		}
318 4563d12f Seth Mos
319 f72ea82a Ermal LUÇI
		if (platform_booting()) {
320
			if (!is_dir("{$g['vardb_path']}/rrd"))
321
				mkdir("{$g['vardb_path']}/rrd", 0775);
322
323
			@chown("{$g['vardb_path']}/rrd", "nobody");
324
		}
325
326 edd2d8b7 smos
		/* process all real and pseudo interfaces */
327 4563d12f Seth Mos
		foreach ($ifdescrs as $ifname => $ifdescr) {
328 65615d89 Seth Mos
			$temp = get_real_interface($ifname);
329
			if($temp <> "") {
330
				$realif = $temp;
331
			}
332 ea4bc46f Seth Mos
333
			/* TRAFFIC, set up the rrd file */
334
			if (!file_exists("$rrddbpath$ifname$traffic")) {
335
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
336
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
337
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
338
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
339
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
340 9bc8b6b6 Seth Mos
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
341
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
342
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
343
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
344 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
345
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
346
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
347 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
348 ea4bc46f Seth Mos
349
				create_new_rrd($rrdcreate);
350 cba9d7d9 Renato Botelho
				unset($rrdcreate);
351 ea4bc46f Seth Mos
			}
352
353
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
354 285ef132 Ermal LUÇI
			if(platform_booting()) {
355 9bc8b6b6 Seth Mos
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
356 ea4bc46f Seth Mos
			}
357
358
			$rrdupdatesh .= "\n";
359 9bc8b6b6 Seth Mos
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
360 a555cc58 Seth Mos
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
361
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
362
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
363
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
364
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
365 ea4bc46f Seth Mos
366
			/* PACKETS, set up the rrd file */
367
			if (!file_exists("$rrddbpath$ifname$packets")) {
368
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
369
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
370
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
371
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
372
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
373 9bc8b6b6 Seth Mos
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
374
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
375
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
376
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
377 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
378
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
379
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
380 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
381 ea4bc46f Seth Mos
382
				create_new_rrd($rrdcreate);
383 cba9d7d9 Renato Botelho
				unset($rrdcreate);
384 ea4bc46f Seth Mos
			}
385
386
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
387 285ef132 Ermal LUÇI
			if(platform_booting()) {
388 9bc8b6b6 Seth Mos
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
389 ea4bc46f Seth Mos
			}
390
391
			$rrdupdatesh .= "\n";
392
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
393 a555cc58 Seth Mos
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
394
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
395
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
396
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
397
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";
398 ea4bc46f Seth Mos
399
			/* WIRELESS, set up the rrd file */
400
			if($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
401
				if (!file_exists("$rrddbpath$ifname$wireless")) {
402
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
403
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
404
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
405
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
406 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
407
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
408
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
409 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
410 5e5d5abc Renato Botelho
411 ea4bc46f Seth Mos
					create_new_rrd($rrdcreate);
412 cba9d7d9 Renato Botelho
					unset($rrdcreate);
413 ea4bc46f Seth Mos
				}
414
415
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
416 285ef132 Ermal LUÇI
				if(platform_booting()) {
417 e256e9d4 smos
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
418 ea4bc46f Seth Mos
				}
419
420
				$rrdupdatesh .= "\n";
421
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
422 e9e295f7 jim-p
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
423 488595df smos
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
424 ea4bc46f Seth Mos
			}
425
426 edd2d8b7 smos
			/* OpenVPN, set up the rrd file */
427
			if(stristr($ifname, "ovpns")) {
428
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
429
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
430
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
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 5e5d5abc Renato Botelho
436 edd2d8b7 smos
					create_new_rrd($rrdcreate);
437 cba9d7d9 Renato Botelho
					unset($rrdcreate);
438 edd2d8b7 smos
				}
439
440
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
441 285ef132 Ermal LUÇI
				if(platform_booting()) {
442 edd2d8b7 smos
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
443
				}
444 ea4bc46f Seth Mos
445 edd2d8b7 smos
				if(is_array($config['openvpn']['openvpn-server'])) {
446
					foreach($config['openvpn']['openvpn-server'] as $server) {
447
						if("ovpns{$server['vpnid']}" == $ifname) {
448
							$port = $server['local_port'];
449 5f250a24 smos
							$vpnid = $server['vpnid'];
450 edd2d8b7 smos
						}
451
					}
452
				}
453
				$rrdupdatesh .= "\n";
454
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
455
				$rrdupdatesh .= "list_current_users() {\n";
456
				$rrdupdatesh .= " sleep 0.2\n";
457
				$rrdupdatesh .= " echo \"status 2\"\n";
458
				$rrdupdatesh .= " sleep 0.2\n";
459
				$rrdupdatesh .= " echo \"quit\"\n";
460
				$rrdupdatesh .= "}\n";
461 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";
462 e9e295f7 jim-p
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
463 edd2d8b7 smos
			}
464 ea4bc46f Seth Mos
465 edd2d8b7 smos
			/* QUEUES, set up the queues databases */
466
			if ($altq_list_queues[$ifname]) {
467
				$altq =& $altq_list_queues[$ifname];
468
				/* NOTE: Is it worth as its own function?! */
469
				switch ($altq->GetBwscale()) {
470
					case "Gb":
471
						$factor = 1024 * 1024 * 1024;
472
							break;
473
					case "Mb":
474
							$factor = 1024 * 1024;
475
							break;
476
					case "Kb":
477
							$factor = 1024;
478
							break;
479
					case "b":
480
					default:
481
							$factor = 1;
482
							break;
483
				}
484
				$qbandwidth = $altq->GetBandwidth() * $factor;
485
				if ($qbandwidth <=0) {
486
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
487
				}
488
				$qlist =& $altq->get_queue_list($notused);
489
				if (!file_exists("$rrddbpath$ifname$queues")) {
490
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
491
					/* loop list of shaper queues */
492
					$q = 0;
493
					foreach ($qlist as $qname => $q) {
494
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
495 ea4bc46f Seth Mos
					}
496
497 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
498
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
499
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
500 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
501 ea4bc46f Seth Mos
502 edd2d8b7 smos
					create_new_rrd($rrdcreate);
503 cba9d7d9 Renato Botelho
					unset($rrdcreate);
504 edd2d8b7 smos
				}
505 ea4bc46f Seth Mos
506 edd2d8b7 smos
				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
507
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
508
					/* loop list of shaper queues */
509
					$q = 0;
510
					foreach ($qlist as $qname => $q) {
511
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
512 ea4bc46f Seth Mos
					}
513
514 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
515
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
516
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
517 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
518 edd2d8b7 smos
519
					create_new_rrd($rrdcreate);
520 cba9d7d9 Renato Botelho
					unset($rrdcreate);
521 edd2d8b7 smos
				}
522
523 285ef132 Ermal LUÇI
				if(platform_booting()) {
524 edd2d8b7 smos
					$rrdqcommand = "-t ";
525
					$rrducommand = "N";
526 857a4a79 jim-p
					$qi = 0;
527 edd2d8b7 smos
					foreach ($qlist as $qname => $q) {
528 857a4a79 jim-p
						if($qi == 0) {
529 edd2d8b7 smos
							$rrdqcommand .= "{$qname}";
530
						} else {
531
							$rrdqcommand .= ":{$qname}";
532 ea4bc46f Seth Mos
						}
533 857a4a79 jim-p
						$qi++;
534 edd2d8b7 smos
						$rrducommand .= ":U";
535 ea4bc46f Seth Mos
					}
536 edd2d8b7 smos
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
537
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
538 ea4bc46f Seth Mos
				}
539 edd2d8b7 smos
540
				/* awk function to gather shaper data */
541
				/* yes, it's special */
542
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
543
				$rrdupdatesh .= "{ ";
544
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
545
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
546
				$rrdupdatesh .= " q=1; ";
547
				$rrdupdatesh .= "} ";
548
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
549
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
550
				$rrdupdatesh .= " q=0; ";
551
				$rrdupdatesh .= "} ";
552
				$rrdupdatesh .= "} END { ";
553
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
554
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
555
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
556
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
557
558
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
559
				$rrdupdatesh .= "{ ";
560
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
561
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
562
				$rrdupdatesh .= " q=1; ";
563
				$rrdupdatesh .= "} ";
564
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
565
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
566
				$rrdupdatesh .= " q=0; ";
567
				$rrdupdatesh .= "} ";
568
				$rrdupdatesh .= "} END { ";
569
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
570
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
571
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
572
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
573
			}
574 5e589685 smos
575
			/* 3G interfaces */
576
			if(preg_match("/ppp[0-9]+/i", $realif))	{
577
				if (!file_exists("$rrddbpath$ifname$cellular")) {
578
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
579
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
580
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
581
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
582 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
583
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
584
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
585 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
586 5e589685 smos
					create_new_rrd($rrdcreate);
587 cba9d7d9 Renato Botelho
					unset($rrdcreate);
588 5e589685 smos
				}
589
590
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
591 285ef132 Ermal LUÇI
				if(platform_booting()) {
592 5e589685 smos
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
593
				}
594
595
				$rrdupdatesh .= "\n";
596
				$rrdupdatesh .= "# polling 3G\n";
597 2b095a33 smos
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
598
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
599 5e589685 smos
			}
600
601 ea4bc46f Seth Mos
		}
602
		$i++;
603
604
		/* System only statistics */
605
		$ifname = "system";
606
607 edd2d8b7 smos
		/* STATES, create pf states database */
608
		if(! file_exists("$rrddbpath$ifname$states")) {
609
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
610
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
611
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
612
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
613
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
614
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
615 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
616
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
617
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
618 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
619 edd2d8b7 smos
620
			create_new_rrd($rrdcreate);
621 cba9d7d9 Renato Botelho
			unset($rrdcreate);
622 edd2d8b7 smos
		}
623 ea4bc46f Seth Mos
624 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
625 285ef132 Ermal LUÇI
		if(platform_booting()) {
626 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
627
		}
628 ea4bc46f Seth Mos
629 edd2d8b7 smos
		/* the pf states gathering function. */
630
		$rrdupdatesh .= "\n";
631
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
632
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
633
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
634
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
635
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
636
		$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";
637
		$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";
638
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
639
640
		/* End pf states statistics */
641
642
		/* CPU, create CPU statistics database */
643
		if(! file_exists("$rrddbpath$ifname$proc")) {
644
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
645
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
646
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
647
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
648
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
649
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
650 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
651
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
652
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
653 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
654 edd2d8b7 smos
655
			create_new_rrd($rrdcreate);
656 cba9d7d9 Renato Botelho
			unset($rrdcreate);
657 edd2d8b7 smos
		}
658 ea4bc46f Seth Mos
659 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
660 285ef132 Ermal LUÇI
		if(platform_booting()) {
661 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
662
		}
663 ea4bc46f Seth Mos
664 edd2d8b7 smos
		/* the CPU stats gathering function. */
665 e9e295f7 jim-p
		$rrdupdatesh .= "CPU=`$cpustats | cut -f1-4 -d':'`\n";
666
		/* Using ps uxaH will count all processes including system threads. Top was undercounting. */
667
		$rrdupdatesh .= "PROCS=`ps uxaH | wc -l | awk '{print \$1;}'`\n";
668
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$proc N:\${CPU}:\${PROCS}\n";
669 edd2d8b7 smos
670
		/* End CPU statistics */
671
672
		/* Memory, create Memory statistics database */
673
		if(! file_exists("$rrddbpath$ifname$mem")) {
674
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
675
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
676
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
677
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
678
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
679
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
680 492b1314 N0YB
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
681
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
682
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
683 3e2ecafe N0YB
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
684 492b1314 N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
685
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
686
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
687 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
688 492b1314 N0YB
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
689
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
690
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
691 3e2ecafe N0YB
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
692 edd2d8b7 smos
693
			create_new_rrd($rrdcreate);
694 cba9d7d9 Renato Botelho
			unset($rrdcreate);
695 edd2d8b7 smos
		}
696 ea4bc46f Seth Mos
697 edd2d8b7 smos
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
698 285ef132 Ermal LUÇI
		if(platform_booting()) {
699 edd2d8b7 smos
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
700
		}
701 ea4bc46f Seth Mos
702 edd2d8b7 smos
		/* the Memory stats gathering function. */
703 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 | ";
704
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
705
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
706
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";
707 5e5d5abc Renato Botelho
708 edd2d8b7 smos
		/* End Memory statistics */
709
710 3ed917c7 jim-p
		/* mbuf, create mbuf statistics database */
711
		if(! file_exists("$rrddbpath$ifname$mbuf")) {
712
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
713
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
714
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
715
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
716
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
717
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
718
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
719
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
720 3e2ecafe N0YB
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
721 3ed917c7 jim-p
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
722
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
723
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
724 3e2ecafe N0YB
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
725 3ed917c7 jim-p
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
726
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
727
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
728 3e2ecafe N0YB
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";
729 3ed917c7 jim-p
730
			create_new_rrd($rrdcreate);
731
			unset($rrdcreate);
732
		}
733
734
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
735 285ef132 Ermal LUÇI
		if(platform_booting()) {
736 06ff126e N0YB
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
737 3ed917c7 jim-p
		}
738
739
		/* the mbuf stats gathering function. */
740
		$rrdupdatesh .= "MBUF=`$netstat -m | ";
741
		$rrdupdatesh .= " $awk '/mbuf clusters in use/ { gsub(/\//, \":\", $1); print $1; }'`\n";
742
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mbuf N:\${MBUF}\n";
743
744
		/* End mbuf statistics */
745
746 edd2d8b7 smos
		/* SPAMD, set up the spamd rrd file */
747
		if (isset($config['installedpackages']['spamdsettings']) &&
748 0ad7bcd8 Erik Fonnesbeck
			 $config['installedpackages']['spamdsettings']['config'][0]['enablerrd']) {
749 edd2d8b7 smos
			/* set up the spamd rrd file */
750
			if (!file_exists("$rrddbpath$ifname$spamd")) {
751
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
752
				$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
753
				$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
754 492b1314 N0YB
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
755
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
756
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
757 3e2ecafe N0YB
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
758 492b1314 N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
759
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
760
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
761 3e2ecafe N0YB
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
762 492b1314 N0YB
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
763
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
764
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
765 3e2ecafe N0YB
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
766 ea4bc46f Seth Mos
767
				create_new_rrd($rrdcreate);
768 cba9d7d9 Renato Botelho
				unset($rrdcreate);
769 ea4bc46f Seth Mos
			}
770
771 edd2d8b7 smos
			$rrdupdatesh .= "\n";
772
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
773
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
774
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";
775 ea4bc46f Seth Mos
776 edd2d8b7 smos
		}
777 ea4bc46f Seth Mos
		/* End System statistics */
778
779 20413b72 Warren Baker
		/* Captive Portal statistics, set up the rrd file */
780 cba9d7d9 Renato Botelho
		if(is_array($config['captiveportal'])) {
781
			foreach ($config['captiveportal'] as $cpkey => $cp) {
782
				if (!isset($cp['enable']))
783
					continue;
784
785
				$ifname= "captiveportal";
786
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
787
				if (!file_exists("$concurrent_filename")) {
788
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
789
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
790 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
791
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
792
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
793 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
794 492b1314 N0YB
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
795
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
796
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
797 3e2ecafe N0YB
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
798 492b1314 N0YB
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
799
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
800
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
801 3e2ecafe N0YB
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
802 492b1314 N0YB
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
803
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
804
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
805 3e2ecafe N0YB
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
806 20413b72 Warren Baker
807 cba9d7d9 Renato Botelho
					create_new_rrd($rrdcreate);
808
					unset($rrdcreate);
809
				}
810 20413b72 Warren Baker
811 cba9d7d9 Renato Botelho
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
812 285ef132 Ermal LUÇI
				if(platform_booting()) {
813 cba9d7d9 Renato Botelho
					mwexec("$rrdtool update $concurrent_filename N:U");
814
				}
815 5e5d5abc Renato Botelho
816 cba9d7d9 Renato Botelho
				/* the Captive Portal stats gathering function. */
817
				$rrdupdatesh .= "\n";
818
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
819 9e378421 jim-p
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
820 cba9d7d9 Renato Botelho
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";
821
822
				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
823
				if (!file_exists("$loggedin_filename")) {
824
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
825
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
826 492b1314 N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
827
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
828
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
829 3e2ecafe N0YB
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
830 492b1314 N0YB
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
831
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
832
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
833 3e2ecafe N0YB
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
834 492b1314 N0YB
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
835
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
836
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
837 3e2ecafe N0YB
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
838 492b1314 N0YB
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
839
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
840
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
841 3e2ecafe N0YB
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";
842 20413b72 Warren Baker
843 cba9d7d9 Renato Botelho
					create_new_rrd($rrdcreate);
844
					unset($rrdcreate);
845
				}
846 20413b72 Warren Baker
847 cba9d7d9 Renato Botelho
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
848 285ef132 Ermal LUÇI
				if(platform_booting()) {
849 cba9d7d9 Renato Botelho
					mwexec("$rrdtool update $loggedin_filename N:U");
850
				}
851 20413b72 Warren Baker
852 cba9d7d9 Renato Botelho
				/* the Captive Portal stats gathering function. */
853
				$rrdupdatesh .= "\n";
854
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
855 9e378421 jim-p
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
856 cba9d7d9 Renato Botelho
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";
857 7e5f3edb Warren Baker
858 cba9d7d9 Renato Botelho
			}
859 20413b72 Warren Baker
		}
860 1f3eff9b nagyrobi
		/* End Captive Portal statistics */
861
862
		/* NTP, set up the ntpd rrd file */
863
		if (isset($config['ntpd']['statsgraph'])) {
864
			/* set up the ntpd rrd file */
865 a88376d9 nagyrobi
			if (!file_exists("$rrddbpath$ntpd")) {
866 1f3eff9b nagyrobi
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
867
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:0:1000 ";
868
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
869
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
870
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
871
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:0:1000 ";
872
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
873
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
874
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
875
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
876
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
877
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
878
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
879
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
880
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
881
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
882
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
883
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
884
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
885
886
				create_new_rrd($rrdcreate);
887
				unset($rrdcreate);
888
			}
889
890
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
891 285ef132 Ermal LUÇI
			if(platform_booting()) {
892 a88376d9 nagyrobi
			mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
893 1f3eff9b nagyrobi
			}
894
895
			/* the ntp stats gathering function. */
896
			$rrdupdatesh .= "\n";
897
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
898
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
899
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
900
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
901
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
902
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
903
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
904
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
905
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
906
			$rrdupdatesh .= "\n";
907
908
		}
909
		/* End NTP statistics */
910 ec51a222 thompsa
911 ea4bc46f Seth Mos
		$rrdupdatesh .= "sleep 60\n";
912
		$rrdupdatesh .= "done\n";
913 9eda6186 Carlos Eduardo Ramos
		log_error(gettext("Creating rrd update script"));
914 ea4bc46f Seth Mos
		/* write the rrd update script */
915
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
916
		$fd = fopen("$updaterrdscript", "w");
917
		fwrite($fd, "$rrdupdatesh");
918
		fclose($fd);
919
920 cba9d7d9 Renato Botelho
		unset($rrdupdatesh);
921
922 ea4bc46f Seth Mos
		/* kill off traffic collectors */
923
		kill_traffic_collector();
924
925
		/* start traffic collector */
926
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
927
928
	} else {
929
		/* kill off traffic collectors */
930
		kill_traffic_collector();
931
	}
932
933 25cca20b jim-p
	$databases = glob("{$rrddbpath}/*.rrd");
934
	foreach($databases as $database) {
935
		chown($database, "nobody");
936
	}
937
938 285ef132 Ermal LUÇI
	if(platform_booting())
939 9eda6186 Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
940 5e5d5abc Renato Botelho
941 ea4bc46f Seth Mos
}
942
943 d55cba63 N0YB
# Create gateway quality RRD with settings suitable for pfSense graph set.
944
function create_gateway_quality_rrd($rrd_file) {
945 3f7f26ae N0YB
	global $g;
946 f72ea82a Ermal LUÇI
947 d55cba63 N0YB
	$rrdinterval = 60;
948
	$valid = $rrdinterval * 2;
949
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
950
951
	/* GATEWAY QUALITY, set up the rrd file */
952
	if (!file_exists("$rrd_file")) {
953
		$rrdcreate = "$rrdtool create $rrd_file --step $rrdinterval ";
954
		$rrdcreate .= "DS:loss:GAUGE:$valid:0:100 ";
955
		$rrdcreate .= "DS:delay:GAUGE:$valid:0:100000 ";
956
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
957
		$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
958
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
959
		$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
960
961
		create_new_rrd($rrdcreate);
962
		unset($rrdcreate);
963
	}
964
965
	/* enter UNKNOWN values in the RRD so it knows we rebooted. */
966 285ef132 Ermal LUÇI
	if(platform_booting()) {
967 f72ea82a Ermal LUÇI
		if (!is_dir("{$g['vardb_path']}/rrd"))
968
			mkdir("{$g['vardb_path']}/rrd", 0775);
969
970
		@chown("{$g['vardb_path']}/rrd", "nobody");
971
972 d55cba63 N0YB
		mwexec("$rrdtool update $rrd_file N:U:U");
973
	}
974
	unset($rrdtool, $rrdinterval, $valid, $rrd_file);
975
}
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 05b7eef9 Chris Buechler
?>