#!/usr/bin/perl -w

'**************************************************************
'Plugin Nagios - chequeo folder in share con usuario guest
' Realizador: Jaime Andres Cardona
' Correo: ja_an_ca@hotmail.com
' Version: 1.0
' Fecha: 2012-May-10

# ***********************************************************
# Ejemplo de ejecucion del script con el usuario guest listando los directorios sin espacios
# smbclient -U guest -N -L IP
# Anonymous login successful
# Domain=[TQCLO01] OS=[Unix] Server=[Samba 3.5.1]
#
#         Sharename       Type      Comment
#         ---------       ----      -------
#         TIMEMACHINE     Disk      
#         Backup          Disk      
#         Public          Disk      
#         Share           Disk      
#         IPC$            IPC       

use Getopt::Long;
&Getopt::Long::config('auto_abbrev');

my $numbarg = @ARGV;
($numbarg < 4) && &show_help;

my %STATUSCODE = (  'UNKNOWN' => '-1',
                    'OK' => '0',
                    'WARNING' => '1',
                    'CRITICAL' => '2');


sub show_help {
printf("usage: ./check_smb_folder.pl -h <host> -f <foldertocheck>
ejemplo: ./check_smb_folder.pl -h 192.168.1.1 -f Backup
");
    exit;
}


# GetOptions (	"length=i" => \$length, 	# numeric
#				"file=s" => \$data, 		# string
#				"verbose" => \$verbose); 	# flag

GetOptions( 	"h=s" => \$host,
						"f=s" => \$folder);

# print("proceso=$proceso,cpuw=$cpuw,cpuc=$cpuc,tw=$tw,tc=$tc\n");
# retorna el unico proceso que consume

open(PS_F, "smbclient -U guest -N -L $host | grep Disk | awk {'print \$1'}|");
# retorna todos los procesos 
# open(PS_F, "top -b -n 1 -u applprod | grep f60webmx |");
$folder_encontrado=0;
while (<PS_F>) {
  ($carpeta) = split;
  # do whatever I want with the variables here ...
 # print("tiempo sin split = $time\n");
 if ("$folder" eq "$carpeta")
 {
	$folder_encontrado=1;
 }
}
close(PS_F);

#Si no retorna datos, no hay proceso consumiendo para evaluar
if ($folder_encontrado == 1) {
		printf("OK - Carpeta compartida en smb = '$folder'\n");
		exit($STATUSCODE{"OK"});
 	}
else
{
		printf("CRITICAL - La carpeta '$folder' no está compartida en smb\n");
		exit($STATUSCODE{"CRITICAL"});
}

printf("UNKNOWN\n");
exit($STATUSCODE{"UNKNOWN"});

