]>
Commit | Line | Data |
---|---|---|
ee38a438 GI |
1 | @echo off |
2 | :: | |
3 | :: smartd warning script | |
4 | :: | |
f9e10201 JD |
5 | :: Home page of code is: http://www.smartmontools.org |
6 | :: | |
7 | :: Copyright (C) 2012-17 Christian Franke | |
ee38a438 | 8 | :: |
ff28b140 | 9 | :: SPDX-License-Identifier: GPL-2.0-or-later |
ee38a438 | 10 | :: |
ff28b140 | 11 | :: $Id: smartd_warning.cmd 4760 2018-08-19 18:45:53Z chrfranke $ |
ee38a438 GI |
12 | :: |
13 | ||
f9e10201 JD |
14 | verify other 2>nul |
15 | setlocal enableextensions enabledelayedexpansion | |
16 | if errorlevel 1 goto UNSUPPORTED | |
ee38a438 GI |
17 | set err= |
18 | ||
f9e10201 JD |
19 | :: Change to script directory (not necessary if run from smartd service) |
20 | cd /d %~dp0 | |
21 | if errorlevel 1 goto ERROR | |
22 | ||
ee38a438 GI |
23 | :: Parse options |
24 | set dryrun= | |
25 | if "%1" == "--dryrun" ( | |
f9e10201 | 26 | set dryrun=--dryrun |
ee38a438 GI |
27 | shift |
28 | ) | |
f9e10201 | 29 | if not "!dryrun!" == "" echo cd /d !cd! |
ee38a438 GI |
30 | |
31 | if 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"' | |
f9e10201 | 36 | echo set SMARTD_ADDRESS='Space separated mail addresses, empty if none' |
ee38a438 GI |
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] | |
f9e10201 | 49 | goto ERROR |
ee38a438 GI |
50 | ) |
51 | ||
f9e10201 | 52 | if "!SMARTD_ADDRESS!!SMARTD_MAILER!" == "" ( |
ee38a438 | 53 | echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set |
f9e10201 | 54 | goto ERROR |
ee38a438 GI |
55 | ) |
56 | ||
57 | :: USERDNSDOMAIN may be unset if running as service | |
f9e10201 JD |
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 | |
ee38a438 | 60 | ) |
f9e10201 JD |
61 | :: Remove possible trailing \r appended by above command (requires %...%) |
62 | set USERDNSDOMAIN=%USERDNSDOMAIN% | |
ee38a438 GI |
63 | |
64 | :: Format subject | |
f9e10201 | 65 | set SMARTD_SUBJECT=SMART error (!SMARTD_FAILTYPE!) detected on host: !COMPUTERNAME! |
ee38a438 GI |
66 | |
67 | :: Temp file for message | |
f9e10201 JD |
68 | if not "!TMP!" == "" set SMARTD_FULLMSGFILE=!TMP!\smartd_warning-!RANDOM!.txt |
69 | if "!TMP!" == "" set SMARTD_FULLMSGFILE=smartd_warning-!RANDOM!.txt | |
ee38a438 GI |
70 | |
71 | :: Format message | |
72 | ( | |
73 | echo This message was generated by the smartd service running on: | |
74 | echo. | |
f9e10201 JD |
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! | |
ee38a438 GI |
79 | echo. |
80 | echo The following warning/error was logged by the smartd service: | |
81 | echo. | |
f9e10201 JD |
82 | if not "!SMARTD_MESSAGE!" == "" echo !SMARTD_MESSAGE! |
83 | if "!SMARTD_MESSAGE!" == "" echo [SMARTD_MESSAGE] | |
ee38a438 GI |
84 | echo. |
85 | echo Device info: | |
f9e10201 JD |
86 | if not "!SMARTD_DEVICEINFO!" == "" echo !SMARTD_DEVICEINFO! |
87 | if "!SMARTD_DEVICEINFO!" == "" echo [SMARTD_DEVICEINFO] | |
ee38a438 GI |
88 | echo. |
89 | echo For details see the event log or log file of smartd. | |
f9e10201 | 90 | if not "!SMARTD_FAILTYPE!" == "EmailTest" ( |
ee38a438 GI |
91 | echo. |
92 | echo You can also use the smartctl utility for further investigation. | |
f9e10201 JD |
93 | if not "!SMARTD_PREVCNT!" == "0" echo The original message about this issue was sent at !SMARTD_TFIRST! |
94 | if "!SMARTD_NEXTDAYS!" == "" ( | |
ee38a438 | 95 | echo No additional messages about this problem will be sent. |
f9e10201 | 96 | ) else ( if "!SMARTD_NEXTDAYS!" == "1" ( |
ee38a438 GI |
97 | echo Another message will be sent in 24 hours if the problem persists. |
98 | ) else ( | |
f9e10201 | 99 | echo Another message will be sent in !SMARTD_NEXTDAYS! days if the problem persists. |
ee38a438 GI |
100 | )) |
101 | ) | |
f9e10201 JD |
102 | ) > "!SMARTD_FULLMSGFILE!" |
103 | if errorlevel 1 goto ERROR | |
ee38a438 | 104 | |
f9e10201 JD |
105 | if not "!dryrun!" == "" ( |
106 | echo !SMARTD_FULLMSGFILE!: | |
107 | type "!SMARTD_FULLMSGFILE!" | |
ee38a438 GI |
108 | echo --EOF-- |
109 | ) | |
110 | ||
111 | :: Check first address | |
112 | set first= | |
f9e10201 | 113 | for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set first=%%a) |
ee38a438 | 114 | set wtssend= |
f9e10201 JD |
115 | if "!first!" == "console" set wtssend=-c |
116 | if "!first!" == "active" set wtssend=-a | |
117 | if "!first!" == "connected" set wtssend=-s | |
ee38a438 | 118 | |
f9e10201 | 119 | if not "!wtssend!" == "" ( |
ee38a438 | 120 | :: Show Message box(es) via WTSSendMessage() |
f9e10201 JD |
121 | if not "!dryrun!" == "" ( |
122 | echo call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - ^< "!SMARTD_FULLMSGFILE!" | |
ee38a438 | 123 | ) else ( |
f9e10201 | 124 | call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - < "!SMARTD_FULLMSGFILE!" |
ee38a438 GI |
125 | if errorlevel 1 set err=t |
126 | ) | |
127 | :: Remove first address | |
f9e10201 | 128 | for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set SMARTD_ADDRESS=%%b) |
ee38a438 | 129 | ) |
ee38a438 GI |
130 | |
131 | :: Make comma separated address list | |
132 | set SMARTD_ADDRCSV= | |
f9e10201 | 133 | if not "!SMARTD_ADDRESS!" == "" set SMARTD_ADDRCSV=!SMARTD_ADDRESS: =,! |
ee38a438 | 134 | |
f9e10201 JD |
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 | |
138 | ) | |
139 | ||
140 | :: Get mailer extension | |
141 | set ext= | |
142 | for /f "delims=" %%f in ("!SMARTD_MAILER!") do (set ext=%%~xf) | |
ee38a438 GI |
143 | |
144 | :: Send mail or run command | |
f9e10201 | 145 | if "!ext!" == ".ps1" ( |
ee38a438 | 146 | |
f9e10201 JD |
147 | :: Run PowerShell script |
148 | if not "!dryrun!" == "" ( | |
149 | set esc=^^ | |
150 | echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command !esc!^& '!SMARTD_MAILER!' ^<nul | |
ee38a438 | 151 | ) else ( |
f9e10201 JD |
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 | |
ee38a438 | 167 | if errorlevel 1 set err=t |
f9e10201 JD |
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 | ||
ee38a438 GI |
180 | ) |
181 | ||
f9e10201 | 182 | ) else ( if not "!SMARTD_MAILER!" == "" ( |
ee38a438 GI |
183 | |
184 | :: Run command | |
f9e10201 JD |
185 | if not "!dryrun!" == "" ( |
186 | echo call "!SMARTD_MAILER!" ^<nul | |
ee38a438 | 187 | ) else ( |
f9e10201 | 188 | call "!SMARTD_MAILER!" <nul |
ee38a438 GI |
189 | if errorlevel 1 set err=t |
190 | ) | |
191 | ||
f9e10201 JD |
192 | ))) |
193 | ||
194 | del "!SMARTD_FULLMSGFILE!" >nul 2>nul | |
ee38a438 | 195 | |
f9e10201 JD |
196 | if not "!err!" == "" goto ERROR |
197 | endlocal | |
198 | exit /b 0 | |
ee38a438 | 199 | |
f9e10201 JD |
200 | :ERROR |
201 | endlocal | |
202 | exit /b 1 |