1@echo off
2::
3:: smartd warning script
4::
5:: Home page of code is: http://www.smartmontools.org
6::
7:: Copyright (C) 2012-17 Christian Franke
8::
9:: SPDX-License-Identifier: GPL-2.0-or-later
10::
11:: $Id: smartd_warning.cmd 4760 2018-08-19 18:45:53Z chrfranke $
12::
13
14verify other 2>nul
15setlocal enableextensions enabledelayedexpansion
16if errorlevel 1 goto UNSUPPORTED
17set err=
18
19:: Change to script directory (not necessary if run from smartd service)
20cd /d %~dp0
21if errorlevel 1 goto ERROR
22
23:: Parse options
24set dryrun=
25if "%1" == "--dryrun" (
26  set dryrun=--dryrun
27  shift
28)
29if not "!dryrun!" == "" echo cd /d !cd!
30
31if not "%1" == "" (
32  echo smartd warning message script
33  echo.
34  echo Usage:
35  echo set SMARTD_MAILER='Path to external script, empty for "blat"'
36  echo set SMARTD_ADDRESS='Space separated mail addresses, empty if none'
37  echo set SMARTD_MESSAGE='Error Message'
38  echo set SMARTD_FAILTYPE='Type of failure, "EMailTest" for tests'
39  echo set SMARTD_TFIRST='Date of first message sent, empty if none'
40  echo :: set SMARTD_TFIRSTEPOCH='time_t format of above'
41  echo set SMARTD_PREVCNT='Number of previous messages, 0 if none'
42  echo set SMARTD_NEXTDAYS='Number of days until next message, empty if none'
43  echo set SMARTD_DEVICEINFO='Device identify information'
44  echo :: set SMARTD_DEVICE='Device name'
45  echo :: set SMARTD_DEVICESTRING='Annotated device name'
46  echo :: set SMARTD_DEVICETYPE='Device type from -d directive, "auto" if none'
47
48  echo smartd_warning.cmd [--dryrun]
49  goto ERROR
50)
51
52if "!SMARTD_ADDRESS!!SMARTD_MAILER!" == "" (
53  echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set
54  goto ERROR
55)
56
57:: USERDNSDOMAIN may be unset if running as service
58if "!USERDNSDOMAIN!" == "" (
59  for /f "delims== tokens=2 usebackq" %%d in (`wmic PATH Win32_Computersystem WHERE "PartOfDomain=TRUE" GET Domain /VALUE ^<nul 2^>nul`) do set USERDNSDOMAIN=%%~d
60)
61:: Remove possible trailing \r appended by above command (requires %...%)
62set USERDNSDOMAIN=%USERDNSDOMAIN%
63
64:: Format subject
65set SMARTD_SUBJECT=SMART error (!SMARTD_FAILTYPE!) detected on host: !COMPUTERNAME!
66
67:: Temp file for message
68if not "!TMP!" == "" set SMARTD_FULLMSGFILE=!TMP!\smartd_warning-!RANDOM!.txt
69if     "!TMP!" == "" set SMARTD_FULLMSGFILE=smartd_warning-!RANDOM!.txt
70
71:: Format message
72(
73  echo This message was generated by the smartd service running on:
74  echo.
75  echo.   host name:  !COMPUTERNAME!
76  if not "!USERDNSDOMAIN!" == "" echo.   DNS domain: !USERDNSDOMAIN!
77  if     "!USERDNSDOMAIN!" == "" echo.   DNS domain: [Empty]
78  if not "!USERDOMAIN!"    == "" echo.   Win domain: !USERDOMAIN!
79  echo.
80  echo The following warning/error was logged by the smartd service:
81  echo.
82  if not "!SMARTD_MESSAGE!" == "" echo !SMARTD_MESSAGE!
83  if     "!SMARTD_MESSAGE!" == "" echo [SMARTD_MESSAGE]
84  echo.
85  echo Device info:
86  if not "!SMARTD_DEVICEINFO!" == "" echo !SMARTD_DEVICEINFO!
87  if     "!SMARTD_DEVICEINFO!" == "" echo [SMARTD_DEVICEINFO]
88  echo.
89  echo For details see the event log or log file of smartd.
90  if not "!SMARTD_FAILTYPE!" == "EmailTest" (
91    echo.
92    echo You can also use the smartctl utility for further investigation.
93    if not "!SMARTD_PREVCNT!" == "0" echo The original message about this issue was sent at !SMARTD_TFIRST!
94    if "!SMARTD_NEXTDAYS!" == "" (
95      echo No additional messages about this problem will be sent.
96    ) else ( if "!SMARTD_NEXTDAYS!" == "1" (
97      echo Another message will be sent in 24 hours if the problem persists.
98    ) else (
99      echo Another message will be sent in !SMARTD_NEXTDAYS! days if the problem persists.
100    ))
101  )
102) > "!SMARTD_FULLMSGFILE!"
103if errorlevel 1 goto ERROR
104
105if not "!dryrun!" == "" (
106  echo !SMARTD_FULLMSGFILE!:
107  type "!SMARTD_FULLMSGFILE!"
108  echo --EOF--
109)
110
111:: Check first address
112set first=
113for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set first=%%a)
114set wtssend=
115if "!first!" == "console"   set wtssend=-c
116if "!first!" == "active"    set wtssend=-a
117if "!first!" == "connected" set wtssend=-s
118
119if not "!wtssend!" == "" (
120  :: Show Message box(es) via WTSSendMessage()
121  if not "!dryrun!" == "" (
122    echo call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - ^< "!SMARTD_FULLMSGFILE!"
123  ) else (
124    call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - < "!SMARTD_FULLMSGFILE!"
125    if errorlevel 1 set err=t
126  )
127  :: Remove first address
128  for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set SMARTD_ADDRESS=%%b)
129)
130
131:: Make comma separated address list
132set SMARTD_ADDRCSV=
133if not "!SMARTD_ADDRESS!" == "" set SMARTD_ADDRCSV=!SMARTD_ADDRESS: =,!
134
135:: Default mailer is smartd_mailer.ps1 (if configured) or blat.exe
136if not "!SMARTD_ADDRESS!" == "" if "!SMARTD_MAILER!" == "" (
137  if not exist smartd_mailer.conf.ps1 set SMARTD_MAILER=blat
138)
139
140:: Get mailer extension
141set ext=
142for /f "delims=" %%f in ("!SMARTD_MAILER!") do (set ext=%%~xf)
143
144:: Send mail or run command
145if "!ext!" == ".ps1" (
146
147  :: Run PowerShell script
148  if not "!dryrun!" == "" (
149    set esc=^^
150    echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command !esc!^& '!SMARTD_MAILER!' ^<nul
151  ) else (
152    PowerShell -NoProfile -ExecutionPolicy Bypass -Command ^& '!SMARTD_MAILER!' <nul
153    if errorlevel 1 set err=t
154  )
155
156) else ( if not "!SMARTD_ADDRCSV!" == "" (
157
158  :: Send mail
159  if "!SMARTD_MAILER!" == "" (
160
161    :: Use smartd_mailer.ps1
162    if not "!dryrun!" == "" (
163      echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 ^<nul
164      echo ==========
165    )
166    PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 !dryrun! <nul
167    if errorlevel 1 set err=t
168    if not "!dryrun!" == "" echo ==========
169
170  ) else (
171
172    :: Use blat mailer or compatible
173    if not "!dryrun!" == "" (
174      echo call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" ^< "!SMARTD_FULLMSGFILE!"
175    ) else (
176      call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" < "!SMARTD_FULLMSGFILE!"
177      if errorlevel 1 set err=t
178    )
179
180  )
181
182) else ( if not "!SMARTD_MAILER!" == "" (
183
184  :: Run command
185  if not "!dryrun!" == "" (
186    echo call "!SMARTD_MAILER!" ^<nul
187  ) else (
188    call "!SMARTD_MAILER!" <nul
189    if errorlevel 1 set err=t
190  )
191
192)))
193
194del "!SMARTD_FULLMSGFILE!" >nul 2>nul
195
196if not "!err!" == "" goto ERROR
197endlocal
198exit /b 0
199
200:ERROR
201endlocal
202exit /b 1
203