
#!/bin/sh
# File	: get_filer_inventory_files.sh
# By	: Maarten.deBoer@AtosOrigin.com, 091125
# Subject	: Get filer information in .TXT / .CSV in a .ZIP file
# (0.2)	: Added; useradmin group, role & ${FILER} at .CVS, RLM status, route -s
# (0.3)	: Added; ifconfig -a, /etc/hosts, rc, vif status
# (0.4) : ${USER} added & test for uuencode + quota, df -g & -s awk extended. Options & /etc/hosts & LUNs added
#(0.5)  : Added; snap list, nbtstat, vscan, ping -c1, usermap.cfg
#{0.6)	: Name change get_more_filer_info.sh|get_filer_info.sh -> get_filer_inventory.sh. Added ${UNIX2DOS} |${UUENC} .asc
#(0.7)	: Place all files in 1 zip
#(0.8)	: Added `storage show tape | mc` & ZIPFILE, vfiler status -a, SNMP
#(0.9)	: Added vfilers
#(0.10)	: Renamed to get_filer_inventory_files.sh (from get_filer_inventory.sh)
#(0.11)	: $PING added
#(0.12) : Added; for FILER in `cat ${FILERS}|grep -v \^#|awk -F\; '{print $1}'|sort` & exit 0 at and & `type ping`
#(0.13)	: Added USAGE & options + -f(ilter)
#(0.14) : Added sysconfig -v (For CJ Grothaus)
#(0.15) : Added LOG
#(0.16)	: Added Vfilers (for Replace-FSOD-filers project)
#(0.17) : Added aggr show_space
#(0.18) : Added -e (ETC file)
#(0.19}	: mailx -a added (RHEL6)
#(0.20}	: Added --toll + ${SUBJECT}. Changed .asc into .asc (so windword can be related to .acs)
#set -x
PGM="`basename $0|cut -d\. -f1`" 
VERSION="0.20"
FILERS="${HOME}/etc/filers"
MAILTO="gmnl-msscentral@atos.net"
MAILTO="maarten.deboer@atos.net"
UUENC="uuencode"
SSH="ssh -n"
#SSH="rsh"
UNIX2DOS="unix2dos"
USER=""
ZIP="/usr/bin/zip"
TMPDIR="/tmp/${PGM}.$$"
PING="ping"
MAIL=""
FILTER="[1234567890]*"
LOG="${HOME}/log/${PGM}.log"
PATH="${PATH}:/usr/bin:/appl/tsu/bin:/usr/sbin:$HOME/bin"

mkdir ${TMPDIR}

# Functions
USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "  Version: ${VERSION}"
  echo "  options    :"
  echo "    -e       : specify Etc-file (${FILERS})"
  echo "    -f       : filter filername (${FILTER})"
  echo "    -h       : this help"
  echo "    -V       : Version"
  echo "    -x       : set -x"
  echo "    --toll   : send to LiveLink (via mailbox-role)"
  echo "    --mailto : change MAILTO address & do send mail (${MAILTO})"
  echo "    --help   : this help"
}

## MAIN
# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -e) FILERS=${2}; shift ;;
    -f) FILTER=${2}; shift ;;
    --toll) TOLL=1; MAIL=1; MAILTO="maarten.deboer@atos.net" ;;
    --mailto) MAILTO=${2}; MAIL=1; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done


echo "`date` ${PGM} Started" | tee -a ${LOG}

echo "FILERS=${FILERS}" | tee -a ${LOG}
echo "FILTER=${FILTER}" | tee -a ${LOG}
echo "MAILTO=${MAILTO}" | tee -a ${LOG}
echo "MAIL=${MAIL}" | tee -a ${LOG}
echo "TOLL=${TOLL}" | tee -a ${LOG}

# 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}|tee -a ${LOG}
# 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}|tee -a ${LOG}
# 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 ${ZIP}|tee -a ${LOG}
# Get full ping PATH
type ${PING} 1> /dev/null 2>&1
EC=$?
if [ ${EC} -eq 0 ]; then
  PING="`type ping | awk '{print $3}'`"
else
  PING="ping"
fi
echo ${PING}|tee -a ${LOG}

# Check ${FILERS} file
if [ ! -f ${FILERS} ]; then
  echo "Etc-file (${FILERS}) not found. Exiting ..."|tee -a ${LOG};exit 2;
fi

# Start getting info
for FILER in `cat ${FILERS}|grep -v \^#|grep ${FILTER}|awk -F\; '{print $1}'|sort`
do
  echo "`date` Filer ${FILER}..."|tee -a ${LOG}
  ${PING} -c 1 ${FILER} 1> /dev/null 2>&1
  EC=$?
  if [ ${EC} -eq 0 ]; then
    ZIPFILE="FSOD-Inventory-v${VERSION}-${FILER}.zip"
    ${SSH} ${USER}${FILER} sysconfig -a|${UNIX2DOS} > ${TMPDIR}/${FILER}-sysconfig-a.asc
    ${SSH} ${USER}${FILER} sysconfig -r|${UNIX2DOS} > ${TMPDIR}/${FILER}-sysconfig-r.asc
    ${SSH} ${USER}${FILER} sysconfig -v|${UNIX2DOS} > ${TMPDIR}/${FILER}-sysconfig-v.asc
    ${SSH} ${USER}${FILER} rdfile /etc/rc|${UNIX2DOS} > ${TMPDIR}/${FILER}-rc.asc
    ${SSH} ${USER}${FILER} rdfile /etc/hosts|${UNIX2DOS} > ${TMPDIR}/${FILER}-hosts.asc
    ${SSH} ${USER}${FILER} rdfile /etc/hosts.equiv|${UNIX2DOS} > ${TMPDIR}/${FILER}-hosts-equiv.asc
    ${SSH} ${USER}${FILER} ifconfig -a|${UNIX2DOS} > ${TMPDIR}/${FILER}-ifconfig-a.asc
    ${SSH} ${USER}${FILER} vif status |${UNIX2DOS} > ${TMPDIR}/${FILER}-vif-status.asc
    ${SSH} ${USER}${FILER} useradmin user list|${UNIX2DOS} > ${TMPDIR}/${FILER}-useradmin-user-list.asc
    ${SSH} ${USER}${FILER} useradmin group list|${UNIX2DOS} > ${TMPDIR}/${FILER}-useradmin-group-list.asc
    ${SSH} ${USER}${FILER} useradmin role list|${UNIX2DOS} > ${TMPDIR}/${FILER}-useradmin-role-list.asc
    ${SSH} ${USER}${FILER} exportfs|${UNIX2DOS} > ${TMPDIR}/${FILER}-exportfs.asc
    ${SSH} ${USER}${FILER} cifs shares|${UNIX2DOS} > ${TMPDIR}/${FILER}-cifs-shares.asc
    ${SSH} ${USER}${FILER} license|${UNIX2DOS} > ${TMPDIR}/${FILER}-license.asc
    ${SSH} ${USER}${FILER} vfiler status -a|${UNIX2DOS} > ${TMPDIR}/${FILER}-vfiler-status-a.asc
    ${SSH} ${USER}${FILER} rlm status|${UNIX2DOS} > ${TMPDIR}/${FILER}-rlm-status.asc
    ${SSH} ${USER}${FILER} vol status|grep ' root'|${UNIX2DOS} > ${TMPDIR}/${FILER}-root-volume.asc
    ${SSH} ${USER}${FILER} options|${UNIX2DOS} > ${TMPDIR}/${FILER}-options.asc
    ${SSH} ${USER}${FILER} route -s|${UNIX2DOS} > ${TMPDIR}/${FILER}-route-s.asc
    ${SSH} ${USER}${FILER} route -sn|${UNIX2DOS} > ${TMPDIR}/${FILER}-route-sn.asc
    ${SSH} ${USER}${FILER} lun show|${UNIX2DOS} > ${TMPDIR}/${FILER}-lun-show.asc
    ${SSH} ${USER}${FILER} iscsi status|${UNIX2DOS} > ${TMPDIR}/${FILER}-iscsi-status.asc
    ${SSH} ${USER}${FILER} vscan|${UNIX2DOS} > ${TMPDIR}/${FILER}-vscan.asc
    ${SSH} ${USER}${FILER} nbtstat|${UNIX2DOS} > ${TMPDIR}/${FILER}-nbtstat.asc
    ${SSH} ${USER}${FILER} rdfile /etc/usermap.cfg|${UNIX2DOS} > ${TMPDIR}/${FILER}-usermap-cfg.asc
    ${SSH} ${USER}${FILER} sis config|${UNIX2DOS} > ${TMPDIR}/${FILER}-sis-config.asc
    ${SSH} ${USER}${FILER} storage show mc|${UNIX2DOS} > ${TMPDIR}/${FILER}-storage-show-mc.asc
    ${SSH} ${USER}${FILER} storage show tape|${UNIX2DOS} > ${TMPDIR}/${FILER}-storage-show-tape.asc
    ${SSH} ${USER}${FILER} snmp|${UNIX2DOS} > ${TMPDIR}/${FILER}-snmp.asc
    ${SSH} ${USER}${FILER} aggr show_space|${UNIX2DOS} > ${TMPDIR}/${FILER}-aggr-show_space.asc

    ${SSH} ${USER}${FILER} snap sched|${UNIX2DOS} > ${TMPDIR}/${FILER}-snap-sched.asc
    echo "Getting a snap list can take a while ..."
    ${SSH} ${USER}${FILER} snap list|${UNIX2DOS} > ${TMPDIR}/${FILER}-snap-list.asc

    ${SSH} ${USER}${FILER} df -gA|awk '{print $1";"$2";"$3";"$4";"$5";"}' > ${TMPDIR}/${FILER}-aggr.csv
    ${SSH} ${USER}${FILER} df -g|awk '{print $1";"$2";"$3";"$4";"$5";"}' > ${TMPDIR}/${FILER}-volumes.csv
    ${SSH} ${USER}${FILER} df -s|awk '{print $1";"$2";"$3";"$4";"$5";"}' > ${TMPDIR}/${FILER}-sis.csv
    ${SSH} ${USER}${FILER} qtree status|awk '{print $1";"$2";"$3";"$4";"}' > ${TMPDIR}/${FILER}-qtrees.csv
    ${SSH} ${USER}${FILER} quota report|awk '{print $1";"$2";"$3";"$4";"$5";"$6";"$7";"$8";"$9";"}' > ${TMPDIR}/${FILER}-quotas.csv
    ${SSH} ${USER}${FILER} snapvault status|awk '{print $1";"$2";"$3";"$4";"}' > ${TMPDIR}/${FILER}-SnapVault-status.csv
    ${SSH} ${USER}${FILER} snapmirror status|awk '{print $1";"$2";"$3";"$4";"}' > ${TMPDIR}/${FILER}-SnapMirror-status.csv

# Start vfiler part
  for VFILER in `${SSH} ${FILER} vfiler status|grep -v vfiler|awk '{print $1}'`
  do
    echo "`date` vFiler ${FILER}/${VFILER} ..." | tee -a ${LOG}
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs domaininfo |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-domaininfo.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs homedir |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-homedir.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs nbalias |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-nbalias.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs prefdc print |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-prefdc-print.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs sessions |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-sessions.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs shares |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-shares.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} cifs stat |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-cifs-stat.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} dns info |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-dns-info.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} exportfs |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-exportfs.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} iscsi status |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-iscsi-status.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} lun show |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-lun-show.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} nbtstat |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-nbtstat.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} nis info |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-nis-info.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} nfs stat |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-nfs-stat.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} options |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-options.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} route -s |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-route-s.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} route -sn |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-route-sn.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} secureadmin status |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-secureadmin-status.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} useradmin user list |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-useradmin-user-list.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} useradmin group list |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-useradmin-group-list.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} useradmin role list |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-useradmin-role-list.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} vscan |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-vscan.asc
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} ypwhich |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-ypwhich.asc

    ${SSH} ${USER}${FILER} vfiler run ${VFILER} qtree status|awk '{print $1";"$2";"$3";"$4";"}' > ${TMPDIR}/${FILER}-${VFILER}-qtrees.csv
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} quota report|awk '{print $1";"$2";"$3";"$4";"$5";"$6";"$7";"$8";"$9";"}' > ${TMPDIR}/${FILER}-${VFILER}-quotas.csv
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} snapvault status|awk '{print $1";"$2";"$3";"$4";"}' > ${TMPDIR}/${FILER}-${VFILER}-SnapVault-status.csv
    ${SSH} ${USER}${FILER} vfiler run ${VFILER} snapmirror status|awk '{print $1";"$2";"$3";"$4";"}' > ${TMPDIR}/${FILER}-${VFILER}-SnapMirror-status.csv

# Get vfiler-vol0-etc dir
    VFILERETCDIR="`${SSH} ${FILER} vfiler status -r ${VFILER}|grep etc|awk '{print $2}'`"
    ${SSH} ${FILER} rdfile ${VFILERETCDIR}/etc/usermap.cfg |${UNIX2DOS} > ${TMPDIR}/${FILER}-${VFILER}-usermap-cfg.asc

  done


# Zipping & mailing
    ${ZIP} -v /tmp/${ZIPFILE} ${TMPDIR}/*
    RHREL="`/bin/rpm -qa|grep redhat-release|cut -d\- -f4| grep -i server`"
# Subject
    SUBJECT="${ZIPFILE} [${PGM} v${VERSION}]"
    if [ ${TOLL} ]; then
      SUBJECT="${SUBJECT} {TO_LL:fsod_inventory}"
    fi
    if [ "${RHREL}" = "5Server" ]; then
      cat /tmp/${ZIPFILE}|${UUENC} ${ZIPFILE}|mailx -s "${SUBJECT}" ${MAILTO}
    fi
    if [ "${RHREL}" = "6Server" ]; then
      echo "${PGM}:${ZIPFILE} [${PGM} v${VERSION}] at `date`"|mailx -s "${SUBJECT}" -a /tmp/${ZIPFILE} ${MAILTO}
    fi
    echo "`date` Information mailed to ${MAILTO}" | tee -a ${LOG}
# Cleaningup Filer info
    rm /tmp/${ZIPFILE}
    rm ${TMPDIR}/${FILER}*

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

# Clean-up
if [ "${TMPDIR}" != "" ]; then
  echo "Cleaningup ${TMPDIR} ..."
  rm -rf ${TMPDIR}
fi
echo "`date` ${PGM} Finished. " | tee -a ${LOG}
exit 0

