3 :: smartd warning script
5 :: Home page of code is: http://www.smartmontools.org
7 :: Copyright (C) 2012-17 Christian Franke
9 :: This program is free software; you can redistribute it and/or modify
10 :: it under the terms of the GNU General Public License as published by
11 :: the Free Software Foundation; either version 2, or (at your option)
14 :: You should have received a copy of the GNU General Public License
15 :: (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
17 :: $Id: smartd_warning.cmd 4569 2017-10-25 20:13:11Z chrfranke $
21 setlocal enableextensions enabledelayedexpansion
22 if errorlevel 1 goto UNSUPPORTED
25 :: Change to script directory (not necessary if run from smartd service)
27 if errorlevel 1 goto ERROR
31 if "%1" == "--dryrun" (
35 if not "!dryrun!" == "" echo cd /d !cd!
38 echo smartd warning message script
41 echo set SMARTD_MAILER='Path to external script, empty for "blat"'
42 echo set SMARTD_ADDRESS='Space separated mail addresses, empty if none'
43 echo set SMARTD_MESSAGE='Error Message'
44 echo set SMARTD_FAILTYPE='Type of failure, "EMailTest" for tests'
45 echo set SMARTD_TFIRST='Date of first message sent, empty if none'
46 echo :: set SMARTD_TFIRSTEPOCH='time_t format of above'
47 echo set SMARTD_PREVCNT='Number of previous messages, 0 if none'
48 echo set SMARTD_NEXTDAYS='Number of days until next message, empty if none'
49 echo set SMARTD_DEVICEINFO='Device identify information'
50 echo :: set SMARTD_DEVICE='Device name'
51 echo :: set SMARTD_DEVICESTRING='Annotated device name'
52 echo :: set SMARTD_DEVICETYPE='Device type from -d directive, "auto" if none'
54 echo smartd_warning.cmd [--dryrun]
58 if "!SMARTD_ADDRESS!!SMARTD_MAILER!" == "" (
59 echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set
63 :: USERDNSDOMAIN may be unset if running as service
64 if "!USERDNSDOMAIN!" == "" (
65 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
67 :: Remove possible trailing \r appended by above command (requires %...%)
68 set USERDNSDOMAIN=%USERDNSDOMAIN%
71 set SMARTD_SUBJECT=SMART error (!SMARTD_FAILTYPE!) detected on host: !COMPUTERNAME!
73 :: Temp file for message
74 if not "!TMP!" == "" set SMARTD_FULLMSGFILE=!TMP!\smartd_warning-!RANDOM!.txt
75 if "!TMP!" == "" set SMARTD_FULLMSGFILE=smartd_warning-!RANDOM!.txt
79 echo This message was generated by the smartd service running on:
81 echo. host name: !COMPUTERNAME!
82 if not "!USERDNSDOMAIN!" == "" echo. DNS domain: !USERDNSDOMAIN!
83 if "!USERDNSDOMAIN!" == "" echo. DNS domain: [Empty]
84 if not "!USERDOMAIN!" == "" echo. Win domain: !USERDOMAIN!
86 echo The following warning/error was logged by the smartd service:
88 if not "!SMARTD_MESSAGE!" == "" echo !SMARTD_MESSAGE!
89 if "!SMARTD_MESSAGE!" == "" echo [SMARTD_MESSAGE]
92 if not "!SMARTD_DEVICEINFO!" == "" echo !SMARTD_DEVICEINFO!
93 if "!SMARTD_DEVICEINFO!" == "" echo [SMARTD_DEVICEINFO]
95 echo For details see the event log or log file of smartd.
96 if not "!SMARTD_FAILTYPE!" == "EmailTest" (
98 echo You can also use the smartctl utility for further investigation.
99 if not "!SMARTD_PREVCNT!" == "0" echo The original message about this issue was sent at !SMARTD_TFIRST!
100 if "!SMARTD_NEXTDAYS!" == "" (
101 echo No additional messages about this problem will be sent.
102 ) else ( if "!SMARTD_NEXTDAYS!" == "1" (
103 echo Another message will be sent in 24 hours if the problem persists.
105 echo Another message will be sent in !SMARTD_NEXTDAYS! days if the problem persists.
108 ) > "!SMARTD_FULLMSGFILE!"
109 if errorlevel 1 goto ERROR
111 if not "!dryrun!" == "" (
112 echo !SMARTD_FULLMSGFILE!:
113 type "!SMARTD_FULLMSGFILE!"
117 :: Check first address
119 for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set first=%%a)
121 if "!first!" == "console" set wtssend=-c
122 if "!first!" == "active" set wtssend=-a
123 if "!first!" == "connected" set wtssend=-s
125 if not "!wtssend!" == "" (
126 :: Show Message box(es) via WTSSendMessage()
127 if not "!dryrun!" == "" (
128 echo call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - ^< "!SMARTD_FULLMSGFILE!"
130 call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - < "!SMARTD_FULLMSGFILE!"
131 if errorlevel 1 set err=t
133 :: Remove first address
134 for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set SMARTD_ADDRESS=%%b)
137 :: Make comma separated address list
139 if not "!SMARTD_ADDRESS!" == "" set SMARTD_ADDRCSV=!SMARTD_ADDRESS: =,!
141 :: Default mailer is smartd_mailer.ps1 (if configured) or blat.exe
142 if not "!SMARTD_ADDRESS!" == "" if "!SMARTD_MAILER!" == "" (
143 if not exist smartd_mailer.conf.ps1 set SMARTD_MAILER=blat
146 :: Get mailer extension
148 for /f "delims=" %%f in ("!SMARTD_MAILER!") do (set ext=%%~xf)
150 :: Send mail or run command
151 if "!ext!" == ".ps1" (
153 :: Run PowerShell script
154 if not "!dryrun!" == "" (
156 echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command !esc!^& '!SMARTD_MAILER!' ^<nul
158 PowerShell -NoProfile -ExecutionPolicy Bypass -Command ^& '!SMARTD_MAILER!' <nul
159 if errorlevel 1 set err=t
162 ) else ( if not "!SMARTD_ADDRCSV!" == "" (
165 if "!SMARTD_MAILER!" == "" (
167 :: Use smartd_mailer.ps1
168 if not "!dryrun!" == "" (
169 echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 ^<nul
172 PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 !dryrun! <nul
173 if errorlevel 1 set err=t
174 if not "!dryrun!" == "" echo ==========
178 :: Use blat mailer or compatible
179 if not "!dryrun!" == "" (
180 echo call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" ^< "!SMARTD_FULLMSGFILE!"
182 call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" < "!SMARTD_FULLMSGFILE!"
183 if errorlevel 1 set err=t
188 ) else ( if not "!SMARTD_MAILER!" == "" (
191 if not "!dryrun!" == "" (
192 echo call "!SMARTD_MAILER!" ^<nul
194 call "!SMARTD_MAILER!" <nul
195 if errorlevel 1 set err=t
200 del "!SMARTD_FULLMSGFILE!" >nul 2>nul
202 if not "!err!" == "" goto ERROR