View Single Post
Old 04-17-2009, 05:22 PM   #9
TargetIT
CrackBerry Addict
 
Join Date: Jan 2008
Model: 9700
PIN: N/A
Carrier: Rogers
Posts: 709
Default

I had similar problems that eventually went away but I wrote a batch script to restart said services that I still run every 5 minutes. It's fairly generic as you can see. You create a list of services by simply running "net start >MasterServicesList.txt" and move that file into the directory where the batch program is. You can cut the list of services down to just what you want to monitor as well. It requires:

- blat to send the email alert out - also residing in the directory of the bstch file.
- A directory called ServicesNotStarted in the directory
- command extensions enabled
- A command argument of /ServiceRestart=YES if you want it to restart the services

Some tweaking is in order for the variables to suit your needs.

Code:
rem ***************** Service Error Handler **********************
rem * Version 1.00                                               *
rem **************************************************************
rem Reset and initialize variables

set servicefailcount=
set servicename=
set ServiceRestart=
set StartPath=%~dp0
set EmailText=%StartPath%Email.txt
set EmailServer=exchange:25
set EmailFrom=TechSupport@YourCompany.com
set EmailTo=TechSupport@YourCompany.com
set EmailSubject=Service Failure on %ComputerName%
set MasterServicesList=%StartPath%MasterServicesList.txt
set CurrentServicesList=%StartPath%CurrentServicesList.txt
set ServicesNotStarted=%StartPath%ServicesNotStarted.txt
set ServicesReStarted=%StartPath%ServicesReStarted.txt

del %ServicesNotStarted%
del %EmailText%

:Enumerateparameters

if "%1"=="" goto StartProcessing

:CheckIfSwitch

echo %1 | find "/"
if errorlevel 1 goto ParameterFailure

:CheckEachPossibleSwitch

if /i "%1"=="/ServiceRestart" set ServiceRestart=%~2& goto FoundParameter

:InvalidParameter

goto ParameterFailure

:FoundParameter

shift
shift
goto enumerateparameters

:ParameterFailure

echo Parameter Failure! > %EmailText%
echo. >> %EmailText%
echo These were the command line parameters given: %* >> %EmailText%
goto email

:StartProcessing

if exist %MasterServicesList% goto CompareCurrentToMaster
echo Master Services List has not been created! > %EmailText%
echo. >> %EmailText%
echo Some services MAY NOT BE RUNNING >> %EmailText%
goto email

:CompareCurrentToMaster

net start > %CurrentServicesList%
for /f "skip=2 delims=," %%i in (%MasterServicesList%) do (
	set CurrentService=%%i
	set CurrentService=!CurrentService:~3!
	find "!CurrentService!" %CurrentServicesList%
	if errorlevel 1 (
		if not exist "%StartPath%ServicesNotStarted\!CurrentService!" (
			echo !CurrentService!>> %ServicesNotStarted%
			echo. > "%StartPath%ServicesNotStarted\!CurrentService!"
		)			
	) else (
		if exist "%StartPath%ServicesNotStarted\!CurrentService!" echo !CurrentService!>> %ServicesReStarted%
		del "%StartPath%ServicesNotStarted\!CurrentService!"
	)
	)

if not exist %ServicesNotStarted% if not exist %ServicesRestarted% goto cleanup

echo Service Restart?: %ServiceRestart% >> %EmailText%
echo. >> %EmailText%

if not exist %ServicesReStarted% goto ServicesNotStarted

:ServicesRestarted

echo Services that have ReStarted:>> %EmailText%
echo. >> %EmailText%
type %ServicesReStarted% >> %EmailText%
echo. >> %EmailText%

if not exist %ServicesNotStarted% goto footer

:ServicesNotStarted

echo Services Currently Not Running:>> %EmailText%
echo. >> %EmailText%
for %%i in (%StartPath%ServicesNotStarted\*.*) do echo %%~ni Since: %%~ti>> %EmailText%
echo. >> %EmailText%
if /i NOT "%ServiceRestart%"=="YES" goto footer
echo Services Restarting - Results Below: >> %EmailText%
echo. >> %EmailText%
for %%i in (%StartPath%ServicesNotStarted\*.*) do (
	net stop "%%~ni" >> %EmailText%
	net start "%%~ni" >> %EmailText%
	)

:footer

echo Current Running Services: >> %EmailText%
echo. >> %EmailText%
net start  >> %EmailText%

:email

%StartPath%blat %EmailText% -q -to %EmailTo% -s "%EmailSubject%" -f %EmailFrom% -server %EmailServer% -priority 1

:Cleanup

del %emailtext%
del %ServicesNotStarted%
del %ServicesReStarted%
del %CurrentServicesList%
set servicefailcount=
set servicename=
set ServiceRestart=
set StartPath=
set EmailText=
set EmailServer=
set EmailFrom=
set EmailTo=
set EmailSubject=

:end
Offline   Reply With Quote