#!/usr/bin/perl

#
# Interface for apache mod_traffic and Navynet Nanemon (navynet network monitor)
# (http://www.navynet.it/prodotti.php?Content=nanemon)
#
# Copyright 2005 by Massimo Cetra <mcetra@gmail.com>.  All rights reserved.
#

use strict;

my $dirfiles="/var/spool/apache/mod_ntraffic/";

#*******************************************
#*******************************************  DON'T EDIT BELOW HERE
#*******************************************

my $par=@ARGV[0];
my $val=@ARGV[1];

if ($par eq "--help") {
    ShowHelp();
    exit;
} else {
    ParseAllVhostData($dirfiles);
}


#********************************************
sub ShowHelp() {

print qq~Plugin Informations:
c_apache_all v.1.0
Navynet S.r.l. 2005 info\@navynet.it
Measures traffic by vhost - Requires mod_ntraffic
[|||]
ST OK Done
~;


}
#********************************************

sub ParseAllVhostData($) {
    my ($dir) = @_;
    my $filename;
    opendir ( DIR, $dir );

    while( ($filename = readdir(DIR))){
	if ( $filename=~/\.data$/ ) {
	    my $file = $dir.''.$filename;
	    my $vh = $filename;
	    $vh =~ s/\.data$//;
	    ParseVhostData($vh, $file);
	}
    }

    closedir(DIR);
}

sub ParseVhostData() {
    my ($vhost,$file)=@_;
    my $line;

    if ( ! -f $file ) {
	print "ST ER No data found for virtual host. Cannot open data file.\n";
	return;
    }

    open (FILE,$file);
    $line=<FILE>;
    close(FILE);

    my ($sent,$rec,$hits)=split(" ",$line);

    print "VA ${vhost}-outBytes $sent int\n";
    print "VA ${vhost}-inBytes $rec int\n";
    print "VA ${vhost}-hits $hits int\n";
    print "TY INC\n";
    print "ST OK \n";

}

1;
