
#!/bin/ksh
# File	: get_filer_inventory.ksh
# By	: Maarten.deBoer@AtosOrigin.com, 091125
# Subject	: Get filer inventory (information) to be send to MSS-Tooling
#(0.1)
#set -x
PGM="`basename $0|cut -d\. -f1`" 
TMP="/tmp/${PGM}.$$"
VERSION="0.1"
FILERS="$HOME/etc/filers"
MAILTO="maarten.deboer@atosorigin.com"
UUENC="/usr/bin/uuencode"
SSH="/usr/bin/ssh"
#SSH="rsh"
UNIX2DOS="/usr/bin/unix2dos"
USER=""
ZIP="/usr/bin/zip"
TMPDIR="/tmp/${PGM}.$$.dir"
PING="/usr/sbin/ping"
OUT="$HOME/data/out/${PGM}.out"

touch ${TMP}
mkdir ${TMPDIR}

# Test if uuencode exists. Otherwise use tee
type ${UUENC} 1> /dev/null 2>&1
EC=$?
if [ ${EC} -eq 0 ]; then
  UUENC="`type uuencode|awk '{print $3}'`"
else
  UUENC="tee"
fi
echo ${UUENC}
# Test if unix2dos exists. Otherwise use tee
type ${UNIX2DOS} 1> /dev/null 2>&1
EC=$?
if [ ${EC} -eq 0 ]; then
  UNIX2DOS="`type unix2dos|awk '{print $3}'`"
else
  UNIX2DOS="tee"
fi
echo ${UNIX2DOS}
# Test if zip exists. Otherwise use tar
type ${ZIP} 1> /dev/null 2>&1
EC=$?
if [ ${EC} -eq 0 ]; then
  ZIP="`type zip|awk '{print $3}'`"
else
  ZIP="tar -c"
fi
echo "UUENC=${UUENC}, UNIX2DOS=${UNIX2DOS}, ZIP=${ZIP}"

for FILER in `cat $FILERS | grep naf02`
do
  echo "Filer ${FILER}..."
  ${PING} -c 1 ${FILER} 1> /dev/null 2>&1
  EC=$?
  if [ ${EC} -eq 0 ]; then
    ZIPFILE="FSOD-Inventory-v${VERSION}-${FILER}.zip"
    OUT="$HOME/data/out/${PGM}-${FILER}.out"
    CSV="FSOD-Inventory-v${VERSION}-${FILER}.csv"
    cp /dev/null ${OUT}
# = CAP
# Filer(name)
    echo "# Filer ; ${FILER} ;"|tee -a ${OUT}
# Amount of disks, 
    ${SSH} -n $FILER sysconfig -r|grep -v FAILED|egrep 'dparity|parity|spare|data' > ${TMP}
    DISKCNT="`cat ${TMP}|wc -l`"
    echo "# DiskCnt ; ${DISKCNT}; "|tee -a ${OUT}
# with type & cap
    cat ${TMP}|awk '{print $10}'|sort -u | while read LINE
    do
      echo "# Disktype ; ${LINE}; \c"|tee -a ${OUT}
      grep ${LINE} ${TMP}|wc -l|tee -a ${OUT}
      echo ""|tee -a ${OUT}
    done
# Amount of spares
    SPARECNT="`cat ${TMP}|grep spare|wc -l`"
    echo "# SpareCnt ; ${SPARECNT}; "|tee -a ${OUT}

# Aggr Total, Used [ df -Ak]. K's is better then G's
    echo "# Aggrs ; Name ; Total (K) ; Used (K) ;"|tee -a ${OUT}
    ${SSH} -n ${FILER} df -Ak|grep -v snapshot|sort|grep -v Aggregate|awk '{print ";"$1";"$2";"$3";"}'|sed -e's/KB//g'|tee -a ${OUT}
# Volumes Total + Used [df -k]
    echo "# Volumes ; Name ; Total (K) ; Used (K) ;"|tee -a ${OUT}
    ${SSH} -n ${FILER} df -k|grep -v snapshot|sort|grep -v Filesystem|awk '{print ";"$1";"$2";"$3";"}'|sed -e's/KB//g'|tee -a ${OUT}
# Volumes Used, Saved (A-SIS), [df -sk]
    echo "# A-SIS volumes ; Name ; Used (K) ; Saved (K) ;"|tee -a ${OUT}
    ${SSH} -n ${FILER} df -sk|grep -v snapshot|sort|grep -v Filesystem|awk '{print ";"$1";"$2";"$3";"}'|sed -e's/KB//g'|tee -a ${OUT}

# Quotas (Used & Limit) [quota report]

# Qtee [qtree status]

# Qtree used %
# = Config
# Filer(name)
    echo "# Filer ; ${FILER} ;"|tee -a ${OUT}
# Filer type
    MODEL="`${SSH} -n ${FILER} sysconfig -a|grep Model|awk -F\: '{print $2}'|sed -e 's/ //g'`"
    echo "# Model ; ${MODEL} ;"|tee -a ${OUT}
# ONTAP version
    RELEASE="`${SSH} -n ${FILER} sysconfig -a|grep Release|awk '{print $3}'|cut -d\: -f1`"
    echo "# ONTAP release ; ${RELEASE} ;"|tee -a ${OUT}
# Vfilers
    echo "# Vfilers ; Name ; Status ;"|tee -a ${OUT}
    ${SSH} -n ${FILER} vfiler status|grep -v vfiler0|awk '{print ";"$1";"$2";"}'|tee -a ${OUT}
# Per Vfiler
    ${SSH} -n ${FILER} vfiler status|grep -v vfiler0|grep running|awk '{print $1}' > ${TMP}
# Volumes per vfiler
    cat ${TMP}|while read VFILER
    do
      echo "# Vfiler ; ${VFILER} ; Volumes ;"|tee -a ${OUT}
      ${SSH} -n ${FILER} vfiler status -a ${VFILER}|grep Path|awk '{print "; ;"$2";"}'|sort|tee -a ${OUT}
    done
# Qtrees style [qtree status]
    echo "# Qtrees ; Volume ; Tree ; Style ; Oplocks ;"|tee -a ${OUT}
    ${SSH} -n ${FILER} qtree status|egrep -v '^$|Volume|=====|--------'|sort|awk '{print ";"$1";"$2";"$3";"$4";"}'|sort|tee -a ${OUT}
# CIFS shares (+rechten) [cifs shares]
    cat ${TMP}|while read VFILER
    do
      echo "# CIFS shares ; ${VFILER} ; Name ; Mount Point ; Description ;"|tee -a ${OUT}
      ${SSH} -n ${FILER} vfiler run ${VFILER} cifs shares|egrep -v '^$|Volume|=====|--------|Description'|cut -f1|sort|awk '{print "; ;"$1";"$2";"$3";"}'|tee -a ${OUT}
    done

# NFS exports (+rechten) [exportfs]
    cat ${TMP}|while read VFILER
    do
      echo "# NFS exports ; ${VFILER} ; Volume ; Options ;"|tee -a ${OUT}
      ${SSH} -n ${FILER} vfiler run ${VFILER} exportfs|egrep -v '^$|=====|--------'|awk '{print "; ;"$1";"$2";"}'|sort|tee -a ${OUT}
    done



# Interfaces
    ${SSH} -n ${FILER} ifconfig -a|grep flags|cut -d\: -f1 | while read IF
    do
      echo "# IF ; ${IF} ; "|tee -a ${OUT}
      ${SSH} -n ${FILER} ifconfig ${IF}|grep inet|awk '{print ";"$1";"$2";"$4";"$6";"}'|tee -a ${OUT}
      ${SSH} -n ${FILER} ifconfig ${IF}|grep ether|awk '{print ";"$1";"$2";"$3";"$4";"$5";"}'|tee -a ${OUT}
      ${SSH} -n ${FILER} ifconfig ${IF}|grep trunked|awk '{print ";"$1";"$2";"}'|tee -a ${OUT}
    done



# LUNs
# iSCSI + IQN (van de filer) 
# SnapMirror relations [snapmirror status]
# SnapVault relations [snapvault status]
# Snap Schedule [snap sched]
# Snaps per volume [snap list]
# A-sis (de-dupe) config [sis config]



# Zipping & mailing
#    ${ZIP} -v /tmp/${ZIPFILE} ${TMPDIR}/*
    cat ${OUT}|${UUENC} ${CSV}|mailx -s ":`hostname`: Inventory ${FILER} [${PGM} v${VERSION}]" ${MAILTO}
    echo "Information mailed to ${MAILTO}"
# Cleaningup Filer info
#    rm ${OUT}
#    rm ${TMPDIR}/${FILER}*

  else
    echo "Problem with connection to ${FILER}"|mailx -s "[${PGM} v${VERSION}]" ${MAILTO}
  fi
done

# Clean-up
if [ "${TMPDIR}" != "" ]; then
  echo "Cleaningup ${TMPDIR} ..."
  rm -rf ${TMPDIR}
  rm ${TMP}
fi
exit

