﻿<#
Author: David Schaerer, ETH Zurich, July 2016

example of smartd env variables:
    SMARTD_ADDRESS=user@yourdomain.local
    SMARTD_DEVICE=/dev/sda
    SMARTD_DEVICEINFO=WDC WD5000AAKS-00V1A0, S/N:WD-WCAWF3694327, WWN:5-0014ee-157d29a56, FW:05.01D05, 500 GB
    SMARTD_DEVICESTRING=/dev/sda
    SMARTD_DEVICETYPE=ata
    SMARTD_FAILTYPE=EmailTest
    SMARTD_MESSAGE=TEST EMAIL from smartd for device: /dev/sda
    SMARTD_PREVCNT=0
    SMARTD_TFIRST=Thu Jul 14 09:04:10 2016 WEDT
    SMARTD_TFIRSTEPOCH=1468479850
#>


# Configure Email settings here
$from = "SmartMonTools <root@$($env:COMPUTERNAME.ToLower()).yourdomain.local>"
$defaultToAddress = "admin@yourdomain.local"
$smtpSrv = "smtp.yourdomain.local"



#############  Email from/to part  #############

# To
# SMARTD_ADDRESS will be defined by smartd.conf (comma separated string of email addresses without spaces)
if (!$env:SMARTD_ADDRESS){
    # If no email recipient was defined set it to a predefined email address
    $to = $defaultToAddress
} else {
    # Convert recipient string from smartd.config into a valid string array for Send-MailMessage cmdlet
    [string[]]$to = $env:SMARTD_ADDRESS.Split(" ")
    
    # If any spaces, remove them
    for ($i=0;$i -lt $to.Count;$i++){
        $to[$i] = $to[$i].Trim()
    }
}

#############  Subject part  #############

$subject="SMART error ($env:SMARTD_FAILTYPE) detected on host: $env:COMPUTERNAME"

#############  Email text part  #############

if (!($env:SMARTD_FAILTYPE -eq "EmailTest")){
    $furtherInvest="You can also use the smartctl utility for further investigation.`n"
}

if (!($env:SMARTD_PREVCNT -eq 0)){
    $sentTime = "The original message about this issue was sent at $env:SMARTD_TFIRST.`n"
}

switch ($env:SMARTD_NEXTDAYS){
    "" {$nextWarn="No additional messages about this problem will be sent.`n"}
    1 {$nextWarn="Another message will be sent in 24 hours if the problem persists.`n"}
    default {$nextWarn="Another message will be sent in $env:SMARTD_NEXTDAYS days if the problem persists.`n"}
}

$emailBody=("
This message was generated by the smartd service running on:

   host name:  $env:COMPUTERNAME
   DNS domain: $((Get-WmiObject win32_computersystem).Domain)
   Win domain: $env:USERDOMAIN

The following warning/error was logged by the smartd service:

$env:SMARTD_MESSAGE

Device info:
$env:SMARTD_DEVICEINFO

For details see the event log or log file of smartd.
$($furtherInvest)$($sentTime)$($nextWarn)

--
This message has been generated with SmartMonTools $((Get-Item .\smartd.exe).VersionInfo.FileVersion)
")
Write-Debug $emailBody > .\smartdMsg-$env:COMPUTERNAME.txt

#############  Email delivery part  #############

if (!$subject){
    $subject = "smartd email error: missing subject"
} elseif (!$emailBody){
    $emailBody = "smartd email error: missing email body."
}

# Send the email
Send-MailMessage -SmtpServer $smtpSrv -to $to -from $from -Subject $subject -Body $emailBody