Project

General

Profile

Download (19.5 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 - (8 * 3600);
132
			}
133
			$start = $_GET['start'];
134
	} else {
135
			$start = $now - (8 * 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
	log_error("start $start is smaller than end $end");
148
        $end = $now;
149
}
150

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

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

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

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

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

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

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

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

    
218
	switch($curperiod) {
219
		case "previous":
220
			$offset = -1;
221
			break;
222
		default:
223
			$offset = 0;
224
	}
225
	switch($graph) {
226
		case "8hour":
227
			if($curhour < 24)
228
				$starthour = 16;
229
			if($curhour < 16)
230
				$starthour = 8;
231
			if($starthour < 8)                                  
232
				$starthour = 0;
233

    
234
			switch($offset) {
235
				case 0;
236
					$houroffset = $starthour;
237
					break;
238
				default:
239
					$houroffset = $starthour + ($offset * 8);
240
					break;
241
			}
242
			$start = mktime($houroffset, 0, 0, $curmonth, $curday, $curyear);
243
			if($offset != 0) {
244
				$end = mktime(($houroffset + 8), 0, 0, $curmonth, $curday, $curyear);
245
			}
246
			break;
247
		case "day":
248
			$start = mktime(0, 0, 0, $curmonth, ($curday + $offset), $curyear);
249
			if($offset != 0)
250
				$end = mktime(0, 0, 0, $curmonth, (($curday + $offset) + 1), $curyear);
251
			break;
252
		case "week":
253
			switch($offset) {
254
				case 0;
255
					$weekoffset = 0;
256
					break;
257
				default:
258
					$weekoffset = ($offset * 7) - 7;
259
					break;
260
			}
261
			$start = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset), $curyear);
262
			if($offset != 0)
263
				$end = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset + 7), $curyear);
264
			break;
265
		case "month":
266
			$start = mktime(0, 0, 0, ($curmonth + $offset), 0, $curyear);
267
			if($offset != 0)
268
				$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
269
			break;
270
		case "quarter":
271
			$start = mktime(0, 0, 0, (($curmonth - 2) + $offset), 0, $curyear);
272
			if($offset != 0)
273
				$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
274
			break;
275
		case "year":
276
			$start = mktime(0, 0, 0, 1, 0, ($curyear + $offset));
277
			if($offset != 0)
278
				$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
279
			break;
280
		case "4year": 
281
			$start = mktime(0, 0, 0, 1, 0, (($curyear - 3) + $offset));
282
			if($offset != 0)
283
				$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
284
			break;
285
	}
286
	// 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>";
287
	$dates = array();
288
	$dates['start'] = $start;
289
	$dates['end'] = $end;
290
	return $dates;
291
}
292

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

    
350
					if($curcat == "custom") {
351
						foreach ($custom_databases as $db => $database) {
352
							$optionc = split("-", $database);
353
							$search = array("-", ".rrd", $optionc);
354
							$replace = array(" :: ", "", $friendly);
355
							echo "<option value=\"{$database}\"";
356
							$prettyprint = ucwords(str_replace($search, $replace, $database));
357
							if($curoption == $database) {
358
								echo " selected";
359
							}
360
							echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
361
						}
362
					}
363
					foreach ($ui_databases as $db => $database) {
364
						if(! preg_match("/($curcat)/i", $database)) {
365
							continue;
366
						}
367
						$optionc = split("-", $database);
368
						$search = array("-", ".rrd", $optionc);
369
						$replace = array(" :: ", "", $friendly);
370

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

    
395
					?>
396
					</select>
397

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

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

    
593
<?php include("fend.inc"); ?>
594
</body>
595
</html>
(166-166/221)