Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted May 5, 2004

Checking SQL Server's Heart Beat - Page 2

By Muthusamy Anantha Kumar aka The MAK

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



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM