>/D_
Published on

vnStat hack to measure data usage in a given billing cycle

Authors
  • avatar
    Name
    Frank
    Twitter

I'm using vnStat to measure internet usage on my Ubuntu 10.04 laptop, coupled with the vnStat PHP frontend it works nicely. Only problem was the summaries of disk usage started at the beginning of the month (sensibly) and we get billed on the 23rd, which is when our month ticks over and we get a new data cap.

Not wanting to waste any data - to get a summary of the data use starting and ending on some random day of the month here is some simple code to go in index.php->write_summary():

//Billable month
$sum[4]['act'] = 1;
$sum[4]['label'] = T('Billable month');
$sum[4]['rx'] = 0;
$sum[4]['tx'] = 0;
foreach($day as $dayData) {
    //Change 23 to whatever day you are billed on
    $start = mktime(0, 0, 0, (date('m') - 1), 23, date('Y'));
    $end = mktime(0, 0, 0, (date('m')), 23, date('Y'));
 
    if ($dayData['time'] >= $start && $dayData['time'] < $end) {
        $sum[4]['rx'] += $dayData['rx'];
        $sum[4]['tx'] += $dayData['tx'];
    }
}

Note: this probably won't work too well if you get billed on the 31st - I can imagine that causing problems with months that do not have 31 days ;-)

Here is a nice howto - install vnstat on Ubuntu to get started.