#!/bin/bash
#-------------------------------------------------------------------------------
# charon_menu_logchk
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2023 STROMASYS.
# All rights reserved.
# Products: PAR
#set -x
VERSION=2.14

. `dirname $0`/charon_common
. `dirname $0`/charon_common_menu

LOGFLIST="${CHARONDIR}/utils/charon_logchk.list"
BOOTLIST="${CHARONDIR}/utils/charon_gstart.boot"

SVCNAME="charon_logchk"
MONLOG="${CHARONDIR}/log/charon_logchk.log"
TAILCMD=`grep ^TAILCMD= ${CHARONDIR}/utils/charon_logchk 2>/dev/null | cut -f2 -d'=' | tr -d '"'`

get_preferences

function display_hr
{
  if test "${SEMIGRAPH}" = "enabled"
  then
    tput smacs
    echo "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
    tput rmacs
  else
    echo "--------------------------------------------------------------------------------"
  fi
}

function display_logsmonitored
{
  LOGARRAY=()
  typeset -i I=0
  touch ${LOGFLIST}
  if test $(cat ${LOGFLIST} | wc -l) -eq 0
  then
    test ${GLOBALLOGMON} = "enabled" && echo -n "${FGORANGE}"
    echo "No virtual machine log file monitored found.[0m"
  else
    if test -s ${BOOTLIST}
    then
      while read -r LINE
      do
        LINE=$(printf '%s\n' "$LINE")
        I=I+1
        if test "$1" = "-num"
        then
          printf "%2d" ${I}
          echo -n " - "
        fi

        EMU=$(echo ${LINE} | cut -f1 -d';')
        CFG=$(echo ${LINE} | cut -f2 -d';')
        BOO=$(echo ${LINE} | cut -f3 -d';')
        get_logfile ${CFG}
        LOGARRAY[$I]="${LOGF}"
        CFG=$(basename ${CFG} | sed "s=\.cfg$==g")
        if test -z "$(grep ^${LOGF}$ ${LOGFLIST})"
        then
          if test "${GLOBALLOGMON}" = "enabled"
          then
            #--- Log is not monitored
            echo "[1m[31mLog ${LOGF} (${CFG}) is not monitored![0m"
          fi
        else
          #--- Log is monitored
          echo -n "${CFG}("
          SVCSTA=$(systemctl is-active charon_${CFG} 2>/dev/null)
          case "${SVCSTA}"
          in
            "active")
              echo -n "[32m${SVCSTA}[0m"
              ;;
            "unknown")
              if test "${BOO}" != "N"
              then
                echo -n "[31m${SVCSTA}[0m"
              else
                SVCACT=$(systemctl show -p ActiveState charon_${CFG}.service 2>/dev/null | cut -f2 -d'=' | tr [:upper:] [:lower:])
                if test "${SVCACT}" = "inactive"
                then
                  echo -n "[32m${SVCSTA}[0m"
                  GSTO=$(systemctl -o short-iso status charon_${CFG}.service 2>/dev/null |grep -w systemd | grep -w Stopped | awk '{print $1}')
                  if test -n "${GSTO}"
                  then
                    echo -n "[31mstopped[0m"
                  else
                    echo -n "${FGORANGE}${SVCSTA}[0m"
                  fi
                fi
              fi
              ;;
            *)
              echo -n "${FGORANGE}${SVCSTA}[0m"
              ;;
          esac
          echo -n "): ${LOGF}"
          tput hpa 50
          tput el
          echo -n " "
          if test ${ROTA:-0} -eq 0
          then
            printf "%-9s" "no"
          else
            printf "%-9s" "yes"
          fi

          if test ! -e ${ETCCHARON}/CharonToolkit.${CFG}.alertsdisabled
          then
            SVCCHK=$(systemctl is-enabled charon_logmon_${CFG}.service 2>/dev/null)
            test "${SVCCHK}" = "enabled" && echo -n "[32m" || echo -n "${FGORANGE}"
            echo -n "${SVCCHK}[0m, "
            SVCCHK=$(systemctl is-active charon_logmon_${CFG}.service 2>/dev/null)
            if test "${SVCCHK}" = "active"
            then
              PRC=$(systemctl show -p ExecMainPID charon_logmon_${CFG}.service 2>/dev/null | cut -f2 -d'=')
              if test -n "$(ps --ppid ${PRC} | grep -w tail)"
              then
                LOGVER=$(grep ^VERSION= ${CHARONDIR}/utils/charon_logchk | cut -f2 -d '=')
                if test "${LOGVER}" = "$(cat /var/run/charon_logchk.vm.${CFG} 2>/dev/null)"
                then
                  echo "[32mrunning[0m"
                else
                  echo "${FGORANGE}running(!)[0m"
                  touch /tmp/charon_menu_logchk.alert.version 2>/dev/null
                fi
              else
                echo "${FGORANGE}starting[0m"
              fi
            else
              echo "[31m${SVCCHK}[0m"
            fi
          else
            tput hpa 60
            echo "${BGORANGE} Alerts disabled [0m"
          fi
        fi
      done < <(grep -v "^#" ${BOOTLIST})
    else
      echo "${FGORANGE}No virtual machine managed found.[0m"
    fi
  fi
}

function manage_systemd_service
{
  # $1 = create, delete, start, stop, is-active
  # $2 = log name full path
  if test -s ${BOOTLIST}
  then
    rm -f /tmp/charon_menu_logchk.cfgbas.tmp 2>/dev/null
    grep -v ^# ${BOOTLIST} | while read LINE
    do
      EMU=`echo ${LINE} | cut -f1 -d';'`
      CFG=`echo ${LINE} | cut -f2 -d';'`
      get_logfile ${CFG}
      test "${LOGF}" = "$2" && basename ${CFG} | sed "s=\.cfg==g" >/tmp/charon_menu_logchk.cfgbas.tmp
    done
    CFGBAS=`cat /tmp/charon_menu_logchk.cfgbas.tmp 2>/dev/null`
    if test -n "${CFGBAS}"
    then
      case "$1"
      in
        "create")
          echo
          if test ! -s /etc/systemd/system/charon_logmon_${CFGBAS}.service
          then
            #--- Virtual machine log monitoring service creation
            SVC_DESCRI="CHARON log monitoring `basename ${EMU}`/${CFGBAS}"
            SVC_BEFORE="charon_${CFGBAS}"
            SVC_WORKDI=`dirname $2`
            SVC_LOGFIL=$2
            cat ${CHARONDIR}/utils/templates/charon_logmon_VIRTUALMACHINE.service | \
                sed -e "s=<SVC_DESCRI>=${SVC_DESCRI}=g" \
                    -e "s=<SVC_BEFORE>=${SVC_BEFORE}=g" \
                    -e "s=<SVC_WORKDI>=${SVC_WORKDI}=g" \
                    -e "s=<SVC_LOGFIL>=${SVC_LOGFIL}=g" \
                       >/etc/systemd/system/charon_logmon_${CFGBAS}.service
            systemctl daemon-reload
          fi

          SVCCHK=`systemctl is-enabled charon_logmon_${CFGBAS}.service 2>/dev/null`
          if test "${SVCCHK}" != "enabled"
          then
            systemctl enable charon_logmon_${CFGBAS}.service 2>/dev/null
            if test $? = 0
            then
              echo "Service charon_logmon_${CFGBAS}.service is enabled."
            else
              echo "[1mFailed to enable service charon_logmon_${CFGBAS}.service. Please check manually.[0m"
            fi
          fi
          ;;
        "start")
          echo
          systemctl reset-failed charon_logmon_${CFGBAS}.service
          systemctl start        charon_logmon_${CFGBAS}.service
          ;;
        "stop")
          echo
          systemctl stop         charon_logmon_${CFGBAS}.service
          ;;
        "delete")
          echo
          systemctl disable      charon_logmon_${CFGBAS}.service
          rm -f /etc/systemd/system/charon_logmon_${CFGBAS}.service
          systemctl daemon-reload
          ;;
        "is-active")
          systemctl is-active charon_logmon_${CFGBAS}.service 2>/dev/null
          ;;
      esac
    else
      echo "[41m[37m ERROR [0m Cannot $1 service. No managed guest associated with the log file"
      echo
      Press_Enter
    fi
  else
    echo "[41m[37m ERROR [0m Cannot $1 service. No managed guest defined"
    echo
    Press_Enter
  fi
}



while test 1
do
  display_header "Manage monitored guests logs"
  if test `id -u` -ne 0
  then
    echo
    echo "[41m[37m ERROR [0m Must be root !"
    Press_Enter
    echo
    exit 1
  fi

  get_preferences

  if test "${GLOBALLOGMON}" != "enabled"
  then
    echo "${BGORANGE} WARNING [0m Charon log monitoring (global) is DISABLED."
  fi

  tput bold
  echo "${FGBLUE}Monitoring service description                     Rotating  State[0m"
  tput sgr0
  if test "${SEMIGRAPH}" = "enabled"
  then
    tput smacs
    echo "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqq qqqqqqqqqqqqqqqqqqqq"
    tput rmacs
  else
    echo "-------------------------------------------------- -------- --------------------"
  fi

  if test "${GLOBALLOGMON}" = "enabled"
  then
    OPTBOLD="[1m"
  else
    echo "${FGORANGE}Charon log monitoring (global) is disabled.[0m"
    OPTBOLD="[90m"
  fi

  # test "${WARNONDONGLEREMOVAL}" = "disabled" && echo "${FGORANGE}Monitoring in silent mode due to alerts management settings.[0m"

  rm -f /tmp/charon_menu_logchk.alert.version 2>/dev/null

  #--- Display aksusbd (+ SEH) monitoring status
  if test -x /usr/sbin/aksusbd
  then
    echo -n "aksusbd("
    SVCSTA=$(systemctl is-active aksusbd 2>/dev/null)
    case "${SVCSTA}"
    in
      "active")
          echo -n "[32m${SVCSTA}[0m"
          ;;
      "unknown")
          echo -n "[31m${SVCSTA}[0m"
          ;;
      *)
          echo -n "${FGORANGE}${SVCSTA}[0m"
          ;;
    esac
    echo -n ") / Immediate dongle (dis)connection"
    tput hpa 50
    tput el
    echo -n " -        "

    if test ! -e ${MONUSBALTDISABLED}
    then
      SVCCHK=`systemctl is-enabled charon_monusb.service 2>/dev/null`
      if test -n "${SVCCHK}"
      then
        test "${SVCCHK}" = "enabled" && echo -n "[32m" || echo -n "[31m"
        echo -n "${SVCCHK}[0m, "
        SVCCHK=`systemctl is-active charon_monusb.service 2>/dev/null`
        if test "${SVCCHK}" = "active"
        then
          PRC=`systemctl show -p ExecMainPID charon_monusb 2>/dev/null | cut -f2 -d'='`
          if test -n "`ps --ppid ${PRC} | grep journalctl`"
          then
            LOGVER=$(grep ^VERSION= ${CHARONDIR}/utils/charon_logchk | cut -f2 -d '=')
            if test "${LOGVER}" = "$(cat /var/run/charon_logchk.monusb 2>/dev/null)"
            then
              echo "[32mrunning[0m"
            else
              echo "${FGORANGE}running(!)[0m"
              touch /tmp/charon_menu_logchk.alert.version 2>/dev/null
            fi
          else
            echo "${FGORANGE}starting[0m"
          fi
        else
          echo "[31m${SVCCHK}[0m"
        fi
      else
        echo "${FGORANGE}disabled[0m"
      fi
    else
      tput hpa 60
      echo "${BGORANGE} Alerts disabled [0m"
    fi

    #--- Display HASP error.log monitoring status
    echo -n "aksusbd("
    SVCSTA=$(systemctl is-active aksusbd 2>/dev/null)
    case "${SVCSTA}"
    in
      "active")
          echo -n "[32m${SVCSTA}[0m"
          ;;
      "unknown")
          echo -n "[31m${SVCSTA}[0m"
          ;;
      *)
          echo -n "${FGORANGE}${SVCSTA}[0m"
          ;;
    esac
    echo -n ") / HASP error log"
    tput hpa 50
    tput el
    echo -n " -        "

    if test ! -e ${MONHASPALTDISABLED}
    then
      SVCCHK=`systemctl is-enabled charon_monhasperrlog.service 2>/dev/null`
      if test -n "${SVCCHK}"
      then
        test "${SVCCHK}" = "enabled" && echo -n "[32m" || echo -n "[31m"
        echo -n "${SVCCHK}[0m, "
        SVCCHK=`systemctl is-active charon_monhasperrlog.service 2>/dev/null`
        if test "${SVCCHK}" = "active"
        then
          PRC=`systemctl show -p ExecMainPID charon_monhasperrlog 2>/dev/null | cut -f2 -d'='`
          if test -n "`ps --ppid ${PRC} | grep -w tail`"
          then
            LOGVER=$(grep ^VERSION= ${CHARONDIR}/utils/charon_logchk | cut -f2 -d '=')
            if test "${LOGVER}" = "$(cat /var/run/charon_logchk.monhasperrlog 2>/dev/null)"
            then
              echo "[32mrunning[0m"
            else
              echo "${FGORANGE}running(!)[0m"
              touch /tmp/charon_menu_logchk.alert.version 2>/dev/null
            fi
          else
            echo "${FGORANGE}starting[0m"
          fi
        else
          echo "[31m${SVCCHK}[0m"
        fi
      else
        echo "${FGORANGE}disabled[0m"
      fi
    else
      tput hpa 60
      echo "${BGORANGE} Alerts disabled [0m"
    fi
  fi

  #--- Display VE license server monitoring status
  if test -x /opt/license-server/license_viewer
  then
    echo -n "VE license server("
    SVCSTA=$(systemctl is-active licensed 2>/dev/null)
    case "${SVCSTA}"
    in
      "active")
        echo -n "[32m${SVCSTA}[0m"
        ;;
      "unknown")
        echo -n "[31m${SVCSTA}[0m"
        ;;
      "")
        echo -n "${FGORANGE}disabled[0m"
        ;;
      *)
        echo -n "${FGORANGE}${SVCSTA}[0m"
        ;;
    esac
    echo -n "): log file monitoring"
    tput hpa 50
    tput el
    echo -n " -        "

    if test ! -e ${MONVESVRALTDISABLED}
    then
      SVCCHK=`systemctl is-enabled charon_monvesvrlog.service 2>/dev/null`
      if test -n "${SVCCHK}"
      then
        test "${SVCCHK}" = "enabled" && echo -n "[32m" || echo -n "[31m"
        echo -n "${SVCCHK:-disabled}[0m, "
        SVCCHK=`systemctl is-active charon_monvesvrlog.service 2>/dev/null`
        if test "${SVCCHK}" = "active"
        then
          PRC=`systemctl show -p ExecMainPID charon_monvesvrlog 2>/dev/null | cut -f2 -d'='`
          if test -n "`ps --ppid ${PRC} | grep -w tail`"
          then
            LOGVER=$(grep ^VERSION= ${CHARONDIR}/utils/charon_logchk | cut -f2 -d '=')
            if test "${LOGVER}" = "$(cat /var/run/charon_logchk.monvesvrlog 2>/dev/null)"
            then
              echo "[32mrunning[0m"
            else
              echo "${FGORANGE}running(!)[0m"
              touch /tmp/charon_menu_logchk.alert.version 2>/dev/null
            fi
          else
            echo "${FGORANGE}starting[0m"
          fi
        else
          echo "[31m${SVCCHK}[0m"
        fi
      else
        echo "${FGORANGE}disabled[0m"
      fi
    else
      tput hpa 60
      echo "${BGORANGE} Alerts disabled [0m"
    fi
  fi

  display_logsmonitored

  RALBOLD=${OPTBOLD}
  if test -e /tmp/charon_menu_logchk.alert.version
  then
    echo
    echo "${BGORANGE} WARNING [0m"
    echo "${FGORANGE}Version running is outdated. Please restart all log monitoring services.[0m"
    RALBOLD="${RALBOLD}${FGORANGE}"
    rm -f /tmp/charon_menu_logchk.alert.version 2>/dev/null
  fi

  echo
  tput bold
  echo "${FGBLUE}Available options[0m"
  tput sgr0

  AKSMONI="[90m"
  AKSREST="[90m"
  HSPMONI="[90m"
  HSPREST="[90m"
  VESMONI="[90m"
  VESREST="[90m"
  AKSMONICOL=""
  HSPMONICOL=""
  VESMONICOL=""
  if test "${GLOBALLOGMON}" = "enabled"
  then
    rpm -q aksusbd >/dev/null
    if test $? -eq 0
    then
      AKSMONI="[1m"
      systemctl --all --type service | grep -qw "charon_monusb.service"
      test $? -eq 0 && AKSREST="[1m"
      HSPMONI="[1m"
      systemctl --all --type service | grep -qw "charon_monhasperrlog.service"
      test $? -eq 0 && HSPREST="[1m"

      if test "${MONAKSUSBDLOG}" = "enabled"
      then
        AKSMONICOL="[32m"
      else
        AKSMONICOL=${FGORANGE}
      fi

      if test "${MONHASPERROR}" = "enabled"
      then
        HSPMONICOL="[32m"
      else
        HSPMONICOL=${FGORANGE}
      fi
    else
      MONAKSUSBDLOG="disabled"
      MONHASPERROR="disabled"
      set_preferences
    fi

    rpm -q license-server >/dev/null
    if test $? -eq 0
    then
      VESMONI="[1m"
      systemctl --all --type service | grep -qw "charon_monvesvrlog.service"
      test $? -eq 0 && VESREST="[1m"

      if test "${MONVELSLOG}" = "enabled"
      then
        VESMONICOL="[32m"
      else
        VESMONICOL=${FGORANGE}
      fi
    else
      MONVELSLOG="disabled"
      set_preferences
    fi
  fi

  cat <<EOF
${OPTBOLD} 1[0m- Update log files list              $(tput hpa 39)${HSPMONI} 7[0m- HASP error log monitoring (${HSPMONICOL}${MONHASPERROR}[0m)
${OPTBOLD} 2[0m- Start/stop monitoring log file     $(tput hpa 39)${HSPREST} 8[0m- (Re)start HASP error log monitor
[1m 3[0m- View guest log files               $(tput hpa 39)${VESMONI} 9[0m- VE server log monitoring (${VESMONICOL}${MONVELSLOG}[0m)
${OPTBOLD} 4[0m- Manage systemd services            $(tput hpa 39)${VESREST}10[0m- (Re)start VE license svr log monitor
${AKSMONI} 5[0m- aksusbd monitoring (${AKSMONICOL}${MONAKSUSBDLOG}[0m) $(tput hpa 39)${RALBOLD}11[0m- (Re)start all log monitoring services
${AKSREST} 6[0m- (Re)start aksusbd log monitor      $(tput hpa 39)${OPTBOLD}12[0m- Resume/disable alerts

EOF

  printf "${FGBLUE}Enter your choice (enter to refresh, 'q' to quit):[0m "
  read CH
  echo
  case "${CH}"
  in
    1)
      #---------------------------------------------------
      # Edit log files list
      #---------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        touch ${LOGFLIST}
        echo
        echo "[44m[37m Current log files list [0m"
        cat ${LOGFLIST}

        grep -v ^# ${BOOTLIST} | while read LINE
        do
          EMU=`echo ${LINE} | cut -f1 -d';'`
          CFG=`echo ${LINE} | cut -f2 -d';'`
          get_logfile ${CFG}
          RLOGF=${LOGF}
          if test -z "`grep ^${RLOGF}$ ${LOGFLIST}`"
          then
            echo "${FGBLUE}${LOGF} (`basename ${EMU}`) found and added.[0m"
            echo ${LOGF} >>${LOGFLIST}
            manage_systemd_service create ${LOGF}
          fi
        done
        echo

        cp -f ${LOGFLIST} /tmp/`basename ${LOGFLIST}`.before_edit

        while test 1
        do
          printf "Do you want to edit the log files list (y/n) ? "
          read ANS      
          case "${ANS}"
          in
            y)
              cat <<EOF
You will be editing the log file list.

Please respect the following:
- one log file per line
- log file with full path
- no blank line
- there is no file name check so ensure you enter the correct file name
EOF

              ask_editor noquit
              if test -n "${EDI}"
              then
                while test 1
                do
                  echo
                  echo "Updating file using '`basename ${EDI}`'..."
            
                  ${EDI} ${LOGFLIST} 2>/dev/null
                  grep -v ^$ ${LOGFLIST} >${LOGFLIST}.noemptylines
                  mv -f ${LOGFLIST}.noemptylines ${LOGFLIST}
                  if test -s ${LOGFLIST}
                  then
                    tput bold
                    echo "${FGBLUE}Log file list[0m"
                    tput sgr0
                    display_hr
                    cat ${LOGFLIST}
                    display_hr
                  else
                    echo "Log file list is empty !"
                  fi

                  while test 1
                  do
                    echo
                    tput bold
                    printf "Do you want to edit the file again (y/n) ? "
                    tput sgr0
                    read ANS
                    case "${ANS}"
                    in
                      y)
                        break 1
                        ;;
                      n)
                        diff /tmp/`basename ${LOGFLIST}`.before_edit ${LOGFLIST} | grep "^<" | awk '{print $2}' | while read RLOGF
                        do                      
                          manage_systemd_service stop   ${RLOGF}
                          manage_systemd_service delete ${RLOGF}
                        done
                        break 2
                        ;;
                      *)
                        tput cuu1;tput el
                        ;;
                    esac
                  done
                done
              fi
              break
              ;;
            n)
              break
              ;;
            *)
              tput cuu1;tput el
              ;;
          esac
        done
      fi
      ;;
    2)
      #---------------------------------------------------
      # Start/stop monitoring log file
      #---------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        while test 1
        do
          #display_guestslist
          #echo
          display_logsmonitored -num

          echo
          NLOGS=`grep -v ^# ${LOGFLIST} | wc -l`
          if test ${NLOGS:-0} -eq 1
          then 
            ANS=1
          else
            tput bold
            printf "Enter the log file number you want to start/stop (q to quit): "
            tput sgr0
            read ANS
            test "${ANS}" = "q" && break
          fi
          if test -n "${ANS}"
          then
            if test -z "`echo ${ANS} | tr -d [0123456789]`"
            then
              if test ${ANS} -lt 1 -o ${ANS} -gt `cat ${LOGFLIST} | wc -l`
              then
                echo "Incorrect value, out of range."
              else
                RLOGF=${LOGARRAY[${ANS}]}
                SVC_IS_ACTIVE=`manage_systemd_service is-active ${RLOGF} | cut -f1 -d'-' | tr [:lower:] [:upper:]`
                if test "${SVC_IS_ACTIVE}" = "DEACTIVATING"
                then
                  LOGACTION="stopping"
                  LOGACTDISP=""
                else
                  if test "${SVC_IS_ACTIVE}" = "ACTIVE"
                  then
                    LOGACTION="stop"
                    LOGACTDISP="[31mstop"
                  else
                    LOGACTION="start"
                    LOGACTDISP="[32mstart"
                  fi
                fi

                echo
                if test "${LOGACTION}" = "stop" -o "${LOGACTION}" = "start"
                then
                  while test 1
                  do
                    printf "Please confirm you want to `tput bold`${LOGACTDISP}`tput sgr0` (y/n): "
                    read ANS
                    case "${ANS}"
                    in
                      y)
                        manage_systemd_service ${LOGACTION} ${RLOGF}
                        if test ${NLOGS:-0} -eq 1
                        then
                          break 2
                        else
                          break
                        fi
                        ;;
                      n)
                        if test ${NLOGS:-0} -eq 1
                        then
                          break 2
                        else
                          break
                        fi
                        ;;
                      *)
                        tput cuu1;tput el
                        ;;
                    esac
                  done
                else
                  echo "[44m[37m Current state is starting, cannot start nor stop at the moment [0m"
                  echo
                fi
              fi
            else
              echo "Incorrect value."         
            fi
          fi
        done
      fi
      ;;
    3)
      #---------------------------------------------------
      # View guest log file
      #---------------------------------------------------
      while test 1
      do
        display_guestslist -num -l
        echo
        NLOGS=`grep -v ^# ${LOGFLIST} | wc -l`
        if test ${NLOGS:-0} -eq 1
        then 
          ANS=1
        else
          tput bold
          printf "Select the guest you want the log file to be viewed (q to quit): "
          tput sgr0
          read ANS
          test "${ANS}" = "q" && break
        fi

        if test -n "${ANS}"
        then
          if test -z "`echo ${ANS} | tr -d [0123456789]`"
          then
            if test ${ANS} -lt 1 -o ${ANS} -gt `grep -v ^# ${BOOTLIST} | wc -l`
            then
              tput bold
              echo "Incorrect value, out of range."
              tput sgr0
            else
              CFG=`grep -v ^# ${BOOTLIST} | head -${ANS} | tail -1 | cut -f2 -d';'`
              get_logfile ${CFG}
              if test -n "${LOGF}"
              then
                echo
                echo "Found definition of log file: ${LOGF}"
                echo
                select_logfile
              else
                tput bold
                echo "Log file name cannot be extracted from the configuration file."
                echo "Please check syntax."
                tput sgr0
              fi
            fi
            test ${NLOGS:-0} -eq 1 && break
          else
            tput bold
            echo "Incorrect value, must be numeric."
            tput sgr0
          fi
        fi
      done
      ;;
    4)
      #-----------------------------------------------------------------------
      # Manage 'systemd' services
      #-----------------------------------------------------------------------
      touch ${LOGFLIST}
      if test "${GLOBALLOGMON}" = "enabled"
      then
        while test 1
        do
          display_guestslist -num -l
          echo
          NGUESTS=`grep -v ^# ${BOOTLIST} | wc -l`
          if test ${NGUESTS:-0} -eq 0
          then 
            tput bold
            echo "Guests list is empty."
            tput sgr0
            break
          fi
          if test ${NGUESTS:-0} -eq 1
          then
            ANS=1
          else
            printf "Select the guest you want to see service state (q to quit, enter to refresh): "
            read ANS
            test "${ANS}" = "q" && break
          fi
          if test -n "${ANS}"
          then
            if test -z "`echo ${ANS} | tr -d [0123456789]`"
            then
              if test ${ANS} -lt 1 -o ${ANS} -gt ${NGUESTS:-0}
              then
                tput bold
                echo "Incorrect value, out of range."
                tput sgr0
              else
                GUEST_CFG=`grep -v ^# ${BOOTLIST} | head -${ANS} | tail -1 | cut -f2 -d';'`
                get_logfile ${GUEST_CFG}
                if test -n "${LOGF}"
                then
                  echo
                  GUEST_CFGBAS=`basename ${GUEST_CFG}|sed "s=\.cfg==g"`
                  while test 1
                  do
                    echo "[44m[37m charon_logmon_${GUEST_CFGBAS}.service status [0m"
                    systemctl -l --no-pager status charon_logmon_${GUEST_CFGBAS}.service
                    echo
                    echo -n "Do you want to edit the .service file (y/n) ? "
                    read AN
                    case "${AN}"
                    in 
                      y)
                        ask_editor
                        if test -n "${EDI}"
                        then
                          ${EDI} /etc/systemd/system/charon_logmon_${GUEST_CFGBAS}.service 2>/dev/null
                          echo "Reloading systemd manager configuration..."
                          systemctl daemon-reload
                        fi
                        if test ${NGUESTS:-0} -eq 1
                        then
                          break 2
                        else
                          break
                        fi
                        ;;
                      n)
                        if test ${NGUESTS:-0} -eq 1
                        then
                          break 2
                        else
                          break
                        fi
                        ;;
                      *)
                        tput cuu1;tput el
                        ;;
                    esac
                  done
                else
                  tput bold
                  echo "Log file name cannot be extracted from the configuration file."
                  echo "Please check syntax."
                  tput sgr0
                fi
              fi
            else
              tput bold
              echo "Incorrect value, must be numeric."
              tput sgr0
            fi
          fi
        done
        echo
      fi
      ;;
    5)
      #-----------------------------------------------------------------------
      # aksusbd monitoring enable/disable
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        systemctl --all --type service | grep -qw "aksusbd.service"
        if test $? -eq 0
        then
          if test -x /usr/sbin/aksusbd
          then
            if test "${MONAKSUSBDLOG}" = "enabled"
            then
              if test "$(systemctl is-active charon_monusb.service 2>/dev/null)" = "active"
              then
                echo "Stopping aksusbd monitoring service..."
                systemctl stop charon_monusb.service 2>/dev/null
                echo
              fi
              echo "Disabling aksusbd monitoring service..."
              systemctl disable charon_monusb.service 2>/dev/null
              rm -f /etc/systemd/system/charon_monusb.service 2>/dev/null
              sleep 2
              MONAKSUSBDLOG="disabled"
            else
              if test ! -s /etc/systemd/system/charon_monusb.service
              then
                echo "Installing aksusbd monitoring service..."
                cp -f ${CHARONDIR}/utils/templates/charon_monusb.service /etc/systemd/system/charon_monusb.service
                systemctl daemon-reload
              fi
              sleep 1
              echo "Enabling aksusbd monitoring service..."
              systemctl enable charon_monusb.service
              sleep 2
              echo "Starting aksusbd monitoring service..."
              systemctl start  charon_monusb.service
              sleep 2
              if test "$(systemctl is-active charon_monusb.service 2>/dev/null)" != "active"
              then
                #--- In case it needs a 2nd start...
                systemctl start  charon_monusb.service
              fi
              MONAKSUSBDLOG="enabled"
            fi
          else
            echo
            echo "Option not available."
            echo "[1mReason[0m: ${FGORANGE}aksusbd executable not found[0m"
            Press_Enter
          fi
        else
          echo
          echo "Option not available."
          echo "[1mReason[0m: ${FGORANGE}aksusbd service is not installed on this server[0m"
          Press_Enter
        fi
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      set_preferences
      ;;
    6)
      #-----------------------------------------------------------------------
      # (Re)start aksusbd log monitor
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        systemctl --all --type service | grep -qw "charon_monusb.service"
        if test $? -eq 0
        then
          if test -x /usr/sbin/aksusbd
          then
            echo
            echo "Restarting aksusbd log monitor service..."
            systemctl restart charon_monusb.service
            sleep 2
          else
            echo
            echo "Option not available."
            echo "[1mReason[0m: ${FGORANGE}aksusbd executable not found[0m"
            Press_Enter
          fi
        else
          echo
          echo "Option not available."
          if test "${MONAKSUSBDLOG}" = "disabled"
          then
            echo "[1mReason[0m: ${FGORANGE}Monitoring service is disabled[0m"
          else
            echo "[1mReason[0m: ${FGORANGE}Monitoring service is not installed[0m"
          fi
          Press_Enter
        fi
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      ;;
    7)
      #-----------------------------------------------------------------------
      # HASP error log monitoring enable/disable
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        systemctl --all --type service | grep -qw "aksusbd.service"
        if test $? -eq 0
        then
          if test -x /usr/sbin/aksusbd
          then
            if test "${MONHASPERROR}" = "enabled"
            then
              if test "$(systemctl is-active charon_monhasperrlog.service 2>/dev/null)" = "active"
              then
                echo "Stopping HASP log monitoring service..."
                systemctl stop charon_monhasperrlog.service 2>/dev/null
                echo
              fi
              echo "Disabling HASP log monitoring service..."
              systemctl disable charon_monhasperrlog.service 2>/dev/null
              rm -f /etc/systemd/system/charon_monhasperrlog.service 2>/dev/null
              sleep 1
              MONHASPERROR="disabled"
            else
              if test ! -s /etc/systemd/system/charon_monhasperrlog.service
              then
                echo "Installing HASP log monitoring service..."
                cp -f ${CHARONDIR}/utils/templates/charon_monhasperrlog.service /etc/systemd/system/charon_monhasperrlog.service
                systemctl daemon-reload
              fi
              sleep 1
              echo "Enabling HASP log monitoring service..."
              systemctl enable charon_monhasperrlog.service
              sleep 2
              echo "Starting HASP log monitoring service..."
              systemctl start  charon_monhasperrlog.service
              sleep 2
              if test "$(systemctl is-active charon_monhasperrlog.service 2>/dev/null)" != "active"
              then
                #--- In case it needs a 2nd start...
                systemctl start  charon_monhasperrlog.service
              fi
              MONHASPERROR="enabled"
            fi
          fi
        else
          echo
          echo "Option not available."
          echo "[1mReason[0m: ${FGORANGE}aksusbd service is not installed on this server[0m"
          Press_Enter
        fi
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      set_preferences
      ;;
    8)
      #-----------------------------------------------------------------------
      # (Re)start HASP error log monitor
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        if test "${MONHASPERROR}" = "enabled"
        then
          if test -x /usr/sbin/aksusbd
          then
            echo
            echo "Restarting HASP error log monitor service..."
            systemctl restart charon_monhasperrlog.service
            sleep 2
          else
            echo
            echo "Option not available."
            echo "[1mReason[0m: ${FGORANGE}aksusbd executable not found[0m"
            Press_Enter
          fi
        else
          echo
          echo "Option not available."
          echo "[1mReason[0m: ${FGORANGE}Monitoring service is disabled[0m"
          Press_Enter
        fi
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      ;;
    9)
      #-----------------------------------------------------------------------
      # VE server log monitoring enable/disable
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        systemctl --all --type service | grep -qw "licensed.service"
        if test $? -eq 0
        then
          if test ${MONVELSLOG} = "enabled"
          then
            systemctl -q is-active charon_monvesvrlog.service
            if test $? -eq 0
            then
              echo "Stopping service..."
              systemctl stop charon_monvesvrlog.service
              sleep 1
              echo "Disabling service..."
              systemctl disable charon_monvesvrlog.service
              rm -f /etc/systemd/system/charon_monvesvrlog.service 2>/dev/null
            fi
            MONVELSLOG="disabled"
          else
            if test -x /opt/license-server/license_viewer
            then
              if test ! -s /etc/systemd/system/charon_monvesvrlog.service
              then
                echo "Installing VE license server log monitoring service..."
                cp -f ${CHARONDIR}/utils/templates/charon_monvesvrlog.service /etc/systemd/system/charon_monvesvrlog.service
                systemctl daemon-reload
                systemctl enable charon_monvesvrlog.service
              fi
              echo "Starting service..."
              systemctl start  charon_monvesvrlog.service
              sleep 2
              if test "$(systemctl is-active charon_monvesvrlog.service 2>/dev/null)" != "active"
              then
                #--- In case it needs a 2nd start...
                systemctl start  charon_monvesvrlog.service
              fi
            fi
            MONVELSLOG="enabled"
          fi
        else
          echo
          echo "Option not available."
          echo "[1mReason[0m: ${FGORANGE}licensed service is not installed on this server[0m"
          Press_Enter
        fi
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      set_preferences
      ;;
    10)
      #-----------------------------------------------------------------------
      # (Re)start VE license svr log monitor
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        if test -x /opt/license-server/license_viewer
        then
          systemctl --all --type service | grep -qw "charon_monvesvrlog.service"
          if test $? -eq 0
          then
            echo
            echo "Restarting VE license server log monitor service..."
            systemctl restart charon_monvesvrlog.service
            sleep 2
          else
            echo
            echo "Option not available."
            if test "${MONVELSLOG}" = "disabled"
            then
              echo "[1mReason[0m: ${FGORANGE}Monitoring service is disabled[0m"
            else
              echo "[1mReason[0m: ${FGORANGE}Monitoring service is not installed[0m"
            fi
            Press_Enter
          fi
        else
          echo
          echo "Option not available."
          echo "[1mReason[0m: ${FGORANGE}VE license server is not installed on this server[0m"
          Press_Enter
        fi
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      ;;
    11)
      #-----------------------------------------------------------------------
      # (Re)start all log monitoring services
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        echo
        if test "${MONAKSUSBDLOG}" = "enabled"
        then
          if test -x /usr/sbin/aksusbd
          then
            echo "Restarting aksusbd log monitor service..."
            systemctl reset-failed charon_monusb.service
            systemctl restart      charon_monusb.service
            if test "${MONHASPERROR}" = "enabled"
            then
              echo
              echo "Restarting HASP error log monitor service..."
              systemctl reset-failed charon_monhasperrlog.service
              systemctl restart      charon_monhasperrlog.service
            fi
          fi
        fi

        if test "${MONVELSLOG}" = "enabled"
        then
          if test -x /opt/license-server/license_viewer
          then
            echo "Restarting VE license server log monitor service..."
            systemctl reset-failed charon_monvesvrlog.service
            systemctl restart      charon_monvesvrlog.service
          fi
        fi

        for RLOGF in $(cat ${LOGFLIST})
        do
          if test -n "${RLOGF}"
          then
            if test -e ${RLOGF}
            then
              echo
              SVC_IS_ACTIVE=`manage_systemd_service is-active ${RLOGF} | cut -f1 -d'-' | tr [:lower:] [:upper:]`
              if test "${SVC_IS_ACTIVE}" = "ACTIVE"
              then
                echo "Stopping log monitoring service for ${RLOGF}"
                manage_systemd_service stop ${RLOGF}
                sleep 1
                echo "Starting log monitoring service for ${RLOGF}"
                manage_systemd_service start ${RLOGF}
              else
                echo "Log monitoring service for ${RLOGF} is not running."
                while test 1
                do
                  echo -n "Do you want to start it (y/n) ? "
                  read ANS
                  case "${ANS}"
                  in
                    y|Y)
                      echo "Starting log monitoring service for ${RLOGF}"
                      manage_systemd_service start ${RLOGF}
                      break
                      ;;
                    n|N)
                      break
                      ;;
                    *)
                      tput cuu1;tput el
                      ;;
                  esac
                done
              fi
            else
              echo "${BGORANGE} WARNING [0m"
              echo "${FGORANGE}Log file '${RLOGF}' does not exist. Skipped.[0m"
              echo
            fi
          fi
        done
        Press_Enter
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      ;;
    12)
      #-----------------------------------------------------------------------
      # Resume/disable alerts
      #-----------------------------------------------------------------------
      if test "${GLOBALLOGMON}" = "enabled"
      then
        while test 1
        do
          unset freezeitems 2>/dev/null
          declare -a freezeitems
          if test -x /usr/sbin/aksusbd
          then
            freezeitems+=("monusb")
            if test "${MONHASPERROR}" = "enabled"
            then
              freezeitems+=("monhasperrorlog")
            fi
          fi

          if test -x /opt/license-server/license_viewer
          then
            freezeitems+=("monvesvrlog")
          fi

          for LINE in $(cat ${BOOTLIST} 2>/dev/null)
          do
            GCFG=$(echo ${LINE} | cut -f2 -d';')
            VMNAME=$(basename ${GCFG} | sed "s=\.cfg==g")
            VMEMU=$(basename $(echo ${LINE} | cut -f1 -d';'))
            get_logfile ${GCFG}
            if test -n "${LOGF}"
            then
              freezeitems+=("VM:${VMNAME}:${VMEMU}:${LOGF}")
            fi
          done

          if test ${#freezeitems[*]} -gt 0
          then
            display_header "Manage monitored guests logs"
            test ${#freezeitems[*]} -gt 1 && freezeitems+=("Freezeall" "Unfreezeall")
            tput bold
            echo "${FGBLUE}Resume/disable alerts[0m"
            tput sgr0
            typeset -i NIT=0
            for ITEM in "${freezeitems[@]}"
            do
              NIT=NIT+1
              printf "%2d - " ${NIT}
              case "${ITEM}"
              in
                VM:*)
                  VMNAME=$(echo ${ITEM} | cut -f2 -d':')
                  VMEMU=$(echo ${ITEM} | cut -f3 -d':')
                  VMLOG=$(echo ${ITEM} | cut -f4 -d':')
                  echo -n "${VMNAME} (${VMEMU}): ${VMLOG}"
                  if test -e ${ETCCHARON}/CharonToolkit.${VMNAME}.alertsdisabled
                  then
                    tput hpa 60
                    echo -n "${BGORANGE} Alerts disabled [0m"
                  fi
                  ;;
                monusb)
                  echo -n "aksusbd: immediate dongle (dis)connection "
                  if test -e ${MONUSBALTDISABLED}
                  then
                    tput hpa 60
                    echo -n "${BGORANGE} Alerts disabled [0m"
                  fi
                  ;;
                monhasperrorlog)
                  echo -n "HASP error log "
                  if test -e ${MONHASPALTDISABLED}
                  then
                    tput hpa 60
                    echo -n "${BGORANGE} Alerts disabled [0m"
                  fi
                  ;;
                monvesvrlog)
                  echo -n "VE license server log "
                  if test -e ${MONVESVRALTDISABLED}
                  then
                    tput hpa 60
                    echo -n "${BGORANGE} Alerts disabled [0m"
                  fi
                  ;;
                Freezeall)
                  echo -n "${FGORANGE}Disable all alerts[0m"
                  ;;
                Unfreezeall)
                  echo -n "[32mEnable (resume) all alerts[0m"
                  ;;
              esac
              echo
            done
          else
            echo "${FGORANGE}Nothing to enable/disable."
            Press_Enter
            break
          fi

          echo
          while test 1
          do
            echo -n "${FGBLUE}Enter item number to enable/disable ('q' to quit):[0m "
            read ANS
            test "${ANS}" = "q" && break 2
            test "${ANS}" = "Q" && break 2
            if test -n "$(echo ${ANS} | tr -d [:digit:])"
            then
              echo "Numeric value only..."
              sleep 2
              tput cuu1;tput cuu1;tput ed
            else
              ANS=$(echo ${ANS} | tr -dc [:digit:])
              if test ${ANS} -eq 0
              then
                echo "Incorrect value..."
                sleep 2
                tput cuu1;tput cuu1;tput ed
              else
                N=${ANS}-1
                if test -z "${freezeitems[${N}]}"
                then
                  echo "Incorrect value..."
                  sleep 2
                  tput cuu1;tput cuu1;tput ed
                else
                  ITEM=${freezeitems[${N}]}
                  case "${ITEM}"
                  in
                    VM:*)
                      VMNAME=$(echo ${ITEM} | cut -f2 -d':')
                      if test -e ${ETCCHARON}/CharonToolkit.${VMNAME}.alertsdisabled
                      then
                        rm -f ${ETCCHARON}/CharonToolkit.${VMNAME}.alertsdisabled 2>/dev/null
                      else
                        touch ${ETCCHARON}/CharonToolkit.${VMNAME}.alertsdisabled 2>/dev/null
                      fi
                      ;;
                    monusb)
                      if test -e ${MONUSBALTDISABLED}
                      then
                        rm -f ${MONUSBALTDISABLED} 2>/dev/null
                      else
                        touch ${MONUSBALTDISABLED} 2>/dev/null
                      fi
                      ;;
                    monhasperrorlog)
                      if test -e ${MONHASPALTDISABLED}
                      then
                        rm -f ${MONHASPALTDISABLED} 2>/dev/null
                      else
                        touch ${MONHASPALTDISABLED} 2>/dev/null
                      fi
                      ;;
                    monvesvrlog)
                      if test -e ${MONVESVRALTDISABLED}
                      then
                        rm -f ${MONVESVRALTDISABLED} 2>/dev/null
                      else
                        touch ${MONVESVRALTDISABLED} 2>/dev/null
                      fi
                      ;;
                    Freezeall)
                      touch ${MONUSBALTDISABLED} 2>/dev/null
                      touch ${MONHASPALTDISABLED} 2>/dev/null
                      touch ${MONVESVRALTDISABLED} 2>/dev/null
                      for LINE in $(cat ${BOOTLIST} 2>/dev/null)
                      do
                        GCFG=$(echo ${LINE} | cut -f2 -d';')
                        VMNAME=$(basename ${GCFG} | sed "s=\.cfg==g")
                        touch ${ETCCHARON}/CharonToolkit.${VMNAME}.alertsdisabled 2>/dev/null
                      done
                      ;;
                    Unfreezeall)
                      rm -f ${MONUSBALTDISABLED} 2>/dev/null
                      rm -f ${MONHASPALTDISABLED} 2>/dev/null
                      rm -f ${MONVESVRALTDISABLED} 2>/dev/null
                      rm -f ${ETCCHARON}/CharonToolkit.*.alertsdisabled 2>/dev/null
                      ;;
                  esac
                  break
                fi
              fi
            fi
          done
        done
      else
        echo
        echo "Option not available."
        echo "[1mReason[0m: ${FGORANGE}Global monitoring is disabled[0m"
        Press_Enter
      fi
      ;;
    q)
      break
      ;;
  esac
done

exit
