#!/bin/bash 

#
# Monitor for mon which test cifs shares.
# The idea is to use smbclient to obtain the list of shares exposed by the host(s) passed as argument.
#
# Es:
# hostgroup CIFS_Shares windowssrv1 windowssrv2 sambasrv1 filer1
#
# watch CIFS_Shares
#    service cifs_share
#        description Test share CIFS
#        interval 2m
#        monitor cifs.monitor
#        period
#            alert mail.alert admin@yourdomain.org
#            upalert mail.alert -u admin@yourdomain.org
#
#
#
#
#
#
#
# Roberto Torresani
# Information Technology and Telecommunications Administrative Department
# University of Trento - Italy
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#


if [ "$#" == 0 ]; 
then
 echo "mon_cifs_monitor.sh host1 host2 host2 ... hostn" 
 exit 0;
fi

OUTFILE_OK=/tmp/$$mon_cifs_OUTOK
OUTFILE_KO=/tmp/$$mon_cifs_OUTKO
OUTFILE_KO_DETAIL=/tmp/$$mon_cifs_OUTKO_DETAIL

touch $OUTFILE_OK
touch $OUTFILE_KO
touch $OUTFILE_KO_DETAIL

GLOBAL_EXITCODE=0
for CIFSHOST in $*
do
	smbclient -N -L $CIFSHOST > /tmp/$$mon_cifs_$CIFSHOST
	CIFSHOST_STATUS=$?
	let "GLOBAL_EXITCODE= $GLOBAL_EXITCODE || $CIFSHOST_STATUS"
	
	if [ "$CIFSHOST_STATUS" == "0" ]; 
	then
	   echo "OK: CIFS SHARES SU $CIFSHOST" >> $OUTFILE_OK
	   #cat /tmp/$$mon_cifs_$CIFSHOST 
	else
	   echo "KO: CIFS SHARES SU $CIFSHOST" >> $OUTFILE_KO
	   echo "KO: CIFS SHARES SU $CIFSHOST" >> $OUTFILE_KO_DETAIL
           echo "**********************" >> $OUTFILE_KO_DETAIL
	   cat /tmp/$$mon_cifs_$CIFSHOST >> $OUTFILE_KO_DETAIL
           echo "**********************" >> $OUTFILE_KO_DETAIL
	fi

	rm /tmp/$$mon_cifs_$CIFSHOST
done


if [ $GLOBAL_EXITCODE == "0" ]; then
   SUMMARYOUT="CIFS Share OK"
else
   SUMMARYOUT="CIFS Share KO"
fi

echo $SUMMARYOUT
echo " ***** CIFS share OK *****"
echo "_________________________________"
cat $OUTFILE_OK
echo "_________________________________"
echo 
echo
echo " ***** CIFS share KO *****"
echo "_________________________________"
cat $OUTFILE_KO
echo "_________________________________"
echo 
echo
echo " **** Errors detail *****"
echo "_________________________________"
cat $OUTFILE_KO_DETAIL
echo "_________________________________"



rm $OUTFILE_OK
rm $OUTFILE_KO
rm $OUTFILE_KO_DETAIL

#echo "exit code: $GLOBAL_EXITCODE"
exit $GLOBAL_EXITCODE