rrd.inc 36.2 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php

3 4 5
/*
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>
	All rights reserved.
Ad Schellevis's avatar
Ad Schellevis committed
6

7 8
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
Ad Schellevis's avatar
Ad Schellevis committed
9

10 11
	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.
Ad Schellevis's avatar
Ad Schellevis committed
12

13 14 15
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
Ad Schellevis's avatar
Ad Schellevis committed
16

17 18 19 20 21 22 23 24 25 26
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
Ad Schellevis's avatar
Ad Schellevis committed
27 28
*/

29 30 31
function dump_rrd_to_xml($rrddatabase, $xmldumpfile)
{
	$rrdtool = '/usr/local/bin/rrdtool';
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34 35 36 37 38 39

	unlink_if_exists($xmldumpfile);

	exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
	if ($dumpret <> 0) {
		$dumpout = implode(" ", $dumpout);
		log_error(sprintf(gettext('RRD dump failed exited with %1$s, the error is: %2$s'), $dumpret, $dumpout));
	}
40

Ad Schellevis's avatar
Ad Schellevis committed
41 42 43
	return($dumpret);
}

44 45
function restore_rrd()
{
Ad Schellevis's avatar
Ad Schellevis committed
46 47
	global $g, $config;

48 49
	$rrddbpath = '/var/db/rrd/';
	$rrdtool = '/usr/local/bin/rrdtool';
Ad Schellevis's avatar
Ad Schellevis committed
50 51 52

	$rrdrestore = "";
	$rrdreturn = "";
53
	if (file_exists('/conf/rrd.tgz')) {
Ad Schellevis's avatar
Ad Schellevis committed
54 55 56
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
			@unlink($xml_file);
		}
57
		$_gb = exec('cd /;LANG=C /usr/bin/tar -xzf /conf/rrd.tgz 2>&1', $rrdrestore, $rrdreturn);
Ad Schellevis's avatar
Ad Schellevis committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		$rrdrestore = implode(" ", $rrdrestore);
		if($rrdreturn != 0) {
			log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
		}
		unset($rrdrestore);
		foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
			$rrd_file = preg_replace('/\.xml$/', ".rrd", $xml_file);
			if (file_exists("{$rrd_file}")) {
				@unlink($rrd_file);
			}
			$output = array();
			$status = null;
			$_gb = exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
			if ($status) {
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
				continue;
			}
			unset($output);
			@unlink($xml_file);
		}
		/* 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. */
		if (($g['platform'] == "pfSense") && !isset($config['system']['use_mfs_tmpvar'])) {
80
			unlink_if_exists('/conf/rrd.tgz');
Ad Schellevis's avatar
Ad Schellevis committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		}
		return true;
	}
	return false;
}

function create_new_rrd($rrdcreatecmd) {
	$rrdcreateoutput = array();
	$rrdcreatereturn = 0;
	$_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
	if ($rrdcreatereturn <> 0) {
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
		log_error(sprintf(gettext('RRD create failed exited with %1$s, the error is: %2$s'), $rrdcreatereturn, $rrdcreateoutput));
	}
	unset($rrdcreateoutput);
	return $rrdcreatereturn;
}

function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
	if(!file_exists("/tmp/rrd_notice_sent.txt")) {
		$_gb = exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
		@touch("/tmp/rrd_notice_sent.txt");
	}
	$numrraold = count($rrdoldxml['rra']);
	$numrranew = count($rrdnewxml['rra']);
	$numdsold = count($rrdoldxml['ds']);
	$numdsnew = count($rrdnewxml['ds']);
	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));

	/* add data sources not found in the old array from the new array */
	$i = 0;
	foreach($rrdnewxml['ds'] as $ds) {
		if(!is_array($rrdoldxml['ds'][$i])) {
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
			/* set unknown values to 0 */
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
		}
		$i++;
	}

	$i = 0;
	$rracountold = count($rrdoldxml['rra']);
	$rracountnew = count($rrdnewxml['rra']);
	/* process each RRA, which contain a database */
	foreach($rrdnewxml['rra'] as $rra) {
		if(!is_array($rrdoldxml['rra'][$i])) {
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
		}

		$d = 0;
		/* process cdp_prep */
		$cdp_prep = $rra['cdp_prep'];
		foreach($cdp_prep['ds'] as $ds) {
			if(!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
			}
			$d++;
		}

		/* process database */
		$rows = $rra['database'];
		$k = 0;
		$rowcountold = count($rrdoldxml['rra'][$i]['database']['row']);
		$rowcountnew = count($rrdnewxml['rra'][$i]['database']['row']);
		$rowcountdiff = $rowcountnew - $rowcountold;
		/* save old rows for a bit before we put the required empty rows before it */
		$rowsdata = $rows;
		$rowsempty = array();
		$r = 0;
		while($r < $rowcountdiff) {
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
			$r++;
		}
		$rows = $rowsempty + $rowsdata;
		/* now foreach the rows in the database */
		foreach($rows['row'] as $row) {
			if(!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
				$rrdoldxml['rra'][$i]['database']['row'][$k] = $rrdnewxml['rra'][$i]['database']['row'][$k];
			}
			$m = 0;
			$vcountold = count($rrdoldxml['rra'][$i]['database']['row'][$k]['v']);
			$vcountnew = count($rrdnewxml['rra'][$i]['database']['row'][$k]['v']);
			foreach($row['v'] as $value) {
				if(empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
					if(isset($valid)) {
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = "0.0000000000e+00 ";
					} else {
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = $rrdnewxml['rra'][$i]['database']['row'][$k]['v'][$m];
					}
				} else {
					if($value <> " NaN ") {
						$valid = true;
					} else {
						$valid = false;
					}
				}
				$m++;
			}
			$k++;
		}
		$i++;
	}

	$numrranew = count($rrdoldxml['rra']);
	$numdsnew = count($rrdoldxml['ds']);
	log_error(sprintf(gettext('The new RRD now has %1$s DS values and %2$s RRA databases'), $numdsnew, $numrranew));
	return $rrdoldxml;
}

function enable_rrd_graphing() {
	global $config, $g, $altq_list_queues;

Ad Schellevis's avatar
Ad Schellevis committed
199
	if(file_exists("/var/run/booting"))
Ad Schellevis's avatar
Ad Schellevis committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		echo gettext("Generating RRD graphs...");

	$rrddbpath = "/var/db/rrd/";
	$rrdgraphpath = "/usr/local/www/rrd";

	$traffic = "-traffic.rrd";
	$packets = "-packets.rrd";
	$states = "-states.rrd";
	$wireless = "-wireless.rrd";
	$queues = "-queues.rrd";
	$queuesdrop = "-queuedrops.rrd";
	$spamd = "-spamd.rrd";
	$proc = "-processor.rrd";
	$mem = "-memory.rrd";
	$mbuf = "-mbuf.rrd";
	$cellular = "-cellular.rrd";
	$vpnusers = "-vpnusers.rrd";
	$captiveportalconcurrent = "-concurrent.rrd";
	$captiveportalloggedin = "-loggedin.rrd";
	$ntpd = "ntpd.rrd";

221
	$rrdtool = "/usr/local/bin/rrdtool";
Ad Schellevis's avatar
Ad Schellevis committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	$netstat = "/usr/bin/netstat";
	$awk = "/usr/bin/awk";
	$tar = "/usr/bin/tar";
	$pfctl = "/sbin/pfctl";
	$sysctl = "/sbin/sysctl";
	$php = "/usr/local/bin/php";
	$cpustats = "/usr/local/sbin/cpustats";
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
	$ifconfig = "/sbin/ifconfig";
	$captiveportal_gather = "/usr/local/bin/captiveportal_gather_stats.php";
	$ntpq = "/usr/local/sbin/ntpq";

	$rrdtrafficinterval = 60;
	$rrdwirelessinterval = 60;
	$rrdqueuesinterval = 60;
	$rrdqueuesdropinterval = 60;
	$rrdpacketsinterval = 60;
	$rrdstatesinterval = 60;
	$rrdspamdinterval = 60;
	$rrdlbpoolinterval = 60;
	$rrdprocinterval = 60;
	$rrdmeminterval = 60;
	$rrdmbufinterval = 60;
	$rrdcellularinterval = 60;
	$rrdvpninterval = 60;
	$rrdcaptiveportalinterval = 60;
	$rrdntpdinterval = 60;

	$trafficvalid = $rrdtrafficinterval * 2;
	$wirelessvalid = $rrdwirelessinterval * 2;
	$queuesvalid = $rrdqueuesinterval * 2;
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
	$packetsvalid = $rrdpacketsinterval * 2;
	$statesvalid = $rrdstatesinterval*2;
	$spamdvalid = $rrdspamdinterval * 2;
	$lbpoolvalid = $rrdlbpoolinterval * 2;
	$procvalid = $rrdlbpoolinterval * 2;
	$memvalid = $rrdmeminterval * 2;
	$mbufvalid = $rrdmbufinterval * 2;
	$cellularvalid = $rrdcellularinterval * 2;
	$vpnvalid = $rrdvpninterval * 2;
	$captiveportalvalid = $rrdcaptiveportalinterval * 2;
	$ntpdvalid = $rrdntpdinterval * 2;

	/* Assume 2*10GigE for now */
	$downstream = 2500000000;
	$upstream = 2500000000;

	/* read the shaper config */
	read_altq_config();

	if (isset ($config['rrd']['enable'])) {

		/* create directory if needed */
		if (!is_dir($rrddbpath)) {
			mkdir($rrddbpath, 0775);
		}
		chown($rrddbpath, "nobody");

Ad Schellevis's avatar
Ad Schellevis committed
281
		if (file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			restore_rrd();
		}

		/* db update script */
		$rrdupdatesh = "#!/bin/sh\n";
		$rrdupdatesh .= "\n";
		$rrdupdatesh .= "export TERM=dumb\n";
		$rrdupdatesh .= "\n";
		$rrdupdatesh .= 'echo $$ > ' . $g['varrun_path'] . '/updaterrd.sh.pid';
		$rrdupdatesh .= "\n";
		$rrdupdatesh .= "counter=1\n";
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
		$rrdupdatesh .= "do\n";
		$rrdupdatesh .= "";

		$i = 0;
		$ifdescrs = get_configured_interface_with_descr();
		/* IPsec counters */
		$ifdescrs['ipsec'] = "IPsec";
		/* OpenVPN server counters */
302
		if(isset($config['openvpn']['openvpn-server'])) {
Ad Schellevis's avatar
Ad Schellevis committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
			foreach($config['openvpn']['openvpn-server'] as $server) {
				$serverid = "ovpns" . $server['vpnid'];
				$ifdescrs[$serverid] = "{$server['description']}";
			}
		}

		/* process all real and pseudo interfaces */
		foreach ($ifdescrs as $ifname => $ifdescr) {
			$temp = get_real_interface($ifname);
			if($temp <> "") {
				$realif = $temp;
			}

			/* TRAFFIC, set up the rrd file */
			if (!file_exists("$rrddbpath$ifname$traffic")) {
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
				$rrdcreate .= "DS:inpass6:COUNTER:$trafficvalid:0:$downstream ";
				$rrdcreate .= "DS:outpass6:COUNTER:$trafficvalid:0:$upstream ";
				$rrdcreate .= "DS:inblock6:COUNTER:$trafficvalid:0:$downstream ";
				$rrdcreate .= "DS:outblock6:COUNTER:$trafficvalid:0:$upstream ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

				create_new_rrd($rrdcreate);
				unset($rrdcreate);
			}

			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
337
			if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U:U:U:U:U");
			}

			$rrdupdatesh .= "\n";
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif IPv4/IPv6 counters \n";
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:";
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$6 };/Out4\/Pass/ { b4po = \$6 };/In4\/Block/ { b4bi = \$6 };/Out4\/Block/ { b4bo = \$6 };\\\n";
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$6 };/Out6\/Pass/ { b6po = \$6 };/In6\/Block/ { b6bi = \$6 };/Out6\/Block/ { b6bo = \$6 };\\\n";
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";

			/* PACKETS, set up the rrd file */
			if (!file_exists("$rrddbpath$ifname$packets")) {
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
				$rrdcreate .= "DS:inpass6:COUNTER:$packetsvalid:0:$downstream ";
				$rrdcreate .= "DS:outpass6:COUNTER:$packetsvalid:0:$upstream ";
				$rrdcreate .= "DS:inblock6:COUNTER:$packetsvalid:0:$downstream ";
				$rrdcreate .= "DS:outblock6:COUNTER:$packetsvalid:0:$upstream ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

				create_new_rrd($rrdcreate);
				unset($rrdcreate);
			}

			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
370
			if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U:U:U:U:U");
			}

			$rrdupdatesh .= "\n";
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:";
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '\\\n";
			$rrdupdatesh .= "/In4\/Pass/ { b4pi = \$4 };/Out4\/Pass/ { b4po = \$4 };/In4\/Block/ { b4bi = \$4 };/Out4\/Block/ { b4bo = \$4 };\\\n";
			$rrdupdatesh .= "/In6\/Pass/ { b6pi = \$4 };/Out6\/Pass/ { b6po = \$4 };/In6\/Block/ { b6bi = \$4 };/Out6\/Block/ { b6bo = \$4 };\\\n";
			$rrdupdatesh .= "END {print b4pi \":\" b4po \":\" b4bi \":\" b4bo \":\" b6pi \":\" b6po \":\" b6bi \":\" b6bo};'`\n";

			/* WIRELESS, set up the rrd file */
			if($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
				if (!file_exists("$rrddbpath$ifname$wireless")) {
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
399
				if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
				}

				$rrdupdatesh .= "\n";
				$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
				$rrdupdatesh .= "WIFI=`$ifconfig {$realif} list sta| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\${WIFI}\n";
			}

			/* OpenVPN, set up the rrd file */
			if(stristr($ifname, "ovpns")) {
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
424
				if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
				}

				if(is_array($config['openvpn']['openvpn-server'])) {
					foreach($config['openvpn']['openvpn-server'] as $server) {
						if("ovpns{$server['vpnid']}" == $ifname) {
							$port = $server['local_port'];
							$vpnid = $server['vpnid'];
						}
					}
				}
				$rrdupdatesh .= "\n";
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
				$rrdupdatesh .= "list_current_users() {\n";
				$rrdupdatesh .= " sleep 0.2\n";
				$rrdupdatesh .= " echo \"status 2\"\n";
				$rrdupdatesh .= " sleep 0.2\n";
				$rrdupdatesh .= " echo \"quit\"\n";
				$rrdupdatesh .= "}\n";
				$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";
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\${OVPN}\n";
			}

			/* QUEUES, set up the queues databases */
			if ($altq_list_queues[$ifname]) {
				$altq =& $altq_list_queues[$ifname];
				/* NOTE: Is it worth as its own function?! */
				switch ($altq->GetBwscale()) {
					case "Gb":
						$factor = 1024 * 1024 * 1024;
							break;
					case "Mb":
							$factor = 1024 * 1024;
							break;
					case "Kb":
							$factor = 1024;
							break;
					case "b":
					default:
							$factor = 1;
							break;
				}
				$qbandwidth = $altq->GetBandwidth() * $factor;
				if ($qbandwidth <=0) {
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
				}
				$qlist =& $altq->get_queue_list($notused);
				if (!file_exists("$rrddbpath$ifname$queues")) {
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
					/* loop list of shaper queues */
					$q = 0;
					foreach ($qlist as $qname => $q) {
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
					}

					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

				if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
					/* loop list of shaper queues */
					$q = 0;
					foreach ($qlist as $qname => $q) {
						$rrdcreate .= "DS:$qname:COUNTER:$queuesdropvalid:0:$qbandwidth ";
					}

					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

Ad Schellevis's avatar
Ad Schellevis committed
506
				if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
					$rrdqcommand = "-t ";
					$rrducommand = "N";
					$qi = 0;
					foreach ($qlist as $qname => $q) {
						if($qi == 0) {
							$rrdqcommand .= "{$qname}";
						} else {
							$rrdqcommand .= ":{$qname}";
						}
						$qi++;
						$rrducommand .= ":U";
					}
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
				}

				/* awk function to gather shaper data */
				/* yes, it's special */
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
				$rrdupdatesh .= "{ ";
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
				$rrdupdatesh .= " q=1; ";
				$rrdupdatesh .= "} ";
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
				$rrdupdatesh .= " q=0; ";
				$rrdupdatesh .= "} ";
				$rrdupdatesh .= "} END { ";
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";

				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
				$rrdupdatesh .= "{ ";
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
				$rrdupdatesh .= " q=1; ";
				$rrdupdatesh .= "} ";
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
				$rrdupdatesh .= " q=0; ";
				$rrdupdatesh .= "} ";
				$rrdupdatesh .= "} END { ";
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
			}

			/* 3G interfaces */
			if(preg_match("/ppp[0-9]+/i", $realif))	{
				if (!file_exists("$rrddbpath$ifname$cellular")) {
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
					$rrdcreate .= "DS:rssi:GAUGE:$cellularvalid:0:100 ";
					$rrdcreate .= "DS:upstream:GAUGE:$cellularvalid:0:100000000 ";
					$rrdcreate .= "DS:downstream:GAUGE:$cellularvalid:0:100000000 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
574
				if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
					mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U:U");
				}

				$rrdupdatesh .= "\n";
				$rrdupdatesh .= "# polling 3G\n";
				$rrdupdatesh .= "GSTATS=`awk -F, 'getline 2 {print \$2 \":\" \$8 \":\" \$9}' < /tmp/3gstats.$ifname`\n";
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cellular N:\"\$GSTATS\"";
			}

		}
		$i++;

		/* System only statistics */
		$ifname = "system";

		/* STATES, create pf states database */
		if(! file_exists("$rrddbpath$ifname$states")) {
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

			create_new_rrd($rrdcreate);
			unset($rrdcreate);
		}

		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
608
		if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
		}

		/* the pf states gathering function. */
		$rrdupdatesh .= "\n";
		$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
		$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
		$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
		$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
		$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
		$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";
		$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";
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";

		/* End pf states statistics */

		/* CPU, create CPU statistics database */
		if(! file_exists("$rrddbpath$ifname$proc")) {
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

			create_new_rrd($rrdcreate);
			unset($rrdcreate);
		}

		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
643
		if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
		}

		/* the CPU stats gathering function. */
		$rrdupdatesh .= "CPU=`$cpustats | cut -f1-4 -d':'`\n";
		/* Using ps uxaH will count all processes including system threads. Top was undercounting. */
		$rrdupdatesh .= "PROCS=`ps uxaH | wc -l | awk '{print \$1;}'`\n";
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$proc N:\${CPU}:\${PROCS}\n";

		/* End CPU statistics */

		/* Memory, create Memory statistics database */
		if(! file_exists("$rrddbpath$ifname$mem")) {
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";

			create_new_rrd($rrdcreate);
			unset($rrdcreate);
		}

		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
681
		if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
		}

		/* the Memory stats gathering function. */
		$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 | ";
		$rrdupdatesh .= " $awk '{getline active;getline inactive;getline free;getline cache;getline wire;printf ";
		$rrdupdatesh .= "((active/$0) * 100)\":\"((inactive/$0) * 100)\":\"((free/$0) * 100)\":\"((cache/$0) * 100)\":\"(wire/$0 * 100)}'`\n";
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mem N:\${MEM}\n";

		/* End Memory statistics */

		/* mbuf, create mbuf statistics database */
		if(! file_exists("$rrddbpath$ifname$mbuf")) {
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mbuf --step $rrdmbufinterval ";
			$rrdcreate .= "DS:current:GAUGE:$mbufvalid:0:10000000 ";
			$rrdcreate .= "DS:cache:GAUGE:$mbufvalid:0:10000000 ";
			$rrdcreate .= "DS:total:GAUGE:$mbufvalid:0:10000000 ";
			$rrdcreate .= "DS:max:GAUGE:$mbufvalid:0:10000000 ";
			$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
			$rrdcreate .= "RRA:MIN:0.5:5:720 ";
			$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
			$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
			$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
			$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
			$rrdcreate .= "RRA:MAX:0.5:5:720 ";
			$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
			$rrdcreate .= "RRA:MAX:0.5:1440:2284";

			create_new_rrd($rrdcreate);
			unset($rrdcreate);
		}

		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
718
		if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
			mwexec("$rrdtool update $rrddbpath$ifname$mbuf N:U:U:U:U");
		}

		/* the mbuf stats gathering function. */
		$rrdupdatesh .= "MBUF=`$netstat -m | ";
		$rrdupdatesh .= " $awk '/mbuf clusters in use/ { gsub(/\//, \":\", $1); print $1; }'`\n";
		$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$mbuf N:\${MBUF}\n";

		/* End mbuf statistics */

		/* SPAMD, set up the spamd rrd file */
		if (isset($config['installedpackages']['spamdsettings']) &&
			 $config['installedpackages']['spamdsettings']['config'][0]['enablerrd']) {
			/* set up the spamd rrd file */
			if (!file_exists("$rrddbpath$ifname$spamd")) {
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
				$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
				$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";

				create_new_rrd($rrdcreate);
				unset($rrdcreate);
			}

			$rrdupdatesh .= "\n";
			$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
			$rrdupdatesh .= "`$php -q $spamd_gather`\n";

		}
		/* End System statistics */

		/* Captive Portal statistics, set up the rrd file */
		if(is_array($config['captiveportal'])) {
			foreach ($config['captiveportal'] as $cpkey => $cp) {
				if (!isset($cp['enable']))
					continue;

				$ifname= "captiveportal";
				$concurrent_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalconcurrent;
				if (!file_exists("$concurrent_filename")) {
					$rrdcreate = "$rrdtool create $concurrent_filename --step $rrdcaptiveportalinterval ";
					$rrdcreate .= "DS:concurrentusers:GAUGE:$captiveportalvalid:0:10000 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";

					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
795
				if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
					mwexec("$rrdtool update $concurrent_filename N:U");
				}

				/* the Captive Portal stats gathering function. */
				$rrdupdatesh .= "\n";
				$rrdupdatesh .= "# polling Captive Portal for number of concurrent users\n";
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'concurrent'`\n";
				$rrdupdatesh .= "$rrdtool update $concurrent_filename \${CP}\n";

				$loggedin_filename = $rrddbpath . $ifname . '-' . $cpkey . $captiveportalloggedin;
				if (!file_exists("$loggedin_filename")) {
					$rrdcreate = "$rrdtool create $loggedin_filename --step $rrdcaptiveportalinterval ";
					$rrdcreate .= "DS:loggedinusers:GAUGE:$captiveportalvalid:0:10000 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
					$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
					$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
					$rrdcreate .= "RRA:MIN:0.5:5:720 ";
					$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
					$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
					$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
					$rrdcreate .= "RRA:MAX:0.5:5:720 ";
					$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
					$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
					$rrdcreate .= "RRA:LAST:0.5:1:1200 ";
					$rrdcreate .= "RRA:LAST:0.5:5:720 ";
					$rrdcreate .= "RRA:LAST:0.5:60:1860 ";
					$rrdcreate .= "RRA:LAST:0.5:1440:2284 ";

					create_new_rrd($rrdcreate);
					unset($rrdcreate);
				}

				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
831
				if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
					mwexec("$rrdtool update $loggedin_filename N:U");
				}

				/* the Captive Portal stats gathering function. */
				$rrdupdatesh .= "\n";
				$rrdupdatesh .= "# polling Captive Portal for number of logged in users\n";
				$rrdupdatesh .= "CP=`${php} -q ${captiveportal_gather} '${cpkey}' 'loggedin'`\n";
				$rrdupdatesh .= "$rrdtool update $loggedin_filename \${CP}\n";

			}
		}
		/* End Captive Portal statistics */

		/* NTP, set up the ntpd rrd file */
		if (isset($config['ntpd']['statsgraph'])) {
			/* set up the ntpd rrd file */
			if (!file_exists("$rrddbpath$ntpd")) {
				$rrdcreate = "$rrdtool create $rrddbpath$ntpd --step $rrdntpdinterval ";
				$rrdcreate .= "DS:offset:GAUGE:$ntpdvalid:0:1000 ";
				$rrdcreate .= "DS:sjit:GAUGE:$ntpdvalid:0:1000 ";
				$rrdcreate .= "DS:cjit:GAUGE:$ntpdvalid:0:1000 ";
				$rrdcreate .= "DS:wander:GAUGE:$ntpdvalid:0:1000 ";
				$rrdcreate .= "DS:freq:GAUGE:$ntpdvalid:0:1000 ";
				$rrdcreate .= "DS:disp:GAUGE:$ntpdvalid:0:1000 ";
				$rrdcreate .= "RRA:MIN:0.5:1:1200 ";
				$rrdcreate .= "RRA:MIN:0.5:5:720 ";
				$rrdcreate .= "RRA:MIN:0.5:60:1860 ";
				$rrdcreate .= "RRA:MIN:0.5:1440:2284 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
				$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
				$rrdcreate .= "RRA:MAX:0.5:1:1200 ";
				$rrdcreate .= "RRA:MAX:0.5:5:720 ";
				$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
				$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";

				create_new_rrd($rrdcreate);
				unset($rrdcreate);
			}

			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
874
			if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
			mwexec("$rrdtool update $rrddbpath$ntpd N:U:U:U:U:U:U");
			}

			/* the ntp stats gathering function. */
			$rrdupdatesh .= "\n";
			$rrdupdatesh .= "$ntpq -c rv | $awk 'BEGIN{ RS=\",\"}{ print }' >> /tmp/ntp-rrdstats.$$\n";
			$rrdupdatesh .= "NOFFSET=`grep offset /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
			$rrdupdatesh .= "NFREQ=`grep frequency /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
			$rrdupdatesh .= "NSJIT=`grep sys_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
			$rrdupdatesh .= "NCJIT=`grep clk_jitter /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
			$rrdupdatesh .= "NWANDER=`grep clk_wander /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
			$rrdupdatesh .= "NDISPER=`grep rootdisp /tmp/ntp-rrdstats.$$ | awk 'BEGIN{FS=\"=\"}{print $2}'`\n";
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ntpd \N:\${NOFFSET}:\${NSJIT}:\${NCJIT}:\${NWANDER}:\${NFREQ}:\${NDISPER}\n";
			$rrdupdatesh .= "rm /tmp/ntp-rrdstats.$$\n";
			$rrdupdatesh .= "\n";

		}
		/* End NTP statistics */

		$rrdupdatesh .= "sleep 60\n";
		$rrdupdatesh .= "done\n";
		log_error(gettext("Creating rrd update script"));
		/* write the rrd update script */
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
		$fd = fopen("$updaterrdscript", "w");
		fwrite($fd, "$rrdupdatesh");
		fclose($fd);

		unset($rrdupdatesh);

		/* kill off traffic collectors */
		kill_traffic_collector();

		/* start traffic collector */
909
		mwexec_bg("/bin/sh $updaterrdscript");
Ad Schellevis's avatar
Ad Schellevis committed
910 911 912 913 914 915 916 917 918 919 920

	} else {
		/* kill off traffic collectors */
		kill_traffic_collector();
	}

	$databases = glob("{$rrddbpath}/*.rrd");
	foreach($databases as $database) {
		chown($database, "nobody");
	}

Ad Schellevis's avatar
Ad Schellevis committed
921
	if(file_exists("/var/run/booting"))
Ad Schellevis's avatar
Ad Schellevis committed
922 923 924 925 926 927 928 929 930
		echo gettext("done.") . "\n";

}

# Create gateway quality RRD with settings suitable for pfSense graph set.
function create_gateway_quality_rrd($rrd_file) {
	global $g;
	$rrdinterval = 60;
	$valid = $rrdinterval * 2;
931
	$rrdtool = "/usr/local/bin/rrdtool";
Ad Schellevis's avatar
Ad Schellevis committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

	/* GATEWAY QUALITY, set up the rrd file */
	if (!file_exists("$rrd_file")) {
		$rrdcreate = "$rrdtool create $rrd_file --step $rrdinterval ";
		$rrdcreate .= "DS:loss:GAUGE:$valid:0:100 ";
		$rrdcreate .= "DS:delay:GAUGE:$valid:0:100000 ";
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1200 ";
		$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
		$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";

		create_new_rrd($rrdcreate);
		unset($rrdcreate);
	}

	/* enter UNKNOWN values in the RRD so it knows we rebooted. */
Ad Schellevis's avatar
Ad Schellevis committed
948
	if(file_exists("/var/run/booting")) {
Ad Schellevis's avatar
Ad Schellevis committed
949 950 951 952 953 954 955 956 957 958 959 960
		mwexec("$rrdtool update $rrd_file N:U:U");
	}
	unset($rrdtool, $rrdinterval, $valid, $rrd_file);
}

function kill_traffic_collector() {
	global $g;

	killbypid("{$g['varrun_path']}/updaterrd.sh.pid");
}

?>