
#!/bin/sh
# File	: chk_asup_date.sh
# By	: Maarten.deBoer@Atos.net, 181015
# Subject	: Script to check ASUP date. > Month; report (system may be gone?)
#(0.2),181015	: Added COUNTRY
PGM=`basename $0|cut -d\. -f1`
VER="0.2"
TMP="/tmp/${PGM}.$$"
MAILFROM="maarten.deboer@atos.net"
MAILTO="maarten.deboer@atos.net"
CSV="/tmp/${PGM}.csv"

BASEDIR="`pwd | sed 's/scripts//'|sed 's/bin//'`"
LOG="${BASEDIR}/log/${PGM}.log"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
touch ${TMP}
CURMON=`date +%b`

cd ../..
BASEDIR=`pwd`
ls -1d ?? | while read COUNTRY REST
do
  DATABASEDIR="${BASEDIR}/${COUNTRY}/data/database"
  if [ -f ${BASEDIR}/${COUNTRY}/etc/default.mailto ]; then
    MAILTO="${MAILFROM} `cat ${BASEDIR}/${COUNTRY}/etc/default.mailto|grep -v ^#`"
  fi
  echo "BASEDIR=${BASEDIR}"
  echo "COUNTRY=${COUNTRY}"
  echo "DATABASEDIR=${DATABASEDIR}"
  echo "MAILTO=${MAILTO}"
  sleep 1

# Empty TMP
  cp /dev/null ${TMP}

  cd ${DATABASEDIR}
  ls -1 *.asc | while read FILE REST
  do
    ASUP_GENERATED_ON=`grep ASUP_GENERATED_ON ${FILE}|cut -d\= -f2`
    ASUP_TZ=`echo ${ASUP_GENERATED_ON}|awk '{print $5}'`
    ASUP_SNMP_LOCATION=`grep SNMP_LOCATION ${FILE}|cut -d\= -f2`
    ASUP_SNMP_CONTACT=`grep SNMP_CONTACT ${FILE}|cut -d\= -f2`
    ASUP_CONTROLER_NAME=`grep CONTROLER_NAME ${FILE}|cut -d\= -f2`
    ASUP_SERIAL_NUMBER=`grep SERIAL_NUMBER ${FILE}|cut -d\= -f2`
#  echo "${FILE} ..."

# Check is ASUP is from this month. If not, then save
    ANSW=`echo ${ASUP_GENERATED_ON}|grep ${CURMON}`
    if [ "${ANSW}" = "" ]; then
      echo "${ASUP_SERIAL_NUMBER}; ${ASUP_CONTROLER_NAME}; ${ASUP_GENERATED_ON}; ${ASUP_SNMP_LOCATION}; ${ASUP_SNMP_CONTACT};"|tee -a ${TMP}

    fi  # ANSW
  done  # ls -1

  if [ -s ${TMP} ]; then
    echo "ASUP_SERIAL_NUMBER;ASUP_CONTROLER_NAME;ASUP_GENERATED_ON;ASUP_SNMP_LOCATION;ASUP_SNMP_CONTACT;" >>${CSV}
    cat ${TMP} >> ${CSV}
    echo "Hello Co, Attached file is a list of NetApp-systems with ASUP date not ${CURMON}. Are those systems still active? Please check and reply to ${MAILFROM}. Thanks & kind regard, Maarten de Boer - Lead architect NetApp Competence Center at Atos."|mailx -a ${CSV} -s "Check ASUP date of ${COUNTRY} [${PGM} v${VER}]" -r "${MAILFROM}" "${MAILTO}"
    rm ${CSV}
    echo "  Mailed to ${MAILTO} (for ${COUNTRY})"|tee -a ${LOG}
  fi  # -s ${TMP}
done  # COUNTRY

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

