SPB Git

Repository Public

sp-mail-monitor.sh 4.4 KB 73 lines shellscript

1#!/usr/bin/env bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : SP Mail8# File             : infra/monitoring/sp-mail-monitor.sh9# Module           : infra10# Repository       : https://git.spboucher.ai/sp-mail11# Project Website  : https://mail.spboucher.ai12# Created          : 2026-11-1813# Last Modified    : 2026-11-1814#15# Author           : Simon-Pierre Boucher16# Position         : Professor of Finance17# Institution      : Université du Québec en Outaouais (UQO)18# Organization     : SP Boucher19# Website          : https://spboucher.ai20# Git              : https://git.spboucher.ai21#22# Preferred Contact: support@spboucher.ai23# Technical Contact: dev@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041# SP Mail — surveillance du serveur de courrier (BHS64), toutes les 5 min. Écrit /var/lib/sp-mail-monitor/status.json.42# Alerte seulement quand l'état CHANGE (pas de rafale) : entrée de journal + courriel local à l'adresse d'alerte si elle existe.43set -uo pipefail44D=/var/lib/sp-mail-monitor; mkdir -p "$D"; ALERT_TO="${SPMAIL_ALERT_TO:-info@spboucher.ai}"45problems=()46systemctl is-active -q stalwart || problems+=("stalwart arrêté")47systemctl is-active -q clamav-daemon || problems+=("clamav-daemon arrêté")48systemctl is-active -q clamav-milter || problems+=("clamav-milter arrêté")49banner=$(timeout 8 bash -c 'exec 3<>/dev/tcp/127.0.0.1/25 && head -1 <&3' 2>/dev/null || true)50[[ "$banner" == 220* ]] || problems+=("SMTP 25 sans bannière")51end=$(openssl x509 -in /etc/stalwart/tls/mx1.spboucher.ai.crt -noout -enddate 2>/dev/null | cut -d= -f2)52days=$(( ( $(date -d "$end" +%s 2>/dev/null || echo 0) - $(date +%s) ) / 86400 ))53[ "$days" -ge 14 ] || problems+=("certificat mx1 : $days jours restants")54disk=$(df --output=pcent / | tail -1 | tr -dc 0-9)55[ "$disk" -lt 85 ] || problems+=("disque / à ${disk}%")56queue=$(STALWART_URL=http://127.0.0.1:8080 STALWART_USER=spmail-admin@spboucher.ai STALWART_PASSWORD="$(cat /root/.sp-mail-svc)" NO_COLOR=1 /usr/local/bin/stalwart-cli query QueuedMessage --json 2>/dev/null | grep -c . || true)57[ "${queue:-0}" -lt 50 ] || problems+=("file d'attente : $queue messages")58last=$(ls -1t /var/backups/sp-mail/edge-*.tar.zst.enc 2>/dev/null | head -1)59age=$(( ( $(date +%s) - $(stat -c %Y "${last:-/nonexistent}" 2>/dev/null || echo 0) ) / 3600 ))60[ "$age" -lt 30 ] || problems+=("dernière sauvegarde : il y a ${age} h")61ptr=$(dig +short -x 51.161.112.61 @1.1.1.1 2>/dev/null | head -1)62[ "$ptr" = "mx1.spboucher.ai." ] || problems+=("PTR = ${ptr:-absent}")63state="ok"; [ ${#problems[@]} -eq 0 ] || state=$(printf '%s; ' "${problems[@]}")64printf '{"at":"%s","ok":%s,"problems":"%s","cert_days":%s,"disk_pct":%s,"queue":%s,"backup_age_h":%s}\n' "$(date -u +%FT%TZ)" "$([ "$state" = ok ] && echo true || echo false)" "${state//\"/}" "$days" "$disk" "${queue:-0}" "$age" > "$D/status.json"65prev=$(cat "$D/last-state" 2>/dev/null || echo ok)66if [ "$state" != "$prev" ]; then67  echo "$state" > "$D/last-state"68  logger -t sp-mail-monitor "état : $state"69  printf 'From: SP Mail <postmaster@spboucher.ai>\nTo: %s\nSubject: [SP Mail] %s\nDate: %s\nMessage-ID: <%s@mx1.spboucher.ai>\n\nÉtat du serveur de courrier à %s :\n%s\n' "$ALERT_TO" "$([ "$state" = ok ] && echo 'retour à la normale' || echo 'alerte')" "$(date -R)" "$(date +%s)" "$(date -u +%FT%TZ)" "$state" \70   | python3 -c "import smtplib,sys; m=sys.stdin.buffer.read(); s=smtplib.SMTP('127.0.0.1',25,timeout=15); s.ehlo('mx1.spboucher.ai'); s.sendmail('postmaster@spboucher.ai',['$ALERT_TO'],m); s.quit()" 2>/dev/null || true71fi72cat "$D/status.json"73
Files main
Menu

Theme