File check-clamd-freshclam.sh of Package kolab-scripts

#!/bin/bash
# KPS_CHECK_ORDER: 40

# (c) 2013 Richard Bos

PATH=/usr/bin:/bin:/usr/sbin

echo -n "Checking ClamAV signature database... "

# Determine the state of service clamd.
if [[ -x /usr/bin/systemctl ]] || [[ -x /bin/systemctl ]]; then
  CLAMD_SERVICE_STATE=$(systemctl is-active clamd.service)
else
  # Use the old fashioned way to determine that a process is active
  if ps aux | grep -q "c[l]amd"; then
    CLAMD_SERVICE_STATE="active"
  else
    CLAMD_SERVICE_STATE="inactive"
  fi
fi

# Determine the signature database file to use
# .cld is created from last .cvd + .cdiff, or last .cld + .cdiff
for F in /var/lib/clamav/daily.cld /var/lib/clamav/daily.cvd; do
  [[ -f $F ]] && CLAMDB=$F
done

if [[ -f /usr/bin/freshclam ]]; then

  if [[ -n "$CLAMDB" ]]; then

    DATE=$(date +%Y-%m-%d)

    if [[ "$FORCE_UPDATE" == yes ]]; then
      LAST_MODIFY_DATE=0
    else
      LAST_MODIFY_DATE=$(stat -c %z $CLAMDB | awk '{print $1}')
    fi

    if [[ "$DATE" == "$LAST_MODIFY_DATE" ]]; then
      # ClamAV signature database was already refreshed today
      echo OK
    else
      UPDATE_CLAMAV=yes
    fi

  else
    UPDATE_CLAMAV=yes
  fi

  if [[ "$UPDATE_CLAMAV" == "yes" ]]; then
    if [[ "$INTERNET_CONNECTIVITY" != "no" ]]; then
      echo; echo "  Updating ClamAV database..."

      # On first invocation, clamd is not active, as there are no signature
      # database files available, these are still to be retrieved by freshclam.
      # To suppress the confusing output when clamd is not active:
      #   WARNING: Clamd was NOT notified: Can't connect to clamd through /var/run/clamav/clamd-socket
      #   connect(): No such file or directory
      # freshclam is called with additional arguments and stderr suppressed.
      if [[ $CLAMD_SERVICE_STATE != active ]]; then
        freshclam --no-warnings 2>/dev/null
      else
        freshclam
      fi

    else
      echo
      echo "  Skipping ClamAV signature database as internet connectivity"
      echo "  has been disabled (INTERNET_CONNECTIVITY=no)"
    fi
  fi
else
  echo; echo "  freshclam not found, ClamAV signature database can not be updated"
fi

echo -n "Checking clamd service... "
unset CLAMDB
# The signature database files may have been retrieved by
# freshclam above.
for F in /var/lib/clamav/daily.cld /var/lib/clamav/daily.cvd; do
  [[ -f $F ]] && CLAMDB=$F
done

if [[ -n "$CLAMDB" ]]; then

  CHECK="OK"

  # As the signature data has been retrieved, it is now possible
  # to enable and start clamd
  if [[ -x /usr/bin/systemctl ]] || [[ -x /bin/systemctl ]]; then

    CLAMD_SERVICE=$(systemctl is-enabled clamd.service 2>/dev/null | grep abled)

    if [[ $CLAMD_SERVICE != "enabled" ]]; then
      systemctl enable clamd.service 2>/dev/null
      STRING_CHECK_ENABLE="has been enabled"
      unset CHECK
    else
      STRING_CHECK_ENABLE="enabled"
    fi

    if [[ $CLAMD_SERVICE_STATE != "active" ]]; then
      systemctl start clamd.service
      STRING_CHECK_START="started"
      unset CHECK
    else
      STRING_CHECK_START="active"
    fi
  else

    if chkconfig -c clamd 35; then
      STRING_CHECK_ENABLE="enabled"
    else
      chkconfig clamd 35
      STRING_CHECK_ENABLE="has been enabled"
      unset CHECK
    fi

    if [[ $CLAMD_SERVICE_STATE != "active" ]]; then
      rcclamd start 2>/dev/null
      STRING_CHECK_START="started"
      unset CHECK
    else
      STRING_CHECK_START="active"
    fi
  fi

  if [[ "$CHECK" == "OK" ]]; then
    echo OK
  else
    echo
    echo "  Service clamd $STRING_CHECK_ENABLE and is $STRING_CHECK_START"
  fi
else
  echo; echo "  Signature database files not found"
fi

openSUSE Build Service is sponsored by