go to: sambar.de - code - d/lsfail.bat.zipwait.exe 

fail.bat V0.2 (01-22-2003)

There was a request for a fool proof server restart feature for the Sambar Server to implement. Well, I didn't implement it in the core, that's Tod's job ;) but I wrote a little .BAT file as a temporary workaround for this. In fact, it's not a small one, as it consists of 256 Rows, but it's functionality is rather simple. This can surely be done in Delphi, Perl, C++ and whichever language you can think of, but a .BAT file does it, too. And of course one might think of a cleaner solution, esp. concerning logging. If you got improvements, suggestions or encountered errors, please let me now.

It contains a hopefully explanatory inline help, which surely is written in clear text beneath, too. So I'll try to keep it short now and just drop a few notes on installation and general usage.

Copy'n'paste the code beneath to your favourite text editor. Don't forget to NOW change the settings under the label :customizable_settings to suit your needs! Save it to a file called fail.bat in your /bin directory (usually c:\sambar52\bin, could also be d:\sambar\bin f.e., depends on where you installed).

Then open a DOS box and change to that very directoy. Type fail, and you get the short inline help. Type fail /help /p, and you'll get 2 pages of helpful information. Important: Set your DOS-box to 120x40 for best view! You can do this by clicking in the upper left corner and chosing "Properties...". Now you can set the window size to 120x40 or even bigger.

Surely you can use a pipe (fail /help >failhelp.txt) to redirect the inline help content to a text file and then print it out or open it in your text editor to provide more convenient reading.

If you want to first simulate the behaviour, use the /nc switch. If you don't want to change anything on your installation, be sure to always use /nc and /s together to redirect the output to screen and not write anything in your dirs.

To use it regularly, you could just type fail "My log entry" and hit Enter.

The fool proof restart feature is enabled by using the /f config switch. First you have to create a safe_config.ini, which can easily be done by copying your existing config.ini to safe_config.ini and changing a bit in it. You could shut down every server except http and disable NT Authentication in this very config, for example. Another idea would be to switch DOT file security feature.

Very important: You must not forget to save another copy of your config.ini, as it will get overwritten. A backup is saved each time /f is used though, but a fixed filename will be used and so only ONE backup is saved. The next subsequent call using /f would then kill your existing config.ini. So be sure save another copy to full_config.ini, for example, or regular_config.ini or stuff.

You call the feature using s.th. like fail "Sambar is going down to Safe Mode" /f safe_config.ini - that's all. Switch back to your original config.ini by calling fail "Sambar is back again" /f full_config.ini or even fail "Sambar is back again" /f lastfailed_config.ini, which would bring up the last backuped config.ini. You may customize the name, as you may customize everything in this script, if you want and you know what you do. Just tell me about it, ok?

One word on /dellogs - be careful to use this switch, as it will shoot you in the foot without warning, i.e. it deletes /log/lastfail.log and /log/fail.log immediatly!

BTW, the script is best used when set as a recovery method in Windows NT/2K's service management for a failed sambar service, as it cleanly can redial, too. That's what I use it for. The first and second time Sambar crashes on one day, it simply get's restarted - every subsequent crash sambar will get a slap of fail.bat "Today's Subsequent Sambar Restart #%1%", which includes the Windows built-in error counter. Mail is sent to my mobile phone. As fail.bat is a batch DOS file, the above has to be a paramter to forcedos.exe, which instead has to be called. There are surely other strategies in what to do at which point of failure, you could restart your box on the second failure, too. Think about it.

Oh, my Goodness, I knew, I wouldn't be able to keep it short,
so, here it goes now,

Rob

Code:
@ECHO off
REM fail.bat 0.2
SETLOCAL
:system_requirements
IF NOT "%cmdextversion%"=="2" GOTO usage :minimal_requirements
SET fail.bat_shorthelp=YES
IF "%~1" EQU "" GOTO usage:min_require_ok
SET fail.bat_reason=%~1:customizable_settings
SET fail.bat_serverbase=..\
SET fail.bat_logs=%fail.bat_serverbase%log\
SET fail.bat_logposition=%fail.bat_logs%lastfail.log
SET fail.bat_backupfile=lastfailed_config.ini
SET fail.bat_servicename=sambar
SET fail.bat_machine=machine_name
SET fail.bat_waitsec=
SET fail.bat_dialup=dialupentry
SET fail.bat_dialupuser=username
REM DO NOT change the entry for dialuppass HERE, set it on line 135 instead
REM or switch /v will put it out to the user in clear text!
REM Surely an easy type fail.bat would do so, too, but nevertheless...
SET fail.bat_dialuppass=pass not shown for security reasons
SET fail.bat_externalsmtp=externalsmtp
SET fail.bat_externalsmtpuser=username
SET fail.bat_externalsmtppass=pass
REM fail.bat_externalsmtpauth MUST be a space, DO NOT REMOVE " >NUL:"
SET fail.bat_externalsmtpauth= >NUL:
if "x%fail.bat_externalsmtpuser%" NEQ "x" SET fail.bat_externalsmtpauth=%fail.bat_externalsmtpuser%:%fail.bat_externalsmtppass%
SET fail.bat_sender=%fail.bat_servicename%@%fail.bat_machine%
SET fail.bat_adminmail=adminmail@example.com
SET fail.bat_localmail=scss@%fail.bat_machine%
SET fail.bat_mailittxt="%fail.bat_machine%.ALERT: %DATE%, %TIME%h. ERR: %fail.bat_reason%"
SET fail.bat_attach1=%fail.bat_logposition%
SET fail.bat_attach2=
SET fail.bat_attach3=:default_values
SET fail.bat_commit=COMMITING
SET fail.bat_hangup=HANGUP
SET fail.bat_dial=DIAL
SET fail.bat_server=RESTART
SET fail.bat_mail=MAIL
SET fail.bat_logging=LOGGING
SET fail.bat_pause=:stuff
SET failbatint_divline=------------NEW---LOGENTRY---STARTS---HERE------------------------------:parsing_params
IF "%~1" EQU "" GOTO exit_parsing_params
IF /i "%~1" EQU "/help" (SET fail.bat_shorthelp=
SET fail.bat_viewhelp=YES)
SET fail.bat_zwei=%~1
SET fail.bat_zwei=%fail.bat_zwei:~0,2%
IF /i "%fail.bat_zwei%" EQU "/h" SET fail.bat_viewhelp=YES
IF /i "%fail.bat_zwei%" EQU "/?" SET fail.bat_viewhelp=YES
IF /i "%~1" EQU "/f" (
SET fail.bat_foolproof=STARTING...
SET fail.bat_config=%2
SHIFT /1
GOTO parsing_params)
IF /i "%~1" EQU "/" GOTO
IF /i "%~1" EQU "/dellogs" SET fail.bat_dellogs=TRUE
IF /i "%~1" EQU "/nc" SET fail.bat_commit=
IF /i "%~1" EQU "/nd" SET fail.bat_dial=
IF /i "%~1" EQU "/nh" SET fail.bat_hangup=
IF /i "%~1" EQU "/nl" SET fail.bat_logging=
IF /i "%~1" EQU "/nm" SET fail.bat_mail=
IF /i "%~1" EQU "/nr" SET fail.bat_server=
IF /i "%~1" EQU "/p" SET fail.bat_pause=PAUSE
IF /i "%~1" EQU "/q" (SET fail.bat_logposition=NUL:
SET fail.bat_logging=)
IF /i "%~1" EQU "/s" (SET fail.bat_logposition=CON:
SET fail.bat_logging=)
IF /i "%~1" EQU "/v" SET fail.bat_verbose=VERBOSE
SHIFT /1
GOTO parsing_params
:exit_parsing_paramsIF DEFINED fail.bat_viewhelp GOTO usage
IF DEFINED fail.bat_dellogs GOTO dellogsECHO %failbatint_divline% >%fail.bat_logposition% 2>&1
ECHO Crashlog %DATE%, %TIME% >>%fail.bat_logposition% 2>&1
ECHO Given Reason: %fail.bat_reason% >>%fail.bat_logposition% 2>&1
ECHO used program call: %0 %* >>%fail.bat_logposition% 2>&1
ECHO resulting settings: %fail.bat_commit% %fail.bat_hangup% %fail.bat_dial% %fail.bat_server% %fail.bat_mail% %fail.bat_logging% %fail.bat_verbose% >>%fail.bat_logposition% 2>&1IF DEFINED fail.bat_foolproof (
ECHO FOOL PROOF CONFIG RESTORE MODE: %fail.bat_foolproof% (%fail.bat_serverbase%config\%fail.bat_config%^) >>%fail.bat_logposition% 2>&1
IF EXIST %fail.bat_serverbase%config\%fail.bat_config% (
ECHO Reverting to fool proof start configuration. Using config %fail.bat_serverbase%config\%fail.bat_config% for this. >>%fail.bat_logposition% 2>&1
ECHO A backup copy of your config.ini is saved in %fail.bat_serverbase%config\%fail.bat_backupfile% >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit (
COPY "%fail.bat_serverbase%config\config.ini" "%fail.bat_serverbase%config\%fail.bat_backupfile%" >>%fail.bat_logposition% 2>&1
ECHO Return Code: %errorlevel% >>%fail.bat_logposition% 2>&1
COPY "%fail.bat_serverbase%config\%fail.bat_config%" "%fail.bat_serverbase%config\config.ini" >>%fail.bat_logposition% 2>&1
ECHO Return Code: %errorlevel% >>%fail.bat_logposition% 2>&1
)
) ELSE (
ECHO %fail.bat_serverbase%config\%fail.bat_config% is missing. Cannot revert to fool proof start configuration. >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit mailit %fail.bat_externalsmtp% %fail.bat_sender% %fail.bat_adminmail% "%fail.bat_serverbase%config\%fail.bat_config% is missing. Cannot revert to fool proof start configuration." %fail.bat_attach1% >>%fail.bat_logposition% 2>&1
)
)
ECHO. >>%fail.bat_logposition% 2>&1IF NOT DEFINED fail.bat_verbose GOTO main
ECHO Used Settings: >>%fail.bat_logposition% 2>&1
ECHO -------------- >>%fail.bat_logposition% 2>&1
SET fail.bat_ >>%fail.bat_logposition% 2>&1
ECHO. >>%fail.bat_logposition% 2>&1:main
:stop_server
IF DEFINED fail.bat_server (
ECHO [%TIME%] Stopping "%fail.bat_servicename%" for safety... >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit net stop %fail.bat_servicename% >>%fail.bat_logposition% 2>&1
ECHO [%TIME%] Dial Return Code:%ERRORLEVEL% >>%fail.bat_logposition% 2>&1
) ELSE (ECHO [%TIME%] Starting of the server skipped by user request (/nr^) >>%fail.bat_logposition% 2>&1):hangup
IF DEFINED fail.bat_hangup (
ECHO [%TIME%] Hanging up %fail.bat_dialup%... >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit %windir%\System32\rasdial.exe T-DSL /disconnect >>%fail.bat_logposition% 2>&1
ECHO [%TIME%] Hangup Return Code:%ERRORLEVEL% >>%fail.bat_logposition% 2>&1
) ELSE (ECHO [%TIME%] Hanging up skipped by user request (/nh^) >>%fail.bat_logposition% 2>&1):dial
IF DEFINED fail.bat_dial (
ECHO [%TIME%] Redialing %fail.bat_dialup%... >>%fail.bat_logposition% 2>&1
SET fail.bat_dialuppass=passwort
IF DEFINED fail.bat_commit rasdial %fail.bat_dialup% %fail.bat_dialupuser% %fail.bat_dialuppass% >>%fail.bat_logposition% 2>&1
ECHO [%TIME%] Dialup Return Code:%ERRORLEVEL% >>%fail.bat_logposition% 2>&1
SET fail.bat_dialuppass=
) ELSE (ECHO [%TIME%] Dialing skipped by user request (/nd^) >>%fail.bat_logposition% 2>&1):start_server
IF DEFINED fail.bat_server (
ECHO [%TIME%] Restarting "%fail.bat_servicename%"... >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit net start %fail.bat_servicename% >>%fail.bat_logposition% 2>&1%
ECHO [%TIME%] Server Startup Return Code:%ERRORLEVEL% >>%fail.bat_logposition% 2>&1
) ELSE (ECHO [%TIME%] Starting of the server skipped by user request (/nr^) >>%fail.bat_logposition% 2>&1):send_mail
IF DEFINED fail.bat_mail (
ECHO [%TIME%] Sending Admin Mail... >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit mailit %fail.bat_externalsmtp% %fail.bat_sender% %fail.bat_externalsmtpauth% %fail.bat_adminmail% %fail.bat_mailittxt% %fail.bat_attach1% %fail.bat_attach2% %fail.bat_attach3% >>%fail.bat_logposition% 2>&1
ECHO [%TIME%] Admin Mail Return Code:%ERRORLEVEL% >>%fail.bat_logposition% 2>&1 ECHO [%TIME%] Waiting %fail.bat_waitsec% seconds... >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit IF DEFINED fail.bat_waitsec wait %fail.bat_waitsec% >>%fail.bat_logposition% 2>&1 ECHO [%TIME%] Sending local mail... >>%fail.bat_logposition% 2>&1
IF DEFINED fail.bat_commit mailit localhost %fail.bat_sender% %fail.bat_localmail% %fail.bat_mailittxt% %fail.bat_attach1% %fail.bat_attach2% %fail.bat_attach3% >>%fail.bat_logposition% 2>&1
ECHO [%TIME%] Local Mail Return Code:%ERRORLEVEL% >>%fail.bat_logposition% 2>&1
) ELSE (ECHO [%TIME%] Sending of Mail skipped by user request (/m^) >>%fail.bat_logposition% 2>&1)ECHO [%TIME%] Goodbye. Good luck. >>%fail.bat_logposition% 2>&1
ECHO. >>%fail.bat_logposition% 2>&1:copy_log_files
IF DEFINED fail.bat_logging (
IF EXIST %fail.bat_logs%fail.log (
ECHO Logfile %fail.bat_logs%lastfail.log is merged with %fail.bat_logs%fail.log. >>%fail.bat_logposition% 2>&1
COPY "%fail.bat_logs%fail.log" + "%fail.bat_logs%lastfail.log" "%fail.bat_logs%fail.log" >>%fail.bat_logposition% 2>&1
) ELSE (
ECHO %fail.bat_logs%fail.is created from %fail.bat_logs%lastfail.log. >>%fail.bat_logposition% 2>&1
COPY "%fail.bat_logs%lastfail.log" "%fail.bat_logs%fail.log" >>%fail.bat_logposition% 2>&1)
) ELSE (ECHO [%TIME%] Log files not touched by user request (/nl^) >>%fail.bat_logposition% 2>&1)
ECHO.IF NOT "%fail.bat_logposition%" EQU "CON:" TYPE %fail.bat_logposition%|moreIF DEFINED fail.bat_pause PAUSEGOTO :EOF:dellogs
DEL %fail.bat_logs%fail.log
DEL %fail.bat_logs%lastfail.log
GOTO :EOF:usage
ECHO fail.bat - Fool Proof Restart Feature for the Sambar Server (NT/2K service)
ECHO Version: 0.1b
ECHO.
ECHO USAGE:
ECHO ------
ECHO /bin/fail.bat reason [/f config] [/h ^| /help ^| [/nc] [/nd] [/nh] [/nl] [/nm] [/nr] [/v]]
ECHO.
ECHO necessary parameter:
ECHO reason = reason why script is called, is used in logs and mails
ECHO.
ECHO optional parameters:
ECHO /dellogs = deletes /log/lastfail.log and /log/fail.log - USE WITH CAUTION!
ECHO /f config = fool proof restart server using given configuration file, must reside in /config
ECHO a backup copy of the existing config.ini will be written.
ECHO /h = show this text and exit
ECHO /help = show a long help text and exit. Use it best with switch /p
ECHO /nc = do not commit (just create logs, don't restart or redial or copy config.ini)
ECHO /nd = do not dial
ECHO /nh = do not hangup
ECHO /nl = do not create the logfiles
ECHO /nm = do not send any mail
ECHO /nr = do not restart the server
ECHO /p = pause mode, applies to long /help and general pausing at end
ECHO /q = quiet mode, redirect output to NUL:, sets /nl
ECHO /s = screen mode, redirect output to screen, sets /nl
ECHO /v = verbose mode - puts out most settings
ECHO.
ECHO The switches are case-insensitive.
ECHO.
ECHO EXAMPLES:
ECHO ---------
ECHO fail.bat "Sambar has been switched to Safe Mode at %%time%% on %%date%%." /f safe_mode_config.ini /nd /nh
ECHO fail.bat "Back again - got new IP." /f current_config.ini
ECHO fail.bat "Automatic Server Restart, this happened %%1%% times today."
ECHO fail.bat "Just debuggin' and simulatin'." /nc /v
ECHO fail.bat "Logging? What's it for?" /nl
ECHO fail.bat "At least, lemme see." /s
ECHO fail.bat "Be quiet, son" /q
IF DEFINED fail.bat_shorthelp GOTO :EOF
IF DEFINED fail.bat_pause (ECHO.&PAUSE)
ECHO ...Inline Help to fail.bat - Page 2 (pause using /p^)
ECHO Run from the /bin directory, fail.bat can do the following:
ECHO + copies a given fool proof config.ini over config.ini
ECHO + stops the server, if it's running.
ECHO + hangs up the line, if it's connected
ECHO + redials to your ISP
ECHO + restarts the server
ECHO + Creates two logfiles in the /log directory of your server, lastfail.log and the incremented fail.log.
ECHO + lastfail.log is sent to a remote email using an external SMTP-Server and to local user's mailbox using localhost.
ECHO + Output can be redirected to Logfile (default), CON: (Screen Mode) or NUL: (quiet mode).
ECHO.
ECHO This script is best used when set as a recovery method in Windows NT/2K's service management for a failed service.
ECHO You must call it using s.th. like FORCEDOS.EXE fail.bat "Service restart after %1% failures today." Have the
ECHO failure counter set back every day, if you're not far away from your machine, or every week, if the server is
ECHO far away from you.
ECHO.
ECHO Be sure to edit the full list of available settings in fail.bat itself before your first use to configure fail.bat
ECHO properly for your system. Remember to restrict access to this file as it contains security related material.
ECHO It is wise to always keep an initial "clean" copy of this file. In case you damaged it, you can download it from
ECHO http://www.sambarserver.info or even http://www.robertkehl.de/download.
ECHO.
ECHO You must enable COMMAND EXTENSIONS in CMD.EXE to use this script. Don't use COMMAND.COM; it doesn't provide extensions.
ECHO You can enable them by setting the following registry keys to 0x1 using REGEDIT.EXE or REGEDT32.EXE:
ECHO HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions
ECHO HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions
ECHO Be sure to also have enabled DELAYED EXPANSION of variables. This you set here:
ECHO HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion
ECHO HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion
ECHO Be careful when working on the registry. BTW: CURRENT_USER overrides LOCAL_MACHINE
ECHO Alternatively, you can start your CMD.EXE using the /E:ON /V:ON switches.
ECHO There is currently no version for Windows 9x/ME or Linux.
ECHO.
ECHO This little piece of software is given to the Sambar Community by Robert Kehl (robkehl@robertkehl.de). Feel free to
ECHO use it at your own risk. No warrantees provided. If you modify the script other than just customizing to your needs,
ECHO I'd like a little note from you, just an email. Your bug reports, comments, suggestions, feature wishes and of
ECHO course improvements are welcome, too. If you want to be announced of future versions, mail to fail.bat@robertkehl.de.
ECHO.
ECHO Feel free and have a great time using your Sambar Server.