Fix bandwidth statistics in Bandmin

I recently took a look at the built-in bandmin interface in cPanel and found out that the bandwidth stats doesn’t work as they should, to be very specific the transfer in kbit/s, kb/s, mbit/s doesn’t show the right result.

So I’ll show you how to fix bandwidth statistics in Bandmin.
The whole problem is the way that the bandmin interface gets the statistics, reading the code, you’ll find something like:

 open(BANDDATA,"/var/log/bandwidth/current");
 while(<BANDDATA>) {
 s/\n//g;
 ($ip,$bytespersec) = split(/=/, $_);
 $CURRENTBW{$ip} = $bytespersec;
 }
 close(BANDDATA);

You’ll see the whole problem in the very first line. It’s getting the data from ‘/var/log/bandwidth/current’. We don’t want that, so what we’re going to do, is to use the variable in the script called $TOTALBAND, this is the total bandwidth served within the selected month. Then we’re going to add a small counter, to see how many days we have in the selected month, and divide the stats accordingly, so we get the exact amount of statistics.
Instead of pasting the whole code, you’ll find a diff, to see which lines needs to be changed:

@@ -237,6 +237,8 @@ foreach $path (@PATHS) {
          foreach $unit (sort keys %UNITS) {
             if ($SHOWPERSEC{$unit} == 1) {
-               $bw = &unsci($CURRENTBW{"all-$path"} * eval($UNITS{$unit}));
+               $bw = &unsci($TOTALBAND{"all-$path"} / $daycount / 24 / 60 / 60 * eval($UNITS{$unit}));
                if ($ROUND{$unit} == 1) { $bw = &round($bw); }
                print  "<td class=total>$bw</td>";
             }
@@ -472,6 +474,7 @@ sub loadbanddata {
          delete @TOTALBAND{keys %TOTALBAND};
          delete @DAYBAND{keys %DAYBAND};

+         $daycount = 0;
          foreach $day (@DAYS) {
             $day =~ s/\n//g;
             $daytotal = 0;
@@ -487,6 +490,7 @@ sub loadbanddata {
                   $DAYBAND{$day} += $bytes;
                }
             }
+            $daycount++;
             close(BANDDATA);
          }

Now the bandmin interface should show your transfer in kilobit/s, kilobyte/s and megabit/s correctly, something like this:

ll6tm3rwfkfu170