3 :: smartd warning script
5 :: Home page of code is: http://www.smartmontools.org
7 :: Copyright (C) 2012-17 Christian Franke
9 :: SPDX-License-Identifier: GPL-2.0-or-later
11 :: $Id: smartd_warning.cmd 4760 2018-08-19 18:45:53Z chrfranke $
15 setlocal enableextensions enabledelayedexpansion
16 if errorlevel 1 goto UNSUPPORTED
19 :: Change to script directory (not necessary if run from smartd service)
21 if errorlevel 1 goto ERROR
25 if "%1" == "--dryrun" (
29 if not "!dryrun!" == "" echo cd /d !cd!
32 echo smartd warning message script
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'
48 echo smartd_warning.cmd [--dryrun]
52 if "!SMARTD_ADDRESS!!SMARTD_MAILER!" == "" (
53 echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set
57 :: USERDNSDOMAIN may be unset if running as service
58 if "!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
61 :: Remove possible trailing \r appended by above command (requires %...%)
62 set USERDNSDOMAIN=%USERDNSDOMAIN%
65 set SMARTD_SUBJECT=SMART error (!SMARTD_FAILTYPE!) detected on host: !COMPUTERNAME!
67 :: Temp file for message
68 if not "!TMP!" == "" set SMARTD_FULLMSGFILE=!TMP!\smartd_warning-!RANDOM!.txt
69 if "!TMP!" == "" set SMARTD_FULLMSGFILE=smartd_warning-!RANDOM!.txt
73 echo This message was generated by the smartd service running on:
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!
80 echo The following warning/error was logged by the smartd service:
82 if not "!SMARTD_MESSAGE!" == "" echo !SMARTD_MESSAGE!
83 if "!SMARTD_MESSAGE!" == "" echo [SMARTD_MESSAGE]
86 if not "!SMARTD_DEVICEINFO!" == "" echo !SMARTD_DEVICEINFO!
87 if "!SMARTD_DEVICEINFO!" == "" echo [SMARTD_DEVICEINFO]
89 echo For details see the event log or log file of smartd.
90 if not "!SMARTD_FAILTYPE!" == "EmailTest" (
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.
99 echo Another message will be sent in !SMARTD_NEXTDAYS! days if the problem persists.
102 ) > "!SMARTD_FULLMSGFILE!"
103 if errorlevel 1 goto ERROR
105 if not "!dryrun!" == "" (
106 echo !SMARTD_FULLMSGFILE!:
107 type "!SMARTD_FULLMSGFILE!"
111 :: Check first address
113 for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set first=%%a)
115 if "!first!" == "console" set wtssend=-c
116 if "!first!" == "active" set wtssend=-a
117 if "!first!" == "connected" set wtssend=-s
119 if not "!wtssend!" == "" (
120 :: Show Message box(es) via WTSSendMessage()
121 if not "!dryrun!" == "" (
122 echo call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - ^< "!SMARTD_FULLMSGFILE!"
124 call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - < "!SMARTD_FULLMSGFILE!"
125 if errorlevel 1 set err=t
127 :: Remove first address
128 for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set SMARTD_ADDRESS=%%b)
131 :: Make comma separated address list
133 if not "!SMARTD_ADDRESS!" == "" set SMARTD_ADDRCSV=!SMARTD_ADDRESS: =,!
135 :: Default mailer is smartd_mailer.ps1 (if configured) or blat.exe
136 if not "!SMARTD_ADDRESS!" == "" if "!SMARTD_MAILER!" == "" (
137 if not exist smartd_mailer.conf.ps1 set SMARTD_MAILER=blat
140 :: Get mailer extension
142 for /f "delims=" %%f in ("!SMARTD_MAILER!") do (set ext=%%~xf)
144 :: Send mail or run command
145 if "!ext!" == ".ps1" (
147 :: Run PowerShell script
148 if not "!dryrun!" == "" (
150 echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command !esc!^& '!SMARTD_MAILER!' ^<nul
152 PowerShell -NoProfile -ExecutionPolicy Bypass -Command ^& '!SMARTD_MAILER!' <nul
153 if errorlevel 1 set err=t
156 ) else ( if not "!SMARTD_ADDRCSV!" == "" (
159 if "!SMARTD_MAILER!" == "" (
161 :: Use smartd_mailer.ps1
162 if not "!dryrun!" == "" (
163 echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 ^<nul
166 PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 !dryrun! <nul
167 if errorlevel 1 set err=t
168 if not "!dryrun!" == "" echo ==========
172 :: Use blat mailer or compatible
173 if not "!dryrun!" == "" (
174 echo call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" ^< "!SMARTD_FULLMSGFILE!"
176 call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" < "!SMARTD_FULLMSGFILE!"
177 if errorlevel 1 set err=t
182 ) else ( if not "!SMARTD_MAILER!" == "" (
185 if not "!dryrun!" == "" (
186 echo call "!SMARTD_MAILER!" ^<nul
188 call "!SMARTD_MAILER!" <nul
189 if errorlevel 1 set err=t
194 del "!SMARTD_FULLMSGFILE!" >nul 2>nul
196 if not "!err!" == "" goto ERROR