Trong bài viết này, Zimbra đã hướng dẫn kiểm tra xem chứng thư số ssl sắp hết hạn hay chưa và tự động gửi email về cho admin.

Zimico hiện đang sử dụng kênh Telegram để nhận thông báo về hệ thống nên chúng tôi chỉnh sửa script tự động gửi tin nhắn về telegram để nhận thông báo về tình trạng của chứng thư số. Nếu bạn sử dụng mô hình multiserver, cài đặt script trên proxy server.

Để tạo telegram bot, tham khảo tại đây.

Tạo script check_expiration.sh như sau với quyền root:

mkdir /opt/scripts
touch /opt/scripts/check_expiration.sh
chmod +x /opt/scripts/check_expiration.sh

Nội dung file check_expiration.sh như sau:

#!/bin/bash
WHO=`whoami`
if [ $WHO != "zimbra" ]
then
echo
echo "Execute this scipt as user zimbra (\"su - zimbra\")"
echo
exit 1
fi

host_name=$(hostname --fqdn)
token_zimilab="telegram_zimilab_token"
chat_id_zimilab="-zimilab_chat_id"   #lưu ý có dấu trừ trước id
url_zimilab="https://api.telegram.org/bot${token_zimilab}/sendMessage"

# Thay đổi DAYS cho phù hợp.
DAYS=25

expDates=$(/opt/zimbra/bin/zmcertmgr viewdeployedcrt | grep notAfter| awk -F '=' '{print $2}')
inXdays=$(($(date +%s) + (86400*$DAYS)));

while IFS= read -r expDate; do
  expirationDate=$(date -d "$expDate" +%s)
  if [ $inXdays -gt $expirationDate ]; then
    curl -s -X POST $url_zimilab -d chat_id=$chat_id_zimilab -d text="Zimbra TLS certificate of ${host_name} is about to expire"
    exit 1
  fi;
done <<< "$expDates"

##In case the certificate was renewed but Zimbra not restarted, zmcertmgr viewdeployedcrt shows certificate that will be used after restart, so check the running cert manually
#expDate=$(cat /opt/zimbra/ssl/zimbra/server/server.crt | /opt/zimbra/common/bin/openssl x509 -noout -enddate | grep notAfter| awk -F '=' '{print $2}')
#expirationDate=$(date -d "$expDate" +%s)
#
#if [ $inXdays -gt $expirationDate ]; then
# curl -s -X POST $url_zimilab -d chat_id=$chat_id_zimico -d text="Zimbra TLS certificate of ${host_name} is about to expire"
# exit 1
#fi;

exit 0

Với quyền zimbra, cho thực thi file này trong cron job

su - zimbra
crontab -e
40 * * * * /opt/scripts/check-expiration.sh > /dev/null 2>&1