Project

General

Profile

Download (25.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
  Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>
5
  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
/*
31
	pfSense_BUILDER_BINARIES:	/bin/rm	/usr/bin/nice	/usr/local/bin/rrdtool	/bin/cd
32
	pfSense_MODULE:	rrd
33
*/
34

    
35
/* include all configuration functions */
36

    
37
function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
38
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
39
	if(file_exists($xmldumpfile))
40
		mwexec("rm {$xmldumpfile}");
41

    
42
	exec("$rrdtool dump {$rrddatabase} {$xmldumpfile} 2>&1", $dumpout, $dumpret);
43
	if ($dumpret <> 0) {
44
		$dumpout = implode(" ", $dumpout);
45
		log_error("RRD dump failed exited with $dumpret, the error is: $dumpout");
46
	}
47
	return($dumpret);
48
}
49

    
50
function create_new_rrd($rrdcreatecmd) {
51
	$rrdcreateoutput = array();
52
	$rrdcreatereturn = 0;
53
	exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn);
54
	if ($rrdcreatereturn <> 0) {
55
		$rrdcreateoutput = implode(" ", $rrdcreateoutput);
56
		log_error("RRD create failed exited with $rrdcreatereturn, the error is: $rrdcreateoutput");
57
	}
58
	return $rrdcreatereturn;
59
}
60

    
61
function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
62
	if(!file_exists("/tmp/rrd_notice_sent.txt")) {
63
		exec("echo 'Converting RRD configuration to new format.  This might take a bit...' | wall");
64
		touch("/tmp/rrd_notice_sent.txt");
65
	}
66
	$numrraold = count($rrdoldxml['rra']);
67
	$numdsold = count($rrdoldxml['ds']);
68
	$numrranew = count($rrdnewxml['rra']);
69
	$numdsnew = count($rrdnewxml['ds']);
70
	log_error("Import RRD has $numdsold DS values and $numrraold RRA databases, new format RRD has $numdsnew DS values and $numrranew RRA databases");
71
	
72
	/* add data sources not found in the old array from the new array */
73
	$i = 0;
74
	foreach($rrdnewxml['ds'] as $ds) {
75
		if(!is_array($rrdoldxml['ds'][$i])) {
76
			$rrdoldxml['ds'][$i] = $rrdnewxml['ds'][$i];
77
			/* set unknown values to 0 */
78
			$rrdoldxml['ds'][$i]['last_ds'] = " 0.0000000000e+00 ";
79
			$rrdoldxml['ds'][$i]['value'] = " 0.0000000000e+00 ";
80
			$rrdoldxml['ds'][$i]['unknown_sec'] = "0";
81
		}
82
		$i++;
83
	}
84

    
85
	$i = 0;
86
	$rracountold = count($rrdoldxml['rra']);
87
	$rracountnew = count($rrdnewxml['rra']);
88
	/* process each RRA, which contain a database */
89
	foreach($rrdnewxml['rra'] as $rra) {
90
		if(!is_array($rrdoldxml['rra'][$i])) {
91
			$rrdoldxml['rra'][$i] = $rrdnewxml['rra'][$i];
92
		}
93

    
94
		$d = 0;
95
		/* process cdp_prep */
96
		$cdp_prep = $rra['cdp_prep'];
97
		foreach($cdp_prep['ds'] as $ds) {
98
			if(!is_array($rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d])) {
99
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d] = $rrdnewxml['rra'][$i]['cdp_prep']['ds'][$d];
100
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['primary_value'] = " 0.0000000000e+00 ";
101
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['secondary_value'] = " 0.0000000000e+00 ";
102
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['value'] = " 0.0000000000e+00 ";
103
				$rrdoldxml['rra'][$i]['cdp_prep']['ds'][$d]['unknown_datapoints'] = "0";
104
			}
105
			$d++;
106
		}
107

    
108
		/* process database */
109
		$rows = $rra['database'];
110
		$k = 0;
111
		$rowcountold = count($rrdoldxml['rra'][$i]['database']['row']);
112
		$rowcountnew = count($rrdnewxml['rra'][$i]['database']['row']);
113
		$rowcountdiff = $rowcountnew - $rowcountold;
114
		/* save old rows for a bit before we put the required empty rows before it */
115
		$rowsdata = $rows;
116
		$rowsempty = array();
117
		$r = 0;
118
		while($r < $rowcountdiff) {
119
			$rowsempty[] = $rrdnewxml['rra'][$i]['database']['row'][$r];
120
			$r++;
121
		}
122
		$rows = $rowsempty + $rowsdata;
123
		/* now foreach the rows in the database */
124
		foreach($rows['row'] as $row) {
125
			if(!is_array($rrdoldxml['rra'][$i]['database']['row'][$k])) {
126
				$rrdoldxml['rra'][$i]['database']['row'][$k] = $rrdnewxml['rra'][$i]['database']['row'][$k];
127
			}
128
			$m = 0;
129
			$vcountold = count($rrdoldxml['rra'][$i]['database']['row'][$k]['v']);
130
			$vcountnew = count($rrdnewxml['rra'][$i]['database']['row'][$k]['v']);
131
			foreach($row['v'] as $value) {
132
				if(empty($rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m])) {
133
					if(isset($valid)) {
134
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = "0.0000000000e+00 ";
135
					} else {
136
						$rrdoldxml['rra'][$i]['database']['row'][$k]['v'][$m] = $rrdnewxml['rra'][$i]['database']['row'][$k]['v'][$m];
137
					}
138
				} else {
139
					if($value <> " NaN ") {
140
						$valid = true;
141
					} else {
142
						$valid = false;
143
					}
144
				}
145
				$m++;
146
			}
147
			$k++;
148
		}
149
		$i++;
150
	}
151

    
152
	$numrranew = count($rrdoldxml['rra']);
153
	$numdsnew = count($rrdoldxml['ds']);
154
	log_error("The new RRD now has $numdsnew DS values and $numrranew RRA databases");
155
	return $rrdoldxml;
156
}
157

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

    
161
	if($g['booting']) 
162
		echo "Generating RRD graphs...";
163

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

    
167
	$traffic = "-traffic.rrd";
168
	$packets = "-packets.rrd";
169
	$states = "-states.rrd";
170
	$wireless = "-wireless.rrd";
171
	$queues = "-queues.rrd";
172
	$queuesdrop = "-queuedrops.rrd";
173
	$spamd = "-spamd.rrd";
174
	$proc = "-processor.rrd";
175
	$mem = "-memory.rrd";
176
	$cellular = "-cellular.rrd";
177
	$vpnusers = "-vpnusers.rrd";
178

    
179
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
180
	$netstat = "/usr/bin/netstat";
181
	$awk = "/usr/bin/awk";
182
	$tar = "/usr/bin/tar";
183
	$pfctl = "/sbin/pfctl";
184
	$sysctl = "/sbin/sysctl";
185
	$php = "/usr/local/bin/php";
186
	$top = "/usr/bin/top";
187
	$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
188
	$ifconfig = "/sbin/ifconfig";
189

    
190
	$rrdtrafficinterval = 60;
191
	$rrdwirelessinterval = 60;
192
	$rrdqueuesinterval = 60;
193
	$rrdqueuesdropinterval = 60;
194
	$rrdpacketsinterval = 60;
195
	$rrdstatesinterval = 60;
196
	$rrdspamdinterval = 60;
197
	$rrdlbpoolinterval = 60;
198
	$rrdprocinterval = 60;
199
	$rrdmeminterval = 60;
200
	$rrdcellularinterval = 60;
201
	$rrdvpninterval = 60;
202

    
203
	$trafficvalid = $rrdtrafficinterval * 2;
204
	$wirelessvalid = $rrdwirelessinterval * 2;
205
	$queuesvalid = $rrdqueuesinterval * 2;
206
	$queuesdropvalid = $rrdqueuesdropinterval * 2;
207
	$packetsvalid = $rrdpacketsinterval * 2;
208
	$statesvalid = $rrdstatesinterval*2;
209
	$spamdvalid = $rrdspamdinterval * 2;
210
	$lbpoolvalid = $rrdlbpoolinterval * 2;
211
	$procvalid = $rrdlbpoolinterval * 2;
212
	$memvalid = $rrdmeminterval * 2;
213
	$cellularvalid = $rrdcellularinterval * 2;
214
	$vpnvalid = $rrdvpninterval * 2;
215

    
216
	/* Asume GigE for now */
217
	$downstream = 125000000;
218
	$upstream = 125000000;
219

    
220
	/* read the shaper config */
221
	read_altq_config();
222

    
223
	$rrdrestore = "";
224
	$rrdreturn = "";
225

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

    
228
		/* create directory if needed */
229
		if (!is_dir("$rrddbpath")) {
230
			mkdir("$rrddbpath", 0755);
231
		}
232

    
233
		if ($g['booting']) {
234
			if ($g['platform'] != "pfSense") {
235
				/* restore the databases, if we have one */
236
				if (file_exists("{$g['cf_conf_path']}/rrd.tgz")) {
237
					exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/rrd.tgz 2>&1", $rrdrestore, $rrdreturn);
238
					$rrdrestore = implode(" ", $rrdrestore);
239
					if($rrdreturn <> 0) {
240
						log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
241
					}
242
				}
243
			}
244
		}
245

    
246
		/* db update script */
247
		$rrdupdatesh = "#!/bin/sh\n";
248
		$rrdupdatesh .= "\n";
249
		$rrdupdatesh .= "counter=1\n";
250
		$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
251
		$rrdupdatesh .= "do\n";
252
		$rrdupdatesh .= "";
253

    
254
		$i = 0;
255
		$ifdescrs = get_configured_interface_with_descr();
256
		/* IPsec counters */
257
		$ifdescrs['ipsec'] = "IPsec";
258
		/* OpenVPN server counters */
259
		if(is_array($config['openvpn']['openvpn-server'])) {
260
			foreach($config['openvpn']['openvpn-server'] as $server) {
261
				$serverid = "ovpns" . $server['vpnid'];
262
				$ifdescrs[$serverid] = "{$server['description']}";
263
			}
264
		}
265

    
266
		/* process all real and pseudo interfaces */
267
		foreach ($ifdescrs as $ifname => $ifdescr) {
268
			$temp = get_real_interface($ifname);
269
			if($temp <> "") {
270
				$realif = $temp;
271
			}
272

    
273
			/* TRAFFIC, set up the rrd file */
274
			if (!file_exists("$rrddbpath$ifname$traffic")) {
275
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
276
				$rrdcreate .= "DS:inpass:COUNTER:$trafficvalid:0:$downstream ";
277
				$rrdcreate .= "DS:outpass:COUNTER:$trafficvalid:0:$upstream ";
278
				$rrdcreate .= "DS:inblock:COUNTER:$trafficvalid:0:$downstream ";
279
				$rrdcreate .= "DS:outblock:COUNTER:$trafficvalid:0:$upstream ";
280
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
281
				$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
282
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
283
				$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
284

    
285
				create_new_rrd($rrdcreate);
286
			}
287

    
288
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
289
			if($g['booting']) {
290
				mwexec("$rrdtool update $rrddbpath$ifname$traffic N:U:U:U:U");
291
			}
292

    
293
			$rrdupdatesh .= "\n";
294
			$rrdupdatesh .= "# polling traffic for interface $ifname $realif \n";
295
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:\\\n";
296
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '/In4\/Pass|Out4\/Pass/ {printf \$6 \":\"}'`\\\n";
297
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '/In4\/Block|Out4\/Block/ {printf \$6 \":\"}'|sed -e 's/.\$//'`\n";
298

    
299
			/* PACKETS, set up the rrd file */
300
			if (!file_exists("$rrddbpath$ifname$packets")) {
301
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
302
				$rrdcreate .= "DS:inpass:COUNTER:$packetsvalid:0:$downstream ";
303
				$rrdcreate .= "DS:outpass:COUNTER:$packetsvalid:0:$upstream ";
304
				$rrdcreate .= "DS:inblock:COUNTER:$packetsvalid:0:$downstream ";
305
				$rrdcreate .= "DS:outblock:COUNTER:$packetsvalid:0:$upstream ";
306
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
307
				$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
308
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
309
				$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
310

    
311
				create_new_rrd($rrdcreate);
312
			}
313

    
314
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
315
			if($g['booting']) {
316
				mwexec("$rrdtool update $rrddbpath$ifname$packets N:U:U:U:U");
317
			}
318

    
319
			$rrdupdatesh .= "\n";
320
			$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
321
			$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:\\\n";
322
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '/In4\/Pass|Out4\/Pass/ {printf \$4 \":\"}'`\\\n";
323
			$rrdupdatesh .= "`$pfctl -vvsI -i {$realif} | awk '/In4\/Block|Out4\/Block/ {printf \$4 \":\"}'|sed -e 's/.\$//'`\n";
324

    
325
			/* WIRELESS, set up the rrd file */
326
			if($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
327
				if (!file_exists("$rrddbpath$ifname$wireless")) {
328
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
329
					$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
330
					$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
331
					$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
332
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
333
					$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
334
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
335
					$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
336
	
337
					create_new_rrd($rrdcreate);
338
				}
339

    
340
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
341
				if($g['booting']) {
342
					mwexec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
343
				}
344

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

    
351
			/* OpenVPN, set up the rrd file */
352
			if(stristr($ifname, "ovpns")) {
353
				if (!file_exists("$rrddbpath$ifname$vpnusers")) {
354
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$vpnusers --step $rrdvpninterval ";
355
					$rrdcreate .= "DS:users:GAUGE:$vpnvalid:0:10000 ";
356
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
357
					$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
358
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
359
					$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
360
	
361
					create_new_rrd($rrdcreate);
362
				}
363

    
364
				/* enter UNKNOWN values in the RRD so it knows we rebooted. */
365
				if($g['booting']) {
366
					mwexec("$rrdtool update $rrddbpath$ifname$vpnusers N:U");
367
				}
368

    
369
				if(is_array($config['openvpn']['openvpn-server'])) {
370
					foreach($config['openvpn']['openvpn-server'] as $server) {
371
						if("ovpns{$server['vpnid']}" == $ifname) {
372
							$port = $server['local_port'];
373
						}
374
					}
375
				}
376
				$rrdupdatesh .= "\n";
377
				$rrdupdatesh .= "# polling vpn users for interface $ifname $realif port $port\n";
378
				$rrdupdatesh .= "list_current_users() {\n";
379
				$rrdupdatesh .= " sleep 0.2\n";
380
				$rrdupdatesh .= " echo \"status 2\"\n";
381
				$rrdupdatesh .= " sleep 0.2\n";
382
				$rrdupdatesh .= " echo \"quit\"\n";
383
				$rrdupdatesh .= "}\n";
384
				$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$vpnusers N:\\\n";
385
				$rrdupdatesh .= "`list_current_users | nc localhost {$port} | awk -F\",\" '/^CLIENT_LIST/ {print \$2}' | wc -l | awk '{print $1}'`\n";
386
			}
387

    
388
			/* QUEUES, set up the queues databases */
389
			if ($altq_list_queues[$ifname]) {
390
				$altq =& $altq_list_queues[$ifname];
391
				/* NOTE: Is it worth as its own function?! */
392
				switch ($altq->GetBwscale()) {
393
					case "Gb":
394
						$factor = 1024 * 1024 * 1024;
395
							break;
396
					case "Mb":
397
							$factor = 1024 * 1024;
398
							break;
399
					case "Kb":
400
							$factor = 1024;
401
							break;
402
					case "b":
403
					default:
404
							$factor = 1;
405
							break;
406
				}
407
				$qbandwidth = $altq->GetBandwidth() * $factor;
408
				if ($qbandwidth <=0) {
409
					$qbandwidth = 100 * 1000 * 1000; /* 100Mbit */
410
				}
411
				$qlist =& $altq->get_queue_list($notused);
412
				if (!file_exists("$rrddbpath$ifname$queues")) {
413
					$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
414
					/* loop list of shaper queues */
415
					$q = 0;
416
					foreach ($qlist as $qname => $q) {
417
						$rrdcreate .= "DS:$qname:COUNTER:$queuesvalid:0:$qbandwidth ";
418
					}
419

    
420
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
421
					$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
422
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
423
					$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
424

    
425
					create_new_rrd($rrdcreate);
426
				}
427

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

    
436
					$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
437
					$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
438
					$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
439
					$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
440

    
441
					create_new_rrd($rrdcreate);
442
				}
443

    
444
				if($g['booting']) {
445
					$rrdqcommand = "-t ";
446
					$rrducommand = "N";
447
					$q = 0;
448
					foreach ($qlist as $qname => $q) {
449
						if($q == 0) {
450
							$rrdqcommand .= "{$qname}";
451
						} else {
452
							$rrdqcommand .= ":{$qname}";
453
						}
454
						$q++;
455
						$rrducommand .= ":U";
456
					}
457
					mwexec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
458
					mwexec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
459
				}
460

    
461
				/* awk function to gather shaper data */
462
				/* yes, it's special */
463
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
464
				$rrdupdatesh .= "{ ";
465
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
466
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
467
				$rrdupdatesh .= " q=1; ";
468
				$rrdupdatesh .= "} ";
469
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
470
				$rrdupdatesh .= " dsdata = dsdata \":\" \$5 ; ";
471
				$rrdupdatesh .= " q=0; ";
472
				$rrdupdatesh .= "} ";
473
				$rrdupdatesh .= "} END { ";
474
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
475
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
476
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
477
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
478

    
479
				$rrdupdatesh .= "` pfctl -vsq -i {$realif} | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
480
				$rrdupdatesh .= "{ ";
481
				$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
482
				$rrdupdatesh .= " dsname = dsname \":\" \$2 ; ";
483
				$rrdupdatesh .= " q=1; ";
484
				$rrdupdatesh .= "} ";
485
				$rrdupdatesh .= " else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
486
				$rrdupdatesh .= " dsdata = dsdata \":\" \$8 ; ";
487
				$rrdupdatesh .= " q=0; ";
488
				$rrdupdatesh .= "} ";
489
				$rrdupdatesh .= "} END { ";
490
				$rrdupdatesh .= " dsname = substr(dsname,2); ";
491
				$rrdupdatesh .= " dsdata = substr(dsdata,2); ";
492
				$rrdupdatesh .= " printf \"-t \" dsname \" N:\" dsdata }' ";
493
				$rrdupdatesh .= " dsname=\"\" dsdata=\"\"`\n\n";
494
			}
495
		}
496
		$i++;
497

    
498
		/* System only statistics */
499
		$ifname = "system";
500

    
501
		/* STATES, create pf states database */
502
		if(! file_exists("$rrddbpath$ifname$states")) {
503
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
504
			$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
505
			$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
506
			$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
507
			$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
508
			$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
509
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
510
			$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
511
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
512
			$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
513

    
514
			create_new_rrd($rrdcreate);
515
		}
516

    
517
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
518
		if($g['booting']) {
519
			mwexec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
520
		}
521

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

    
533
		/* End pf states statistics */
534

    
535
		/* CPU, create CPU statistics database */
536
		if(! file_exists("$rrddbpath$ifname$proc")) {
537
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
538
			$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
539
			$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
540
			$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
541
			$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
542
			$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
543
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
544
			$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
545
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
546
			$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
547

    
548
			create_new_rrd($rrdcreate);
549
		}
550

    
551
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
552
		if($g['booting']) {
553
			mwexec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
554
		}
555

    
556
		/* the CPU stats gathering function. */
557
		$rrdupdatesh .= "`$top -d 2 -s 1 0 | $awk '{gsub(/%/, \"\")} BEGIN { \\\n";
558
		$rrdupdatesh .= "printf \"$rrdtool update $rrddbpath$ifname$proc \" } \\\n";
559
		$rrdupdatesh .= "{ if ( \$2 == \"processes:\" ) { processes = \$1; } \\\n";
560
		$rrdupdatesh .= "else if ( \$1 == \"CPU:\" ) { user = \$2; nice = \$4; sys = \$6; interrupt = \$8; } \\\n";
561
		$rrdupdatesh .= "} END { printf \"N:\"user\":\"nice\":\"sys\":\"interrupt\":\"processes }'`\n\n";
562

    
563
		/* End CPU statistics */
564

    
565
		/* Memory, create Memory statistics database */
566
		if(! file_exists("$rrddbpath$ifname$mem")) {
567
			$rrdcreate = "$rrdtool create $rrddbpath$ifname$mem --step $rrdmeminterval ";
568
			$rrdcreate .= "DS:active:GAUGE:$memvalid:0:10000000 ";
569
			$rrdcreate .= "DS:inactive:GAUGE:$memvalid:0:10000000 ";
570
			$rrdcreate .= "DS:free:GAUGE:$memvalid:0:10000000 ";
571
			$rrdcreate .= "DS:cache:GAUGE:$memvalid:0:10000000 ";
572
			$rrdcreate .= "DS:wire:GAUGE:$memvalid:0:10000000 ";
573
			$rrdcreate .= "RRA:MIN:0.5:1:1000 ";
574
			$rrdcreate .= "RRA:MIN:0.5:5:1000 ";
575
			$rrdcreate .= "RRA:MIN:0.5:60:1000 ";
576
			$rrdcreate .= "RRA:MIN:0.5:720:3000 ";
577
			$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
578
			$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
579
			$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
580
			$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
581
			$rrdcreate .= "RRA:MAX:0.5:1:1000 ";
582
			$rrdcreate .= "RRA:MAX:0.5:5:1000 ";
583
			$rrdcreate .= "RRA:MAX:0.5:60:1000 ";
584
			$rrdcreate .= "RRA:MAX:0.5:720:3000";
585

    
586
			create_new_rrd($rrdcreate);
587
		}
588

    
589
		/* enter UNKNOWN values in the RRD so it knows we rebooted. */
590
		if($g['booting']) {
591
			mwexec("$rrdtool update $rrddbpath$ifname$mem N:U:U:U:U:U");
592
		}
593

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

    
601
		/* SPAMD, set up the spamd rrd file */
602
		if (isset($config['installedpackages']['spamdsettings']) &&
603
			 isset ($config['installedpackages']['spamdsettings']['config'][0]['enablerrd'])) {
604
			/* set up the spamd rrd file */
605
			if (!file_exists("$rrddbpath$ifname$spamd")) {
606
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
607
				$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
608
				$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
609
				$rrdcreate .= "RRA:MIN:0.5:1:1000 ";
610
				$rrdcreate .= "RRA:MIN:0.5:5:1000 ";
611
				$rrdcreate .= "RRA:MIN:0.5:60:1000 ";
612
				$rrdcreate .= "RRA:MIN:0.5:720:3000 ";
613
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
614
				$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
615
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
616
				$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
617
				$rrdcreate .= "RRA:MAX:0.5:1:1000 ";
618
				$rrdcreate .= "RRA:MAX:0.5:5:1000 ";
619
				$rrdcreate .= "RRA:MAX:0.5:60:1000 ";
620
				$rrdcreate .= "RRA:MAX:0.5:720:3000 ";
621

    
622
				create_new_rrd($rrdcreate);
623
			}
624

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

    
630
		}
631
		/* End System statistics */
632

    
633
		/* 3G WIRELESS, set up the rrd file */
634
		/* XXX: Are multiple 3G interfaces not possible? smos@ */
635
		if(isset($config['ppps']['ppp'])) {
636
			$ifname = "ppp";
637
			if (!file_exists("$rrddbpath$ifname$cellular")) {
638
				$rrdcreate = "$rrdtool create $rrddbpath$ifname$cellular --step $rrdcellularinterval ";
639
				$rrdcreate .= "DS:signal1:GAUGE:$cellularvalid:-200:200 ";
640
				$rrdcreate .= "DS:signal2:GAUGE:$cellularvalid:-200:200 ";
641
				$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
642
				$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
643
				$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
644
				$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
645

    
646
				create_new_rrd($rrdcreate);
647
			}
648

    
649
			/* enter UNKNOWN values in the RRD so it knows we rebooted. */
650
			if($g['booting']) {
651
				mwexec("$rrdtool update $rrddbpath$ifname$cellular N:U:U");
652
			}
653

    
654
			$rrdupdatesh .= "\n";
655
			$rrdupdatesh .= "# polling 3G\n";
656
			$rrdupdatesh .= "dev=`usbconfig show_ifdrv | awk -F. '/ u3g|umodem/ {print \"/dev/\" $1 \".\" $2}'`\n";
657
			$rrdupdatesh .= "if [ -n \"\$dev\" ]; then $rrdtool update $rrddbpath$ifname$cellular N:`3gstat -s -d \$dev`\n";
658
			$rrdupdatesh .= "else $rrdtool update $rrddbpath$ifname$cellular N:U:U; fi\n";
659
		}
660

    
661

    
662
		$rrdupdatesh .= "sleep 60\n";
663
		$rrdupdatesh .= "done\n";
664
		log_error("Creating rrd update script");
665
		/* write the rrd update script */
666
		$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
667
		$fd = fopen("$updaterrdscript", "w");
668
		fwrite($fd, "$rrdupdatesh");
669
		fclose($fd);
670

    
671
		/* kill off traffic collectors */
672
		kill_traffic_collector();
673

    
674
		/* start traffic collector */
675
		mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
676

    
677
	} else {
678
		/* kill off traffic collectors */
679
		kill_traffic_collector();
680
	}
681

    
682
	if($g['booting']) 
683
		echo "done.\n";
684
		
685
}
686

    
687
function kill_traffic_collector() {
688
	mwexec("/bin/pkill -f updaterrd.sh", true);
689
}
690

    
691
?>
(34-34/51)