Sample Script - Check web server from a batch script
OK, just to prove you can do anything with a command line batch script... how about verifying your web server is up...? Why use an application when you can script it? :-)
Thanks to the IIS 6.0 Resource Kit tool TinyGet.exe, you can do amazing things from the command line.
I'll leave "whether you should" up to you...
Requirements:
- You need tinyget.exe.
- A command line SMTP mail utilty like bmail, or postie would be helpful. I used to use postie a lot, but bmail does 80% of the features for free. This script is written for bmail.
- A web server to monitor... doh!
How it works:
Pretty simple really, you need a page on your web server with static text content that tinyget can grab. The script then runs as often as you schedule it (mine runs every 3 hours during the day), verifies it can get the page and trigger string, and alerts you if it can't. I use this to verify that my home server on my DSL line is up, and that my Dynamic DNS registration is current.
If it goes down, I fix it when I get home. TinyGet is capable of quite a lot, including grabbing pages over SSL, but I just need a simple "It's down alert". Whatsup or any number of monitoring tools are capable of this, but batch scripts are free.
Download the script here or cut and paste from the following.
::REM ------------------- Begin sample batch script ------------------------
::REM Sample batch programming script Serverup.cmd
::REM (C)2006 John D. Seaman, Copylefted under terms of the GNU/GPL
::REM by John D. Seaman, www.japan-page.net/batch
@echo off
cls
echo.
echo.
echo _____ __ __
echo ^/ ___^/___ ______ _____ _____^/ ^/ ^/ ^/___
echo ^\__ \^/ _ \^/ ___^/ ^| ^/ ^/ _ \^/ ___^/ ^/ ^/ ^/ __ \
echo ___^/ ^/ __^/ ^/ ^| ^|^/ ^/ __^/ ^/ ^/ ^/_^/ ^/ ^/_^/ ^/
echo ^/____^/\___^/_^/ ^|___^/\___^/_^/ \____^/ .___^/
echo ^/_^/
echo S e r v e r U p V e r i f i c a t i o n S c r i p t 0.02a
echo.
::REM ASCII art generator on the web @ http://www.network-science.de/ascii/
::REM ------------------ Begin user configuration --------------------------------------
::REM Set variables
set _log=log.txt
set _frm=serverMon@yourdomain.com
set _too=cellphone#@yourwirelesscarrier.com
set _hst=smtp.server.com
::REM Initialize the alert variable.
set _alert=0
::REM I look for a specific page (ismyserverup.asp) because I use a blog and didn't choose to embed my test string "smeg".
set _page=ismyserverup.asp
set _trigger=smeg
::REM Generate the servers.ini file. Add or remove FQDN lines for each web server you want to check.
::REM You need at least one entry, but this utility will loop through as many lines as you wish to add.
echo www.mydomain.com>servers.ini
echo home.mydomain.com>>servers.ini
echo alternate.mydomain.com>>servers.ini
::REM ------------------ End user configuration --------------------------------------
::REM Create the log file
echo.>%_log%
echo Server test script executed on %computername% on %date% at %time%. >>%_log%
echo.>>%_log%
echo.>>%_log%
::REM Do it already...
for /f %%i in (servers.ini) do call :doCheck %%i
echo.
echo Finished.
sleep 5
::REM Check the alert status
if "%_alert%" == "1" (goto :blastSMTP)
echo Completed without errors.
echo Completed without errors.>>%_log%
goto :EOF
:blastSMTP
::REM Send out the SMTP alert
set _sbj="Alert: One or more errors found, web services are down."
echo.
echo.>>%_log%
echo Errors detected, e.mail sent.
echo Errors detected, e.mail sent.>>%_log%
bmail -f %_frm% -s %_hst% -t %_too% -a %_sbj% -m %_log% -d
goto :EOF
::REM ------------ F U N C T I O N S -----------------------------
:doCheck
::REM Do it already!
::REM Get content from the web server
tinyget -d %1 /%_page% >_out.txt
::REM Check the content
type _out.txt | find "%_trigger%"
goto :result%ERRORLEVEL%
:result0
::REM Server is
echo Server %1 is up, no problems detected.
echo Server %1 is up, no problems detected.>>%_log%
echo.>>%_log%
goto :EOF
:result1
::REM Server is down
echo The server %1 was not available.
echo ALERT: The server %1 was not available !! >>%_log%
echo.>>%_log%
set _alert=1
goto :EOF
:EOF
::REM ------------------- End sample batch script ------------------------






Search