#!/usr/bin/bash
#
# test_services
#
# check running services
#
#############################################
#
. `dirname $0`/uls_header
#
if [[ -f /etc/uls/services.conf ]]; then
  ( echo "D;$DT;$ULSHOSTNAME;System;Services;"
    awk -F '[ \t]*#' '$1 != ""{print $1}' /etc/uls/services.conf | while read n rcs; do
      RESTART=''
      for rc in $rcs; do
        if [[ "$rc" = "RESTART" ]]; then
          RESTART=1
        elif [[ "$rc" = "SYSTEMD" ]] && which systemctl &>/dev/null && systemctl is-enabled $n &>/dev/null; then
          systemctl status $n &>/dev/null
          R=$?
          echo "V;;;;;$n;$R;#"
          if [[ -n "$RESTART" && "$R" != "0" ]]; then
            systemctl restart $n &>/dev/null
            R=$?
            echo "V;;;;;Restart $n;$R;#"
          fi
          break
        elif [[ "$rc" = "SYSTEMD!" ]] && which systemctl &>/dev/null; then
          systemctl status $n &>/dev/null
          R=$?
          echo "V;;;;;$n;$R;#"
          if [[ -n "$RESTART" && "$R" != "0" ]]; then
            systemctl restart $n &>/dev/null
            R=$?
            echo "V;;;;;Restart $n;$R;#"
          fi
          break
        elif [[ -f "$rc" ]]; then
          /bin/bash $rc status &>/dev/null
          R=$?
          echo "V;;;;;$n;$R;#"
          if [[ -n "$RESTART" && "$R" != "0" ]]; then
            /bin/bash $rc restart &>/dev/null
            R=$?
            echo "V;;;;;Restart $n;$R;#"
          fi
          break
        fi
      done
    done
  ) | send_test_tab
fi
