
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: quota_check.sh                                                     #
#                                                                          #
# Description: Script collects data about quota information                #
#              Runs ad-hoc from command line and scheduled from crontab    #
#                                                                          #
# Version: 1.0                                                             #
#                                                                          #
# References:                                                              #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: None                                                         #
#                                                                          #
# Usage: volresize.sh                                                      #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# Author(s): Atos Origin                                                   #
#                                                                          #
# Change log:                                                              #
# aut   date          vers   comments                                      #
# RtB   10-07-2009    1.0    Copy from make_report.sh                      #
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Read parameter                                                           #
#--------------------------------------------------------------------------#

#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
DATE=`date +%Y%m%d`
DATADIR=${HOME}/data/out
LOG=${DATADIR}/$DATE-quota_check.log
MAILTO="gmnl-msscentral@atosorigin.com fsod@atosorigin.com"
EXCL=${HOME}/etc/quota_exclude.lst


#--------------------------------------------------------------------------#
# Collect data of vfilers, volumes, qtrees, quotas and exportfs            #
#--------------------------------------------------------------------------#

for FILER in $(cat ${HOME}/etc/filers)
do
  for VFILER in `ssh ${FILER} vfiler status | grep -v vfiler0 | grep running | awk '{ print $1 }'`
  do
    for VOLUME in `ssh ${FILER} vfiler run ${VFILER} vol status | grep online | awk '{ print $1 }' | grep -v ^vol0 | /usr/xpg4/bin/grep -v -f ${EXCL} | sort`
    do
    QSTATUS=`ssh ${FILER} vfiler run ${VFILER} quota status ${VOLUME} 2>/dev/null | grep quotas | grep -v snapmirrored | awk '{print $4}' | cut -d. -f1`
      if test ${QSTATUS} 
      then
         if test ${QSTATUS} = off
         then
            echo "${VOLUME}: Quotas off (${FILER})" >> ${LOG}
         else
           for QTREE in `ssh ${FILER} vfiler run ${VFILER} quota report | grep /vol/${VOLUME} | awk '{print $4}'`
           do
             for QSIZE in `ssh ${FILER} vfiler run ${VFILER} quota report | grep /vol/${VOLUME} | grep ${QTREE} | awk '{print $6}'`
             do
               if test ${QSIZE} = '-'
               then
                  echo "${VOLUME}: No quota on ${QTREE} (${FILER})" >> ${LOG}
               fi
             done
           done
         fi
      fi
    done
  done
done


#--------------------------------------------------------------------------#
# Send output as attachment in email.                                      #
# If script doesn't start on console, send mail to terminal                #
#--------------------------------------------------------------------------#
if `tty -s`
then
  cat ${LOG}
else
  cat ${LOG} | mailx -s ":nlxfsd01: ${DATE} Errors from quota check script" ${MAILTO}
fi

rm ${LOG}

