Project

General

Profile

Download (17.6 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
foreach($databases as $database) {
179
	if(stristr($database, "-wireless")) {
180
		$wireless = true;
181
	}
182
	if(stristr($database, "-queues")) {
183
		$queues = true;
184
	}
185
	if(stristr($database, "-cellular")) {
186
		$cellular = true;
187
	}
188
	if(stristr($database, "-vpnusers")) {
189
		$vpnusers = true;
190
	}
191
}
192
/* append the existing array to the header */
193
$ui_databases = array_merge($dbheader, $databases);
194

    
195
$styles = array('inverse' => gettext('Inverse'),
196
		'absolute' => gettext('Absolute'));
197
$graphs = array("12hour", "day", "week", "month", "quarter", "year", "4year");
198
$periods = array("current" => gettext("Current Period"), "previous" => gettext("Previous Period"));
199

    
200
$pgtitle = array(gettext("Status"),gettext("RRD Graphs"));
201
include("head.inc");
202

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

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

    
279

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

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

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

    
382
					?>
383
					</select>
384

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

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

    
541
<?php include("fend.inc"); ?>
542
</body>
543
</html>
(166-166/222)