"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/examples/vnstat-json.cgi" (21 Aug 2021, 1293 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-json.cgi": 2.7_vs_2.8.

    1 #!/usr/bin/perl -w
    2 
    3 # vnstat-json.cgi -- example cgi for vnStat json output
    4 # copyright (c) 2015-2021 Teemu Toivola <tst at iki dot fi>
    5 # released under the GNU General Public License
    6 
    7 use strict;
    8 
    9 # location of vnstat binary
   10 my $vnstat_cmd = '/usr/bin/vnstat';
   11 
   12 # individually accessible interfaces with ?interface=N or /interfacename suffix
   13 # for static list, uncomment first line below, update the list and comment out second line
   14 #my @interfaces = ('eth0', 'eth1');
   15 my @interfaces = `$vnstat_cmd --dbiflist 1`;
   16 
   17 
   18 ################
   19 
   20 
   21 my $iface = "";
   22 chomp @interfaces;
   23 
   24 if (defined $ENV{PATH_INFO}) {
   25     my @fields = split(/\//, $ENV{PATH_INFO});
   26     my $interface = $fields[-1];
   27     for my $i (0..$#interfaces) {
   28         if ($interfaces[${i}] eq $interface) {
   29             $iface = "-i $interface";
   30             last;
   31         }
   32     }
   33 }
   34 
   35 if (length($iface) == 0 and defined $ENV{QUERY_STRING}) {
   36     my $getiface = "";
   37     my @values = split(/&/, $ENV{QUERY_STRING});
   38     foreach my $i (@values) {
   39         my ($varname, $varvalue) = split(/=/, $i);
   40         if ($varname eq 'interface' && $varvalue =~ /^(\d+)$/) {
   41             $getiface = $varvalue;
   42         }
   43     }
   44 
   45     if (length($getiface) > 0 && $getiface >= 0 && $getiface <= $#interfaces) {
   46         $iface = "-i @interfaces[int($getiface)]";
   47     }
   48 }
   49 
   50 print "Content-Type: application/json\n\n";
   51 exec("$vnstat_cmd --json $iface");