Project

General

Profile

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