#!/usr/bin/perl

use strict;
use Getopt::Long;
use POSIX;
use File::Basename;
use Capture::Tiny ':all';
use Term::ANSIColor;

$ENV{"USER"} = "opuser";
$ENV{"HOME"} = "/home/opuser";

our $name = basename($0, ".pl");

our ($oHelp, $cmd, $stdout,$msg_share, $stderr, $exit, $oVerbose, $oWarn, $oCrit, $perf, $username, $password, $workgroup, $share);
#--------------------------------------------------------------------------------------
sub main {

        getoption();
        $msg_share = $share;
        $share =~ tr/\\/\//;
        if ($share =~ /\$\$/  || $share =~ /\$/){
                $share =~ s/\$\$/\$/;
                $share =~ s/\$\/\$/\$/;
                $share =~ s/\/\$/\$/;
                $share =~ s/\/\$\$/\$/;
                $msg_share = $share;
                $msg_share =~ s/\//\\/g;
        #print "$share\n";
        }


        if ($workgroup){
                $cmd = "/usr/bin/smbclient '$share' -U'$username%$password' -W $workgroup -c 'dir;exit'";
        }else{
                $cmd = "/usr/bin/smbclient '$share' -U'$username%$password' -c 'dir;exit'";
        }

        ($stdout, $stderr, $exit) = capture {
                        system($cmd);
        };

        if ($stderr =~ m/ntlmssp3_handle_neg_flags/){
                $cmd = "$cmd --option='client use spnego'=no";
                ($stdout, $stderr, $exit) = capture {
                        system($cmd);
                };
        }

        if ($stderr =~ m/smbclient/){
                print "Critical - Smblcient not found. Run: yum install samba-client !\n";
                exit(2);
        }


        chomp ($stdout, $stderr, $exit);
        if ($stderr =~ m/NT_STATUS(\S+)/ || $stdout =~ m/NT_STATUS(\S+)/){
                $stderr = "NT_STATUS$1";
        }

        if ($exit != 0){
                quit("CRITICAL - Share $msg_share is inaccessible. ERROR ($stderr)",2);
        }else{
                quit("OK Available - ".$msg_share,0);
        }

}

#--------------------------------------------------------------------------------------
sub quit {
        my $mgs = shift;
        my $code = shift;
        print $mgs,"\n";
        exit($code);
}
#--------------------------------------------------------------------------------------
sub getoption  {
        Getopt::Long::Configure('bundling');
        GetOptions(
                'h|help' => \$oHelp,
                'u|username=s' => \$username,
                'p|password=s' => \$password,
                'w|workgroup=s' => \$workgroup,
                's|share=s' => \$share
        );

        if ( ($oHelp) ){
                printUsage();
                exit(3);
        }
	elsif ((!$username) or (!$password) or (!$share)){
		quit ("Unknown - Missing parameters to validate the share. Check if username, password and share name is set appropriately.",3);
	}
	
}

#--------------------------------------------------------------------------------------
sub printUsage {
print color("red"), "\nUsage: $name.pl [OPTION]...\n",color("reset");
print color("green"),<<EOB
\n\t-u, --username
\t-p, --password
\t-W, --workgroup
\t-s, --share
EOB
,color("reset");
print color("yellow"),<<EOH
\n\tExample:
\t$name.pl -u \'workgroup\\username\' -p \'password\' -s '\\\\ip address or hostname\\share'
\t$name.pl -u \'username\' -w  workgroup -p \'password\' -s '\\\\ip address or hostname\\share'\n
EOH
,color("reset");
}
#--------------------------------------------------------------------------------------
&main;
