
#!/bin/sh
# File	: noc_port-crc-err_rpt-rst.sh
# By	: Maarten.deBoer@Atos.net, 230801
# Subject	: Script to collect (report & reset ) CRC-errors. For specific ports at NetappOntap via Cli (noc_)
#(0.2),230801	: Added $1 = MAILTO, HIST_CSV
#(0.3),230801	: Added ZERO -z command
#(0.4),240205	: From cdot_rpt_crc-errors.sh 
PGM=`basename $0|cut -d\. -f1`
VER="0.4"
LOG="${HOME}/log/${PGM}.log"
TMP="/tmp/${PGM}.$$"
SSH="/usr/bin/ssh -n"
HOSTNAME=`hostname |cut -d\. -f1`
RCFILE="${HOME}/etc/${PGM}.rc"
PORTLIST="${HOME}/etc/${PGM}.portlist"
CSV="/tmp/${PGM}.csv"
HIST_CSV="${HOME}/data/${PGM}.csv"
MAILTO="maarten.deboer@atos.net"
MAILTO=""
ZERO_1ST=""
CLUSTERS="${HOME}/etc/clusters"

CFILTER="nlnaf10[01]"


# https://kb.netapp.com/onprem/ontap/hardware/NIC_port_seeing_CRC_errors_in_ifstat

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
touch ${TMP}
if [ -f ${RCFILE} ]; then
  echo "  RCFILE (${RCFILE}) found. Using ..."|tee -a ${LOG}
  . ${RCFILE}
fi  # RCFILE
if [ "${1}" != "" ]; then
  MAILTO="${1}"
fi
if [ "${1}" = "ZERO" ]; then
  echo "  Ports will be ZERO-ed"|tee -a ${LOG}
  sleep 1
  ZERO_1ST=1
  MAILTO=""
fi

IFGRPS="a199a a199b"

echo "  CLUSTERS=${CLUSTERS}"
echo "  CFILTER=${CFILTER}"
echo "  MAILTO=${MAILTO}"
echo "  ZERO_1ST=${ZERO_1ST}"
sleep 2

# From PORTLIST to "dynamisch" with a199a & a199b ifgrp's
cat ${CLUSTERS}| grep -v ^#|grep "${CFILTER}"|awk '{print $1}'|while read CLUSTER
do
  echo "  CLUSTER=${CLUSTER}"
  for IFGRP in ${IFGRPS}
  do
    echo "    IFGRP=${IFGRP}"

    ${SSH} ${CLUSTER} "ifgrp show -ifgrp ${IFGRP} -field node,ifgrp,ports"|grep ${IFGRP}|while read LINE
    do
      NODE=`echo ${LINE}|awk '{print $1}'`
      PORT1=`echo ${LINE}|awk '{print $3}'|cut -d, -f1`
      PORT2=`echo ${LINE}|awk '{print $3}'|cut -d, -f2`
      echo "      NODE=${NODE} PORT1=${PORT1} PORT2=${PORT2} "

#if [ -f ${PORTLIST} ]; then
#  cat ${PORTLIST}|grep -v ^#|while read LINE
#  do
#    echo "${LINE}"
# CLUSTER;NODE;PORT;REMARKS;
#    CLUSTER=`echo ${LINE}|awk -F\; '{print $1}'`
#    NODE=`echo ${LINE}|awk -F\; '{print $2}'`
#    PORT=`echo ${LINE}|awk -F\; '{print $3}'`
#    REMARKS=`echo ${LINE}|awk -F\; '{print $4}'`

    REMARKS="${IFGRP}"
# ssh nlnaf100 'system node run -node nlnaf100-11 -command ifstat e0e'| grep -i crc
    DATI=`date +%Y-%m-%d" "%H:%M:%S`
# Non-primary u/c:     0  | CRC errors:      24495  | Runt frames:         0
# OR
#  CRC errors:      39964  | Runt frames:         0  | Fragment:            0
    if [ ${ZERO_1ST} ]; then
      ${SSH} ${CLUSTER} "system node run -node ${NODE} -command ifstat -z ${PORT1}"
    fi  # ZERO_1ST

    if [ "${PORT1}" != "" ]; then
      CRC_ERRORS=`${SSH} ${CLUSTER} "system node run -node ${NODE} -command ifstat ${PORT1}"|grep -i crc| cut -d'C' -f3| cut -d\: -f2| cut -d\| -f1`
      echo "${CLUSTER};${NODE};${PORT1};${DATI};${CRC_ERRORS};${REMARKS}"|tee -a ${TMP}
    fi  # PORT1
    if [ "${PORT2}" != "" ] && [ "${PORT2}" != "${PORT1}" ] ; then
      CRC_ERRORS=`${SSH} ${CLUSTER} "system node run -node ${NODE} -command ifstat ${PORT2}"|grep -i crc| cut -d'C' -f3| cut -d\: -f2| cut -d\| -f1`
      echo "${CLUSTER};${NODE};${PORT1};${DATI};${CRC_ERRORS};${REMARKS}"|tee -a ${TMP}
    fi  # PORT2
  done  # LINE
  cat ${TMP} >> ${HIST_CSV}
  if [ "${MAILTO}" != "" ]; then
#    cp ${TMP} ${CSV}
    date | mailx -a ${HIST_CSV} -s ":${HOSTNAME}: CRC-error report [${PGM} v${VER}]" ${MAILTO}
    echo "  Mailed to ${MAILTO}"|tee -a ${LOG}
  fi  # MAILTO
#else
#  echo "  NO PORTLIST (${PORTLIST}) found. Exiting ..."|tee -a ${LOG}
#fi  # PORTLIST

  done  # IFGRP
done  # CLUSTER


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

