
# File  : netapp-cdot_get_filer_inventory_files.sh
# By    : Maarten.deBoer@AtosOrigin.com, 091125
# Subject       : Get filer information in .ASC(.TXT) / .CSV in a .tar-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 vCLUSTERS
#(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 ${CLUSTERS}|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 VCLUSTERS (for Replace-FSOD-CLUSTERS 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)
#(0.21) : Mod. All ASCii; UNIX2DOS rem.; Add `aggr statys -v`; zip -> tar
#(0.22) : Added iscsi. VERSION -> VER, Added '|grep running'
#(0.23) : Mod. USAGE. rm /tmp/${ZIPFILE}, Added vFiler0 (rem grep -v vFiler)
#(0.24) : Add SCP, uptime, messages-file & Need to give ONE option
#(0.25) : Added --logfiles (LOGFILES)
#(0.26) : Remove check by ping (did not work with Filers nlnaf63+64) change to check by SSH
#(0.26j): Modified for use by NAM Storage-Siemens team - Jay Kennedy - 26 Aug 15
#(0.27j): Modified for use by NAM Storage-Siemens team for Clustered OnTAP - Jay Kennedy - 08 Sep 15
#(0.28)	: Mod for FSOD (again), MdB
#(0.29)	: Mod KB's + -instance. Removed .CSV MdB,2015-12-22
#(0.30)	: Some Mod.
#(0.31)	: Some mod's about inconfig, options aggr
#       : Remove PING & TOLL
#
#(1.0)	: Renamed to netapp-cdot_get_filer_inventory_files.sh. Finalized due to MAS2ATF
#(1.1)	: Added LOGDIR, changed df -A, -k, -s, # at -s-dedupe & -s-compressed
#(1.2),160711	: Some mod of var places in begin of script
#set -x
PGM="`basename $0|cut -d\. -f1`"
VER="1.2"
UUENC="uuencode"
SSH="ssh -n"
#SSH="rsh"
PING="ping"
MAIL=""
FILTER="[?]*"
TAR="/bin/tar"
USESCP=""
LOGFILES=""

# The following VARs might be changed
CLUSTERS="${HOME}/etc/clusters"
USER=""
MAILTO="maarten.deboer@atos.net"
LOGDIR="${HOME}/log"
LOG="${LOGDIR}/${PGM}.log"
TARDIR="/tmp"
TMPDIR="/tmp/${PGM}.$$"
SCPHOST="nlxmssp1"
SCPUSER="receiver"
SCPDIR="/tmp/"
SCPDIR="/home/receiver/import/inventory/filers/"





PATH="${PATH}:/usr/bin:/appl/tsu/bin:/usr/sbin:$HOME/bin"

mkdir ${TMPDIR}

# Functions
USAGE()
{
  echo "Usage: ${PGM} <option(s)>"
  echo "  Version: ${VER}"
  echo "  options           :"
  echo "    -e              : specify Etc-file (${CLUSTERS})"
  echo "    -f              : filter filername (${FILTER})"
  echo "    -h | --help     : this help"
  echo "    -l | --logfiles : adds (messages & snapmirror) Logfiles in inventory"
  echo "    -m | --mail     : do send mail"
  echo "    -s | --scp      : used SCP for copying (to ${SCPUSER}@${SCPHOST}:${SCPDIR})"
  echo "    -V              : Version"
  echo "    -x              : set -x"
  echo "    --logfiles      : add (message & snapmirror) LOGFILES to inventory-file"
  echo "    --mailto        : change MAILTO address & do send mail (${MAILTO})"
}
## MAIN
# Check options
if [ ${#} -eq 0 ]; then
  echo "No option(s) given. So not to know what to do. Exiting..."; echo; USAGE;
  exit 1
fi
while [ ${#} -ge 1 ]
  do
  case $1 in
    -e) CLUSTERS=${2}; shift ;;
    -f) FILTER=${2}; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -m | --mail) MAIL=1; shift ;;
    -l | --logfiles) LOGFILES="1"; shift ;;
    -s | --scp) USESCP=1;;
    --mailto) MAILTO=${2}; MAIL=1; shift ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
  done

if [ ! -d ${LOGDIR} ]; then
  mkdir -p ${LOGDIR}
fi

echo "`date` ${PGM} v${VER} started" | tee -a ${LOG}
echo "CLUSTERS=${CLUSTERS}" | tee -a ${LOG}
echo "FILTER=${FILTER}" | tee -a ${LOG}
echo "MAILTO=${MAILTO}" | tee -a ${LOG}
echo "MAIL=${MAIL}" | tee -a ${LOG}
echo "LOGFILES=${LOGFILES}" | tee -a ${LOG}
echo "USESCP=${USESCP}" | 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}

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

# Start getting info
for CLUSTER in `cat ${CLUSTERS}|grep -v \^#|grep ${FILTER}|awk -F\; '{print $1}'|sort`
  do
# Check if Cluster is reachable & has the same name
    ANSW="`${SSH} ${USER}${CLUSTER} hostname`"
    if [ "${CLUSTER} = ${ANSW}" ]; then
      echo "`date` Cluster ${CLUSTER}..."|tee -a ${LOG}
      TARFILE="FSOD-Inventory-NetApp-CDOT-v${VER}-${CLUSTER}.tgz"
      ${SSH} ${USER}${CLUSTER} 'node show -instance' > ${TMPDIR}/${CLUSTER}_nodes-instance.asc
      ${SSH} ${USER}${CLUSTER} 'cf status -instance' > ${TMPDIR}/${CLUSTER}_cf-status-instance.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB; storage disk show -instance' > ${TMPDIR}/${CLUSTER}_disk-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'vserver show -instance' > ${TMPDIR}/${CLUSTER}_vserver-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'security login show -instance' > ${TMPDIR}/${CLUSTER}_security-login-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'security ssl show -instance' > ${TMPDIR}/${CLUSTER}_security-ssl-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'security login role show -instance' > ${TMPDIR}/${CLUSTER}_security-role-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'system license show -instance' > ${TMPDIR}/${CLUSTER}_license-instance.asc
#      ${SSH} ${USER}${CLUSTER} 'system node service-processor show -instance' > ${TMPDIR}/${CLUSTER}_sp-status-instance.asc
      ${SSH} ${USER}${CLUSTER} 'network interface show -instance' > ${TMPDIR}/${CLUSTER}_network-interface-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'network port show -instance' > ${TMPDIR}/${CLUSTER}_network-port-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'portset show -instance' > ${TMPDIR}/${CLUSTER}_portset-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'igroup show -instance' > ${TMPDIR}/${CLUSTER}_igroup-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'network routing-groups route show -instance' > ${TMPDIR}/${CLUSTER}_network-routing-groups-route-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'network interface show-routing-group -instance' > ${TMPDIR}/${CLUSTER}_network-interface-show-routing-group-instance.asc
      ${SSH} ${USER}${CLUSTER} 'network interface failover-groups show -instance' > ${TMPDIR}/${CLUSTER}_network-interface-failover-groups-show-instance.asc

      ${SSH} ${USER}${CLUSTER} 'fcp show -instance' > ${TMPDIR}/${CLUSTER}_fcp-show.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;volume show -instance' > ${TMPDIR}/${CLUSTER}_volume-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;volume show -volume *root* -instance' > ${TMPDIR}/${CLUSTER}_root-volume.asc
      ${SSH} ${USER}${CLUSTER} 'lun show -instance' > ${TMPDIR}/${CLUSTER}_lun-show.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;volume efficiency show -instance' > ${TMPDIR}/${CLUSTER}_sis-config.asc
      ${SSH} ${USER}${CLUSTER} 'hardware tape library show -instance' > ${TMPDIR}/${CLUSTER}_tape-library-show.asc
      ${SSH} ${USER}${CLUSTER} 'snmp' > ${TMPDIR}/${CLUSTER}_snmp.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;aggregate show_space -instance' > ${TMPDIR}/${CLUSTER}_aggr-show-space.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;storage aggregate show -instance' > ${TMPDIR}/${CLUSTER}_storage-aggr-show-instance.asc
      ${SSH} ${USER}${CLUSTER} 'volume snapshot policy show -instance' > ${TMPDIR}/${CLUSTER}_snap-sched-policy.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;volume snapshot show -instance' > ${TMPDIR}/${CLUSTER}_snap-list.asc
      ${SSH} ${USER}${CLUSTER} 'df -A' > ${TMPDIR}/${CLUSTER}_df-A.asc
      ${SSH} ${USER}${CLUSTER} 'df -k' > ${TMPDIR}/${CLUSTER}_df-k.asc
      ${SSH} ${USER}${CLUSTER} 'df -s' > ${TMPDIR}/${CLUSTER}_df-s.asc
#      ${SSH} ${USER}${CLUSTER} 'df -dedupe-saved -kilobyte -instance' > ${TMPDIR}/${CLUSTER}_df-s-dedup.asc
#      ${SSH} ${USER}${CLUSTER} 'df -compression-saved -kilobyte -instance' > ${TMPDIR}/${CLUSTER}_df-s-compression.asc
      ${SSH} ${USER}${CLUSTER} 'volume qtree show -instance' > ${TMPDIR}/${CLUSTER}_qtree-status.asc
      ${SSH} ${USER}${CLUSTER} 'set -units KB;volume quota report -instance' > ${TMPDIR}/${CLUSTER}_quota-report.asc
      ${SSH} ${USER}${CLUSTER} 'snapmirror status -instance' > ${TMPDIR}/${CLUSTER}_snapmirror-status.asc

      if [ ${LOGFILES} ]; then
        ${SSH} ${USER}${CLUSTER} 'event log show -instance' > ${TMPDIR}/${CLUSTER}_messages.asc
      fi  # LOGFILES

# Vserver
      ${SSH} ${USER}${CLUSTER} 'vserver options' > ${TMPDIR}/${CLUSTER}_options.asc
      ${SSH} ${USER}${CLUSTER} 'vserver services dns show -instance' > ${TMPDIR}/${CLUSTER}_dns-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver services ldap show -instance' > ${TMPDIR}/${CLUSTER}_ldap-show.asc

# iSCSi
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-status.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi show -fields target-alias -instance' > ${TMPDIR}/${CLUSTER}_iscsi-alias.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi connection show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-connection-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi initiator show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-initiator-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi interface show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-interface-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi interface accesslist show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-interface-accesslist-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi isns show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-isns-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi nodename -instance' > ${TMPDIR}/${CLUSTER}_iscsi_nodename.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi portal show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-portal-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi security show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-security-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi session show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-session-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi portal show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-portal-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver iscsi tpgroup show -instance' > ${TMPDIR}/${CLUSTER}_iscsi-tpgroup-show.asc
# CIFS
      ${SSH} ${USER}${CLUSTER} 'vserver cifs share show -instance' > ${TMPDIR}/${CLUSTER}_cifs-shares.asc
      ${SSH} ${USER}${CLUSTER} 'name-mapping show -instance' > ${TMPDIR}/${CLUSTER}_name-mapping.asc
      ${SSH} ${USER}${CLUSTER} 'vserver cifs domain discovered-servers show  -instance' > ${TMPDIR}/${CLUSTER}_cifs-domaininfo.asc
      ${SSH} ${USER}${CLUSTER} 'vserver cifs home-directory show -instance' > ${TMPDIR}/${CLUSTER}_cifs-homedir.asc
      ${SSH} ${USER}${CLUSTER} 'vserver cifs show -display-netbios-aliases' > ${TMPDIR}/${CLUSTER}_cifs-nbalias.asc
      ${SSH} ${USER}${CLUSTER} 'vserver cifs domain preferred-dc show'  > ${TMPDIR}/${CLUSTER}_cifs-prefdc.asc
      ${SSH} ${USER}${CLUSTER} 'network connections active show -service cifs-* -instance'  > ${TMPDIR}/${CLUSTER}_cifs-sessions.asc
      ${SSH} ${USER}${CLUSTER} 'vserver cifs nbtstat -instance' > ${TMPDIR}/${CLUSTER}_nbtstat.asc

# NFS
      ${SSH} ${USER}${CLUSTER} 'vserver nfs show -instance' > ${TMPDIR}/${CLUSTER}_nfs-show.asc
      ${SSH} ${USER}${CLUSTER} 'vserver export-policy rule show -instance' > ${TMPDIR}/${CLUSTER}_vserver-export-policy.asc
      ${SSH} ${USER}${CLUSTER} 'vserver services unix-user show -instance' > ${TMPDIR}/${CLUSTER}_unix-user.asc
      ${SSH} ${USER}${CLUSTER} 'vserver services unix-group show -instance' > ${TMPDIR}/${CLUSTER}_unix-show.asc

# Start Node Commands
      for NODE in `${SSH} ${USER}${CLUSTER} node show -field node|grep nl|awk '{print $1}'|sort`
      do
        echo "`date` Node ${CLUSTER}/${NODE} ..."|tee -a ${LOG}
        ${SSH} ${USER}${CLUSTER} "node show -node ${NODE} -instance" > ${TMPDIR}/${CLUSTER}-${NODE}_show-instance.asc
        ${SSH} ${USER}${CLUSTER} "node show -node ${NODE} -fields uptime" > ${TMPDIR}/${CLUSTER}-${NODE}_uptime.asc
#        ${SSH} ${USER}${CLUSTER} "system node run -node ${NODE} -ifgrp status -instance" > ${TMPDIR}/${CLUSTER}-${NODE}_ifgrp-status.asc
        ${SSH} ${USER}${CLUSTER} "system node run -node ${NODE} -command sysconfig -a" > ${TMPDIR}/${CLUSTER}-${NODE}_sysconfig-a.asc
        ${SSH} ${USER}${CLUSTER} "system node run -node ${NODE} -command sysconfig -r" > ${TMPDIR}/${CLUSTER}-${NODE}_sysconfig-r.asc
        ${SSH} ${USER}${CLUSTER} "system node run -node ${NODE} -command sysconfig -v" > ${TMPDIR}/${CLUSTER}-${NODE}_sysconfig-v.asc
        ${SSH} ${USER}${CLUSTER} "system node run -node ${NODE} -command ifconfig -a" > ${TMPDIR}/${CLUSTER}-${NODE}_ifconfig-a.asc
      done

# Start vServer Commands
    for VSERVER in `${SSH} ${USER}${CLUSTER} "vserver show -admin-state running -field vserver"|grep nl|sort`
      do
        echo "`date` vServer ${CLUSTER}/${VSERVER} ..."|tee -a ${LOG}
        ${SSH} ${USER}${CLUSTER} "vserver show ${VSERVER} -instance" > ${TMPDIR}/${CLUSTER}-${VSERVER}_vserver-instance.asc
      done

# Zipping & mailing
    cd	${TMPDIR}
    ${TAR} cvzf ${TARDIR}/${TARFILE} *
    RHREL="`/bin/rpm -qa|grep redhat-release|cut -d\- -f4| grep -i server`"
## Subject
    SUBJECT=":${HOSTNAME}: Inventory of ${CLUSTER} in file ${TARFILE} [${PGM} v${VER}]"
    if [ ${MAIL} ]; then
      if [ "${RHREL}" = "5Server" ]; then
        cat ${TARDIR}/${TARFILE}|${UUENC} ${TARFILE}|mailx -s "${SUBJECT}" ${MAILTO}
      fi
      if [ "${RHREL}" = "6Server" ]; then
        echo "${PGM}:${TARFILE} [${PGM} v${VER}] at `date`"|mailx -s "${SUBJECT}" -a ${TARDIR}/${TARFILE} ${MAILTO}
      fi
      echo "`date` Information mailed to ${MAILTO}"|tee -a ${LOG}
    fi  #  MAIL

# copy file by using SCP
    if [ ${USESCP} ]; then
      scp ${TARDIR}/${TARFILE} ${SCPUSER}@${SCPHOST}:${SCPDIR}
      echo "`date` Information SCP-ed to ${SCPUSER}@${SCPHOST}:${SCPDIR} "|tee -a ${LOG}
    fi  # USESCP

# Cleaningup Filer info
    rm ${TARDIR}/${TARFILE}
    rm ${TMPDIR}/${CLUSTER}*

  else
    echo "Problem with connection to ${CLUSTER} (ANSW=${ANSW})"|tee -a ${LOG}|mailx -s "[${PGM} v${VER}]" ${MAILTO}
  fi  # Check by SSH
  done  # for CLUSTER

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

echo "`date` ${PGM} v${VER} Finished. "|tee -a ${LOG}
exit 0

