"Fossies" - the Fresh Open Source Software Archive 
Member "vnstat-2.9/examples/vnstat.cgi" (21 Aug 2021, 11061 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "vnstat.cgi":
2.7_vs_2.8.
1 #!/usr/bin/perl -w
2
3 # vnstat.cgi -- example cgi for vnStat image output
4 # copyright (c) 2008-2021 Teemu Toivola <tst at iki dot fi>
5 #
6 # based on mailgraph.cgi
7 # copyright (c) 2000-2007 ETH Zurich
8 # copyright (c) 2000-2007 David Schweikert <dws@ee.ethz.ch>
9 # released under the GNU General Public License
10
11 package vnStatCGI;
12 use strict;
13
14 # server name in page title
15 # fill to set, otherwise "hostname" command output is used
16 my $servername = '';
17
18 # temporary directory where to store the images
19 my $tmp_dir = '/tmp/vnstatcgi';
20
21 # location of "vnstat" binary
22 my $vnstat_cmd = '/usr/bin/vnstat';
23
24 # location of "vnstati" binary
25 my $vnstati_cmd = '/usr/bin/vnstati';
26
27 # image cache time in minutes, set 0 to disable
28 my $cachetime = '0';
29
30 # shown interfaces, interface specific pages can be accessed directly
31 # by using /interfacename as suffix for the cgi if the httpd supports PATH_INFO
32 # for static list, uncomment and update the list
33 #my @interfaces = ('eth0', 'eth1');
34
35 # center images on page instead of left alignment, set 0 to disable
36 my $aligncenter = '1';
37
38 # use large fonts, set 1 to enable
39 my $largefonts = '0';
40
41 # page background color
42 my $bgcolor = "white";
43
44 # page auto refresh interval in seconds, set 0 to disable
45 my $pagerefresh = '0';
46
47 # cgi script file name for httpd
48 # fill to override automatic detection
49 my $scriptname = '';
50
51 ################
52
53
54 my $VERSION = "1.15";
55 my $cssbody = "body { background-color: $bgcolor; }";
56 my $csscommonstyle = <<CSS;
57 a { text-decoration: underline; }
58 a:link { color: #b0b0b0; }
59 a:visited { color: #b0b0b0; }
60 a:hover { color: #000000; }
61 small { font-size: 8px; color: #cbcbcb; }
62 img { border: 0; vertical-align: top; }
63 table { border: 0; }
64 table td { vertical-align: top; }
65 small { display: block; }
66 CSS
67 my $metarefresh = "";
68
69 sub graph
70 {
71 my ($interface, $file, $param) = @_;
72
73 my $fontparam = '--small';
74 if ($largefonts == '1') {
75 $fontparam = '--large';
76 }
77
78 if (defined $interface and defined $file and defined $param) {
79 my $result = `"$vnstati_cmd" -i "$interface" -c $cachetime $param $fontparam -o "$file"`;
80 } else {
81 show_error("ERROR: invalid input");
82 }
83 }
84
85 sub print_interface_list_html
86 {
87 my @interfaces = @vnStatCGI::interfaces;
88
89 print "Content-Type: text/html\n\n";
90
91 print <<HEADER;
92 <!DOCTYPE html>
93 <html>
94 <head>
95 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">$metarefresh
96 <meta name="generator" content="vnstat.cgi $VERSION">
97 <title>Traffic Statistics for $servername</title>
98 <style>
99 <!--
100 $csscommonstyle
101 $cssbody
102 -->
103 </style>
104 </head>
105 HEADER
106
107 for my $i (0..$#interfaces) {
108 print "<p><a href=\"${scriptname}?${i}-f\"><img src=\"${scriptname}?${i}-hs\" alt=\"$interfaces[${i}] summary\"></a></p>\n";
109 }
110
111 print <<FOOTER;
112 <small style=\"padding: 4px 4px\">Images generated using <a href="https://humdi.net/vnstat/">vnStat</a> image output.</small>
113 <br><br>
114 </body>
115 </html>
116 FOOTER
117 }
118
119 sub print_single_interface_html
120 {
121 my ($interface) = @_;
122 my @interfaces = @vnStatCGI::interfaces;
123
124 print "Content-Type: text/html\n\n";
125
126 print <<HEADER;
127 <!DOCTYPE html>
128 <html>
129 <head>
130 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">$metarefresh
131 <meta name="generator" content="vnstat.cgi $VERSION">
132 <title>Traffic Statistics for $servername - $interfaces[${interface}]</title>
133 <style>
134 <!--
135 $csscommonstyle
136 $cssbody
137 -->
138 </style>
139 </head>
140 HEADER
141
142 print "<table><tr><td>\n";
143 print "<img src=\"${scriptname}?${interface}-s\" alt=\"$interfaces[${interface}] summary\"><br>\n";
144 print "<a href=\"${scriptname}?s-${interface}-d-l\"><img src=\"${scriptname}?${interface}-d\" alt=\"$interfaces[${interface}] daily\" style=\"margin: 4px 0px\"></a><br>\n";
145 print "<a href=\"${scriptname}?s-${interface}-t-l\"><img src=\"${scriptname}?${interface}-t\" alt=\"$interfaces[${interface}] top 10\"></a><br>\n";
146 print "</td><td>\n";
147 print "<a href=\"${scriptname}?s-${interface}-h\"><img src=\"${scriptname}?${interface}-hg\" alt=\"$interfaces[${interface}] hourly\"></a><br>\n";
148 print "<a href=\"${scriptname}?s-${interface}-5\"><img src=\"${scriptname}?${interface}-5g\" alt=\"$interfaces[${interface}] 5 minute\" style=\"margin: 4px 0px\"></a><br>\n";
149 print "<a href=\"${scriptname}?s-${interface}-m-l\"><img src=\"${scriptname}?${interface}-m\" alt=\"$interfaces[${interface}] monthly\"></a><br>\n";
150 print "<a href=\"${scriptname}?s-${interface}-y-l\"><img src=\"${scriptname}?${interface}-y\" alt=\"$interfaces[${interface}] yearly\" style=\"margin: 4px 0px\"></a><br>\n";
151 print "</td></tr>\n</table>\n";
152
153 print <<FOOTER;
154 <small style=\"padding: 12px 4px\">Images generated using <a href="https://humdi.net/vnstat/">vnStat</a> image output.</small>
155 <br><br>
156 </body>
157 </html>
158 FOOTER
159 }
160
161 sub print_single_image_html
162 {
163 my ($image) = @_;
164 my $interface = "-1";
165 my $content = "";
166 my @interfaces = @vnStatCGI::interfaces;
167
168 if ($image =~ /^(\d+)-/) {
169 $interface = $1;
170 } else {
171 show_error("ERROR: invalid query");
172 }
173
174 if ($image =~ /^\d+-5/) {
175 $content = "5 Minute";
176 } elsif ($image =~ /^\d+-h/) {
177 $content = "Hourly";
178 } elsif ($image =~ /^\d+-d/) {
179 $content = "Daily";
180 } elsif ($image =~ /^\d+-m/) {
181 $content = "Monthly";
182 } elsif ($image =~ /^\d+-y/) {
183 $content = "Yearly";
184 } elsif ($image =~ /^\d+-t/) {
185 $content = "Daily Top";
186 } else {
187 show_error("ERROR: invalid query type");
188 }
189
190 print "Content-Type: text/html\n\n";
191
192 print <<HEADER;
193 <!DOCTYPE html>
194 <html>
195 <head>
196 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">$metarefresh
197 <meta name="generator" content="vnstat.cgi $VERSION">
198 <title>$content Traffic Statistics for $servername - $interfaces[${interface}]</title>
199 <style>
200 <!--
201 $csscommonstyle
202 $cssbody
203 -->
204 </style>
205 </head>
206 HEADER
207
208 print "<table><tr><td>\n";
209 print "<img src=\"${scriptname}?${image}\" alt=\"$interfaces[${interface}] ", lc($content), "\">\n";
210 print "</td></tr>\n</table>\n";
211
212 print <<FOOTER;
213 <small style=\"padding: 12px 4px\">Image generated using <a href="https://humdi.net/vnstat/">vnStat</a> image output.</small>
214 <br><br>
215 </body>
216 </html>
217 FOOTER
218 }
219
220 sub send_image
221 {
222 my ($file) = @_;
223
224 -r $file or do {
225 show_error("ERROR: can't find $file");
226 };
227
228 print "Content-type: image/png\n";
229 print "Content-length: ".((stat($file))[7])."\n";
230 print "\n";
231 open(my $IMG_FILE, "<", $file) or die;
232 my $data;
233 print $data while read($IMG_FILE, $data, 16384)>0;
234 }
235
236 sub show_error
237 {
238 my ($error_msg) = @_;
239 print "Content-type: text/plain\n\n$error_msg\n";
240 exit 1;
241 }
242
243 sub main
244 {
245 if (length($scriptname) == 0) {
246 if (defined $ENV{REQUEST_URI}) {
247 ($scriptname) = split(/\?/, $ENV{REQUEST_URI});
248 } else {
249 ($scriptname) = $ENV{SCRIPT_NAME} =~ /([^\/]*)$/;
250 }
251 if ($scriptname =~ /\/$/) {
252 $scriptname = '';
253 }
254 }
255
256 if (not defined $vnStatCGI::interfaces) {
257 our @interfaces = `$vnstat_cmd --dbiflist 1`;
258 }
259 chomp @vnStatCGI::interfaces;
260 my @interfaces = @vnStatCGI::interfaces;
261
262 if (length($servername) == 0) {
263 $servername = `hostname`;
264 chomp $servername;
265 }
266
267 if ($aligncenter != '0') {
268 $cssbody = "html { display: table; width: 100%; }\nbody { background-color: $bgcolor; display: table-cell; text-align: center; vertical-align: middle; }\ntable { margin-left: auto; margin-right: auto; margin-top: 10px; }";
269 }
270
271 if ($pagerefresh != '0') {
272 $metarefresh = "\n<meta http-equiv=\"refresh\" content=\"$pagerefresh\">";
273 }
274
275 mkdir $tmp_dir, 0755 unless -d $tmp_dir;
276
277 my $query = $ENV{QUERY_STRING};
278 if (defined $query and $query =~ /\S/) {
279 if ($query =~ /^(\d+)-s$/) {
280 my $file = "$tmp_dir/vnstat_$1.png";
281 graph($interfaces[$1], $file, "-s");
282 send_image($file);
283 }
284 elsif ($query =~ /^(\d+)-hs$/) {
285 my $file = "$tmp_dir/vnstat_$1_hs.png";
286 graph($interfaces[$1], $file, "-hs");
287 send_image($file);
288 }
289 elsif ($query =~ /^(\d+)-hsh$/) {
290 my $file = "$tmp_dir/vnstat_$1_hsh.png";
291 graph($interfaces[$1], $file, "-hs 0");
292 send_image($file);
293 }
294 elsif ($query =~ /^(\d+)-hs5$/) {
295 my $file = "$tmp_dir/vnstat_$1_hs5.png";
296 graph($interfaces[$1], $file, "-hs 1");
297 send_image($file);
298 }
299 elsif ($query =~ /^(\d+)-vs$/) {
300 my $file = "$tmp_dir/vnstat_$1_vs.png";
301 graph($interfaces[$1], $file, "-vs");
302 send_image($file);
303 }
304 elsif ($query =~ /^(\d+)-vsh$/) {
305 my $file = "$tmp_dir/vnstat_$1_vsh.png";
306 graph($interfaces[$1], $file, "-vs 0");
307 send_image($file);
308 }
309 elsif ($query =~ /^(\d+)-vs5$/) {
310 my $file = "$tmp_dir/vnstat_$1_vs5.png";
311 graph($interfaces[$1], $file, "-vs 1");
312 send_image($file);
313 }
314 elsif ($query =~ /^(\d+)-d$/) {
315 my $file = "$tmp_dir/vnstat_$1_d.png";
316 graph($interfaces[$1], $file, "-d 30");
317 send_image($file);
318 }
319 elsif ($query =~ /^(\d+)-d-l$/) {
320 my $file = "$tmp_dir/vnstat_$1_d_l.png";
321 graph($interfaces[$1], $file, "-d 60");
322 send_image($file);
323 }
324 elsif ($query =~ /^(\d+)-m$/) {
325 my $file = "$tmp_dir/vnstat_$1_m.png";
326 graph($interfaces[$1], $file, "-m 12");
327 send_image($file);
328 }
329 elsif ($query =~ /^(\d+)-m-l$/) {
330 my $file = "$tmp_dir/vnstat_$1_m_l.png";
331 graph($interfaces[$1], $file, "-m 24");
332 send_image($file);
333 }
334 elsif ($query =~ /^(\d+)-t$/) {
335 my $file = "$tmp_dir/vnstat_$1_t.png";
336 graph($interfaces[$1], $file, "-t 10");
337 send_image($file);
338 }
339 elsif ($query =~ /^(\d+)-t-l$/) {
340 my $file = "$tmp_dir/vnstat_$1_t_l.png";
341 graph($interfaces[$1], $file, "-t 20");
342 send_image($file);
343 }
344 elsif ($query =~ /^(\d+)-h$/) {
345 my $file = "$tmp_dir/vnstat_$1_h.png";
346 graph($interfaces[$1], $file, "-h 48");
347 send_image($file);
348 }
349 elsif ($query =~ /^(\d+)-hg$/) {
350 my $file = "$tmp_dir/vnstat_$1_hg.png";
351 graph($interfaces[$1], $file, "-hg");
352 send_image($file);
353 }
354 elsif ($query =~ /^(\d+)-5$/) {
355 my $file = "$tmp_dir/vnstat_$1_5.png";
356 graph($interfaces[$1], $file, "-5 60");
357 send_image($file);
358 }
359 elsif ($query =~ /^(\d+)-5g$/) {
360 my $file = "$tmp_dir/vnstat_$1_5g.png";
361 if ($largefonts == '1') {
362 graph($interfaces[$1], $file, "-5g 576 300");
363 } else {
364 graph($interfaces[$1], $file, "-5g 422 250");
365 }
366 send_image($file);
367 }
368 elsif ($query =~ /^(\d+)-y$/) {
369 my $file = "$tmp_dir/vnstat_$1_y.png";
370 graph($interfaces[$1], $file, "-y 5");
371 send_image($file);
372 }
373 elsif ($query =~ /^(\d+)-y-l$/) {
374 my $file = "$tmp_dir/vnstat_$1_y_l.png";
375 graph($interfaces[$1], $file, "-y 0");
376 send_image($file);
377 }
378 elsif ($query =~ /^(\d+)-f$/) {
379 print_single_interface_html($1);
380 }
381 elsif ($query =~ /^s-(.+)/) {
382 print_single_image_html($1);
383 }
384 else {
385 show_error("ERROR: invalid argument");
386 }
387 }
388 else {
389 my $html_shown = 0;
390 if (defined $ENV{PATH_INFO}) {
391 my @fields = split(/\//, $ENV{PATH_INFO});
392 my $interface = $fields[-1];
393 for my $i (0..$#interfaces) {
394 if ($interfaces[${i}] eq $interface) {
395 print_single_interface_html($i);
396 $html_shown = 1;
397 last;
398 }
399 }
400 if ($html_shown == 0) {
401 show_error("ERROR: no such interface: $interface");
402 }
403 }
404
405 if ($html_shown == 0 and scalar @interfaces == 1) {
406 print_single_interface_html(0);
407 }
408 elsif ($html_shown == 0) {
409 print_interface_list_html();
410 }
411 }
412 }
413
414 main();