
#!/bin/bash
# File	: proc_asup-files_for_disk-fw.sh
# By	: Maarten.deBoer@atos.net, 150925
# Subject	: Script to process ASUP-files for disk info (from ASUP: SYSCONFIG-A.txt)
#set -x
#(0.2)	: Mod. disk lists with index
#(0.3)	: Mod. check orange
#(0.4)	: Mod. ASUPDATADIR
#(0.5)	: Mod. proc_asup-files_for_disk-fw.mailto = SUPPORTMAILTO
#(0.6)	: Added some text
PGM="`basename $0|cut -d\. -f1`" 
VER="0.6"
TMP="/tmp/${PGM}.$$"

BASEDIR="`pwd | sed 's/scripts//'|sed 's/bin//'`"
LOG="${BASEDIR}/log/${PGM}.log"
CSV="afsp-disks-fw.csv"
DATADIR="${BASEDIR}/data"
COUNTRY="`echo ${BASEDIR}| cut -d\/ -f6`"

#ASUPDATADIR="/appl/mdr/mdrglob/asup-data"
ASUPDATADIR="/appl/asup/data"

SUPPORTMAILTO="${BASEDIR}/etc/${PGM}.mailto"
AFSPSUP="maarten.deboer@atos.net"
MAILTO="maarten.deboer@atos.net"

FILTER="[?]*"
#FILTER="2858E2113"

echo "`date` ${PGM} v${VER} Started."|tee -a ${LOG}
touch ${TMP}

# Read Functions file
source ./afsp-db_functions.sh
DB_FUNC_VER

echo ${BASEDIR}

if [ ! -d ${BASEDIR}/data/firmware ]; then
  mkdir ${BASEDIR}/data/firmware
fi

  echo "${COUNTRY} ..."
  echo "# ${COUNTRY} " > ${TMP}
  echo "" > ${TMP}.1
  # If database, then cd to database
  if [ -d /appl/mdr/mdrglob/afsp/${COUNTRY}/data/database ]; then
    cd /appl/mdr/mdrglob/afsp/${COUNTRY}/data/database
    ls -1|grep ${FILTER}|while read DBFILE
    do
#  echo "${DBFILE} ..."
      DB_READ ${DBFILE}

      if [ "${DB_CONTROLER_NAME}" != "" ] && [ "${DB_SERIAL_NUMBER}" != "" ]; then
        if [ -f ${ASUPDATADIR}/${DB_CONTROLER_NAME}_${DB_SERIAL_NUMBER}/SYSCONFIG-A.txt ]; then
          echo "${DB_CONTROLER_NAME}_${DB_SERIAL_NUMBER} ... "
		  # Selecting whole line with NETAPP in to be precessed later
          cat ${ASUPDATADIR}/${DB_CONTROLER_NAME}_${DB_SERIAL_NUMBER}/SYSCONFIG-A.txt|grep NETAPP|egrep -v 'System ID|System Serial'>> ${TMP}
        fi  # -f
      fi  # NAME && SER.NR

# Process lines & substr disktype + version
      echo "Process lines & substr disktype + version ..."
      cat ${TMP}|grep -v ^# | while read LINE
      do
#       echo ${LINE}
        IDX="`/usr/bin/expr index \"${LINE}\" 'NETAPP'`"
        DISKINFO="`/usr/bin/expr substr \"${LINE}\" ${IDX} 50`"
# Make list ($TMP.1) of disks+Cur.FW+Vendor(NETAPP)
        echo "${DISKINFO}"|awk -v FILER=${DB_CONTROLER_NAME} '{print $2";"$3";"$1";"FILER";"}' >> ${TMP}.1
      done  # cat ${TMP}
    done  # DBFILE
  fi  # database

  # Make list unique
  echo "Make list unique ..."
  cat ${TMP}.1 | sort -u > ${TMP}.2
  # Counting the lines. And set in list (TMP.3)
  echo "Counting the lines ..."
  echo "# LINE ; CNT"
  cat ${TMP}.2|grep -v ^#|while read LINE
  do
    if [ "${LINE}" != "" ]; then
      CNT="`grep \"${LINE}\" ${TMP}.1 | wc -l`"
      echo "${LINE}${CNT};"|tee -a ${TMP}.3
    fi
  done  # cat

# Processing
  echo "Processing ..."
  CSV="/appl/mdr/mdrglob/afsp/${COUNTRY}/data/firmware/${COUNTRY}-disk-model-firmware-vendor-more.csv"
  echo "# DISKMODEL;FILER;DISKFW;DISKVENDOR;CNT;PATCHLEVEL1;PATCHLEVEL2;PATCHSTATUS;" > ${CSV}
  DISKPATCHLEVEL1="${BASEDIR}/etc/filer-disk-patch-levels-netapp.csv"
  DISKPATCHLEVEL2="${BASEDIR}/etc/filer-disk-patch-levels-ibm.csv"
  cat ${TMP}.3|grep -v ^#|while read LINE
  do
  echo ${LINE}
    DISKMODEL="`echo ${LINE}|awk -F\; '{print $1}'`"
    DISKFW="`echo ${LINE}|awk -F\; '{print $2}'`"
    DISKVENDOR="`echo ${LINE}|awk -F\; '{print $3}'`"
    FILER="`echo ${LINE}|awk -F\; '{print $4}'`"
    CNT="`echo ${LINE}|awk -F\; '{print $5}'`"
    if [ "${DISKMODEL}" != "" ] && [ "${DISKFW}" != "" ] && [ "${DISKVENDOR}" != "" ] && [ "${FILER}" != "" ]; then
      # sort -u & head -1 added for solving double entries
      PATCHLEVEL1="`cat ${DISKPATCHLEVEL1}|sort -u|grep ${DISKMODEL}|grep -v ^#|awk -F\; '{print $2}'|head -1`"
      PATCHLEVEL2="`cat ${DISKPATCHLEVEL2}|sort -u|grep ${DISKMODEL}|grep -v ^#|awk -F\; '{print $2}'|head -1`"
    fi  # [ ..... ]
    PATCHSTATUS="" 
    DISKFWNR="`echo ${DISKFW}|sed 's/NA//'`"
    PATCHNR1="`echo ${PATCHLEVEL1}|sed 's/NA//'`"
    PATCHNR2="`echo ${PATCHLEVEL2}|sed 's/NA//'`"
    PATCHDIFF1=9
    PATCHDIFF2=9
    if [ "${PATCHNR1}" != "" ]; then
      PATCHDIFF1="`/usr/bin/expr \"${PATCHNR1}\" - \"${DISKFWNR}\"`"
    fi
    if [ "${PATCHNR2}" != "" ]; then
      PATCHDIFF2="`/usr/bin/expr \"${PATCHNR2}\" - \"${DISKFWNR}\"`"
    fi
    echo "MODEL=${DISKMODEL} DISKFW=${DISKFW} PATCH1=${PATCHLEVEL1} PATCH2=${PATCHLEVEL2} DIFF1=${PATCHDIFF1} DIFF2=${PATCHDIFF2} PATCHNR1=${PATCHNR1} PATCHNR2=${PATCHNR2}"

    if [ "${PATCHSTATUS}" = "" ] && [ "${DISKFW}" = "" ]; then
      PATCHSTATUS="No disk-FW;1"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ "${PATCHLEVEL1}" = "" ] && [ "${PATCHLEVEL2}" = "" ]; then
      PATCHSTATUS="No NetApp- & IBM-patchlevel;1"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ ${PATCHDIFF1} -eq 0 ] && [ ${PATCHDIFF1} -eq 0 ]; then
      PATCHSTATUS="ok (NetApp & IBM);0"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ ${PATCHDIFF1} -eq 0 ]; then
      PATCHSTATUS="ok (NetApp);0"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ ${PATCHDIFF2} -eq 0 ]; then
      PATCHSTATUS="ok (IBM);0"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ ${PATCHDIFF1} -eq 1 ]; then
      PATCHSTATUS="Less (-1) ok (NetApp);2"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ ${PATCHDIFF2} -eq 1 ]; then
      PATCHSTATUS="Less (-1) ok (IBM);2"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ "${PATCHLEVEL1}" != "" ] && [ ${DISKFWNR} -gt ${PATCHNR1} ]; then
      PATCHSTATUS="Higher (NetApp);1"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ "${PATCHLEVEL2}" != "" ] && [ ${DISKFWNR} -gt ${PATCHNR2} ]; then
      PATCHSTATUS="Higher (IBM);1"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ "${PATCHLEVEL1}" != "" ] && [ ${PATCHDIFF1} -ge 2 ]; then
      PATCHSTATUS="Lower (-2 or more) (NetApp);3"
    fi
    if [ "${PATCHSTATUS}" = "" ] && [ "${PATCHLEVEL2}" != "" ] && [ ${PATCHDIFF2} -ge 2 ]; then
      PATCHSTATUS="Lower (-2 or more) (IBM);3"
    fi

    if [ "${DISKMODEL}" != "" ]; then
	  echo "${DISKMODEL};${FILER};${DISKFW};${DISKVENDOR};${CNT};${PATCHLEVEL1};${PATCHLEVEL2};${PATCHSTATUS};" |tee -a ${CSV}
    fi
  done  # cat ${TMP.3}

  if [ -f ${SUPPORTMAILTO} ]; then
    MAILTO="`cat ${SUPPORTMAILTO}`"
    echo "List of disk-FW-levels for ${COUNTRY}. See attached file. Questions?  mailto:${AFSPSUP} "|
	mailx -a ${CSV} -s "ASUP: disk FW ${COUNTRY} [${PGM} v${VER}]" ${MAILTO}
	echo "  Mailed to ${MAILTO}"|tee -a ${LOG}
  else
    echo "(NO SUPPORTMAILTO) List of disk-FW-levels for ${COUNTRY}:"| mailx -a ${CSV} -s "ASUP: disk FW ${COUNTRY} [${PGM} v${VER}]" ${MAILTO}
    echo "  (NO supportmailto) Mailed to ${MAILTO}"|tee -a ${LOG}
  fi   # MAILTOFILE

rm ${TMP} ${TMP}.1 ${TMP}.2 ${TMP}.3 
echo "`date` ${PGM} Finished."|tee -a ${LOG}
exit 0

