
#!/bin/sh
# File	: upd_status-atostechlab.sh
# By	: Maarten.deBoer@Atos.net, 210106
# Subject	: Script to update status of Atos TechLab
#(0.2)	: Removed 1st &2nd part of IP & renamed from upd_atostechlab-status.sh
#(0.3),210107	: Mod. with DATADIR
#(0.4),210107	: Mod. red-> yellow (ACCEPTED) & DECOM
PGM=`basename $0|cut -d\. -f1`
VER="0.4"
TMP="/tmp/${PGM}.$$"
BASEDIR="${HOME}/status"
LOG="${BASEDIR}/log/${PGM}.log"
DATI=`date +%Y-%m-%d-%H-%M-%S`

PAGE="/var/www/vhosts/status.atostechlab.nl/html/index.html"
PREV_PAGE="/var/www/vhosts/status.atostechlab.nl/html/index_${DATI}.html"
DATA_CSV="${BASEDIR}/data/status-atostechlab.csv"
DATADIR="${BASEDIR}/data"
ACCEPTED="${BASEDIR}/etc/chk_status-atostechlab.accepted"
DECOM="${BASEDIR}/etc/chk_status-atostechlab.decom"

BGCOLOR="#0066A1"
TBORDER=0
FSIZE=5
FFACE="Verdana"

if [ ! -f ${PAGE} ]; then
  echo "  PAGE (${PAGE}) NOT found. Exiting ..."|tee -a ${LOG}
  exit 3
fi
if [ ! -d ${DATADIR} ]; then
  echo "  DATADIR (${DATADIR}) NOT found. Exiting ..."|tee -a ${LOG}
  exit 4
fi

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

# Creating HEAD
echo "<!DOCTYPE HTML> " > ${TMP}
echo "<HTML> " >> ${TMP}
echo "  <HEAD> " >> ${TMP}
echo "  <META NAME='generator' content='Agrarix.IT:${PGM} v${VER}' /> " >> ${TMP}
echo "  <META NAME='up-date' content='${DATI}' /> " >> ${TMP}
echo "  <meta http-equiv='Content-Language' content='en-us'>" >> ${TMP}
echo "  <meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>" >> ${TMP}
echo "  <meta name='author' content='Maarten@Agrarix.IT' />" >> ${TMP}

echo "  <link rel="icon" href="TechLab_logo.png" type="image/png">" >> ${TMP}

echo "  <title>AtosTechLab-status</title>" >> ${TMP}
echo "  </HEAD> " >> ${TMP}
echo "  <BODY aLink=white link=white vLink=white>" >> ${TMP}

# 1st TABLE
echo "<TABLE class='table1-class' border=${TBORDER} width='100%' id='table1' cellpadding='0' cellspacing='0'> " >> ${TMP}

echo "  <tr>" >> ${TMP}
echo "    <td>" >> ${TMP}
echo "      <table class='table2-class' border=${TBORDER} width='100%' id='table2' cellspacing='0'> " >> ${TMP}
echo "  <TR>" >> ${TMP}
echo "    <TD> <A HREF="//atos.net"><IMG SRC="Logo_Atos_215x72_RGB.jpg"></A></TD>" >> ${TMP}
echo "    <TD> <A HREF="//www.atostechlab.nl"><IMG SRC="Atos_Technology-Lab-banner-black-blue-150.png" height="100"></A></TD>" >> ${TMP}
echo "    <TD><CENTER><font size='6' color=black face=${FFACE}>STATUS page</font></CENTER> </TD>" >> ${TMP}
echo "  </TR>" >> ${TMP}
echo "</TABLE>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "  </tr>" >> ${TMP}

echo "  <tr>" >> ${TMP}
echo "    <td>" >> ${TMP}
echo "    <table class='table3-class' border=${TBORDER} width='100%' id='table3' cellspacing='0'> " >> ${TMP}
echo "      <tr>" >> ${TMP}
echo "        <td bgcolor="${BGCOLOR}" width="5">" >> ${TMP}
echo "        </td>" >> ${TMP}
echo "        <td bgcolor="${BGCOLOR}" align='left' height="30">" >> ${TMP}
echo "          <font size='2' color=white face=${FFACE}> " >> ${TMP}
echo "            <a href='//atos.net' target="_blank"> <B>Atos</a>  " >> ${TMP}
echo "            <a href='//myatos.net' target="_blank"> <B>MyAtos</a>  " >> ${TMP}
echo "          </font>" >> ${TMP}
echo "        </td>" >> ${TMP}

echo "        <td bgcolor="${BGCOLOR}" align='right'>" >> ${TMP}

echo "          <font size='2' color=white face=${FFACE}>" >> ${TMP}
echo "          For updates:" >> ${TMP}
echo "          </font>" >> ${TMP}
echo "          <a href='mailto:maarten@agrarix.it?subject=atostechlab.nl' target="_blank">" >> ${TMP}
echo "            <font size='2' color=white face=${FFACE}>" >> ${TMP}
echo "              <B>Contact</B>" >> ${TMP}
echo "            </font>" >> ${TMP}
echo "          </a>" >> ${TMP}
echo "        </td>" >> ${TMP}
echo "        <td bgcolor="${BGCOLOR}" width="5">" >> ${TMP}
echo "        </td>" >> ${TMP}
echo "      </tr>" >> ${TMP}
echo "    </table>" >> ${TMP}

echo "    </td>" >> ${TMP}
echo "  </tr>" >> ${TMP}

echo "  <tr>" >> ${TMP}
echo "    <td>" >> ${TMP}

echo "    <table class='table4-class' border=${TBORDER} id='table4' cellspacing='0'> " >> ${TMP}
echo "      <tr>" >> ${TMP}
echo "        <TD bgcolor="red"><font size=${FSIZE} color="black" face=${FFACE}>NOT ok</TD>" >> ${TMP}
echo "        <TD bgcolor="orange"><font size=${FSIZE} color="black" face=${FFACE}>not ok</TD>" >> ${TMP}
echo "        <TD bgcolor="yellow"><font size=${FSIZE} color="black" face=${FFACE}>ACCEPTED NOT ok</TD>" >> ${TMP}
echo "        <TD bgcolor="green"><font size=${FSIZE} color="black" face=${FFACE}>OK</TD>" >> ${TMP}
echo "        <TD bgcolor="lightblue"><font size=${FSIZE} color="black" face=${FFACE}>Decom</TD>" >> ${TMP}
echo "        <TD bgcolor="white"><font size=${FSIZE} color="black" face=${FFACE}>Curr:</TD>" >> ${TMP}
echo "        <TD bgcolor="white"><font size=${FSIZE} color="black" face=${FFACE}>${DATI}</TD>" >> ${TMP}
echo "      </tr>" >> ${TMP}
echo "    </table> " >> ${TMP}

echo "    <table class='table5-class' border=${TBORDER} id='table5' cellspacing='0'> " >> ${TMP}

echo "      <tr>" >> ${TMP}
echo "        <TH bgcolor=${STATUS_COL}><font size=${FSIZE} color=${BGCOLOR} face=${FFACE}> NAME </TH>" >> ${TMP}
echo "        <TH bgcolor=${STATUS_COL}><font size=${FSIZE} color=${BGCOLOR} face=${FFACE}> IP </TH>" >> ${TMP}
echo "        <TH bgcolor=${STATUS_COL}><font size=${FSIZE} color=${BGCOLOR} face=${FFACE}> CHECK </TH>" >> ${TMP}
echo "        <TH bgcolor=${STATUS_COL}><font size=${FSIZE} color=${BGCOLOR} face=${FFACE}> STATUS </TH>" >> ${TMP}
echo "        <TH bgcolor=${STATUS_COL}><font size=${FSIZE} color=${BGCOLOR} face=${FFACE}> DATE+TIME </TH>" >> ${TMP}
echo "      </tr>" >> ${TMP}

COLORS="(red) (orange) (yellow) (green)"
for COLOR in ${COLORS}
do
  echo "  ${COLOR}"
# STATUS's
# Get all NAMEs and get last status of this NAME
cd ${DATADIR}
cat 2*.csv|grep -v ^#|grep ${COLOR}|awk -F\; '{print $2}'|sort -u|while read NAME REST1
do
  LAST_NAME_LINE=`grep "${NAME}" 2*.csv|grep ${COLOR}|tail -1|cut -d\: -f2`
#  echo "  ${NAME}:${LAST_NAME_LINE}"

  CSV_DATI=`echo ${LAST_NAME_LINE}|awk -F\; '{print $1}'`
  NAME=`echo ${LAST_NAME_LINE}|awk -F\; '{print $2}'|cut -d\. -f1`
  IP="x.x.`echo ${LAST_NAME_LINE}|awk -F\; '{print $3}'|cut -d\. -f3,4`"
  STATUS=`echo ${LAST_NAME_LINE}|awk -F\; '{print $4}'|awk '{print $1}'`
  STATUS_COL=`echo ${LAST_NAME_LINE}|awk -F\; '{print $4}'|cut -d\( -f2|cut -d\) -f1`
  CHECK=`echo ${LAST_NAME_LINE}|awk -F\; '{print $5}'`

# When ACCEPTED-file, check if NAME | IP is in ACCEPTED-file.
# Then Red -> yellow
  if [ -f ${ACCEPTED} ] && [ "${STATUS_COL}" = "red" ]; then
    ANSW=`egrep "${NAME}|${IP}" ${ACCEPTED}|grep -v ^#`
    if [ "${ANSW}" != "" ]; then
      STATUS_COL="yellow"
    fi
  fi  # ACCEPTED
# When DECOM-file, check if NAME | IP is in DECOM-file.
# Then COLOR=blue
# And for last
  if [ -f ${DECOM} ]; then
    ANSW=`egrep "${NAME}|${IP}" ${DECOM}|grep -v ^#`
    if [ "${ANSW}" != "" ]; then
      STATUS_COL=""
      echo "${LAST_NAME_LINE}" >> ${TMP}.last
    fi
  fi  # DECOM

  echo "  ${NAME}(${IP}):${CHECK}=${STATUS}(${STATUS_COL}):${CSV_DATI}"

  if [ "${STATUS_COL}" != "" ]; then
FCOLOR="black"
echo "  <tr width=100%>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${NAME}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${IP}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${CHECK}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${STATUS}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${CSV_DATI}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "  </tr>" >> ${TMP}
  fi  # ${STATUS_COL}" != ""

done  # cat ${DATA_CSV}

done  # COLORS


cat ${TMP}.last|grep -v ^#|sort|while read LINE REST1
do
  echo "    ${LINE}"
  CSV_DATI=`echo ${LINE}|awk -F\; '{print $1}'`
  NAME=`echo ${LINE}|awk -F\; '{print $2}'|cut -d\. -f1`
  IP="x.x.`echo ${LINE}|awk -F\; '{print $3}'|cut -d\. -f3,4`"
  STATUS=`echo ${LINE}|awk -F\; '{print $4}'|awk '{print $1}'`
  STATUS_COL=`echo ${LINE}|awk -F\; '{print $4}'|cut -d\( -f2|cut -d\) -f1`
  CHECK=`echo ${LINE}|awk -F\; '{print $5}'`

  STATUS_COL="lightblue"

  echo "  ${NAME}(${IP}):${CHECK}=${STATUS}(${STATUS_COL}):${CSV_DATI}"

FCOLOR="black"
echo "  <tr width=100%>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${NAME}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${IP}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${CHECK}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${STATUS}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "    <td bgcolor=${STATUS_COL}>" >> ${TMP}
echo "      <font size=${FSIZE} color=${FCOLOR} face=${FFACE}><CENTER>${CSV_DATI}</font>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "  </tr>" >> ${TMP}

done  # TMP.last




echo "      </table>" >> ${TMP}
echo "    </td>" >> ${TMP}
echo "  </tr>" >> ${TMP}




# Last row
cat << EOT >> ${TMP}
  <tr>
    <td bgcolor=${BGCOLOR} align='center'>
      <a href='//agrarix.it' target="_blank">
        <font size='1' color=while face='Arial'>
Design by Agrarix-IT
        </font>
      </a>
    </td>
  </tr>
EOT

# End of 1st table
echo "</TABLE>" >> ${TMP}
# table1

echo "  </BODY>" >> ${TMP}
echo "</HTML>" >> ${TMP}




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

