# -*- perl -*-
#
# $Revision: 1.16 $
package afs_client;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;

sub print_usage ();
sub print_version ();
sub service_test (%);

sub print_usage () {

    print "
Usage:  Check afs client.
Options:
    -H <hostname> -P <portnumber> -c <afs client version> [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.16 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "afs_client.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $host = $options{"opt_H"};
    if ((!$host) || ($host eq "")) {
        print "\nHost name not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my $afsversion = $options{"opt_c"};
    if ((!$afsversion) || ($afsversion eq "")) {
        print "\nVersion number not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my $port = $options{"opt_P"} ? $options{"opt_P"} : 7001;
    my $ERROR_THRESHOLD = $options{"opt_t"} ? $options{"opt_t"} : 60;

    my $status = "CRITICAL";
    my $lastafs;
    my $output;

    my $rxdebug;
    if ( -f "/usr/afsws/etc/rxdebug") {
    	$rxdebug = "/usr/afsws/etc/rxdebug";
    } elsif ( -f "/usr/sbin/rxdebug") {
    	$rxdebug = "/usr/sbin/rxdebug";
    } else { 
	$status = "CRITICAL";
	print "$status: rxdebug binary not found!";
    	exit $ERRORS{$status};
    }

    my $timeout = "/s/lab/bin/timeout";
    my $CACHE_DIR = "/tmp/nagios_afs_client"; # what directory to create error state information in
    my $CACHE_FILE_SUFFIX = "_last_connect";
    my $DIR_PERMS = 0777;           # Dir permisions for the chache directory
    my $CACHE_FILE_PERMS = 0666;           # Dir permisions for the chache directory
    my $CACHE_FILE = $CACHE_DIR . "/" . $host . $CACHE_FILE_SUFFIX;

    # make sure the state directory exists
    mkdir "$CACHE_DIR" if ! -d $CACHE_DIR;
    chmod $DIR_PERMS, $CACHE_DIR;

    print "$timeout 10 $rxdebug -version $host $port\n" if $options{"verbose"};
    open CMD, "$timeout 10 $rxdebug -version $host $port 2>&1 |";

    LOOP: while ( <CMD> ) {
        if ( /AFS version.*(\d+\.\d+\.\d+)/ ) {
            $lastafs = $1;
            $output = "AFS Client Version: $lastafs";

            my @remote = split('\.', $lastafs);
            my @test = split('\.', $afsversion);
            my $bad = 0;
            TEST: for (my $x = 0; $x <= $#test; $x++) {
                if ($remote[$x] > $test[$x]) {
                    last TEST;
                } elsif ($remote[$x] < $test[$x]) {
                    $bad = 1;
                    last TEST;
                }
            }
            if ($bad == 0) {
                $status = "OK";
            } else {
                $output .= " - should be: $afsversion or newer";
            }
        }
    }

    if ($output) {
        open(OUT, ">$CACHE_FILE") or die "Couldn't create/write $CACHE_FILE: $!\n";
        print OUT "$lastafs\n";
        close OUT;
        chmod $CACHE_FILE_PERMS, $CACHE_FILE;
    } else {
        if (-f $CACHE_FILE) {
            my $last_ok_time = -M "$CACHE_FILE";
            $last_ok_time = ( int ($last_ok_time * 24 * 60 * 10 ) / 10);
            open(IN, $CACHE_FILE) or die "Couldn't read $CACHE_FILE: $!\n";
            $lastafs = <IN>;
            chomp $lastafs;
            close IN;
            $output = "Not Responding - last successful $last_ok_time minutes ago (Version $lastafs)";
#            if ($last_ok_time >= $ERROR_THRESHOLD) {
#                $status = "CRITICAL";
#            } else {
                $status = "WARNING"; # set to OK for shutupshutupshutup
#            }
        } else {
            $status = "CRITICAL";
            $output = "Not Responding - never successfully checked.";
        }
    }

    print "$status: $output\n";
    print "Exit status: $status\n" if $options{"verbose"};

    exit $ERRORS{$status};
}

