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

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

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

    
386
					?>
387
					</select>
388

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

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

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