]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - os_win32/smartd_warning.cmd
9b2d178fce1f2511ab5f9855da3fea9168b5b5c9
[mirror_smartmontools-debian.git] / os_win32 / smartd_warning.cmd
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 :: 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 ::
17 :: $Id: smartd_warning.cmd 4569 2017-10-25 20:13:11Z chrfranke $
18 ::
19
20 verify other 2>nul
21 setlocal enableextensions enabledelayedexpansion
22 if errorlevel 1 goto UNSUPPORTED
23 set err=
24
25 :: Change to script directory (not necessary if run from smartd service)
26 cd /d %~dp0
27 if errorlevel 1 goto ERROR
28
29 :: Parse options
30 set dryrun=
31 if "%1" == "--dryrun" (
32 set dryrun=--dryrun
33 shift
34 )
35 if not "!dryrun!" == "" echo cd /d !cd!
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"'
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'
53
54 echo smartd_warning.cmd [--dryrun]
55 goto ERROR
56 )
57
58 if "!SMARTD_ADDRESS!!SMARTD_MAILER!" == "" (
59 echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set
60 goto ERROR
61 )
62
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
66 )
67 :: Remove possible trailing \r appended by above command (requires %...%)
68 set USERDNSDOMAIN=%USERDNSDOMAIN%
69
70 :: Format subject
71 set SMARTD_SUBJECT=SMART error (!SMARTD_FAILTYPE!) detected on host: !COMPUTERNAME!
72
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
76
77 :: Format message
78 (
79 echo This message was generated by the smartd service running on:
80 echo.
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!
85 echo.
86 echo The following warning/error was logged by the smartd service:
87 echo.
88 if not "!SMARTD_MESSAGE!" == "" echo !SMARTD_MESSAGE!
89 if "!SMARTD_MESSAGE!" == "" echo [SMARTD_MESSAGE]
90 echo.
91 echo Device info:
92 if not "!SMARTD_DEVICEINFO!" == "" echo !SMARTD_DEVICEINFO!
93 if "!SMARTD_DEVICEINFO!" == "" echo [SMARTD_DEVICEINFO]
94 echo.
95 echo For details see the event log or log file of smartd.
96 if not "!SMARTD_FAILTYPE!" == "EmailTest" (
97 echo.
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.
104 ) else (
105 echo Another message will be sent in !SMARTD_NEXTDAYS! days if the problem persists.
106 ))
107 )
108 ) > "!SMARTD_FULLMSGFILE!"
109 if errorlevel 1 goto ERROR
110
111 if not "!dryrun!" == "" (
112 echo !SMARTD_FULLMSGFILE!:
113 type "!SMARTD_FULLMSGFILE!"
114 echo --EOF--
115 )
116
117 :: Check first address
118 set first=
119 for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set first=%%a)
120 set wtssend=
121 if "!first!" == "console" set wtssend=-c
122 if "!first!" == "active" set wtssend=-a
123 if "!first!" == "connected" set wtssend=-s
124
125 if not "!wtssend!" == "" (
126 :: Show Message box(es) via WTSSendMessage()
127 if not "!dryrun!" == "" (
128 echo call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - ^< "!SMARTD_FULLMSGFILE!"
129 ) else (
130 call .\wtssendmsg !wtssend! "!SMARTD_SUBJECT!" - < "!SMARTD_FULLMSGFILE!"
131 if errorlevel 1 set err=t
132 )
133 :: Remove first address
134 for /f "tokens=1*" %%a in ("!SMARTD_ADDRESS!") do (set SMARTD_ADDRESS=%%b)
135 )
136
137 :: Make comma separated address list
138 set SMARTD_ADDRCSV=
139 if not "!SMARTD_ADDRESS!" == "" set SMARTD_ADDRCSV=!SMARTD_ADDRESS: =,!
140
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)
149
150 :: Send mail or run command
151 if "!ext!" == ".ps1" (
152
153 :: Run PowerShell script
154 if not "!dryrun!" == "" (
155 set esc=^^
156 echo PowerShell -NoProfile -ExecutionPolicy Bypass -Command !esc!^& '!SMARTD_MAILER!' ^<nul
157 ) else (
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
173 if errorlevel 1 set err=t
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
186 )
187
188 ) else ( if not "!SMARTD_MAILER!" == "" (
189
190 :: Run command
191 if not "!dryrun!" == "" (
192 echo call "!SMARTD_MAILER!" ^<nul
193 ) else (
194 call "!SMARTD_MAILER!" <nul
195 if errorlevel 1 set err=t
196 )
197
198 )))
199
200 del "!SMARTD_FULLMSGFILE!" >nul 2>nul
201
202 if not "!err!" == "" goto ERROR
203 endlocal
204 exit /b 0
205
206 :ERROR
207 endlocal
208 exit /b 1