Checking SQL Server's Heart BeatMay 5, 2004 It is imperative to have monitoring tools to monitor the services running in a windows operating system based server. However, small shops do not have that kind of budget to get tools for monitoring. Data Base Administrators should be notified as soon as one of the SQL Server services is stopped, restarted or not responding. In this article, I am going to discuss how to check all SQL server services from one location and how to check the heart beat of SQL Server instances every few minutes. Method1This method is useful for Database administrators to check the heart beat of SQL Server services daily. Step1 Create a folder c:\HeartBeat and create a file ServiceList.txt as shown below with all the servers and services you would like to check. SQL,MSSQLServer Note: Add your server names and the corresponding service names. Step2 Create a file c:\HeartBeat\CheckHeartbeat.vbs and copy and paste the below code into the file. 'Objective: TO check the Heartbeat of all SQL Server services
'Author: MAK
'Date: April 2, 2004
on error resume next
Set iFSO = CreateObject("Scripting.FilesyStemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
InputFile="c:\Heartbeat\Servicelist.txt"
Outputfile="c:\Heartbeat\Status.txt"
Set ifile = iFSO.OpenTextFile(inputfile)
Set ofile = ofso.createTextFile(OutputFile, True)
Do until ifile.AtEndOfLine
servicelist= ifile.ReadLine
strcomputer=left(servicelist,instr(servicelist,",")-1)
Service =right(servicelist,len(servicelist)-instr(servicelist,","))
ofile.writeline Now()
ofile.writeline "ServerName: " & strcomputer
ofile.writeline "ServiceName: " & Service
Set objWMIService =nothing
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
if err.number<>0 then
ofile.writeline "Error: " & err.number
ofile.writeline "Error: " & err.description
else
Set colItems = nothing
query=""
query = "Select * from Win32_Service where name = '" & Service & "'"
'msgbox query
Set colItems = objWMIService.ExecQuery(query,,48)
if err.number<>0 then
ofile.writeline "Error: " & err.number
ofile.writeline "Error: " & err.description
else
For Each objItem in colItems
ofile.writeline "Status: " & objItem.Status
ofile.writeline "State: " & objItem.State
next
next
end if
end if
err.number=0
Loop
msgbox "SQL Service Status Check completed"
Step3 Execute the above VB script. The message shown below indicates that the script has completed.
In addition, it creates a file c:\HeartBeat\Status.txt as shown below. 4/8/2004 1:03:46 AM ServerName: SQL ServiceName: MSSQLServer Status: OK State: Running 4/8/2004 1:03:47 AM ServerName: SQL ServiceName: SQLSERVERAGENT Status: OK State: Stopped 4/8/2004 1:03:47 AM ServerName: YUKON ServiceName: MSSQLServer Status: OK State: Running 4/8/2004 1:04:02 AM ServerName: YUKON ServiceName: SQLSERVERAGENT Status: OK State: Running 4/8/2004 1:04:03 AM ServerName: YUKON ServiceName: MSSQLServerOLAPService Status: OK State: Running 4/8/2004 1:04:03 AM ServerName: ETL ServiceName: MSSQLServer Error: 462 Error: The remote server machine does not exist or is unavailable 4/8/2004 1:04:08 AM ServerName: ETL ServiceName: SQLSERVERAGENT Error: 462 Error: The remote server machine does not exist or is unavailable |