Annotation of embedaddon/smartmontools/os_win32/smartd_warning.cmd, revision 1.1.1.2

1.1       misho       1: @echo off
                      2: ::
                      3: :: smartd warning script
                      4: ::
1.1.1.2 ! misho       5: :: Copyright (C) 2012-13 Christian Franke <smartmontools-support@lists.sourceforge.net>
1.1       misho       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: ::
1.1.1.2 ! misho      15: :: $Id: smartd_warning.cmd 3816 2013-06-09 16:16:11Z chrfranke $
1.1       misho      16: ::
                     17: 
                     18: set err=
                     19: 
                     20: :: Parse options
                     21: set dryrun=
                     22: if "%1" == "--dryrun" (
                     23:   set dryrun=t
                     24:   shift
                     25: )
                     26: 
                     27: if not "%1" == "" (
                     28:   echo smartd warning message script
                     29:   echo.
                     30:   echo Usage:
                     31:   echo set SMARTD_MAILER='Path to external script, empty for "blat"'
                     32:   echo set SMARTD_ADDRESS='Space separated mail adresses, empty if none'
                     33:   echo set SMARTD_MESSAGE='Error Message'
                     34:   echo set SMARTD_FAILTYPE='Type of failure, "EMailTest" for tests'
                     35:   echo set SMARTD_TFIRST='Date of first message sent, empty if none'
1.1.1.2 ! misho      36:   echo :: set SMARTD_TFIRSTEPOCH='time_t format of above'
1.1       misho      37:   echo set SMARTD_PREVCNT='Number of previous messages, 0 if none'
                     38:   echo set SMARTD_NEXTDAYS='Number of days until next message, empty if none'
                     39:   echo set SMARTD_DEVICEINFO='Device identify information'
                     40:   echo :: set SMARTD_DEVICE='Device name'
                     41:   echo :: set SMARTD_DEVICESTRING='Annotated device name'
                     42:   echo :: set SMARTD_DEVICETYPE='Device type from -d directive, "auto" if none'
                     43: 
                     44:   echo smartd_warning.cmd [--dryrun]
                     45:   goto EOF
                     46: )
                     47: 
                     48: if "%SMARTD_ADDRESS%%SMARTD_MAILER%" == "" (
                     49:   echo smartd_warning.cmd: SMARTD_ADDRESS or SMARTD_MAILER must be set
                     50:   goto EOF
                     51: )
                     52: 
1.1.1.2 ! misho      53: :: USERDNSDOMAIN may be unset if running as service
        !            54: if "%USERDNSDOMAIN%" == "" (
        !            55:   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
        !            56: )
        !            57: 
1.1       misho      58: :: Format subject
                     59: set SMARTD_SUBJECT=SMART error (%SMARTD_FAILTYPE%) detected on host: %COMPUTERNAME%
                     60: 
                     61: :: Temp file for message
1.1.1.2 ! misho      62: if not "%TMP%" == "" set SMARTD_FULLMSGFILE=%TMP%\smartd_warning-%RANDOM%.txt
        !            63: if     "%TMP%" == "" set SMARTD_FULLMSGFILE=smartd_warning-%RANDOM%.txt
1.1       misho      64: 
                     65: :: Format message
                     66: (
                     67:   echo This message was generated by the smartd service running on:
                     68:   echo.
                     69:   echo.   host name:  %COMPUTERNAME%
                     70:   if not "%USERDNSDOMAIN%" == "" echo.   DNS domain: %USERDNSDOMAIN%
                     71:   if     "%USERDNSDOMAIN%" == "" echo.   DNS domain: [Empty]
                     72:   if not "%USERDOMAIN%"    == "" echo.   Win domain: %USERDOMAIN%
                     73:   echo.
                     74:   echo The following warning/error was logged by the smartd service:
                     75:   echo.
                     76:   :: SMARTD_MESSAGE and SMARTD_DEVICEINFO may contain parentheses
                     77:   for %%m in ("%SMARTD_MESSAGE%") do echo.%%~m
                     78:   echo.
                     79:   echo Device info:
                     80:   for %%m in ("%SMARTD_DEVICEINFO%") do echo.%%~m
                     81:   set m=
                     82:   echo.
                     83:   echo For details see the event log or log file of smartd.
                     84:   if not "%SMARTD_FAILTYPE%" == "EmailTest" (
                     85:     echo.
                     86:     echo You can also use the smartctl utility for further investigation.
                     87:     if not "%SMARTD_PREVCNT%" == "0" echo The original message about this issue was sent at %SMARTD_TFIRST%
                     88:     if "%SMARTD_NEXTDAYS%" == "" (
                     89:       echo No additional messages about this problem will be sent.
                     90:     ) else ( if "%SMARTD_NEXTDAYS%" == "1" (
                     91:       echo Another message will be sent in 24 hours if the problem persists.
                     92:     ) else (
                     93:       echo Another message will be sent in %SMARTD_NEXTDAYS% days if the problem persists.
                     94:     ))
                     95:   )
                     96: ) > "%SMARTD_FULLMSGFILE%"
                     97: 
                     98: if not "%dryrun%" == "" (
                     99:   echo %SMARTD_FULLMSGFILE%:
                    100:   type "%SMARTD_FULLMSGFILE%"
                    101:   echo --EOF--
                    102: )
                    103: 
                    104: :: Check first address
                    105: set first=
                    106: for /F "tokens=1*" %%a in ("%SMARTD_ADDRESS%") do (set first=%%a)
                    107: set wtssend=
                    108: if "%first%" == "console"   set wtssend=-c
                    109: if "%first%" == "active"    set wtssend=-a
                    110: if "%first%" == "connected" set wtssend=-s
                    111: set first=
                    112: 
                    113: if not "%wtssend%" == "" (
                    114:   :: Show Message box(es) via WTSSendMessage()
                    115:   if not "%dryrun%" == "" (
                    116:     echo call wtssendmsg %wtssend% "%SMARTD_SUBJECT%" - ^< "%SMARTD_FULLMSGFILE%"
                    117:   ) else (
                    118:     call wtssendmsg %wtssend% "%SMARTD_SUBJECT%" - < "%SMARTD_FULLMSGFILE%"
                    119:     if errorlevel 1 set err=t
                    120:   )
                    121:   :: Remove first address
                    122:   for /F "tokens=1*" %%a in ("%SMARTD_ADDRESS%") do (set SMARTD_ADDRESS=%%b)
                    123: )
                    124: set wtssend=
                    125: 
                    126: :: Make comma separated address list
                    127: set SMARTD_ADDRCSV=
                    128: if not "%SMARTD_ADDRESS%" == "" set SMARTD_ADDRCSV=%SMARTD_ADDRESS: =,%
                    129: 
                    130: :: Use blat mailer by default
                    131: if not "%SMARTD_ADDRESS%" == "" if "%SMARTD_MAILER%" == "" set SMARTD_MAILER=blat
                    132: 
                    133: :: Send mail or run command
                    134: if not "%SMARTD_ADDRCSV%" == "" (
                    135: 
                    136:   :: Send mail
                    137:   if not "%dryrun%" == "" (
                    138:     echo call "%SMARTD_MAILER%" - -q -subject "%SMARTD_SUBJECT%" -to "%SMARTD_ADDRCSV%" ^< "%SMARTD_FULLMSGFILE%"
                    139:   ) else (
                    140:     call "%SMARTD_MAILER%" - -q -subject "%SMARTD_SUBJECT%" -to "%SMARTD_ADDRCSV%" < "%SMARTD_FULLMSGFILE%"
                    141:     if errorlevel 1 set err=t
                    142:   )
                    143: 
                    144: ) else ( if not "%SMARTD_MAILER%" == "" (
                    145: 
                    146:   :: Run command
                    147:   if not "%dryrun%" == "" (
                    148:     echo call "%SMARTD_MAILER%" ^<nul
                    149:   ) else (
                    150:     call "%SMARTD_MAILER%" <nul
                    151:     if errorlevel 1 set err=t
                    152:   )
                    153: 
                    154: ))
                    155: 
                    156: del "%SMARTD_FULLMSGFILE%" >nul 2>nul
                    157: 
                    158: :EOF
                    159: if not "%err%" == "" goto ERROR 2>nul

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>