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