
#!/bin/sh
# File	: netapp-rpt-de-carbon-kpi-s.sh
# By	: Maarten.deBoer@Atos.net, 201015
# Subject	: Script to (gather &)report de-carbonisation KPI's (like # HDD's, # SDD's, Aggr. size/cap and utilisation) for NetApp-systems and mail this info
#(0.1),201015	: Initial
#(0.2),201015	: Changed name from na_rpt_de-carbon_kpi-s.sh. So, it can be better split at Rx end
#(0.3),201103	: Only AGGR's
#(0.4),201109	: Added (cDOT-)disks again (for KPI.2)
#
PGM=`basename $0|cut -d\. -f1`
VER="0.4"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME=`hostname |cut -d\. -f1`
DATI=`date +%Y-%m-%d-%H-%M-%S`
CSV_CA="/tmp/${PGM}_${HOSTNAME}_${DATI}_cdot-aggrs.csv"
CSV_CD="/tmp/${PGM}_${HOSTNAME}_${DATI}_cdot-disks.csv"
CSV_7="/tmp/${PGM}_${HOSTNAME}_${DATI}_7mode-aggrs.csv"
# _7mode-disks are NOT needed. 7mode is Legacy.
CSV_V="/tmp/${PGM}_${HOSTNAME}_${DATI}_versions.csv"

MAILTO="nl19471@nlxnetapp01.bcklan.ao-srv.com"
NA_SYSTEMS="${HOME}/etc/netapp-systems"

if [ ! -d ${HOME}/log ]; then
  mkdir ${HOME}/log 
fi
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
if [ ! -f ${NA_SYSTEMS} ]; then
  echo "  NO NetApp-systems-file ${NA_SYSTEMS} found. Exiting ..."|tee -a ${LOG}
  exit 3
fi

echo "# Sizing in GB" > ${CSV_CA}
echo "# Sizing in GB" > ${CSV_CD}
echo "# Sizing in GB" > ${CSV_7}
echo "system-name; ontap-version;" > ${CSV_V}

cat ${NA_SYSTEMS}|grep -v ^#|while read NA_SYSTEM REST
do
  echo -n "  ${NA_SYSTEM} ..."

# Register ONTAP (OS) Version
  OS_VER=`${SSH} ${NA_SYSTEM} "version"|cut -d\: -f1|grep NetApp`
  echo "${NA_SYSTEM};${OS_VER}" >> ${CSV_V}
# Check for cDOT (Release 8 & Release 9)
  ANSW=`echo "${OS_VER}"|egrep -i 'Release 8|Release 9'|grep -vi '7-mode'`
  if [ "${ANSW}" != "" ]; then
    echo "  cDOT: ${OS_VER} "
# Get Aggregate info. Remove the GB's
    ${SSH} ${NA_SYSTEM} "set -unit GB -showseparator \";\" ; storage aggregate show -field aggregate,physical-used,size,usedsize,owner-name,storage-type"|egrep "aggregate|GB"|sed 's/GB//g' >> ${CSV_CA}
# Get the list of disk types. Remove the GB's
    ${SSH} ${NA_SYSTEM} "set -unit GB -showseparator \";\" ; disk show -field disk,owner,aggregate,type,model,usable-size"|egrep "disk|GB"|sed 's/GB//g' >> ${CSV_CD}
  else  # grep cDOT

# Check for 7-mode
    ANSW=`echo "${OS_VER}"|grep -i '7-mode'`
    if [ "${ANSW}" != "" ]; then
      echo "  7-MODE: ${OS_VER} "
# Get Aggregate info. Remove the GB's
      ${SSH} ${NA_SYSTEM} 'df -gA'|grep -v snapshot|awk -v NA_SYSTEM="${NA_SYSTEM}" '{print NA_SYSTEM";"$1";"$2";"$3";"$4";"}'|sed 's/GB//g' >> ${CSV_7}
    else  # grep 7-mode
      echo "  ${NA_SYSTEM} : ?? (${OS_VER}) "|tee -a ${LOG}
    fi  # grep 7-mode
  fi  # grep cDOT
done  # NA_SYSTEMS

echo "# `date` by ${PGM} v${VER} at ${HOSTNAME}" >> ${CSV_CA}
echo "# `date` by ${PGM} v${VER} at ${HOSTNAME}" >> ${CSV_CD}
echo "# `date` by ${PGM} v${VER} at ${HOSTNAME}" >> ${CSV_7}
echo "# `date` by ${PGM} v${VER} at ${HOSTNAME}" >> ${CSV_V}

echo "`date` by ${PGM} v${VER} at ${HOSTNAME}"| mailx -a ${CSV_CA} -a ${CSV_CD} -a ${CSV_7} -a ${CSV_V} -s "netapp-de-carbon-kpi-s: from ${HOSTNAME} at ${DATI} [${PGM} v${VER}]" ${MAILTO}
echo "  Info mailed to ${MAILTO}"|tee -a ${LOG}

rm ${CSV_CA} ${CSV_CD} ${CSV_7} ${CSV_V}
echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
exit 0

