Checking SQL Server's Heart Beat

Wednesday May 5th 2004 by Muthusamy Anantha Kumar aka The MAK
Share:

Small shops do not always have the budget for the necessary tools required to monitor the services running on a Windows based server, yet it is imperative that these services be monitored. MAK examines alternative methods for checking all SQL Server services from one location and how to check the heart beat of SQL Server instances every few minutes.

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.

Method1

This 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
SQL,SQLSERVERAGENT
YUKON,MSSQLServer
YUKON,SQLSERVERAGENT
YUKON,MSSQLServerOLAPService
ETL,MSSQLServer
ETL,SQLSERVERAGENT

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

Method 2

This method is useful for checking the heart beat of SQL Server services. If any of the services listed in the ServiceList.txt are not running, it will send an email.

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
SQL,SQLSERVERAGENT
YUKON,MSSQLServer
YUKON,SQLSERVERAGENT
YUKON,MSSQLServerOLAPService
ETL,MSSQLServer
ETL,SQLSERVERAGENT

Note: Add your server names and the corresponding service names.

Step2

Create a file c:\HeartBeat\CheckHeartbeatEmail.vbs and copy and paste the below code into the file.

'Objective: TO check the Heartbeat of all SQL Server services and send email
'if any state of service is not running
'Author: MAK
'Date: April 2, 2004
on error resume next
Set iFSO = CreateObject("Scripting.FilesyStemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")

'Note: change the email ID to your email ID
NotificationEmail="mak_999@yahoo.com"
SMTPServer = "111.222.444.111"

InputFile="c:\Heartbeat\Servicelist.txt"
Outputfile="c:\Heartbeat\Status.txt"

Set ifile = iFSO.OpenTextFile(inputfile)
Set ofile = ofso.createTextFile(OutputFile, True)
ofile.writeline "Check Heart beat started"
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
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "SQL Server HeartBeat" 
objMessage.Sender = NotificationEmail
objMessage.To = NotificationEmail
objMessage.TextBody = "Server :" & strcomputer & "  " & err.description
'msgbox y
'The line below shows how to send a webpage from a remote site 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
'Name or IP of Remote SMTP Server 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
'Server port (typically 25) 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objMessage.Configuration.Fields.Update 

objMessage.Send

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

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "SQL Server HeartBeat" 
objMessage.Sender = NotificationEmail
objMessage.To = NotificationEmail
objMessage.TextBody = "Server :" & strcomputer & "  " & err.description
'msgbox y
'The line below shows how to send a webpage from a remote site 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
'Name or IP of Remote SMTP Server 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
'Server port (typically 25) 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objMessage.Configuration.Fields.Update 

objMessage.Send


else
For Each objItem in colItems
ofile.writeline "Status: " & objItem.Status
ofile.writeline "State: " & objItem.State


If objitem.status <>"OK" or objitem.state <>"Running" then
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "SQL Server HeartBeat" 
objMessage.Sender = NotificationEmail
objMessage.To = NotificationEmail
objMessage.TextBody = "Server :" & strcomputer & ":" & Service & " Status: " _
& objitem.status & " Status: " & objitem.state
'msgbox y
'The line below shows how to send a webpage from a remote site 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
'Name or IP of Remote SMTP Server 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
'Server port (typically 25) 
objMessage.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
objMessage.Configuration.Fields.Update 
objMessage.Send
end if
next
end if
end if

Loop

ofile.writeline "Check Heart beat Completed"

Note: Make sure you change the email-Id and the SMTP server names in the VB Script described above.

Step3

Execute the above VB script. It creates a file c:\HeartBeat\Status.txt and sends emails as shown below.

Check Heart beat started
4/8/2004 1:28:10 AM
ServerName: SQL
ServiceName: MSSQLServer
Status: OK
State: Running
4/8/2004 1:28:11 AM
ServerName: SQL
ServiceName: SQLSERVERAGENT
Status: OK
State: Stopped
4/8/2004 1:28:11 AM
ServerName: YUKON
ServiceName: MSSQLServer
Status: OK
State: Running
4/8/2004 1:28:27 AM
ServerName: YUKON
ServiceName: SQLSERVERAGENT
Status: OK
State: Running
4/8/2004 1:28:28 AM
ServerName: YUKON
ServiceName: MSSQLServerOLAPService
Status: OK
State: Running
4/8/2004 1:28:28 AM
ServerName: ETL
ServiceName: MSSQLServer
Error: 462
Error: The remote server machine does not exist or is unavailable
4/8/2004 1:28:33 AM
ServerName: ETL
ServiceName: SQLSERVERAGENT
Error: 462
Error: The remote server machine does not exist or is unavailable
Check Heart beat Completed

Email 1

Email 2

Conclusion:

By Scheduling the above VB Script to run every 5 minutes or so, the Database administrators can be notified on time, when SQL service is not running. This article can also be used to monitor any services in the Windows operating system.

» See All Articles by Columnist MAK

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved