]>
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 GI |
8 | :: |
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) | |
12 | :: any later version. | |
13 | :: | |
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/>. | |
16 | :: | |
f9e10201 | 17 | :: $Id: smartd_warning.cmd 4569 2017-10-25 20:13:11Z chrfranke $ |
ee38a438 GI |
18 | :: |
19 | ||
f9e10201 JD |
20 | verify other 2>nul |
21 | setlocal enableextensions enabledelayedexpansion | |
22 | if errorlevel 1 goto UNSUPPORTED | |
ee38a438 GI |
23 | set err= |
24 | ||
f9e10201 JD |
25 | :: Change to script directory (not necessary if run from smartd service) |
26 | cd /d %~dp0 | |
27 | if errorlevel 1 goto ERROR | |
28 | ||
ee38a438 GI |
29 | :: Parse options |
30 | set dryrun= | |
31 | if "%1" == "--dryrun" ( | |
f9e10201 | 32 | set dryrun=--dryrun |
ee38a438 GI |
33 | shift |
34 | ) | |
f9e10201 | 35 | if not "!dryrun!" == "" echo cd /d !cd! |
ee38a438 GI |
36 | |
37 | if not "%1" == "" ( | |
38 | echo smartd warning message script | |
39 | echo. | |
40 | echo Usage: | |
41 | echo set SMARTD_MAILER='Path to external script, empty for "blat"' | |
f9e10201 | 42 | echo set SMARTD_ADDRESS='Space separated mail addresses, empty if none' |
ee38a438 GI |
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' | |
53 | ||
54 | echo smartd_warning.cmd [--dryrun] | |
f9e10201 | 55 | goto ERROR |
ee38a438 GI |
56 | ) |
57 | ||
f9e10201 | 58 | if "!SMARTD_ADDRESS!!SMARTD_MAILER!" == "" ( |
ee38a438 | 59 | echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set |
f9e10201 | 60 | goto ERROR |
ee38a438 GI |
61 | ) |
62 | ||
63 | :: USERDNSDOMAIN may be unset if running as service | |
f9e10201 JD |
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 | |
ee38a438 | 66 | ) |
f9e10201 JD |
67 | :: Remove possible trailing \r appended by above command (requires %...%) |
68 | set USERDNSDOMAIN=%USERDNSDOMAIN% | |
ee38a438 GI |
69 | |
70 | :: Format subject | |
f9e10201 | 71 | set SMARTD_SUBJECT=SMART error (!SMARTD_FAILTYPE!) detected on host: !COMPUTERNAME! |
ee38a438 GI |
72 | |
73 | :: Temp file for message | |
f9e10201 JD |
74 | if not "!TMP!" == "" set SMARTD_FULLMSGFILE=!TMP!\smartd_warning-!RANDOM!.txt |
75 | if "!TMP!" == "" set SMARTD_FULLMSGFILE=smartd_warning-!RANDOM!.txt | |
ee38a438 GI |
76 | |
77 | :: Format message | |
78 | ( | |
79 | echo This message was generated by the smartd service running on: | |
80 | echo. | |
f9e10201 JD |
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! | |
ee38a438 GI |
85 | echo. |
86 | echo The following warning/error was logged by the smartd service: | |
87 | echo. | |
f9e10201 JD |
88 | if not "!SMARTD_MESSAGE!" == "" echo !SMARTD_MESSAGE! |
89 | if "!SMARTD_MESSAGE!" == "" echo [SMARTD_MESSAGE] | |
ee38a438 GI |
90 | echo. |
91 | echo Device info: | |
f9e10201 JD |
92 | if not "!SMARTD_DEVICEINFO!" == "" echo !SMARTD_DEVICEINFO! |
93 | if "!SMARTD_DEVICEINFO!" == "" echo [SMARTD_DEVICEINFO] | |
ee38a438 GI |
94 | echo. |
95 | echo For details see the event log or log file of smartd. | |
f9e10201 | 96 | if not "!SMARTD_FAILTYPE!" == "EmailTest" ( |
ee38a438 GI |
97 | echo. |
98 | echo You can also use the smartctl utility for further investigation. | |
f9e10201 JD |
99 | if not "!SMARTD_PREVCNT!" == "0" echo The original message about this issue was sent at !SMARTD_TFIRST! |
100 | if "!SMARTD_NEXTDAYS!" == "" ( | |
ee38a438 | 101 | echo No additional messages about this problem will be sent. |
f9e10201 | 102 | ) else ( if "!SMARTD_NEXTDAYS!" == "1" ( |
ee38a438 GI |
103 | echo Another message will be sent in 24 hours if the problem persists. |
104 | ) else ( | |
f9e10201 | 105 | echo Another message will be sent in !SMARTD_NEXTDAYS! days if the problem persists. |
ee38a438 GI |
106 | )) |
107 | ) | |
f9e10201 JD |
108 | ) > "!SMARTD_FULLMSGFILE!" |
109 | if errorlevel 1 goto ERROR | |
ee38a438 | 110 | |
f9e10201 JD |
111 | if not "!dryrun!" == "" ( |
112 | echo !SMARTD_FULLMSGFILE!: | |
113 | type "!SMARTD_FULLMSGFILE!" | |
ee38a438 GI |
114 | echo --EOF-- |
115 | ) | |
116 | ||
117 | :: Check first address | |
118 | set first= | |
f9e10201 | 119 | for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set first=%%a) |
ee38a438 | 120 | set wtssend= |
f9e10201 JD |
121 | if "!first!" == "console" set wtssend=-c |
122 | if "!first!" == "active" set wtssend=-a | |
123 | if "!first!" == "connected" set wtssend=-s | |
ee38a438 | 124 | |
f9e10201 | 125 | if not "!wtssend!" == "" ( |
ee38a438 | 126 | :: Show Message box(es) via WTSSendMessage() |
f9e10201 JD |
127 | if not "!dryrun!" == "" ( |
128 | echo call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - ^< "!SMARTD_FULLMSGFILE!" | |
ee38a438 | 129 | ) else ( |
f9e10201 | 130 | call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - < "!SMARTD_FULLMSGFILE!" |
ee38a438 GI |
131 | if errorlevel 1 set err=t |
132 | ) | |
133 | :: Remove first address | |
f9e10201 | 134 | for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set SMARTD_ADDRESS=%%b) |
ee38a438 | 135 | ) |
ee38a438 GI |
136 | |
137 | :: Make comma separated address list | |
138 | set SMARTD_ADDRCSV= | |
f9e10201 | 139 | if not "!SMARTD_ADDRESS!" == "" set SMARTD_ADDRCSV=!SMARTD_ADDRESS: =,! |
ee38a438 | 140 | |
f9e10201 JD |
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 | |
144 | ) | |
145 | ||
146 | :: Get mailer extension | |
147 | set ext= | |
148 | for /f "delims=" %%f in ("!SMARTD_MAILER!") do (set ext=%%~xf) | |
ee38a438 GI |
149 | |
150 | :: Send mail or run command | |
f9e10201 | 151 | if "!ext!" == ".ps1" ( |
ee38a438 | 152 | |
f9e10201 JD |
153 | :: Run PowerShell script |
154 | if not "!dryrun!" == "" ( | |
155 | set esc=^^ | |
156 | echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command !esc!^& '!SMARTD_MAILER!' ^<nul | |
ee38a438 | 157 | ) else ( |
f9e10201 JD |
158 | PowerShell -NoProfile -ExecutionPolicy Bypass -Command ^& '!SMARTD_MAILER!' <nul |
159 | if errorlevel 1 set err=t | |
160 | ) | |
161 | ||
162 | ) else ( if not "!SMARTD_ADDRCSV!" == "" ( | |
163 | ||
164 | :: Send mail | |
165 | if "!SMARTD_MAILER!" == "" ( | |
166 | ||
167 | :: Use smartd_mailer.ps1 | |
168 | if not "!dryrun!" == "" ( | |
169 | echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 ^<nul | |
170 | echo ========== | |
171 | ) | |
172 | PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 !dryrun! <nul | |
ee38a438 | 173 | if errorlevel 1 set err=t |
f9e10201 JD |
174 | if not "!dryrun!" == "" echo ========== |
175 | ||
176 | ) else ( | |
177 | ||
178 | :: Use blat mailer or compatible | |
179 | if not "!dryrun!" == "" ( | |
180 | echo call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" ^< "!SMARTD_FULLMSGFILE!" | |
181 | ) else ( | |
182 | call "!SMARTD_MAILER!" - -q -subject "!SMARTD_SUBJECT!" -to "!SMARTD_ADDRCSV!" < "!SMARTD_FULLMSGFILE!" | |
183 | if errorlevel 1 set err=t | |
184 | ) | |
185 | ||
ee38a438 GI |
186 | ) |
187 | ||
f9e10201 | 188 | ) else ( if not "!SMARTD_MAILER!" == "" ( |
ee38a438 GI |
189 | |
190 | :: Run command | |
f9e10201 JD |
191 | if not "!dryrun!" == "" ( |
192 | echo call "!SMARTD_MAILER!" ^<nul | |
ee38a438 | 193 | ) else ( |
f9e10201 | 194 | call "!SMARTD_MAILER!" <nul |
ee38a438 GI |
195 | if errorlevel 1 set err=t |
196 | ) | |
197 | ||
f9e10201 JD |
198 | ))) |
199 | ||
200 | del "!SMARTD_FULLMSGFILE!" >nul 2>nul | |
ee38a438 | 201 | |
f9e10201 JD |
202 | if not "!err!" == "" goto ERROR |
203 | endlocal | |
204 | exit /b 0 | |
ee38a438 | 205 | |
f9e10201 JD |
206 | :ERROR |
207 | endlocal | |
208 | exit /b 1 |