Monday, April 29, 2013

How to get notification when SQL Instance down



This article will help you to get an email alert when your SQL instance down. For this implementation you need to additional instance for monitoring purpose. Please have a look on the below diagram.



For this implementation, I am going to create one stored procedure and one SQL Server job. The job will handle the all the action items like connection to target instance, wait sometime re-connecting, occurring each I minute time etc. Store procedure will send E-mail notification for given email address or alias.

This stored procedure not mandatory and it is for additional step to get notification by calling Stored Procedure. I will explain each step

First, you need to configure E-mail in your monitor instance. To configure your E-mail feature on SQL server please have look on this: http://dbamigos.blogspot.com/2013/04/database-mail-in-sql-server-2008.html

Second, you need to configure Linked Server for Target Instance.



     1.       Open SQL Server Management Studio , Expand Server Object node in object Explorer , Right click on   Linked Servers and Click New Linked Server 



       2.       From below window, you need to select Type of server as “SQL Server” and give the Server name with instance name.




     3.       After that click security tab and select below mention option and give SA login & credentials to connect to Target Instance. Then click OK button




Third, I’m going to create SQL job
<!--[if !supportLists]-->          1.       In Object Explore,  Expand SQL Server Agent Right click on job and click new job
<!--[if !supportLists]-->          2.       <!--[endif]-->Give a name for your job. I gave name as “InstanceMonitoring_I01”




     3.       Go to step tab and create steps to execute






       4.       Give the step name as “Connect1” and give query to execute on remote server. This simple query will    execute on Target SQL Server instance by using Linked Server that we configured previously. Purpose of executing this simple query to get the Target Server instance is up and running.

SELECT TOP 1 object_id
         FROM [PEGASUSWIN7.SOL.NET\I01].master.sys.all_columns







5.       Go to the Advanced tab and select the “On success action” and “On failure action” accordingly



On Success action:  Query execution successful. Target instance is up and running
On failure action: Query Execution Fail. Go to the next step. This step will execute the delay query

         6.       Create step 2, give Step name as “Delay1” and give query as “WAITFOR DELAY '00:00:05' “. This query will delay the execution for five second. This value you can change by using this format “HH:MM:SS”


7.       Go to the Advanced tab and give “On success action” and ‘On failure action” accordingly







On success Action: successfully delay the execution. Go to the next step, will re-execute the query on Target SQL instance
On failure Action: failed to execute delay query quit the job and report as a failure



            8.      Create another step – Connect2
                 Same as the step 4




 
 9.       Create another delay step – Delay2



Whole purpose of these delay queries mean to wait some amount of time ( 5 seconds) to re-execute the query. Because there may have some network issues between Target Server and Monitor Server. (or any other)

             10.       Create another step-5, Connect3




There is no any other steps to execute after this step hence I select “Quit the job reporting failure” as On failure action
Now we completed the steps creation part. Next we need to schedule this job

           11.       Select the Schedule tab and click New button





Give the details accordingly
I scheduled this job to run every day each 1 minute time



           12.       Now you need to select the notification method when job completes. For that you need to click      Notification tab and select the E-mail and select the Operator that you create previously



Now your job is successfully created. Then try to test yourself by stopping Target Instance and you will get an E-mail notification.

Additionally you can set another step to send E-mail after failing Connect3 step. You need to crate store procedure on master database (You can use any database) to send E-mail

1.       Create SP “InstanceMonitorI01_SendMail” in Master database

USE master
                        GO

CREATE PROCEDURE [dbo].[InstanceI01_SendMail]     
AS   
BEGIN

 SET NOCOUNT ON 


 Exec msdb.dbo.sp_send_dbmail
@profile_name='DBMailProfile',
@recipients='hasithak@ecollege.com',
@copy_recipients='hasitha.kanchana@pearson.com',
@body='HASITHAKWIN7.MYCOMPANY.ORG\I01 Instance has failed',
@subject='HASITHAKWIN7.MYCOMPANY.ORG\I01 Instance failed';


            END







2.      Create additional step called “SendMail” after Connect3 and give a command as 



         EXEC dbo.InstanceMonitorI01_SendMail







For Advanced tab,



3.       Now you need to modify step5 ( Connect3) because we added separate step to send mail



On failure action: We move to next step to send E-mail by executing step 6 ( SendMail)

Overall picture of the Job steps


























Sunday, April 21, 2013

Database Mail in SQL Server 2008


Database mail was introduced in SQL Server 2005 and it was new feature in SQL Server 2005. Database mail is also available in SQL Server 2008 and 2012.The basic function of SQL Server 2008 Database Mail to send alert message to DBAs with issue related performance, disk space, SQL Jobs, Backup plans, and other changes in the database schema.

In SQL Server, there is another mail component which is called SQLMail. Database Mail is replacement of SQLMail and it is having more security than the SQLMail.
Database Mail is based on SMTP ( Simple Mail Transfer Protocol)
Database Mail depends on Service Broker and this service must be enabled
Databse can be encrypt for additional security
SQLMail is based on MAPI (Messaging Application Programming Interface)
For SQLMail, to be enabled MAPI it will require Outlook to be installed

More and More efficient method is Database mail when compared with SQLMail.
This code block will help you to enable Database Mail,


sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE 'Database Mail XPs', 1
GO
RECONFIGURE
GO


Create Mail Profile and Account
 Ø  First you need to create profile. Profile is the main element of Database mail. One profile may contain multiple email accounts. There are 2 types of Profile in SQL Server,
o   Public Profile -  Can access by any user and these user can send emails
o   Private Profile – Need permission to send emails. This is for specific users
 Ø  This link will help you more : http://msdn.microsoft.com/en-us/library/ms175100.aspx


      These are the Steps,
      1. Connect to the SQL server
      2. In Object Explore, Expand the Management Node and Right Click on Database Mail and click on   Configure Database Mail





  

     3.       If you are going to configure Database mail first time you may ask to enable Database mail feature. If it is click yes. Form this window you need to select “ Setup Database Mail by ….” And click Next


     4.       For this window, you need to give Profile Name and description for it. Description is not a mandatory. This is the place you can configure SMTP server. Click Add button



     5.       This is place that your SMTP sever configuration need to place


Here, you can use your own mail server configuration. But if you don’t have mail server you can configure with Gmail, Windows Live Mail, Yahoo or any other global mail server provider.

For the demonstration purpose I used Gmail server configuration.

Email address: Your Gmail e-mail ID

Display name: Whatever Name That You want to Display with Your mail ( e.g Name)
Reply e-mail: Any reply-to email account
Server name: smtp.gmail.com
Port number: 587
This server requires a secure connection (SSL): Checked ON

In the next section, choose Basic Authentication and enter the following information: 
User name: Your Gmail e-mail ID
Password: Password for your Gmail ID 
Confirm password: Password for your Gmail ID






   Then Click OK button 


   Then Click Next button to continue
      6.       From This window you select you profile type ( Public or Private) and set Default Profile as well



     After configuring Profile type click Next button
      7.       Here, You can configure the System Parameter and  can specify prohibited attachment extensions


     I didn't change other values and click Next button
      8.       Finally You will get the confirmation window for Database Mail Set up





Click Finish to complete the set up and after set up complete click Close button

To Test Your Mail Configuration,
Go to the Object Explore, Expand the Management Node, Right Click on Database Mail Click Set Test E-Mail


From This window you can sent Test e-mail to your email account, for this you can use any email address



This window will help you to get confirmation on the sent mail (No of sent mail may different)





Check you Inbox and you will see Test mail from your SQL Server