Project

General

Profile

Download (20 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	status_rrd_graph.php
5
	Part of pfSense
6
	Copyright (C) 2007 Seth Mos <seth.mos@dds.nl>
7
	All rights reserved.
8

    
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11

    
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14

    
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18

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

    
34
##|+PRIV
35
##|*IDENT=page-status-rrdgraphs
36
##|*NAME=Status: RRD Graphs page
37
##|*DESCR=Allow access to the 'Status: RRD Graphs' page.
38
##|*MATCH=status_rrd_graph.php*
39
##|-PRIV
40

    
41
require("guiconfig.inc");
42
require_once("filter.inc");
43
require("shaper.inc");
44
require_once("rrd.inc");
45

    
46
/* if the rrd graphs are not enabled redirect to settings page */
47
if(! isset($config['rrd']['enable'])) {
48
	header("Location: status_rrd_graph_settings.php");
49
}
50

    
51
$rrddbpath = "/var/db/rrd/";
52
chdir($rrddbpath);
53
$databases = glob("*.rrd");
54

    
55

    
56
if ($_GET['cat']) {
57
	$curcat = htmlspecialchars($_GET['cat']);
58
} else {
59
	if(! empty($config['rrd']['category'])) {
60
		$curcat = $config['rrd']['category'];
61
	} else {
62
		$curcat = "system";
63
	}
64
}
65

    
66
if ($_GET['period']) {
67
	$curperiod = $_GET['period'];
68
} else {
69
	if(! empty($config['rrd']['period'])) {
70
		$curperiod = $config['rrd']['period'];
71
	} else {
72
		$curperiod = "absolute";
73
	}
74
}
75

    
76
if ($_GET['option']) {
77
	$curoption = $_GET['option'];
78
} else {
79
	switch($curcat) {
80
		case "system":
81
			$curoption = "processor";
82
			break;
83
		case "queues":
84
			$curoption = "queues";
85
			break;
86
		case "queuedrops":
87
			$curoption = "queuedrops";
88
			break;
89
		case "quality":
90
			foreach($databases as $database) {
91
				if(preg_match("/[-]quality\.rrd/i", $database)) {
92
					/* pick off the 1st database we find that matches the quality graph */
93
					$name = explode("-", $database);
94
					$curoption = "$name[0]";
95
					continue 2;
96
				}
97
			}
98
		case "wireless":
99
			foreach($databases as $database) {
100
				if(preg_match("/[-]wireless\.rrd/i", $database)) {
101
					/* pick off the 1st database we find that matches the wireless graph */
102
					$name = explode("-", $database);
103
					$curoption = "$name[0]";
104
					continue 2;
105
				}
106
			}
107
		case "cellular":
108
			foreach($databases as $database) {
109
				if(preg_match("/[-]cellular\.rrd/i", $database)) {
110
					/* pick off the 1st database we find that matches the celullar graph */
111
					$name = explode("-", $database);
112
					$curoption = "$name[0]";
113
					continue 2;
114
				}
115
			}
116
		case "vpnusers":
117
			foreach($databases as $database) {
118
				if(preg_match("/[-]vpnusers\.rrd/i", $database)) {
119
					/* pick off the 1st database we find that matches the VPN graphs */
120
					$name = explode("-", $database);
121
					$curoption = "$name[0]";
122
					continue 2;
123
				}
124
			}
125
		default:
126
			$curoption = "wan";
127
			break;
128
	}
129
}
130

    
131
$now = time();
132
if($curcat == "custom") {
133
	if (is_numeric($_GET['start'])) {
134
			if($start < ($now - (3600 * 24 * 365 * 5))) {
135
					$start = $now - (8 * 3600);
136
			}
137
			$start = $_GET['start'];
138
	} else {
139
			$start = $now - (8 * 3600);
140
	}
141
}
142

    
143
if (is_numeric($_GET['end'])) {
144
        $end = $_GET['end'];
145
} else {
146
        $end = $now;
147
}
148

    
149
/* this should never happen */
150
if($end < $start) {
151
	log_error("start $start is smaller than end $end");
152
        $end = $now;
153
}
154

    
155
$seconds = $end - $start;
156
					
157
if ($_GET['style']) {
158
	$curstyle = $_GET['style'];
159
} else {
160
	if(! empty($config['rrd']['style'])) {
161
		$curstyle = $config['rrd']['style'];
162
	} else {
163
		$curstyle = "inverse";
164
	}
165
}
166

    
167
/* sort names reverse so WAN comes first */
168
rsort($databases);
169

    
170
/* these boilerplate databases are required for the other menu choices */
171
$dbheader = array("allgraphs-traffic.rrd",
172
		"allgraphs-quality.rrd",
173
		"allgraphs-wireless.rrd",
174
		"allgraphs-cellular.rrd",
175
		"allgraphs-vpnusers.rrd",
176
		"allgraphs-packets.rrd",
177
		"system-allgraphs.rrd",
178
		"system-throughput.rrd",
179
		"outbound-quality.rrd",
180
		"outbound-packets.rrd",
181
		"outbound-traffic.rrd");
182

    
183
/* additional menu choices for the custom tab */
184
$dbheader_custom = array("system-throughput.rrd");
185

    
186
foreach($databases as $database) {
187
	if(stristr($database, "-wireless")) {
188
		$wireless = true;
189
	}
190
	if(stristr($database, "-queues")) {
191
		$queues = true;
192
	}
193
	if(stristr($database, "-cellular") && !empty($config['ppps'])) {
194
		$cellular = true;
195
	}
196
	if(stristr($database, "-vpnusers")) {
197
		$vpnusers = true;
198
	}
199
}
200
/* append the existing array to the header */
201
$ui_databases = array_merge($dbheader, $databases);
202
$custom_databases = array_merge($dbheader_custom, $databases);
203

    
204
$styles = array('inverse' => gettext('Inverse'),
205
		'absolute' => gettext('Absolute'));
206
$graphs = array("8hour", "day", "week", "month", "quarter", "year", "4year");
207
$periods = array("absolute" => gettext("Absolute Timespans"), "current" => gettext("Current Period"), "previous" => gettext("Previous Period"));
208
$graph_length = array(
209
	"8hour" => 28800,
210
	"day" => 86400,
211
	"week" => 604800,
212
	"month" => 2764800,
213
	"quarter" => 8035200,
214
	"year" => 31622400,
215
	"4year" => 126489600);
216

    
217
$pgtitle = array(gettext("Status"),gettext("RRD Graphs"));
218
include("head.inc");
219

    
220
function get_dates($curperiod, $graph) {
221
	global $graph_length;
222
	$now = time();
223
	$end = $now;
224

    
225
	if($curperiod == "absolute") {
226
		$start = $end - $graph_length[$graph];
227
	} else {
228
		$curyear = date('Y', $now);
229
		$curmonth = date('m', $now);
230
		$curweek = date('W', $now);
231
		$curweekday = date('N', $now) - 1; // We want to start on monday
232
		$curday = date('d', $now);
233
		$curhour = date('G', $now);
234

    
235
		switch($curperiod) {
236
			case "previous":
237
				$offset = -1;
238
				break;
239
			default:
240
				$offset = 0;
241
		}
242
		switch($graph) {
243
			case "8hour":
244
				if($curhour < 24)
245
					$starthour = 16;
246
				if($curhour < 16)
247
					$starthour = 8;
248
				if($curhour < 8)
249
					$starthour = 0;
250

    
251
				switch($offset) {
252
					case 0:
253
						$houroffset = $starthour;
254
						break;
255
					default:
256
						$houroffset = $starthour + ($offset * 8);
257
						break;
258
				}
259
				$start = mktime($houroffset, 0, 0, $curmonth, $curday, $curyear);
260
				if($offset != 0) {
261
					$end = mktime(($houroffset + 8), 0, 0, $curmonth, $curday, $curyear);
262
				}
263
				break;
264
			case "day":
265
				$start = mktime(0, 0, 0, $curmonth, ($curday + $offset), $curyear);
266
				if($offset != 0)
267
					$end = mktime(0, 0, 0, $curmonth, (($curday + $offset) + 1), $curyear);
268
				break;
269
			case "week":
270
				switch($offset) {
271
					case 0:
272
						$weekoffset = 0;
273
						break;
274
					default:
275
						$weekoffset = ($offset * 7) - 7;
276
						break;
277
				}
278
				$start = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset), $curyear);
279
				if($offset != 0)
280
					$end = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset + 7), $curyear);
281
				break;
282
			case "month":
283
				$start = mktime(0, 0, 0, ($curmonth + $offset), 0, $curyear);
284
				if($offset != 0)
285
					$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
286
				break;
287
			case "quarter":
288
				$start = mktime(0, 0, 0, (($curmonth - 2) + $offset), 0, $curyear);
289
				if($offset != 0)
290
					$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
291
				break;
292
			case "year":
293
				$start = mktime(0, 0, 0, 1, 0, ($curyear + $offset));
294
				if($offset != 0)
295
					$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
296
				break;
297
			case "4year":
298
				$start = mktime(0, 0, 0, 1, 0, (($curyear - 3) + $offset));
299
				if($offset != 0)
300
					$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
301
				break;
302
		}
303
	}
304
	// echo "start $start ". date('l jS \of F Y h:i:s A', $start) .", end $end ". date('l jS \of F Y h:i:s A', $end) ."<br>";
305
	$dates = array();
306
	$dates['start'] = $start;
307
	$dates['end'] = $end;
308
	return $dates;
309
}
310

    
311
?>
312
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
313
<?php include("fbegin.inc"); ?>
314
<table width="100%" border="0" cellpadding="0" cellspacing="0">
315
        <tr>
316
                <td>
317
			<form name="form1" action="status_rrd_graph.php" method="get">
318
			<input type="hidden" name="cat" value="<?php echo "$curcat"; ?>">
319
			<?php
320
			        $tab_array = array();
321
				if($curcat == "system") { $tabactive = True; } else { $tabactive = False; }
322
			        $tab_array[] = array(gettext("System"), $tabactive, "status_rrd_graph.php?cat=system");
323
				if($curcat == "traffic") { $tabactive = True; } else { $tabactive = False; }
324
			        $tab_array[] = array(gettext("Traffic"), $tabactive, "status_rrd_graph.php?cat=traffic");
325
				if($curcat == "packets") { $tabactive = True; } else { $tabactive = False; }
326
			        $tab_array[] = array(gettext("Packets"), $tabactive, "status_rrd_graph.php?cat=packets");
327
				if($curcat == "quality") { $tabactive = True; } else { $tabactive = False; }
328
			        $tab_array[] = array(gettext("Quality"), $tabactive, "status_rrd_graph.php?cat=quality");
329
				if($queues) {
330
					if($curcat == "queues") { $tabactive = True; } else { $tabactive = False; }
331
						$tab_array[] = array(gettext("Queues"), $tabactive, "status_rrd_graph.php?cat=queues");
332
					if($curcat == "queuedrops") { $tabactive = True; } else { $tabactive = False; }
333
						$tab_array[] = array(gettext("QueueDrops"), $tabactive, "status_rrd_graph.php?cat=queuedrops");
334
				}
335
				if($wireless) {
336
					if($curcat == "wireless") { $tabactive = True; } else { $tabactive = False; }
337
				        $tab_array[] = array(gettext("Wireless"), $tabactive, "status_rrd_graph.php?cat=wireless");
338
				}
339
				if($cellular) {
340
					if($curcat == "cellular") { $tabactive = True; } else { $tabactive = False; }
341
				        $tab_array[] = array(gettext("Cellular"), $tabactive, "status_rrd_graph.php?cat=cellular");
342
				}
343
				if($vpnusers) {
344
					if($curcat == "vpnusers") { $tabactive = True; } else { $tabactive = False; }
345
				        $tab_array[] = array("VPN", $tabactive, "status_rrd_graph.php?cat=vpnusers");
346
				}
347
				if($curcat == "custom") { $tabactive = True; } else { $tabactive = False; }
348
			        $tab_array[] = array(gettext("Custom"), $tabactive, "status_rrd_graph.php?cat=custom");
349
				if($curcat == "settings") { $tabactive = True; } else { $tabactive = False; }
350
			        $tab_array[] = array(gettext("Settings"), $tabactive, "status_rrd_graph_settings.php");
351
			        display_top_tabs($tab_array);
352
			?>
353
                </td>
354
        </tr>
355
        <tr>
356
                <td>
357
                        <div id="mainarea">
358
                        <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="0">
359
                                <tr>
360
                                        <td colspan="2" class="list"><p><b><?=gettext("Note: Change of color and/or style may not take effect until the next refresh");?></b></p></td>
361
				</tr>
362
				<tr>
363
                                        <td colspan="2" class="list">
364
					<?=gettext("Graphs:");?>
365
					<select name="option" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
366
					<?php
367

    
368
					if($curcat == "custom") {
369
						foreach ($custom_databases as $db => $database) {
370
							$optionc = split("-", $database);
371
							$search = array("-", ".rrd", $optionc);
372
							$replace = array(" :: ", "", $friendly);
373
							echo "<option value=\"{$database}\"";
374
							$prettyprint = ucwords(str_replace($search, $replace, $database));
375
							if($curoption == $database) {
376
								echo " selected";
377
							}
378
							echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
379
						}
380
					}
381
					foreach ($ui_databases as $db => $database) {
382
						if(! preg_match("/($curcat)/i", $database)) {
383
							continue;
384
						}
385
						$optionc = split("-", $database);
386
						$search = array("-", ".rrd", $optionc);
387
						$replace = array(" :: ", "", $friendly);
388

    
389
						switch($curcat) {
390
							case "system":
391
								$optionc = str_replace($search, $replace, $optionc[1]);
392
								echo "<option value=\"$optionc\"";
393
								$prettyprint = ucwords(str_replace($search, $replace, $optionc));
394
								break;
395
							default:
396
								/* Deduce a interface if possible and use the description */
397
								$optionc = "$optionc[0]";
398
								$friendly = convert_friendly_interface_to_friendly_descr(strtolower($optionc));
399
								if(empty($friendly)) {
400
									$friendly = $optionc;
401
								}
402
								$search = array("-", ".rrd", $optionc);
403
								$replace = array(" :: ", "", $friendly);
404
								echo "<option value=\"$optionc\"";
405
								$prettyprint = ucwords(str_replace($search, $replace, $friendly));
406
						}
407
						if($curoption == $optionc) {
408
							echo " selected";
409
						}
410
						echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
411
					}
412

    
413
					?>
414
					</select>
415

    
416
					<?=gettext("Style:");?>
417
					<select name="style" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
418
					<?php
419
					foreach ($styles as $style => $styled) {
420
						echo "<option value=\"$style\"";
421
						if ($style == $curstyle) echo " selected";
422
						echo ">" . htmlspecialchars($styled) . "</option>\n";
423
					}
424
					?>
425
					</select>
426
					
427
					<?php
428
					if($curcat <> "custom") {
429
					?>
430
						<?=gettext("Period:");?>
431
						<select name="period" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
432
						<?php
433
						foreach ($periods as $period => $value) {
434
							echo "<option value=\"$period\"";
435
							if ($period == $curperiod) echo " selected";
436
							echo ">" . htmlspecialchars($value) . "</option>\n";
437
						}
438
					}
439
					?>
440
					</select>
441
					<?php
442

    
443
					if($curcat == "custom") {
444
						?>
445
						<?=gettext("Start:");?>
446
						<input type="text" name="start" class="formfldunknown" length="32" value="<?php echo $start;?>">
447
						<?=gettext("End:");?>
448
						<input type="text" name="end" class="formfldunknown" length="32" value="<?php echo $now;?>">
449
						<input type="submit" name="Submit" value="<?=gettext("Go"); ?>">
450
						<?php
451
						$curdatabase = $curoption;
452
						$graph = "custom-$curdatabase";
453
						if(in_array($curdatabase, $custom_databases)) {
454
							echo "<tr><td colspan=2 class=\"list\">\n";
455
							echo "<IMG BORDER='0' name='{$graph}-{$curoption}-{$curdatabase}' ";
456
							echo "id='{$graph}-{$curoption}-{$curdatabase}' ALT=\"$prettydb Graph\" ";
457
							echo "SRC=\"status_rrd_graph_img.php?start={$start}&amp;end={$end}&amp;database={$curdatabase}&amp;style={$curstyle}&amp;graph={$graph}\" />\n";
458
							echo "<br /><hr><br />\n";								
459
							echo "</td></tr>\n";
460
						}
461
					} else {
462
						foreach($graphs as $graph) {
463
							/* check which databases are valid for our category */
464
							foreach($ui_databases as $curdatabase) {
465
								if(! preg_match("/($curcat)/i", $curdatabase)) {
466
									continue;
467
								}
468
								$optionc = split("-", $curdatabase);
469
								$search = array("-", ".rrd", $optionc);
470
								$replace = array(" :: ", "", $friendly);
471
								switch($curoption) {
472
									case "outbound":
473
										/* make sure we do not show the placeholder databases in the outbound view */
474
										if((stristr($curdatabase, "outbound")) || (stristr($curdatabase, "allgraphs"))) {
475
											continue 2;
476
										}
477
										/* only show interfaces with a gateway */
478
										$optionc = "$optionc[0]";
479
										if(!interface_has_gateway($optionc)) {
480
											if(!isset($gateways_arr)) {
481
												if(preg_match("/quality/i", $curdatabase))
482
													$gateways_arr = return_gateways_array();
483
												else
484
													$gateways_arr = array();
485
											}
486
											$found_gateway = false;
487
											foreach ($gateways_arr as $gw) {
488
												if ($gw['name'] == $optionc) {
489
													$found_gateway = true;
490
													break;
491
												}
492
											}
493
											if(!$found_gateway) {
494
												continue 2;
495
											}
496
										}
497
										if(! preg_match("/(^$optionc-|-$optionc\\.)/i", $curdatabase)) {
498
											continue 2;
499
										}
500
										break;
501
									case "allgraphs":
502
										/* make sure we do not show the placeholder databases in the all view */
503
										if((stristr($curdatabase, "outbound")) || (stristr($curdatabase, "allgraphs"))) {
504
											continue 2;
505
										}
506
										break;
507
									default:
508
										/* just use the name here */
509
										if(! preg_match("/(^$curoption-|-$curoption\\.)/i", $curdatabase)) {
510
											continue 2;
511
										}
512
								}
513
								if(in_array($curdatabase, $ui_databases)) {
514
									$dates = get_dates($curperiod, $graph);
515
									$start = $dates['start'];
516
									$end = $dates['end'];
517
									echo "<tr><td colspan=2 class=\"list\">\n";
518
									echo "<IMG BORDER='0' name='{$graph}-{$curoption}-{$curdatabase}' ";
519
									echo "id='{$graph}-{$curoption}-{$curdatabase}' ALT=\"$prettydb Graph\" ";
520
									echo "SRC=\"status_rrd_graph_img.php?start={$start}&amp;end={$end}&amp;database={$curdatabase}&amp;style={$curstyle}&amp;graph={$graph}\" />\n";
521
									echo "<br /><hr><br />\n";								
522
									echo "</td></tr>\n";
523
								}
524
							}
525
						}
526
					}
527
					?>
528
					</td>
529
				</tr>
530
				<tr>
531
					<td colspan=2 class="list">
532
					<script language="javascript">
533
						function update_graph_images() {
534
							//alert('updating');
535
							var randomid = Math.floor(Math.random()*11);
536
							<?php
537
							foreach($graphs as $graph) {
538
								/* check which databases are valid for our category */
539
								foreach($ui_databases as $curdatabase) {
540
									if(! stristr($curdatabase, $curcat)) {
541
										continue;
542
									}
543
									$optionc = split("-", $curdatabase);
544
									$search = array("-", ".rrd", $optionc);
545
									$replace = array(" :: ", "", $friendly);
546
									switch($curoption) {
547
										case "outbound":
548
											/* make sure we do not show the placeholder databases in the outbound view */
549
											if((stristr($curdatabase, "outbound")) || (stristr($curdatabase, "allgraphs"))) {
550
												continue 2;
551
											}
552
											/* only show interfaces with a gateway */
553
											$optionc = "$optionc[0]";
554
											if(!interface_has_gateway($optionc)) {
555
												if(!isset($gateways_arr))
556
													if(preg_match("/quality/i", $curdatabase))
557
														$gateways_arr = return_gateways_array();
558
													else
559
														$gateways_arr = array();
560
												$found_gateway = false;
561
												foreach ($gateways_arr as $gw) {
562
													if ($gw['name'] == $optionc) {
563
														$found_gateway = true;
564
														break;
565
													}
566
												}
567
												if(!$found_gateway) {
568
													continue 2;
569
												}
570
											}
571
											if(! preg_match("/(^$optionc-|-$optionc\\.)/i", $curdatabase)) {
572
												continue 2;
573
											}
574
											break;
575
										case "allgraphs":
576
											/* make sure we do not show the placeholder databases in the all view */
577
											if((stristr($curdatabase, "outbound")) || (stristr($curdatabase, "allgraphs"))) {
578
												continue 2;
579
											}
580
											break;
581
										default:
582
											/* just use the name here */
583
											if(! preg_match("/(^$curoption-|-$curoption\\.)/i", $curdatabase)) {
584
												continue 2;
585
											}
586
									}
587
									$dates = get_dates($curperiod, $graph);
588
									$start = $dates['start'];
589
									if($curperiod == "current") {
590
										$end = $dates['end'];
591
									}
592
									/* generate update events utilizing prototype $('') feature */
593
									echo "\n";
594
									echo "\t\t\$('{$graph}-{$curoption}-{$curdatabase}').src='status_rrd_graph_img.php?start={$start}&end={$end}&graph={$graph}&database={$curdatabase}&style={$curstyle}&tmp=' + randomid;\n";
595
									}
596
								}
597
							?>
598
							window.setTimeout('update_graph_images()', 355000);
599
						}
600
						window.setTimeout('update_graph_images()', 355000);
601
					</script>
602
					</form>
603
					</td>
604
				</tr>
605
			</table>
606
		</div>
607
		</td>
608
	</tr>
609
</table>
610

    
611
<?php include("fend.inc"); ?>
612
</body>
613
</html>
(166-166/221)