Project

General

Profile

Download (17 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' => 'Inverse',
196
		'absolute' => 'Absolute');
197
$graphs = array("day", "week", "month", "quarter", "year", "4year");
198
$periods = array("current" => "Current Period", "previous" => "Previous Period");
199

    
200
$pgtitle = array("Status","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 "day":
221
			$start = mktime(0, 0, 0, $curmonth, ($curday + $offset), $curyear);
222
			$end = mktime(0, 0, 0, $curmonth, (($curday + $offset) + 1), $curyear);
223
			break;
224
		case "week":
225
			switch($offset) {
226
				case 0;
227
					$weekoffset = 0;
228
					break;
229
				default:
230
					$weekoffset = ($offset * 7) - 7;
231
					break;
232
			}
233
			$start = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset), $curyear);
234
			$end = mktime(0, 0, 0, $curmonth, (($curday - $curweekday) + $weekoffset + 7), $curyear);
235
			break;
236
		case "month":
237
			$start = mktime(0, 0, 0, ($curmonth + $offset), 0, $curyear);
238
			$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
239
			break;
240
		case "quarter":
241
			$start = mktime(0, 0, 0, (($curmonth - 2) + $offset), 0, $curyear);
242
			$end = mktime(0, 0, 0, (($curmonth + $offset) + 1), 0, $curyear);
243
			break;
244
		case "year":
245
			$start = mktime(0, 0, 0, 1, 0, ($curyear + $offset));
246
			$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
247
			break;
248
		case "4year": 
249
			$start = mktime(0, 0, 0, 1, 0, (($curyear - 3) + $offset));
250
			$end = mktime(0, 0, 0, 1, 0, (($curyear + $offset) +1));
251
			break;
252
	}
253
	// 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>";
254
	$dates = array();
255
	$dates['start'] = $start;
256
	$dates['end'] = $end;
257
	return $dates;
258
}
259

    
260

    
261
?>
262
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
263
<?php include("fbegin.inc"); ?>
264
<table width="100%" border="0" cellpadding="0" cellspacing="0">
265
        <tr>
266
                <td>
267
			<form name="form1" action="status_rrd_graph.php" method="get">
268
			<input type="hidden" name="cat" value="<?php echo "$curcat"; ?>">
269
			<?php
270
			        $tab_array = array();
271
				if($curcat == "system") { $tabactive = True; } else { $tabactive = False; }
272
			        $tab_array[] = array("System", $tabactive, "status_rrd_graph.php?cat=system");
273
				if($curcat == "traffic") { $tabactive = True; } else { $tabactive = False; }
274
			        $tab_array[] = array("Traffic", $tabactive, "status_rrd_graph.php?cat=traffic");
275
				if($curcat == "packets") { $tabactive = True; } else { $tabactive = False; }
276
			        $tab_array[] = array("Packets", $tabactive, "status_rrd_graph.php?cat=packets");
277
				if($curcat == "quality") { $tabactive = True; } else { $tabactive = False; }
278
			        $tab_array[] = array("Quality", $tabactive, "status_rrd_graph.php?cat=quality");
279
				if($queues) {
280
					if($curcat == "queues") { $tabactive = True; } else { $tabactive = False; }
281
						$tab_array[] = array("Queues", $tabactive, "status_rrd_graph.php?cat=queues");
282
					if($curcat == "queuedrops") { $tabactive = True; } else { $tabactive = False; }
283
						$tab_array[] = array("QueueDrops", $tabactive, "status_rrd_graph.php?cat=queuedrops");
284
				}
285
				if($wireless) {
286
					if($curcat == "wireless") { $tabactive = True; } else { $tabactive = False; }
287
				        $tab_array[] = array("Wireless", $tabactive, "status_rrd_graph.php?cat=wireless");
288
				}
289
				if($cellular) {
290
					if($curcat == "cellular") { $tabactive = True; } else { $tabactive = False; }
291
				        $tab_array[] = array("Cellular", $tabactive, "status_rrd_graph.php?cat=cellular");
292
				}
293
				if($vpnusers) {
294
					if($curcat == "vpnusers") { $tabactive = True; } else { $tabactive = False; }
295
				        $tab_array[] = array("VPN", $tabactive, "status_rrd_graph.php?cat=vpnusers");
296
				}
297
				if($curcat == "custom") { $tabactive = True; } else { $tabactive = False; }
298
			        $tab_array[] = array("Custom", $tabactive, "status_rrd_graph.php?cat=custom");
299
				if($curcat == "settings") { $tabactive = True; } else { $tabactive = False; }
300
			        $tab_array[] = array("Settings", $tabactive, "status_rrd_graph_settings.php");
301
			        display_top_tabs($tab_array);
302
			?>
303
                </td>
304
        </tr>
305
        <tr>
306
                <td>
307
                        <div id="mainarea">
308
                        <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="0">
309
                                <tr>
310
                                        <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>
311
				</tr>
312
				<tr>
313
                                        <td colspan="2" class="list">
314
					<?=gettext("Graphs:");?>
315
					<select name="option" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
316
					<?php
317

    
318
					if($curcat == "custom") {
319
						foreach ($databases as $db => $database) {
320
							$optionc = split("-", $database);
321
							$search = array("-", ".rrd", $optionc);
322
							$replace = array(" :: ", "", $friendly);
323
							echo "<option value=\"{$database}\"";
324
							$prettyprint = ucwords(str_replace($search, $replace, $database));
325
							if($curoption == $database) {
326
								echo " selected ";
327
							}
328
							echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
329
						}
330
					}
331
					foreach ($ui_databases as $db => $database) {
332
						if(! preg_match("/($curcat)/i", $database)) {
333
							continue;
334
						}
335
						$optionc = split("-", $database);
336
						$search = array("-", ".rrd", $optionc);
337
						$replace = array(" :: ", "", $friendly);
338

    
339
						switch($curcat) {
340
							case "system":
341
								$optioncf = str_replace($search, $replace, $optionc[1]);
342
								echo "<option value=\"$optioncf\"";
343
								$prettyprint = ucwords(str_replace($search, $replace, $optioncf));
344
								break;
345
							default:
346
								/* Deduce a interface if possible and use the description */
347
								$optionc = "$optionc[0]";
348
								$friendly = convert_friendly_interface_to_friendly_descr(strtolower($optionc));
349
								if(empty($friendly)) {
350
									$friendly = $optionc;
351
								}
352
								$search = array("-", ".rrd", $optionc);
353
								$replace = array(" :: ", "", $friendly);
354
								echo "<option value=\"$optionc\"";
355
								$prettyprint = ucwords(str_replace($search, $replace, $friendly));
356
						}
357
						if($curoption == $optionc) {
358
							echo " selected ";
359
						}
360
						echo ">" . htmlspecialchars($prettyprint) . "</option>\n";
361
					}
362

    
363
					?>
364
					</select>
365

    
366
					<?=gettext("Style:");?>
367
					<select name="style" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
368
					<?php
369
					foreach ($styles as $style => $styled) {
370
						echo "<option value=\"$style\"";
371
						if ($style == $curstyle) echo " selected";
372
						echo ">" . htmlspecialchars($styled) . "</option>\n";
373
					}
374
					?>
375
					</select>
376
					
377
					<?
378
					if($curcat <> "custom") {
379
					?>
380
						<?=gettext("Period:");?>
381
						<select name="period" class="formselect" style="z-index: -10;" onchange="document.form1.submit()">
382
						<?php
383
						foreach ($periods as $period => $value) {
384
							echo "<option value=\"$period\"";
385
							if ($period == $curperiod) echo " selected";
386
							echo ">" . htmlspecialchars($value) . "</option>\n";
387
						}
388
					}
389
					?>
390
					</select>
391
					<?php
392

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

    
522
<?php include("fend.inc"); ?>
523
</body>
524
</html>
(166-166/222)