
#!/bin/ksh
# File	: cdot_get_useradmin.sh
# By	: Maarten.deBoer@atos.net, 2017-04-12
# Subject	: Script to get useradmin info
#(0.3),210916	: Added DATE at CSV (Req. by Keith.Dixon)
PGM="`basename $0|cut -d\. -f1`"
VER="0.3"
TMP="/tmp/${PGM}.$$"
#CSV="/tmp/${PGM}.csv"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
CLUSTERS="${HOME}/etc/clusters"
PREFIX="nlnaf"
MAIL=""
MAILTO="maarten.deboer@atos.net"
FILTER="[?]*"
DATE="`date +%Y-%m-%d`"
CSV="/tmp/FSOD_cluster_useradmin_${DATE}.csv"

SSHCMD()
# 1: Filername 2:Command-string
# When issue with connection to cluster, try the nodes (-01 & -02)
# "There are no entries matching your query." => EC=255
# "no connection" is also EC=255
{
  TMPERR="/tmp/${PGM}.$$.err"
  /usr/bin/ssh -n ${1} "${2}" 2> ${TMPERR}
  EC=${?}
  # Check if "ssh: connect to host 10.192.109.202 port 22: Connection refused" If so (EC2=0), the 2nd
  grep 'Connection refused' ${TMPERR}
  EC2=${?}
  if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
    sleep 2
    /usr/bin/ssh -n ${1}-01 "${2}" 2> ${TMPERR}
    EC=${?}
    grep 'Connection refused' ${TMPERR}
    EC2=${?}
    if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
      sleep 2
      /usr/bin/ssh -n ${1}-02 "${2}" 2> ${TMPERR}
      EC=${?}
      grep 'Connection refused' ${TMPERR}
      EC2=${?}
      if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
        sleep 2
        /usr/bin/ssh -n ${1}-03 "${2}" 2> ${TMPERR}
        EC=${?}
        grep 'Connection refused' ${TMPERR}
        EC2=${?}
        if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
          echo "  EC=${EC} sleep 2 ..."
          sleep 2
          /usr/bin/ssh -n ${1}-04 "${2}" 2> ${TMPERR}
          EC=${?}
          grep 'Connection refused' ${TMPERR}
          EC2=${?}
          if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
            echo  "`date` ${PGM} ERROR with communication to ${1}. Connection to -01 - -04 failed too."|tee -a ${LOG} 
          fi
        fi
      fi
    fi
  fi
  rm ${TMPERR}
}

USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "  Version: ${VER}"
  echo "  options          :"
  echo "    -e             : Etc/clusters file (${CLUSTERS})"
  echo "    -f             : Filter (cluster) (${FILTER})"
  echo "    -m | --mail    : Mail the logging"
  echo "    -h | --help    : this help"
  echo "    -V             : Version"
  echo "    -x             : set -x"
  echo "    --mailto       : set new MAILTO (${MAILTO})"
}

# Check options
while [ ${#} -gt 0 ]
  do
  case ${1} in
    -e) CLUSTERS=${2}; shift ;;
    -f) FILTER=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    --mailto) MAIL=1; MAILTO=${2}; shift ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done


# MAIN
#echo "# Vserver;User Name or Active Directory Group Name;User Name or Active Directory Group Name;Application;Authentication Method; Role Name;Role Name;Account Locked;Comment Text; " > ${TMP}
echo "# Vserver;User Name or Group Name;Application;Authentication Method;Role Name;Account Locked;Comment Text; " > ${TMP}

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

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep ${FILTER}`
do
#  echo "${CLUSTER} ..."
#  ${SSH} ${CLUSTER} 'set -showseparator ";" ; security login show -field vserver,user-or-group-name,application,authmethod,role,acctlocked,comment'|grep ${PREFIX} | tee -a ${TMP}
  ${SSH} ${CLUSTER} 'security login show -field vserver,user-or-group-name,application,authmethod,role,acctlocked,comment'|grep ${PREFIX} | awk '{print $1";"$2";"$3";"$4";"$6";"$7";"$8 }' | tee -a ${TMP}

done  # for CLUSTER

if [ ${MAIL} ]; then
  echo "`date` Mailing to ${MAILTO}"|tee -a ${LOG} ${TMP}
  cp ${TMP} ${CSV}
  date | mailx -a ${CSV} -s "@${HOSTNAME}: User admin [${PGM} v${VER}]" ${MAILTO}
  rm ${CSV}
fi  # MAIL

# Finish

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

