#!/usr/bin/perl

use strict;
use Tie::File;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);
use Getopt::Long;
use vars qw($opt_I $opt_w $opt_c $opt_V $opt_h $opt_D);

#my $TIMEOUT = 5;
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $PROGNAME = "Networkusage base on Device";
our $update_time = time();
our $file = "/proc/net/dev";
my $new_bytes = 0;
my $new_out_bytes=0;
my $new_in_bytes=0;
our $last_bytes  = 0;
my $last_in_bytes=0;
my $last_out_bytes=0;
our $last_check_time;
our $update_time=0;
our @last_values=undef;
our $flg_created = 0;
our $row=0;
my $month=0;
my $year=0;
my $last_month=0;
my $count=0;
my $last_count=0;
my $last_month_count=0;
my @dataset_in;
my @dataset_out;
my @splitinterface;
my $completestring;
my $countedoutput;


Getopt::Long::Configure('bundling');
GetOptions
	("V=s"	=> \$opt_V, "Version" 	=> \$opt_V ,
	 "I=s" => \$opt_I, "Interface" 	=> \$opt_I ,
	 "h"	=> \$opt_h, "help" 	=> \$opt_h ,
	 "D=s" => \$opt_D, "Description"	=> \$opt_D,
	 "w=s" => \$opt_w, "Warngrenze"	=> \$opt_w,
	 "c=s" => \$opt_c, "Kritischegrenze"=> \$opt_c);

if ($opt_I eq '') {
	print_help(); exit $ERRORS{0};
}
if ($opt_D eq '') {
	print_help(); exit $ERRORS{0};
}
if ($opt_c eq '') {
	print_help(); exit $ERRORS{0};
}
if ($opt_w eq '') {
	print_help(); exit $ERRORS{0};
}

open(my $FH, $file) or die "$!\n";
while (<$FH>){
	$completestring = $_ if ($_=~ /$opt_I/);
	@splitinterface = split(/:/,$completestring,2);
	@dataset_in = split(/\s+/,@splitinterface [1]);
	$new_in_bytes = @dataset_in[0];
	@dataset_out = split(/\s+/,@splitinterface [1]);	
	$new_out_bytes = @dataset_out[8];
}

close $FH;
	$month = ((localtime)[4] +1);
	$year = ((localtime)[5] +1900);
	my $monthbefore = $month -1;

    	if (-e "/usr/local/nagios/var/lib/networkcounting/networkcounting-".$opt_I."-".$opt_D."-".$monthbefore."-".$year) {
	    open(FILE,"<"."/usr/local/nagios/var/lib/networkcounting/networkcounting-".$opt_I."-".$opt_D."-".$monthbefore ."-".$year);
	    while($row = <FILE>){
	                @last_values = split(":",$row);
			  $last_month_count = $last_values[3];
	   }
	}
	if (-e "/usr/local/nagios/var/lib/networkcounting/networkcounting-".$opt_I."-".$opt_D."-".$month."-".$year) {
	    open(FILE,"<"."/usr/local/nagios/var/lib/networkcounting/networkcounting-".$opt_I."-".$opt_D."-".$month."-".$year);
	    while($row = <FILE>){
	                @last_values = split(":",$row);
	                $last_check_time = $last_values[0];
	                $last_in_bytes = $last_values[1];
			  $last_out_bytes = $last_values[2];
			  $last_count = $last_values[3];
			  $last_month = $last_values[4];
	                $flg_created = 1;
	    }
	    close(FILE);
	}  else {
	$flg_created = 0;
	}


	$new_bytes = $new_in_bytes + $new_out_bytes;
	$update_time = time();
	$count = $new_bytes-$last_month_count;
	$countedoutput = $count/1024/1024/1024;
	$countedoutput = sprintf("%.2f", $countedoutput);
	
	unless (open(FILE,">"."/usr/local/nagios/var/lib/networkcounting/networkcounting-".$opt_I."-".$opt_D."-".$month."-".$year)){
	    print "Check mod for temporary file : /usr/local/nagios/var/lib/networkcounting/networkcounting-".$opt_I."-".$opt_D."-".$month."-".$year."!\n";
	    exit $ERRORS{"UNKNOWN"};
	}	
	print FILE "$update_time:$new_in_bytes:$new_out_bytes:$count:$month:$last_month_count";
	close(FILE);
	
	if ($flg_created == 0){
	    print "First execution : Buffer in creation.... \n";
	    exit($ERRORS{"UNKNOWN"});
	}


	
	

	if ($countedoutput >= $opt_w  && $countedoutput < $opt_c) {
		printf("Traffic aktueller Monat : $countedoutput | Traffic (GB)=$countedoutput;$opt_w;$opt_c\n");
		exit $ERRORS{"WARNING"};
	}
	elsif ($countedoutput >= $opt_c ) {
		printf("Traffic aktueller Monat : $countedoutput | Traffic (GB)=$countedoutput;$opt_w;$opt_c\n");
		exit $ERRORS{"CRITICAL"};
	}
	else {
		printf("Traffic aktueller Monat : $countedoutput | Traffic (GB)=$countedoutput;$opt_w;$opt_c\n");
		exit $ERRORS{"OK"};
	}
	



sub print_usage () {
	print "Usage: networkusage.pl -I Interface -D Description -c Kritische Grenze -w Warnung Grenze\n";
}

sub print_help () {
	print "$PROGNAME, Version: 1.0 \n";
	print "Copyright (c) 2009 Rudolf Kasper, SonOG\n\n";
	print_usage();
	print "
-I = Interface
-D = Description
-c = Kritische Grenze
-w = Warngrenze\n";
}