1 | @echo off |
---|
2 | :: |
---|
3 | :: smartd warning script |
---|
4 | :: |
---|
5 | :: Copyright (C) 2012-16 Christian Franke |
---|
6 | :: |
---|
7 | :: This program is free software; you can redistribute it and/or modify |
---|
8 | :: it under the terms of the GNU General Public License as published by |
---|
9 | :: the Free Software Foundation; either version 2, or (at your option) |
---|
10 | :: any later version. |
---|
11 | :: |
---|
12 | :: You should have received a copy of the GNU General Public License |
---|
13 | :: (for example COPYING); If not, see <http://www.gnu.org/licenses/>. |
---|
14 | :: |
---|
15 | :: $Id: smartd_warning.cmd 3816M 2013-06-09 16:16:11Z chrfranke $ |
---|
16 | :: |
---|
17 | |
---|
18 | setlocal |
---|
19 | set err= |
---|
20 | |
---|
21 | :: Change to script directory (not necessary if run from smartd service) |
---|
22 | cd /d %~dp0 |
---|
23 | if errorlevel 1 goto ERROR |
---|
24 | |
---|
25 | :: Parse options |
---|
26 | set dryrun= |
---|
27 | if "%1" == "--dryrun" ( |
---|
28 | set dryrun=--dryrun |
---|
29 | echo cd /d %~dp0 |
---|
30 | shift |
---|
31 | ) |
---|
32 | |
---|
33 | if not "%1" == "" ( |
---|
34 | echo smartd warning message script |
---|
35 | echo. |
---|
36 | echo Usage: |
---|
37 | echo set SMARTD_MAILER='Path to external script, empty for "blat"' |
---|
38 | echo set SMARTD_ADDRESS='Space separated mail addresses, empty if none' |
---|
39 | echo set SMARTD_MESSAGE='Error Message' |
---|
40 | echo set SMARTD_FAILTYPE='Type of failure, "EMailTest" for tests' |
---|
41 | echo set SMARTD_TFIRST='Date of first message sent, empty if none' |
---|
42 | echo :: set SMARTD_TFIRSTEPOCH='time_t format of above' |
---|
43 | echo set SMARTD_PREVCNT='Number of previous messages, 0 if none' |
---|
44 | echo set SMARTD_NEXTDAYS='Number of days until next message, empty if none' |
---|
45 | echo set SMARTD_DEVICEINFO='Device identify information' |
---|
46 | echo :: set SMARTD_DEVICE='Device name' |
---|
47 | echo :: set SMARTD_DEVICESTRING='Annotated device name' |
---|
48 | echo :: set SMARTD_DEVICETYPE='Device type from -d directive, "auto" if none' |
---|
49 | |
---|
50 | echo smartd_warning.cmd [--dryrun] |
---|
51 | goto ERROR |
---|
52 | ) |
---|
53 | |
---|
54 | if "%SMARTD_ADDRESS%%SMARTD_MAILER%" == "" ( |
---|
55 | echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set |
---|
56 | goto ERROR |
---|
57 | ) |
---|
58 | |
---|
59 | :: USERDNSDOMAIN may be unset if running as service |
---|
60 | if "%USERDNSDOMAIN%" == "" ( |
---|
61 | for /f "delims== tokens=2 usebackq" %%d in (`WMIC PATH Win32_Computersystem WHERE "PartOfDomain=TRUE" GET Domain /VALUE 2^>nul ^| find "Domain=" 2^>nul`) do set USERDNSDOMAIN=%%~d |
---|
62 | ) |
---|
63 | |
---|
64 | :: Format subject |
---|
65 | set SMARTD_SUBJECT=SMART error (%SMARTD_FAILTYPE%) detected on host: %COMPUTERNAME% |
---|
66 | |
---|
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 |
---|
70 | |
---|
71 | :: Format message |
---|
72 | ( |
---|
73 | echo This message was generated by the smartd service running on: |
---|
74 | echo. |
---|
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% |
---|
79 | echo. |
---|
80 | echo The following warning/error was logged by the smartd service: |
---|
81 | echo. |
---|
82 | :: SMARTD_MESSAGE and SMARTD_DEVICEINFO may contain parentheses |
---|
83 | for %%m in ("%SMARTD_MESSAGE%") do echo.%%~m |
---|
84 | echo. |
---|
85 | echo Device info: |
---|
86 | for %%m in ("%SMARTD_DEVICEINFO%") do echo.%%~m |
---|
87 | set m= |
---|
88 | echo. |
---|
89 | echo For details see the event log or log file of smartd. |
---|
90 | if not "%SMARTD_FAILTYPE%" == "EmailTest" ( |
---|
91 | echo. |
---|
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. |
---|
98 | ) else ( |
---|
99 | echo Another message will be sent in %SMARTD_NEXTDAYS% days if the problem persists. |
---|
100 | )) |
---|
101 | ) |
---|
102 | ) > "%SMARTD_FULLMSGFILE%" |
---|
103 | if errorlevel 1 goto ERROR |
---|
104 | |
---|
105 | if not "%dryrun%" == "" ( |
---|
106 | echo %SMARTD_FULLMSGFILE%: |
---|
107 | type "%SMARTD_FULLMSGFILE%" |
---|
108 | echo --EOF-- |
---|
109 | ) |
---|
110 | |
---|
111 | :: Check first address |
---|
112 | set first= |
---|
113 | for /F "tokens=1*" %%a in ("%SMARTD_ADDRESS%") do (set first=%%a) |
---|
114 | set wtssend= |
---|
115 | if "%first%" == "console" set wtssend=-c |
---|
116 | if "%first%" == "active" set wtssend=-a |
---|
117 | if "%first%" == "connected" set wtssend=-s |
---|
118 | |
---|
119 | if not "%wtssend%" == "" ( |
---|
120 | :: Show Message box(es) via WTSSendMessage() |
---|
121 | if not "%dryrun%" == "" ( |
---|
122 | echo call wtssendmsg %wtssend% "%SMARTD_SUBJECT%" - ^< "%SMARTD_FULLMSGFILE%" |
---|
123 | ) else ( |
---|
124 | call wtssendmsg %wtssend% "%SMARTD_SUBJECT%" - < "%SMARTD_FULLMSGFILE%" |
---|
125 | if errorlevel 1 set err=t |
---|
126 | ) |
---|
127 | :: Remove first address |
---|
128 | for /F "tokens=1*" %%a in ("%SMARTD_ADDRESS%") do (set SMARTD_ADDRESS=%%b) |
---|
129 | ) |
---|
130 | |
---|
131 | :: Make comma separated address list |
---|
132 | set SMARTD_ADDRCSV= |
---|
133 | if not "%SMARTD_ADDRESS%" == "" set SMARTD_ADDRCSV=%SMARTD_ADDRESS: =,% |
---|
134 | |
---|
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 | :: Send mail or run command |
---|
141 | if "%SMARTD_MAILER%" == "" ( |
---|
142 | |
---|
143 | :: Send mail via smartd_mailer.ps1 |
---|
144 | if not "%dryrun%" == "" ( |
---|
145 | echo PowerShell -NoProfile -ExecutionPolicy Bypass .\smartd_mailer.ps1 |
---|
146 | echo ========== |
---|
147 | ) |
---|
148 | PowerShell -NoProfile -ExecutionPolicy Bypass -Command .\smartd_mailer.ps1 %dryrun% |
---|
149 | if errorlevel 1 set err=t |
---|
150 | if not "%dryrun%" == "" echo ========== |
---|
151 | |
---|
152 | ) else ( if not "%SMARTD_ADDRCSV%" == "" ( |
---|
153 | |
---|
154 | :: Send mail via blat mailer or compatible |
---|
155 | if not "%dryrun%" == "" ( |
---|
156 | echo call "%SMARTD_MAILER%" - -q -subject "%SMARTD_SUBJECT%" -to "%SMARTD_ADDRCSV%" ^< "%SMARTD_FULLMSGFILE%" |
---|
157 | ) else ( |
---|
158 | call "%SMARTD_MAILER%" - -q -subject "%SMARTD_SUBJECT%" -to "%SMARTD_ADDRCSV%" < "%SMARTD_FULLMSGFILE%" |
---|
159 | if errorlevel 1 set err=t |
---|
160 | ) |
---|
161 | |
---|
162 | ) else ( if not "%SMARTD_MAILER%" == "" ( |
---|
163 | |
---|
164 | :: Run command |
---|
165 | if not "%dryrun%" == "" ( |
---|
166 | echo call "%SMARTD_MAILER%" ^<nul |
---|
167 | ) else ( |
---|
168 | call "%SMARTD_MAILER%" <nul |
---|
169 | if errorlevel 1 set err=t |
---|
170 | ) |
---|
171 | |
---|
172 | ))) |
---|
173 | |
---|
174 | del "%SMARTD_FULLMSGFILE%" >nul 2>nul |
---|
175 | |
---|
176 | if not "%err%" == "" goto ERROR |
---|
177 | endlocal |
---|
178 | exit /b 0 |
---|
179 | |
---|
180 | :ERROR |
---|
181 | endlocal |
---|
182 | exit /b 1 |
---|