Example of feature_bandwidth function

I writing new version of nginx_webmin plugin and want to add bandwidth counting feature.
In API I find function feature_bandwidth, but can’t find the working example how it must works.
Can you give me working example?

At now I write:
sub feature_bandwidth
{
my ($d, $start, %bwhash) = @_;
$file=$d->{‘home’}.‘logs/nginx_access.log’;

open(LOG,$file);

$i2=0;

while($line=) {

@line_spl = split(/ /,$line);

$line =~ m/[([^]]+)]/;
$date = $1;

$time=str2time($date);

$bwtime = int($time/86400);

$line =~ m/(\d+) “[\d.-]+”/;
$in = $1;

$out = $line_spl[10];

$bwhash{$bwtime} += ($in+$out);
return $bwhash;
}
Is it correct? How I can debug this function?

Will be good to create dummy example module, that have all api functions with comments and examples.

The way the function is supposed to work is like this - it takes in three parameters :

  1. The virtual server object hash reference
  2. The Unix time at which bandwidth usage accounting should start. If you are scanning some log file to count usage, you should ignore entries before this time.
  3. A hash reference that your function should update.

The return value must be the unix time of the last log entry that was scanned.

Actual accounting is done by updating the hash in the 3rd parameter. It is a map from keys like web_15085 to a number of bytes used. The part of the key after web_ is a day number, which is the number of days since 1st Jan 1970.

You function should get called from the /etc/webmin/virtual-server/bw.pl cron job. One way to debug it is to put some print STDERR ... statements in your function, then SSH into the system and run bw.pl as root manually from the shell.

Thank’s for info, I try debug via STDERR. I need name keys like web_9999 or other name, nginx_9999?
Value of the key must be integer with total traffic in bytes?

I can’t find the feature_bandwidth function that parse proftpd or apache logs, where it is located?
Or maybe you can post perl source of some working function or write a simple example?

I update my function, now it saves bandwidth normally to file:
root@rise:/etc/webmin/virtual-server/bandwidth# cat ./13028756286394
last_nginx-webmin=1303385042
last_web=1303385042
last_mail=1303413843
last_ftp=
web_15079=417724
last_unix=1302877657
ftp_15079=37333063
web_15082=501093
web_15085=47925
nginx_15083=1493123
nginx_15084=1495121
nginx_15085=1493271

But i didn’t see my part of bandwidth in graphs: http://img849.imageshack.us/img849/9523/snapshot4.png

Maybe I must additionally describe my bandwidth type in any function?

You need to use keys starting with web_ .

For an example of the version of this function for Apache logs, see virtual-server/feature-web.pl under the Webmin root, in the bandwidth_web function.

Thank’s, with web_ all works, but I see my traffic in “Website traffic” section. Can I separate my nginx traffic from apache and show it with different color and column?
Because nginx working as proxy for dynamic files, and now traffic for php scripts counts double time.

You can’t define custom bandwidth types unfortunately - all you can do is use one of the built-in types, which are web , mail , ftp , unix , backup and restore .

JamieCameron, thank’s for the help! I do the new working release of nginx module for virtualmin and webmin, you can look on it here: https://github.com/MurzNN/virtualmin-nginx

Cool, looks good … are you supporting serving content using nginx, or just proxying?

By default nginx serve static content by youself, dynamic and 404 errors proxies to apache. But you can set custom config in settings.