Subversion Repositories ALCASAR

Rev

Rev 1533 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 1533 Rev 1675
Line 15... Line 15...
15
    // You should have received a copy of the GNU General Public License
15
    // You should have received a copy of the GNU General Public License
16
    // along with this program; if not, write to the Free Software
16
    // along with this program; if not, write to the Free Software
17
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
    //
18
    //
19
    //
19
    //
20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html 
20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html
21
    // for more information.
21
    // for more information.
22
    //
22
    //
23
    require 'config.php';
23
    require 'config.php';
24
    require 'localize.php';
24
    require 'localize.php';
25
    require 'vnstat.php';
25
    require 'vnstat.php';
Line 59... Line 59...
59
    function svg_group_end()
59
    function svg_group_end()
60
    {
60
    {
61
	print "</g>\n";
61
	print "</g>\n";
62
    }
62
    }
63
 
63
 
64
    function svg_text($x, $y, $text, $options = array()) 
64
    function svg_text($x, $y, $text, $options = array())
65
    {	
65
    {
66
	printf("<text x=\"%F\" y=\"%F\" ", $x, $y);
66
	printf("<text x=\"%F\" y=\"%F\" ", $x, $y);
67
	svg_options($options);
67
	svg_options($options);
68
	print ">$text</text>\n";
68
	print ">$text</text>\n";
69
    }
69
    }
70
 
70
 
Line 73... Line 73...
73
	printf("<line x1=\"%F\" y1=\"%F\" x2=\"%F\" y2=\"%F\" ", $x1, $y1, $x2, $y2);
73
	printf("<line x1=\"%F\" y1=\"%F\" x2=\"%F\" y2=\"%F\" ", $x1, $y1, $x2, $y2);
74
	svg_options($options);
74
	svg_options($options);
75
	print "/>\n";
75
	print "/>\n";
76
    }
76
    }
77
 
77
 
78
    function svg_rect($x, $y, $w, $h, $options = array()) 
78
    function svg_rect($x, $y, $w, $h, $options = array())
79
    {
79
    {
80
	printf("<rect x=\"%F\" y=\"%F\" width=\"%F\" height=\"%F\" ", $x, $y, $w, $h);
80
	printf("<rect x=\"%F\" y=\"%F\" width=\"%F\" height=\"%F\" ", $x, $y, $w, $h);
81
	svg_options($options);
81
	svg_options($options);
82
	print "/>\n";
82
	print "/>\n";
83
    }
83
    }
Line 96... Line 96...
96
    {
96
    {
97
	$col['rgb'] = sprintf("#%02X%02X%02X", $colors[0], $colors[1], $colors[2]);
97
	$col['rgb'] = sprintf("#%02X%02X%02X", $colors[0], $colors[1], $colors[2]);
98
	$col['opacity'] = sprintf("%F", (127 - $colors[3]) / 127);
98
	$col['opacity'] = sprintf("%F", (127 - $colors[3]) / 127);
99
	return $col;
99
	return $col;
100
    }
100
    }
101
            
101
 
102
    function init_image()
102
    function init_image()
103
    {
103
    {
104
        global $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style;
104
        global $xlm, $xrm, $ytm, $ybm, $iw, $ih,$graph, $cl, $iface, $colorscheme, $style;
105
 
105
 
106
        if ($graph == 'none')
106
        if ($graph == 'none')
107
            return;
107
            return;
108
 
108
 
109
        //
109
        //
110
        // image object
110
        // image object
111
        //    
111
        //
112
        $xlm = 70;
112
        $xlm = 70;
113
        $xrm = 20;
113
        $xrm = 20;
114
        $ytm = 35;
114
        $ytm = 35;
115
        $ybm = 60;
115
        $ybm = 60;
116
        if ($graph == 'small')
116
        if ($graph == 'small')
117
        {
117
        {
118
            $iw = 300 + $xrm + $xlm;
118
            $iw = 300 + $xrm + $xlm;
119
            $ih = 100 + $ytm + $ybm;    
119
            $ih = 100 + $ytm + $ybm;
120
        }
120
        }
121
        else
121
        else
122
        {
122
        {
123
            $iw = 600 + $xrm + $xlm;
123
            $iw = 600 + $xrm + $xlm;
124
            $ih = 200 + $ytm + $ybm;
124
            $ih = 200 + $ytm + $ybm;
125
        }
125
        }
126
	
126
 
127
	svg_create($iw, $ih);
127
	svg_create($iw, $ih);
128
 
128
 
129
        //
129
        //
130
        // colors
130
        // colors
131
        //
131
        //
Line 139... Line 139...
139
        $cl['border'] = allocate_color($cs['border']);
139
        $cl['border'] = allocate_color($cs['border']);
140
        $cl['rx'] = allocate_color($cs['rx']);
140
        $cl['rx'] = allocate_color($cs['rx']);
141
        $cl['rx_border'] = allocate_color($cs['rx_border']);
141
        $cl['rx_border'] = allocate_color($cs['rx_border']);
142
        $cl['tx'] = allocate_color($cs['tx']);
142
        $cl['tx'] = allocate_color($cs['tx']);
143
        $cl['tx_border'] = allocate_color($cs['tx_border']);
143
        $cl['tx_border'] = allocate_color($cs['tx_border']);
144
	
144
 
145
        svg_rect(0, 0, $iw, $ih, array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['image_background']['rgb']) );
145
        svg_rect(0, 0, $iw, $ih, array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['image_background']['rgb']) );
146
	svg_rect($xlm, $ytm, $iw-$xrm-$xlm, $ih-$ybm-$ytm, array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['background']['rgb']) );
146
	svg_rect($xlm, $ytm, $iw-$xrm-$xlm, $ih-$ybm-$ytm, array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['background']['rgb']) );
147
	
147
 
148
	$depth = 12;
148
	$depth = 12*SVG_DEPTH_SCALING;
149
	svg_group( array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['background_2']['rgb'], 'fill-opacity' => $cl['background_2']['opacity']) );
149
	svg_group( array( 'stroke' => 'none', 'stroke-width' => 0, 'fill' => $cl['background_2']['rgb'], 'fill-opacity' => $cl['background_2']['opacity']) );
150
	svg_poly(array($xlm, $ytm, $xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $xlm - $depth, $ytm + $depth));
150
	svg_poly(array($xlm, $ytm, $xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $xlm - $depth, $ytm + $depth));
151
	svg_poly(array($xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $iw - $xrm - $depth, $ih - $ybm  + $depth, $iw - $xrm, $ih - $ybm));
151
	svg_poly(array($xlm, $ih - $ybm, $xlm - $depth, $ih - $ybm + $depth, $iw - $xrm - $depth, $ih - $ybm  + $depth, $iw - $xrm, $ih - $ybm));
152
	svg_group_end();
152
	svg_group_end();
153
 
153
 
154
	// draw title
154
	// draw title
155
	$text = T('Traffic data for')." $iface";
155
	$text = T('Traffic data for')." $iface";
156
	svg_text($iw / 2, ($ytm / 2), $text, array( 'stroke' => $cl['text'], 'fill' => $cl['text']['rgb'],'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-weight' => 'bold', 'text-anchor' => 'middle' ));
156
	svg_text($iw / 2, ($ytm / 2), $text, array( 'stroke' => 'none', 'fill' => $cl['text']['rgb'],'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-weight' => 'bold', 'text-anchor' => 'middle' ));
157
    }
157
    }
158
 
158
 
159
    function draw_border()
159
    function draw_border()
160
    {
160
    {
161
        global $cl, $iw, $ih;
161
        global $cl, $iw, $ih;
162
	svg_rect(1, 1, $iw-2, $ih-2, array( 'stroke' => $cl['border']['rgb'], 'stroke-opacity' => $cl['border']['opacity'], 'stroke-width' => 1, 'fill' => 'none') );
162
	svg_rect(1, 1, $iw-2, $ih-2, array( 'stroke' => $cl['border']['rgb'], 'stroke-opacity' => $cl['border']['opacity'], 'stroke-width' => 1, 'fill' => 'none') );
163
    }
163
    }
164
    
164
 
165
    function draw_grid($x_ticks, $y_ticks)
165
    function draw_grid($x_ticks, $y_ticks)
166
    {
166
    {
167
        global $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
167
        global $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
168
        $x_step = ($iw - $xlm - $xrm) / $x_ticks;
168
        $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
169
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
169
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
170
	
170
 
171
	$depth = 12;
171
	$depth = 12*SVG_DEPTH_SCALING;
172
 
172
 
173
	svg_group( array( 'stroke' => $cl['grid_stipple_1']['rgb'], 'stroke-opacity' => $cl['grid_stipple_1']['opacity'], 'stroke-width' => '1px', 'stroke-dasharray' => '1,1' ) );
173
	svg_group( array( 'stroke' => $cl['grid_stipple_1']['rgb'], 'stroke-opacity' => $cl['grid_stipple_1']['opacity'], 'stroke-width' => '1px', 'stroke-dasharray' => '1,1' ) );
174
        for ($i = $xlm; $i <= ($iw - $xrm); $i += $x_step)
174
        for ($i = $xlm; $i <= ($iw - $xrm); $i += $x_step)
175
        {
175
        {
176
	    svg_line($i, $ytm, $i, $ih-$ybm);
176
	    svg_line($i, $ytm, $i, $ih-$ybm);
177
            svg_line($i, $ih-$ybm, $i-$depth, $ih-$ybm+$depth);
177
            svg_line($i, $ih-$ybm, $i-$depth, $ih-$ybm+$depth);
178
        }
178
        }
179
        for ($i = $ytm; $i <= ($ih - $ybm); $i += $y_step)
179
        for ($i = $ytm; $i <= ($ih - $ybm); $i += $y_step)
180
        {
180
        {
181
            svg_line($xlm, $i, $iw - $xrm, $i); 
181
            svg_line($xlm, $i, $iw - $xrm, $i);
182
	    svg_line($xlm, $i, $xlm - $depth, $i + $depth);
182
	    svg_line($xlm, $i, $xlm - $depth, $i + $depth);
183
        }
183
        }
184
	svg_group_end();
184
	svg_group_end();
185
 
185
 
186
	svg_group( array( 'stroke' => $cl['border']['rgb'], 'stroke-width' => '1px', 'stroke-opacity' => $cl['border']['opacity'] ) );
186
	svg_group( array( 'stroke' => $cl['border']['rgb'], 'stroke-width' => '1px', 'stroke-opacity' => $cl['border']['opacity'] ) );
187
        svg_line($xlm, $ytm, $xlm, $ih - $ybm);
187
        svg_line($xlm, $ytm, $xlm, $ih - $ybm);
188
        svg_line($xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm);
188
        svg_line($xlm, $ih - $ybm, $iw - $xrm, $ih - $ybm);
189
	svg_group_end();
189
	svg_group_end();
190
    }
190
    }
191
    
191
 
192
    
192
 
193
    function draw_data($data)
193
    function draw_data($data)
194
    {
194
    {
195
        global $cl,$iw,$ih,$xlm,$xrm,$ytm,$ybm;
195
        global $cl,$iw,$ih,$xlm,$xrm,$ytm,$ybm;
196
 
196
 
197
        sort($data);
197
        sort($data);
Line 201... Line 201...
201
        $y_scale = 1;
201
        $y_scale = 1;
202
        $prescale = 1;
202
        $prescale = 1;
203
        $unit = 'K';
203
        $unit = 'K';
204
        $offset = 0;
204
        $offset = 0;
205
        $gr_h = $ih - $ytm - $ybm;
205
        $gr_h = $ih - $ytm - $ybm;
206
        $x_step = ($iw - $xlm - $xrm) / $x_ticks;
206
        $x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
207
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
207
        $y_step = ($ih - $ytm - $ybm) / $y_ticks;
208
        $bar_w = ($x_step / 2) ;
208
        $bar_w = ($x_step / 2) ;
209
 
209
 
210
        //
210
        //
211
        // determine scale
211
        // determine scale
Line 229... Line 229...
229
            $y_scale = $y_scale * 2;
229
            $y_scale = $y_scale * 2;
230
            if ($y_scale >= 1024)
230
            if ($y_scale >= 1024)
231
            {
231
            {
232
            $prescale = $prescale * 1024;
232
            $prescale = $prescale * 1024;
233
            $y_scale = $y_scale / 1024;
233
            $y_scale = $y_scale / 1024;
234
            if ($unit == 'K') 
234
            if ($unit == 'K')
235
                $unit = 'M';
235
                $unit = 'M';
236
            else if ($unit == 'M')
236
            else if ($unit == 'M')
237
                $unit = 'G';
237
                $unit = 'G';
238
            else if ($unit == 'G')
238
            else if ($unit == 'G')
239
                $unit = 'T';
239
                $unit = 'T';
240
            }
240
            }
241
        }
241
        }
242
 
242
 
243
        draw_grid($x_ticks, $y_ticks);
243
        draw_grid($x_ticks, $y_ticks);
244
	
244
 
245
        //
245
        //
246
        // graph scale factor (per pixel)
246
        // graph scale factor (per pixel)
247
        //
247
        //
248
        $sf = ($prescale * $y_scale * $y_ticks) / $gr_h;
248
        $sf = ($prescale * $y_scale * $y_ticks) / $gr_h;
249
 
249
 
250
        if ($data[0] == 'nodata')
250
        if (count($data) == 0)
251
        {
251
        {
252
            $text = 'no data available';
252
            $text = 'no data available';
253
	    svg_text($iw/2, $ytm + 80, $text, array( 'stroke' => $cl['text']['rgb'], 'fill' => $cl['text']['rgb'], 'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-size' => '16pt', 'text-anchor' => 'middle') );
253
	    svg_text($iw/2, $ytm + 80, $text, array( 'stroke' => $cl['text']['rgb'], 'fill' => $cl['text']['rgb'], 'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-size' => '16pt', 'text-anchor' => 'middle') );
254
        }
254
        }
255
        else
255
        else
256
        {
256
        {
257
            //
257
            //
258
            // draw bars
258
            // draw bars
259
            //      
259
            //
260
            for ($i=0; $i<$x_ticks; $i++)
260
            for ($i=0; $i<$x_ticks; $i++)
261
            {
261
            {
262
        	$x = $xlm + ($i * $x_step);
262
        	$x = $xlm + ($i * $x_step);
263
        	$y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf);
263
        	$y = $ytm + ($ih - $ytm - $ybm) - (($data[$i]['rx'] - $offset) / $sf);
264
		
264
 
265
		$depth = ($x_ticks < 20) ? 8 : 6;
265
		$depth = ($x_ticks < 20) ? 8*SVG_DEPTH_SCALING : 6*SVG_DEPTH_SCALING;
266
		$space = 0;
266
		$space = 0;
267
		
267
 
268
		$x1 = (int)$x;
268
		$x1 = (int)$x;
269
		$y1 = (int)$y;
269
		$y1 = (int)$y;
270
		$w = (int)($bar_w - $space);
270
		$w = (int)($bar_w - $space);
271
		$h = (int)($ih - $ybm - $y);
271
		$h = (int)($ih - $ybm - $y);
272
		$x2 = (int)($x + $bar_w - $space);
272
		$x2 = (int)($x + $bar_w - $space);
273
		$y2 = (int)($ih - $ybm);
273
		$y2 = (int)($ih - $ybm);
274
		
274
 
275
		svg_group( array( 'stroke' => $cl['rx_border']['rgb'], 'stroke-opacity' => $cl['rx_border']['opacity'], 
275
		svg_group( array( 'stroke' => $cl['rx_border']['rgb'], 'stroke-opacity' => $cl['rx_border']['opacity'], 
276
				  'stroke-width' => 1, 'stroke-linejoin' => 'round',
276
				  'stroke-width' => 1, 'stroke-linejoin' => 'round',
277
			          'fill' => $cl['rx']['rgb'], 'fill-opacity' => $cl['rx']['opacity'] ) );
277
			          'fill' => $cl['rx']['rgb'], 'fill-opacity' => $cl['rx']['opacity'] ) );
278
        	svg_rect($x1, $y1, $w, $h);
278
        	svg_rect($x1, $y1, $w, $h);
279
		svg_rect($x1 - $depth, $y1 + $depth, $w, $h);
279
		svg_rect($x1 - $depth, $y1 + $depth, $w, $h);
Line 285... Line 285...
285
		$x1 = (int)($x1 + $bar_w);
285
		$x1 = (int)($x1 + $bar_w);
286
		$x2 = (int)($x2 + $bar_w);
286
		$x2 = (int)($x2 + $bar_w);
287
		$w = (int)($bar_w - $space);
287
		$w = (int)($bar_w - $space);
288
		$h = (int)($ih - $ybm - $y1 - 1);
288
		$h = (int)($ih - $ybm - $y1 - 1);
289
 
289
 
290
		svg_group( array( 'stroke' => $cl['tx_border']['rgb'], 'stroke-opacity' => $cl['tx_border']['opacity'], 
290
		svg_group( array( 'stroke' => $cl['tx_border']['rgb'], 'stroke-opacity' => $cl['tx_border']['opacity'],
291
				  'stroke-width' => 1, 'stroke-linejoin' => 'round',
291
				  'stroke-width' => 1, 'stroke-linejoin' => 'round',
292
			          'fill' => $cl['tx']['rgb'], 'fill-opacity' => $cl['tx']['opacity'] ) );
292
			          'fill' => $cl['tx']['rgb'], 'fill-opacity' => $cl['tx']['opacity'] ) );
293
        	svg_rect($x1, $y1, $w, $h);
293
        	svg_rect($x1, $y1, $w, $h);
294
		svg_rect($x1 - $depth, $y1 + $depth, $w, $h);
294
		svg_rect($x1 - $depth, $y1 + $depth, $w, $h);
295
		svg_poly(array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth));
295
		svg_poly(array($x1, $y1, $x2, $y1, $x2 - $depth, $y1 + $depth, $x1 - $depth, $y1 + $depth));
296
		svg_poly(array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth));
296
		svg_poly(array($x2, $y1, $x2, $y2, $x2 - $depth, $y2 + $depth, $x2 - $depth, $y1 + $depth));
297
		svg_group_end();
297
		svg_group_end();
298
            }
298
            }
299
    
299
 
300
            //
300
            //
301
            // axis labels
301
            // axis labels
302
            //
302
            //
303
	    svg_group( array( 'fill' => $cl['text']['rgb'], 'fill-opacity' => $cl['text']['opacity'], 'stroke-width' => '0', 'font-family' => SVG_FONT, 'font-size' => '10pt', 'text-anchor' => 'end' ) );
303
	    svg_group( array( 'fill' => $cl['text']['rgb'], 'fill-opacity' => $cl['text']['opacity'], 'stroke-width' => '0', 'font-family' => SVG_FONT, 'font-size' => '10pt', 'text-anchor' => 'end' ) );
304
            for ($i=0; $i<=$y_ticks; $i++)
304
            for ($i=0; $i<=$y_ticks; $i++)
Line 357... Line 357...
357
	svg_end();
357
	svg_end();
358
    }
358
    }
359
 
359
 
360
    get_vnstat_data();
360
    get_vnstat_data();
361
    output_image();
361
    output_image();
362
?>        
362
?>