cover-smmtp.jpeg
cover-smmtp.jpeg

Purpose:
Collect S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology) data from multiple disks (/dev/sda, /dev/sdb, /dev/sdc, /dev/sdd) and automatically email the report using 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:
    Ensure passwordless sudo access for smartctl by adding the following line to /etc/sudoers:

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

    $USER is the system user running 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
[email protected]
AuthPass=xxxxxxxxxxxxxxx

/etc/ssmtp/ssmtp.conf Configuration

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
AuthUser[email protected]Full 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="[email protected]"    # Replace with target email  
sender="[email protected]"      # 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

Configuration Steps

  1. Update Email Addresses:

  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 execute permissions:

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

      ./smart-report.sh  

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

To automate 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