#!/usr/bin/perl
=begin
Version: 1.0

Copyright (c) 2009 by Rudolf Kasper, SonOG
=cut

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";
our $new_in_bytes=0;
our $new_out_bytes=0;
our $last_in_bytes = 0;
our $last_out_bytes  = 0;
our $last_check_time;
our $update_time=0;
our @last_values=undef;
our $flg_created = 0;
our $row=0;
our $in_diff=0;
our $out_diff=0;
our $in_rate=0;
our $out_rate=0;
our $time_diff=0;
my @dataset_in;
my @dataset_out;
my @splitinterface;
my $completestring;


Getopt::Long::Configure('bundling');
GetOptions
	("V=s"	=> \$opt_V, "Version" 	=> \$opt_V ,
	 "I=s" => \$opt_I, "Interface" 	=> \$opt_I ,
	 "D=s" => \$opt_D, "Direction" 	=> \$opt_D ,
	 "h"	=> \$opt_h, "help" 	=> \$opt_h ,
	 "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;

if ($opt_D eq 'in') {
create_incoming_database ();
} elsif ($opt_D eq 'out') {
create_outgoing_database ();
} else {
print_usage ();
}


sub create_outgoing_database () {
	if (-e "/usr/local/nagios/var/lib/networkusage/networkusage_out_".$opt_I) {
	    open(FILE,"<"."/usr/local/nagios/var/lib/networkusage/networkusage_out_".$opt_I);
	    while($row = <FILE>){
	                @last_values = split(":",$row);
	                $last_check_time = $last_values[0];
	                $last_out_bytes = $last_values[1];
	                $flg_created = 1;
	    }
	    close(FILE);
	} else {
	    $flg_created = 0;
	}
	
	$update_time = time();
	
	unless (open(FILE,">"."/usr/local/nagios/var/lib/networkusage/networkusage_out_".$opt_I)){
	    print "Check mod for temporary file : /usr/local/nagios/var/lib/networkusage/networkusage_out_".$opt_I. " !\n";
	    exit $ERRORS{"UNKNOWN"};
	}	
	print FILE "$update_time:$new_out_bytes";
	close(FILE);
	
	if ($flg_created == 0){
	    print "First execution : Buffer in creation.... \n";
	    exit($ERRORS{"UNKNOWN"});
	}

	$time_diff=$update_time - $last_check_time;
	$time_diff=$update_time if ($time_diff < 0);
	
	$out_diff=$new_out_bytes - $last_out_bytes;
	$out_diff=$new_out_bytes if ($out_diff < 0);
	
	$out_rate = $out_diff / ( $time_diff );
	$out_rate = $out_rate/1024 ;
	$out_rate = sprintf("%.2f", $out_rate);

	if ($in_rate >= $opt_w  && $in_rate < $opt_c) {
		printf("Network out : $out_rate KB/s | Netowrk-out (KB/s)=$out_rate;$opt_w;$opt_c\n");
		exit $ERRORS{"WARNING"};
	}
	elsif ($in_rate >= $opt_c ) {
		printf("Network out : $out_rate KB/s | Netowrk-out (KB/s)=$out_rate;$opt_w;$opt_c\n");
		exit $ERRORS{"CRITICAL"};
	}
	else {
		printf("Network out : $out_rate KB/s | Netowrk-out (KB/s)=$out_rate;$opt_w;$opt_c\n");
		exit $ERRORS{"OK"};
	}
	
}

sub create_incoming_database () {

	if (-e "/usr/local/nagios/var/lib/networkusage/networkusage_in_".$opt_I) {
	    open(FILE,"<"."/usr/local/nagios/var/lib/networkusage/networkusage_in_".$opt_I);
	    while($row = <FILE>){
	                @last_values = split(":",$row);
	                $last_check_time = $last_values[0];
	                $last_in_bytes = $last_values[1];
	                $flg_created = 1;
	    }
	    close(FILE);
	} else {
	    $flg_created = 0;
	}
	
	$update_time = time();
	
	unless (open(FILE,">"."/usr/local/nagios/var/lib/networkusage/networkusage_in_".$opt_I)){
	    print "Check mod for temporary file : /usr/local/nagios/var/lib/networkusage/networkusage_in_".$opt_I. " !\n";
	    exit $ERRORS{"UNKNOWN"};
	}	
	print FILE "$update_time:$new_in_bytes";
	close(FILE);
	
	if ($flg_created == 0){
	    print "First execution : Buffer in creation.... \n";
	    exit($ERRORS{"UNKNOWN"});
	}

	
	$in_diff=$new_in_bytes - $last_in_bytes;
	$in_diff=$new_in_bytes if ($in_diff < 0);

	$time_diff=$update_time - $last_check_time;
	$time_diff=$update_time if ($time_diff < 0);
	
	$in_rate = $in_diff / ( $time_diff );
	$in_rate = $in_rate/1024 ;
	$in_rate = sprintf("%.2f", $in_rate);

	if ($in_rate >= $opt_w  && $in_rate < $opt_c) {
		printf("Network in : $in_rate KB/s | Network-in (KB/s)=$in_rate;$opt_w;$opt_c\n");
		exit $ERRORS{"WARNING"};
	}
	elsif ($in_rate >= $opt_c ) {
		printf("Network in : $in_rate KB/s | Network-in (KB/s)=$in_rate;$opt_w;$opt_c\n");
		exit $ERRORS{"CRITICAL"};
	}
	else {
		printf("Network in : $in_rate KB/s | Network-in (KB/s)=$in_rate;$opt_w;$opt_c\n");
		exit $ERRORS{"OK"};
	}
	
}


sub print_usage () {
	print "Usage: check_networkusage.pl -I Interface -D Direction -c Kritikal-Bytes -w Warnung-Bytes\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 = Direction
-c = Kritische Grenze
-w = Warngrenze\n";
}
