Project

General

Profile

Download (19.3 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 = $_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
	$curperiod = "current";
70
}
71

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

    
127
$now = time();
128
if($curcat == "custom") {
129
	if (is_numeric($_GET['start'])) {
130
			if($start < ($now - (3600 * 24 * 365 * 5))) {
131
					$start = $now - (4 * 3600);
132
			}
133
			$start = $_GET['start'];
134
	} else {
135
			$start = $now - (4 * 3600);
136
	}
137
}
138

    
139
if (is_numeric($_GET['end'])) {
140
        $end = $_GET['end'];
141
} else {
142
        $end = $now;
143
}
144

    
145
/* this should never happen */
146
if($end < $start) {
147
        $end = $now;
148
}
149

    
150
$seconds = $end - $start;
151
					
152
if ($_GET['style']) {
153
	$curstyle = $_GET['style'];
154
} else {
155
	if(! empty($config['rrd']['style'])) {
156
		$curstyle = $config['rrd']['style'];
157
	} else {
158
		$curstyle = "inverse";
159
	}
160
}
161

    
162
/* sort names reverse so WAN comes first */
163
rsort($databases);
164

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

    
178
/* additional menu choices for the custom tab */
179
$dbheader_custom = array("system-throughput.rrd");
180

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

    
199
$styles = array('inverse' => gettext('Inverse'),
200
		'absolute' => gettext('Absolute'));
201
$graphs = array("day", "week", "month", "quarter", "year", "4year");
202
$periods = array("current" => gettext("Current Period"), "previous" => gettext("Previous Period"));
203

    
204
$pgtitle = array(gettext("Status"),gettext("RRD Graphs"));
205
include("head.inc");
206

    
207
function get_dates($curperiod, $graph) {
208
	$now = time();
209
	$end = $now;
210
	$curyear = date('Y', $now);
211
	$curmonth = date('m', $now);
212
	$curweek = date('W', $now);
213
	$curweekday = date('N', $now) - 1; // We want to start on monday
214
	$curday = date('d', $now);
215

    
216
	switch($curperiod) {
217
		case "previous":
218
			$offset = -1;
219
			break;
220
		default:
221
			$offset = 0;
222
	}
223
	switch($graph) {
224
		case "12hour":
225
			switch($offset) {
226
				case 0;
227
					$houroffset = 0;
228
					break;
229
				default:
230
					$houroffset = ($offset * 12) - 12;
231
					break;
232
			}
233
			$start = mktime((8 + $houroffset), 0, 0, $curmonth, $curday, $curyear);
234
			if(($offset != 0) || (($end - ($start + (12 * 3600)) ) > 0) ) {
235
				$end = mktime((8 + $houroffset) + 12, 0, 0, $curmonth, $curday, $curyear);
236
			}
237
			break;
238
		case "day":
239
			$start = mktime(0, 0, 0, $curmonth, ($curday + $offset), $curyear);
240
			if($offset != 0)
241
				$end = mktime(0, 0, 0, $curmonth, (($curday + $offset) + 1), $curyear);
242
			break;
243
		case "week":
244
			switch($offset) {
245
				case 0;
246
					$weekoffset = 0;
247
					break;
248
				default:
249
					$weekoffset = ($offset * 7) - 7;
250
					break;
251
			}
252
			$start = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset), $curyear);
253
			if($offset != 0)
254
				$end = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset + 7), $curyear);
255
			break;
256
		case "month":
257
			$start = mktime(0, 0, 0, ($curmonth + $offset), 0, $curyear);
258
			if($offset != 0)
259
				$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
260
			break;
261
		case "quarter":
262
			$start = mktime(0, 0, 0, (($curmonth - 2) + $offset), 0, $curyear);
263
			if($offset != 0)
264
				$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
265
			break;
266
		case "year":
267
			$start = mktime(0, 0, 0, 1, 0, ($curyear + $offset));
268
			if($offset != 0)
269
				$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
270
			break;
271
		case "4year": 
272
			$start = mktime(0, 0, 0, 1, 0, (($curyear - 3) + $offset));
273
			if($offset != 0)
274
				$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
275
			break;
276
	}
277
	// 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>";
278
	$dates = array();
279
	$dates['start'] = $start;
280
	$dates['end'] = $end;
281
	return $dates;
282
}
283

    
284

    
285
?>
286
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
287
<?php include("fbegin.inc"); ?>
288
<table width="100%" border="0" cellpadding="0" cellspacing="0">
289
        <tr>
290
                <td>
291
			<form name="form1" action="status_rrd_graph.php" method="get">
292
			<input type="hidden" name="cat" value="<?php echo "$curcat"; ?>">
293
			<?php
294
			        $tab_array = array();
295
				if($curcat == "system") { $tabactive = True; } else { $tabactive = False; }
296
			        $tab_array[] = array(gettext("System"), $tabactive, "status_rrd_graph.php?cat=system");
297
				if($curcat == "traffic") { $tabactive = True; } else { $tabactive = False; }
298
			        $tab_array[] = array(gettext("Traffic"), $tabactive, "status_rrd_graph.php?cat=traffic");
299
				if($curcat == "packets") { $tabactive = True; } else { $tabactive = False; }
300
			        $tab_array[] = array(gettext("Packets"), $tabactive, "status_rrd_graph.php?cat=packets");
301
				if($curcat == "quality") { $tabactive = True; } else { $tabactive = False; }
302
			        $tab_array[] = array(gettext("Quality"), $tabactive, "status_rrd_graph.php?cat=quality");
303
				if($queues) {
304
					if($curcat == "queues") { $tabactive = True; } else { $tabactive = False; }
305
						$tab_array[] = array(gettext("Queues"), $tabactive, "status_rrd_graph.php?cat=queues");
306
					if($curcat == "queuedrops") { $tabactive = True; } else { $tabactive = False; }
307
						$tab_array[] = array(gettext("QueueDrops"), $tabactive, "status_rrd_graph.php?cat=queuedrops");
308
				}
309
				if($wireless) {
310
					if($curcat == "wireless") { $tabactive = True; } else { $tabactive = False; }
311
				        $tab_array[] = array(gettext("Wireless"), $tabactive, "status_rrd_graph.php?cat=wireless");
312
				}
313
				if($cellular) {
314
					if($curcat == "cellular") { $tabactive = True; } else { $tabactive = False; }
315
				        $tab_array[] = array(gettext("Cellular"), $tabactive, "status_rrd_graph.php?cat=cellular");
316
				}
317
				if($vpnusers) {
318
					if($curcat == "vpnusers") { $tabactive = True; } else { $tabactive = False; }
319
				        $tab_array[] = array("VPN", $tabactive, "status_rrd_graph.php?cat=vpnusers");
320
				}
321
				if($curcat == "custom") { $tabactive = True; } else { $tabactive = False; }
322
			        $tab_array[] = array(gettext("Custom"), $tabactive, "status_rrd_graph.php?cat=custom");
323
				if($curcat == "settings") { $tabactive = True; } else { $tabactive = False; }
324
			        $tab_array[] = array(gettext("Settings"), $tabactive, "status_rrd_graph_settings.php");
325
			        display_top_tabs($tab_array);
326
			?>
327
                </td>
328
        </tr>
329
        <tr>
330
                <td>
331
                        <div id="mainarea">
332
                        <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="0">
333
                                <tr>
334
                                        <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>
335
				</tr>
336
				<tr>
337
                                        <td colspan="2" class="list">
338
					<?=gettext("Graphs:");?>
339
					<select name="option" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
340
					<?php
341

    
342
					if($curcat == "custom") {
343
						foreach ($custom_databases as $db => $database) {
344
							$optionc = split("-", $database);
345
							$search = array("-", ".rrd", $optionc);
346
							$replace = array(" :: ", "", $friendly);
347
							echo "<option value=\"{$database}\"";
348
							$prettyprint = ucwords(str_replace($search, $replace, $database));
349
							if($curoption == $database) {
350
								echo " selected";
351
							}
352
							echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
353
						}
354
					}
355
					foreach ($ui_databases as $db => $database) {
356
						if(! preg_match("/($curcat)/i", $database)) {
357
							continue;
358
						}
359
						$optionc = split("-", $database);
360
						$search = array("-", ".rrd", $optionc);
361
						$replace = array(" :: ", "", $friendly);
362

    
363
						switch($curcat) {
364
							case "system":
365
								$optionc = str_replace($search, $replace, $optionc[1]);
366
								echo "<option value=\"$optionc\"";
367
								$prettyprint = ucwords(str_replace($search, $replace, $optionc));
368
								break;
369
							default:
370
								/* Deduce a interface if possible and use the description */
371
								$optionc = "$optionc[0]";
372
								$friendly = convert_friendly_interface_to_friendly_descr(strtolower($optionc));
373
								if(empty($friendly)) {
374
									$friendly = $optionc;
375
								}
376
								$search = array("-", ".rrd", $optionc);
377
								$replace = array(" :: ", "", $friendly);
378
								echo "<option value=\"$optionc\"";
379
								$prettyprint = ucwords(str_replace($search, $replace, $friendly));
380
						}
381
						if($curoption == $optionc) {
382
							echo " selected";
383
						}
384
						echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
385
					}
386

    
387
					?>
388
					</select>
389

    
390
					<?=gettext("Style:");?>
391
					<select name="style" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
392
					<?php
393
					foreach ($styles as $style => $styled) {
394
						echo "<option value=\"$style\"";
395
						if ($style == $curstyle) echo " selected";
396
						echo ">" . htmlspecialchars($styled) . "</option>\n";
397
					}
398
					?>
399
					</select>
400
					
401
					<?php
402
					if($curcat <> "custom") {
403
					?>
404
						<?=gettext("Period:");?>
405
						<select name="period" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
406
						<?php
407
						foreach ($periods as $period => $value) {
408
							echo "<option value=\"$period\"";
409
							if ($period == $curperiod) echo " selected";
410
							echo ">" . htmlspecialchars($value) . "</option>\n";
411
						}
412
					}
413
					?>
414
					</select>
415
					<?php
416

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

    
583
<?php include("fend.inc"); ?>
584
</body>
585
</html>
(165-165/220)