Ticket #731: emailSender.ps1

File emailSender.ps1, 3.0 KB (added by Gabriele Pohl, 6 years ago)
Line 
1<#
2Author: David Schaerer, ETH Zurich, July 2016
3
4example of smartd env variables:
5    SMARTD_ADDRESS=user@yourdomain.local
6    SMARTD_DEVICE=/dev/sda
7    SMARTD_DEVICEINFO=WDC WD5000AAKS-00V1A0, S/N:WD-WCAWF3694327, WWN:5-0014ee-157d29a56, FW:05.01D05, 500 GB
8    SMARTD_DEVICESTRING=/dev/sda
9    SMARTD_DEVICETYPE=ata
10    SMARTD_FAILTYPE=EmailTest
11    SMARTD_MESSAGE=TEST EMAIL from smartd for device: /dev/sda
12    SMARTD_PREVCNT=0
13    SMARTD_TFIRST=Thu Jul 14 09:04:10 2016 WEDT
14    SMARTD_TFIRSTEPOCH=1468479850
15#>
16
17
18# Configure Email settings here
19$from = "SmartMonTools <root@$($env:COMPUTERNAME.ToLower()).yourdomain.local>"
20$defaultToAddress = "admin@yourdomain.local"
21$smtpSrv = "smtp.yourdomain.local"
22
23
24
25#############  Email from/to part  #############
26
27# To
28# SMARTD_ADDRESS will be defined by smartd.conf (comma separated string of email addresses without spaces)
29if (!$env:SMARTD_ADDRESS){
30    # If no email recipient was defined set it to a predefined email address
31    $to = $defaultToAddress
32} else {
33    # Convert recipient string from smartd.config into a valid string array for Send-MailMessage cmdlet
34    [string[]]$to = $env:SMARTD_ADDRESS.Split(" ")
35   
36    # If any spaces, remove them
37    for ($i=0;$i -lt $to.Count;$i++){
38        $to[$i] = $to[$i].Trim()
39    }
40}
41
42#############  Subject part  #############
43
44$subject="SMART error ($env:SMARTD_FAILTYPE) detected on host: $env:COMPUTERNAME"
45
46#############  Email text part  #############
47
48if (!($env:SMARTD_FAILTYPE -eq "EmailTest")){
49    $furtherInvest="You can also use the smartctl utility for further investigation.`n"
50}
51
52if (!($env:SMARTD_PREVCNT -eq 0)){
53    $sentTime = "The original message about this issue was sent at $env:SMARTD_TFIRST.`n"
54}
55
56switch ($env:SMARTD_NEXTDAYS){
57    "" {$nextWarn="No additional messages about this problem will be sent.`n"}
58    1 {$nextWarn="Another message will be sent in 24 hours if the problem persists.`n"}
59    default {$nextWarn="Another message will be sent in $env:SMARTD_NEXTDAYS days if the problem persists.`n"}
60}
61
62$emailBody=("
63This message was generated by the smartd service running on:
64
65   host name:  $env:COMPUTERNAME
66   DNS domain: $((Get-WmiObject win32_computersystem).Domain)
67   Win domain: $env:USERDOMAIN
68
69The following warning/error was logged by the smartd service:
70
71$env:SMARTD_MESSAGE
72
73Device info:
74$env:SMARTD_DEVICEINFO
75
76For details see the event log or log file of smartd.
77$($furtherInvest)$($sentTime)$($nextWarn)
78
79--
80This message has been generated with SmartMonTools $((Get-Item .\smartd.exe).VersionInfo.FileVersion)
81")
82Write-Debug $emailBody > .\smartdMsg-$env:COMPUTERNAME.txt
83
84#############  Email delivery part  #############
85
86if (!$subject){
87    $subject = "smartd email error: missing subject"
88} elseif (!$emailBody){
89    $emailBody = "smartd email error: missing email body."
90}
91
92# Send the email
93Send-MailMessage -SmtpServer $smtpSrv -to $to -from $from -Subject $subject -Body $emailBody