summaryrefslogtreecommitdiffstats
path: root/usr/local/www/status_rrd_graph_img.php
blob: c3a7c9e10e741a2a65d99640e063bc69ddfe93f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/* $Id$ */
/*
	status_rrd_graph_img.php
	Part of pfSense
	Copyright (C) 2004 Scott Ullrich
	All rights reserved.

	Originally part of m0n0wall (http://m0n0.ch/wall)
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

require("guiconfig.inc");

if ($_GET['if']) {
	$curif = $_GET['if'];
	$ifnum = $config['interfaces'][$curif]['if'];
} else {
	$curif = "wan";
}

if ($_GET['graph']) {
	$curgraph = $_GET['graph'];
} else {
	$curgraph = "traffic";
}

if ($_GET['interval']) {
	$interval = $_GET['interval'];
} else {
	$interval = "2h";
}

$ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
$graphs = array('traffic' => 'Traffic', 'quality' => 'Quality', 'queues' => 'Queues', 'packets' => 'Packets', 'spamd' => 'Spamd');

for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
	$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
}

$periods = array("2h", "6h", "48h", "14d", "2m", "18m");

$found = 0;
foreach($periods as $period) if($period == $interval) $found = 1;
if($found == 0) {
        PRINT "Graph interval $interval is not valid <br>\n";
        die();
}

$graphs['2h']['seconds'] = 7200;
$graphs['2h']['average'] = 60;
$graphs['2h']['scale'] = "MINUTE:5:MINUTE:10:MINUTE:30:0:%H%:%M";
$graphs['6h']['seconds'] = 21600;
$graphs['6h']['average'] = 300;
$graphs['6h']['scale'] = "MINUTE:10:MINUTE:30:MINUTE:30:0:%H%:%M";
$graphs['48h']['seconds'] = 108000;
$graphs['48h']['average'] = 300;
$graphs['48h']['scale'] = "HOUR:1:HOUR:6:HOUR:2:0:%H";
$graphs['14d']['seconds'] = 1209600;
$graphs['14d']['average'] = 600;
$graphs['14d']['scale'] = "HOUR:6:DAY:1:DAY:1:0:%a";
$graphs['2m']['seconds'] = 5184000;
$graphs['2m']['average'] = 3600;
$graphs['2m']['scale'] = "DAY:1:WEEK:1:WEEK:1:0:Week %W";
$graphs['18m']['seconds'] = 46656000;
$graphs['18m']['average'] = 86400;
$graphs['18m']['scale'] = "MONTH:1:MONTH:1:MONTH:1:0:%b";

$rrddbpath = "/var/db/rrd/";
$traffic = "-traffic.rrd";
$quality = "-quality.rrd";
$queues = "-queues.rrd";
$packets = "-packets.rrd";
$spamd = "spamd.rrd";
$rrdtool = "/usr/local/bin/rrdtool";
$uptime = "/usr/bin/uptime";
$sed = "/usr/bin/sed";

/* FIXME: We need real shaper speeds here, yes i have a 22/2Mbps 
internet connection */
/* compare bytes/sec counters, divide bps by 8 */
$downstream = (22000000/8);
$upstream   =  (2000000/8);

/* FIXME: determine down and up interface, note: case insensitive */
$upif = "wan";
$downif = "lan";

/* generate the graphs when we request the page. */
$seconds = $graphs[$interval]['seconds'];
$average = $graphs[$interval]['average'];
$scale = $graphs[$interval]['scale'];

if(($curgraph == "traffic") && (file_exists("$rrddbpath$curif$traffic"))) {
	/* define graphcmd for traffic stats */
	$graphcmd = "$rrdtool graph $rrddbpath$curif-$interval-$curgraph.png \\
		--start -$seconds -e -$average \\
		--vertical-label \"bits/sec\" \\
		--title \"`hostname` - $curgraph - $interval\" \\
		--height 100 --width 650 -x \"$scale\" \\
		DEF:$curif-in_bytes=$rrddbpath$curif$traffic:in:AVERAGE \\
		DEF:$curif-out_bytes=$rrddbpath$curif$traffic:out:AVERAGE \\
		\"CDEF:$curif-in_bits=$curif-in_bytes,8,*\" \\
		\"CDEF:$curif-out_bits=$curif-out_bytes,8,*\" \\
		\"CDEF:$curif-bits_io=$curif-in_bits,$curif-out_bits,+\" \\
		\"CDEF:$curif-out_bits_neg=$curif-out_bits,-1,*\" \\
		\"CDEF:$curif-bytes_in=$curif-in_bytes,0,12500000,LIMIT,UN,0,$curif-in_bytes,IF,$average,*\" \\
		\"CDEF:$curif-bytes_out=$curif-out_bytes,0,12500000,LIMIT,UN,0,$curif-out_bytes,IF,$average,*\" \\
		\"CDEF:$curif-bytes=$curif-bytes_in,$curif-bytes_out,+\" \\
		\"CDEF:$curif-bytes_in_t=$curif-in_bytes,0,12500000,LIMIT,UN,0,$curif-in_bytes,IF,$seconds,*\" \\
		\"CDEF:$curif-bytes_out_t=$curif-out_bytes,0,12500000,LIMIT,UN,0,$curif-out_bytes,IF,$seconds,*\" \\
		\"CDEF:$curif-bytes_t=$curif-bytes_in_t,$curif-bytes_out_t,+\" \\
		AREA:$curif-in_bits#990000:$curif-in \\
		AREA:$curif-out_bits_neg#666666:$curif-out \\
		COMMENT:\"\\n\"\\
		COMMENT:\"\t\t  maximum       average       current        period\\n\"\\
		COMMENT:\"in\t\"\\
		GPRINT:$curif-in_bits:MAX:'%7.2lf %sb/s'\\
		GPRINT:$curif-in_bits:AVERAGE:'%7.2lf %Sb/s'\\
		GPRINT:$curif-in_bits:LAST:'%7.2lf %Sb/s'\\
		GPRINT:$curif-bytes_in_t:AVERAGE:'%7.2lf %sB i'\\
		COMMENT:\"\\n\"\\
		COMMENT:\"out\t\"\\
		GPRINT:$curif-out_bits:MAX:'%7.2lf %sb/s'\\
		GPRINT:$curif-out_bits:AVERAGE:'%7.2lf %Sb/s'\\
		GPRINT:$curif-out_bits:LAST:'%7.2lf %Sb/s'\\
		GPRINT:$curif-bytes_out_t:AVERAGE:'%7.2lf %sB o'\\
		COMMENT:\"\\n\"\\
		COMMENT:\"totals\"\\
		GPRINT:$curif-bits_io:MAX:'%7.2lf %sb/s'\\
		GPRINT:$curif-bits_io:AVERAGE:'%7.2lf %sb/s'\\
		GPRINT:$curif-bits_io:LAST:'%7.2lf %sb/s'\\
		GPRINT:$curif-bytes_t:AVERAGE:'%7.2lf %sB t'\\
        	COMMENT:\"\\n\"\\
		COMMENT:\"\t\t\t\t\t\t\t\t\t\t\t\t\t\t`date +\"%b %d %H\:%M\:%S %Y\"`\"";
	}
elseif(($curgraph == "packets") && (file_exists("$rrddbpath$curif$packets"))) {
	/* define graphcmd for packets stats */
	$graphcmd = "$rrdtool graph $rrddbpath$curif-$interval-$curgraph.png \\
		--start -$seconds -e -$average \\
		--vertical-label \"packets/sec\" \\
		--title \"`hostname` - $curgraph - $interval\" \\
		--height 100 --width 650 -x \"$scale\" \\
		DEF:$curif-in_pps=$rrddbpath$curif$packets:in:AVERAGE \\
		DEF:$curif-out_pps=$rrddbpath$curif$packets:out:AVERAGE \\
		\"CDEF:$curif-out_pps_neg=$curif-out_pps,-1,*\" \\
		\"CDEF:$curif-pps_in=$curif-in_pps,0,12500000,LIMIT,UN,0,$curif-in_pps,IF,$average,*\" \\
		\"CDEF:$curif-pps_out=$curif-out_pps,0,12500000,LIMIT,UN,0,$curif-out_pps,IF,$average,*\" \\
		\"CDEF:$curif-pps_io=$curif-in_pps,$curif-out_pps,+\" \\
		\"CDEF:$curif-pps=$curif-pps_in,$curif-pps_out,+\" \\
		\"CDEF:$curif-pps_in_t=$curif-in_pps,0,12500000,LIMIT,UN,0,$curif-in_pps,IF,$seconds,*\" \\
		\"CDEF:$curif-pps_out_t=$curif-out_pps,0,12500000,LIMIT,UN,0,$curif-out_pps,IF,$seconds,*\" \\
		\"CDEF:$curif-pps_t=$curif-pps_in_t,$curif-pps_out_t,+\" \\
		AREA:$curif-in_pps#990000:$curif-in \\
		AREA:$curif-out_pps_neg#666666:$curif-out \\
		COMMENT:\"\\n\"\\
		COMMENT:\"\t\t  maximum       average       current        period\\n\"\\
		COMMENT:\"in\t\"\\
		GPRINT:$curif-in_pps:MAX:'%7.2lf %s pps'\\
		GPRINT:$curif-in_pps:AVERAGE:'%7.2lf %S pps'\\
		GPRINT:$curif-in_pps:LAST:'%7.2lf %S pps'\\
		GPRINT:$curif-pps_in_t:AVERAGE:'%7.2lf %s pkts'\\
		COMMENT:\"\\n\"\\
		COMMENT:\"out\t\"\\
		GPRINT:$curif-out_pps:MAX:'%7.2lf %s pps'\\
		GPRINT:$curif-out_pps:AVERAGE:'%7.2lf %S pps'\\
		GPRINT:$curif-out_pps:LAST:'%7.2lf %S pps'\\
		GPRINT:$curif-pps_out_t:AVERAGE:'%7.2lf %s pkts'\\
		COMMENT:\"\\n\"\\
		COMMENT:\"totals\"\\
		GPRINT:$curif-pps_io:MAX:'%7.2lf %s pps'\\
		GPRINT:$curif-pps_io:AVERAGE:'%7.2lf %s pps'\\
		GPRINT:$curif-pps_io:LAST:'%7.2lf %s pps'\\
		GPRINT:$curif-pps_t:AVERAGE:'%7.2lf %s pkts'\\
        	COMMENT:\"\\n\"\\
		COMMENT:\"\t\t\t\t\t\t\t\t\t\t\t\t\t\t`date +\"%b %d %H\:%M\:%S %Y\"`\"";
	}
elseif(($curgraph == "queues") && (file_exists("$rrddbpath$curif$queues"))) {
	/* define graphcmd for queue stats */
	$graphcmd = "$rrdtool graph $rrddbpath$curif-$interval-$curgraph.png \\
		--start -$seconds -e -$average \\
		--vertical-label \"bits/sec\" \\
		--title \"`hostname` - $curgraph - $interval\" \\
		--height 200 --width 650 -x \"$scale\" \\";
		if (!is_array($config['shaper']['queue'])) {
			$config['shaper']['queue'] = array();
		}
		$a_queues = &$config['shaper']['queue'];
		/* determine in and out interface at a later time. Asume wan for now.*/
		$i = 0;
		$t = 0;
		$colors = array('000000','7B0000','990000','BB0000','CC0000','D90000','EE0000','FF0000','CC0000');
		foreach ($a_queues as $queue) {
			$name = $queue['name'];
			if((stristr($name, "$upif")) || (stristr($name, "up"))) {
				$color = "$colors[$t]";
				if($t > 0) { $stack = ":STACK"; }
				$graphcmd .= "DEF:$name=$rrddbpath$curif$queues:$name:AVERAGE \\
					\"CDEF:$name-bytes_out=$name,0,$upstream,LIMIT,UN,0,$name,IF\" \\
					\"CDEF:$name-bits_out=$name-bytes_out,8,*\" \\
					\"CDEF:$name-bits_out_neg=$name-bits_out,-1,*\" \\
					AREA:$name-bits_out_neg#${color}:$name$stack \\";
					$t++;
					if($t > 7) { $t = 0; }
			}
		}
		$graphcmd .= "COMMENT:\"\\n\" \\";
		$colors = array('000000','7B7B7B','999999','BBBBBB','CCCCCC','D9D9D9','EEEEEE','FFFFFF','CCCCCC');
		$stack = "";
		foreach ($a_queues as $queue) {
			$name = $queue['name'];
			if((stristr($name, "$downif")) || (stristr($name, "down"))) {
				$color = "$colors[$i]";
				if($i > 0) { $stack = ":STACK"; }
				$graphcmd .= "DEF:$name=$rrddbpath$curif$queues:$name:AVERAGE \\
					\"CDEF:$name-bytes_in=$name,0,$downstream,LIMIT,UN,0,$name,IF\" \\
					\"CDEF:$name-bits_in=$name-bytes_in,8,*\" \\
					AREA:$name-bits_in#${color}:$name$stack \\";
					$i++;
					if($i > 7) { $i = 0; }
			}
		}
		$graphcmd .= "COMMENT:\"\\n\" \\";
		$graphcmd .= "COMMENT:\"\t\t\t\t\t\t\t\t\t\t\t\t\t\t`date +\"%b %d %H\:%M\:%S %Y\"`\"";
	}
elseif(($curgraph == "quality") && (file_exists("$rrddbpath$curif$quality"))) {
	/* make a link quality graphcmd, we only have WAN for now, others too follow */
	$graphcmd = "$rrdtool graph $rrddbpath$curif-$interval-$curgraph.png \\
		--start -$seconds -e -$average \\
		--title=\"Link quality last $interval for $curif\" \\
		--vertical-label \"ms / %\" \\
		--height 100 --width 650 \\
		-x \"$scale\" --lower-limit 0 \\
		DEF:roundtrip=$rrddbpath$curif$quality:roundtrip:AVERAGE \\
		DEF:loss=$rrddbpath$curif$quality:loss:AVERAGE \\
		\"CDEF:roundavg=roundtrip,PREV(roundtrip),+,2,/\" \\
		\"CDEF:loss10=loss,10,*\" \\
		\"CDEF:r0=roundtrip,20,MIN\" \\
		\"CDEF:r1=roundtrip,60,MIN\" \\
		\"CDEF:r2=roundtrip,180,MIN\" \\
		\"CDEF:r3=roundtrip,420,MIN\" \\
		COMMENT:\"               * Roundtrip *                                     * Packet loss *\\n\" \\
		COMMENT:\"\\n\" \\
		COMMENT:\"  \" \\
		AREA:roundtrip#990000:\"> 420      ms\" \\
		GPRINT:roundtrip:MIN:\"    Min\\: %7.2lf ms\" \\
		COMMENT:\"               \" \\
		GPRINT:loss:MIN:\"Min\\: %3.1lf %%\\n\" \\
		COMMENT:\"  \" \\
    		AREA:r3#a83c3c:\"180-420    ms\" \\
		GPRINT:roundtrip:AVERAGE:\"    Avg\\: %7.2lf ms\" \\
		COMMENT:\"               \" \\
		GPRINT:loss:AVERAGE:\"Avg\\: %3.1lf %%\" \\
		COMMENT:\"   Packet loss multiplied\\n\" \\
		COMMENT:\"  \" \\
		AREA:r2#b36666:\"60-180     ms\" \\
		GPRINT:roundtrip:MAX:\"    Max\\: %7.2lf ms\" \\
		COMMENT:\"               \" \\
		GPRINT:loss:MAX:\"Max\\: %3.1lf %%\" \\
		COMMENT:\"   by 10 in graph.\\n\" \\
		COMMENT:\"  \" \\
		AREA:r1#bd9090:\"20-60      ms\" \\
		COMMENT:\"\\n\" \\
		COMMENT:\"  \" \\
		AREA:r0#cccccc:\"< 20       ms\" \\
		GPRINT:roundtrip:LAST:\"    Last\\: %7.2lf ms\" \\
		COMMENT:\"              \" \\
		GPRINT:loss:LAST:\"Last\: %3.1lf %%\" \\
		COMMENT:\"   \" \\
		AREA:loss10#ee0000:\"Packet loss\\n\" \\
		COMMENT:\"  \" \\
		LINE1:roundtrip#000000:\"roundtrip average\\n\" \\
		COMMENT:\"\t\t\t\t\t\t\t\t\t\t\t\t\t\t`date +\"%b %d %H\:%M\:%S %Y\"`\"";
	}
elseif(($curgraph == "spamd") && (file_exists("$rrddbpath$spamd"))) {
	/* graph a spamd statistics graph */
	$graphcmd = "$rrdtool graph $rrddbpath$curif-$interval-$curgraph.png \\
		--start -$seconds -e -$average \\
		--title=\"Spamd statistics for last $interval\" \\
		--vertical-label=\"Conn / Time, sec.\" \\
		--height 150 --width 650 --no-gridfit \\
		-x \"$scale\" --lower-limit 0  \\
		DEF:consmin=$rrddbpath$spamd:conn:MIN \\
		DEF:consavg=$rrddbpath$spamd:conn:AVERAGE \\
		DEF:consmax=$rrddbpath$spamd:conn:MAX \\
		DEF:timemin=$rrddbpath$spamd:time:MIN \\
		DEF:timeavg=$rrddbpath$spamd:time:AVERAGE \\
		DEF:timemax=$rrddbpath$spamd:time:MAX \\
		\"CDEF:timeminadj=timemin,0,86400,LIMIT,UN,0,timemin,IF\" \\
		\"CDEF:timeavgadj=timeavg,0,86400,LIMIT,UN,0,timeavg,IF\" \\
		\"CDEF:timemaxadj=timemax,0,86400,LIMIT,UN,0,timemax,IF\" \\
		\"CDEF:t1=timeminadj,timeavgadj,+,2,/,timeminadj,-\" \\
		\"CDEF:t2=timeavgadj,timemaxadj,+,2,/,timeminadj,-,t1,-\" \\
		\"CDEF:t3=timemaxadj,timeminadj,-,t1,-,t2,-\" \\
		AREA:timeminadj \\
		AREA:t1#DDDDFF::STACK \\
		AREA:t2#AAAAFF::STACK \\
		AREA:t3#DDDDFF::STACK \\
		LINE2:timeavgadj#000066:\"Time \" \\
		GPRINT:timeminadj:MIN:\"Min\\:%6.2lf\\t\" \\
		GPRINT:timeavgadj:AVERAGE:\"Avg\\:%6.2lf\\t\" \\
		GPRINT:timemaxadj:MAX:\"Max\\:%6.2lf\\n\" \\
		AREA:consmax#00AA00BB \\
		AREA:consmin#FFFFFFFF \\
		LINE1:consmin#00660088 \\
		LINE1:consmax#FFFFFF88 \\
		LINE1:consavg#006600:\"Cons \" \\
		GPRINT:consmin:MIN:\"Min\\:%6.2lf\\t\" \\
		GPRINT:consavg:AVERAGE:\"Avg\\:%6.2lf\\t\" \\
		GPRINT:consmax:MAX:\"Max\\:%6.2lf\\n\" \\
		COMMENT:\"\t\t\t\t\t\t\t\t\t\t\t\t\t\t`date +\"%b %d %H\:%M\:%S %Y\"`\"";
	}
else
	{
		PRINT "<b>Sorry we do not have data to graph $curgraph for $curif with.</b><br>";
	} 

	/* check modification time to see if we need to generate image */
	if (file_exists("$rrddbpath$curif-$interval-$curgraph.png")) {
		if((time() - filemtime("$rrddbpath$curif-$interval-$curgraph.png")) >= 280 ) {
			exec("$graphcmd 2>&1", $graphcmdoutput, $graphcmdreturn);
			usleep(500);
		}			
	} else {
		exec("$graphcmd 2>&1", $graphcmdoutput, $graphcmdreturn);
		usleep(500);
	}
	if($graphcmdreturn != 0) {
		PRINT "Failed to create graph with error code 
$graphcmdreturn, the error is: $graphcmdoutput[0]";
	}

	if(file_exists("$rrddbpath/$curif-$interval-$curgraph.png")) {
		header("Content-type: image/png");
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		$file= "$rrddbpath/$curif-$interval-$curgraph.png";
		$size= filesize($file);
		header("Content-Length: $size bytes");
		readfile($file);
	}


?>
OpenPOWER on IntegriCloud