cover-smmtp.jpeg
cover-smmtp.jpeg

Purpose:
Col­lect S.M.A.R.T. (Self-Mon­i­tor­ing, Analy­sis, and Re­port­ing Tech­nol­ogy) data from mul­ti­ple disks (/dev/sda, /dev/sdb, /dev/sdc, /dev/sdd) and au­to­mat­i­cally email the re­port us­ing ssmtp.


Prerequisites

commands that require root privileges

  1. Required Tools:

    • smartmontools: Retrieves S.M.A.R.T. data.
    • ssmtp: Handles email delivery.
      Install both with:
    apt install smartmontools ssmtp -y
  2. Permissions:
    En­sure pass­word­less sudo ac­cess for smartctl by adding the fol­low­ing line to /etc/sudoers:

    $USER ALL=(ALL) NOPASSWD: /usr/sbin/smartctl

    $USER is the sys­tem user run­ning the script.

  3. Email Configuration:
    Configure ssmtp in /etc/ssmtp/ssmtp.conf with valid SMTP server credentials (e.g., Gmail, Outlook, or a custom SMTP server).

Using sSMTP with Gmail

# Encryption Settings
UseTLS=YES
UseSTARTTLS=YES

# Server Configuration
mailhub=smtp.gmail.com:587

# Authentication
AuthMethod=LOGIN
AuthUser=username@gmail.com
AuthPass=xxxxxxxxxxxxxxx

/etc/ssmtp/ssmtp.conf Con­fig­u­ra­tion

ParameterValueDescriptionNotes
AuthMethodLOGINAuthentication method for SMTP serverStandard method for username/password authentication
UseTLSYESEnables implicit TLS encryptionTypically used with port 465 (SMTPS)
UseSTARTTLSYESEnables opportunistic TLS via STARTTLS commandTypically used with port 587
mailhubsmtp.gmail.com:587SMTP server address and portGmail's submission port with STARTTLS
AuthUserusername@gmail.comFull email address for authenticationMust match registered email in service provider
AuthPassemail-passwordApp password or account passwordFor Gmail: Use app password if 2FA enabled

Setup a Shell Script

cat << EOF > smart-report.sh
#!/bin/

# Email configuration
recipient="admin@example.com"    # Replace with target email
sender="server@example.com"      # Replace with sender email

# Disks to monitor
disks=("/dev/sda" "/dev/sdb" "/dev/sdc" "/dev/sdd")

# Temporary file for email content
tempfile=$(mktemp)

# Email headers
echo "To: $recipient" >> "$tempfile"
echo "From: $sender" >> "$tempfile"
echo "Subject: Disk S.M.A.R.T. Health Report" >> "$tempfile"
echo "" >> "$tempfile"

# Collect S.M.A.R.T. data
for disk in "${disks[@]}"; do
    echo "=== $disk S.M.A.R.T. Data ===" >> "$tempfile"
    sudo smartctl -a "$disk" >> "$tempfile" 2>&1
    echo -e "\n\n" >> "$tempfile"
done

# Send email
ssmtp "$recipient" < "$tempfile"

# Cleanup
rm "$tempfile"

EOF

Con­fig­u­ra­tion Steps

  1. Update Email Addresses:

    • Replace admin@example.com with the recipient’s email.
    • Replace server@example.com with the sender email (must match ssmtp configuration).
  2. Customize Disks:
    Modify the disks array to include relevant disk devices (e.g., /dev/nvme0n1 for NVMe drives).
  3. Test Execution:

    • Save the script as smart-report.sh.
    • Grant ex­e­cute per­mis­sions:

      chmod +x smart-report.sh
    • Run the script:

      ./smart-report.sh

Add Weekly S.M.A.R.T. Reports via Cron

To au­to­mate the script to run every Monday at midnight (first day of the week), add a cron job:

# Open the crontab editor
crontab -e

Add this line to the crontab file:

0 0 * * 1 /path/to/smart-report.sh  # Runs every Monday at 00:00
  • 0 0: Minute (0) and hour (0 = midnight).
  • * * 0: Day-of-week (0 = Sunday, 1 = Monday).

Reference:

sSMTP - Simple SMTP