Showing posts with label monitoring. Show all posts
Showing posts with label monitoring. Show all posts

Monday, March 5, 2012

Monitoring Database – Using Policy Based Management


In this article we will make use of Policy based management to check some of the basic but important properties of database. We will use policy based management to report if the specific database properties are changed. Some of the basic database properties that we monitor here are to database owner, recovery model of database, compatibility level, auto shrink and auto update statistics properties of database, etc.
Prior to SQL Server 2008, we use to write complex scripts and schedule jobs to get reports on these database properties and if some makes any changes to the scripts and it fails, again we have to spend time to fix the scripts and make sure we get the reports. Thanks to SQL Server 2008 policy based management, now their is no need to write scripts as most of the monitoring task can be done by using this very good feature.
Step as how to make use of policy based management to monitor these database properties and to report in case of violation:-
1) Connect to the database server and navigate to Management à Policy Management à Policies. Right click on Policies, thenclick on New Policy to start creating a Policy.
2) In ‘Create New Policy’ window give a relevant name to Policy like in example below its ‘Database Monitor Policy’. Then in the check condition click on New condition (if you have already created a condition you can either select the condition.  But here we will create a new condition).
Click on ‘New Condition..’ New window will open.
3) Here type the name of new condition like ‘Database Monitoring Condition’ and then against Facet click on drop down and select Database. After selecting the database (next to Facet), start adding expression. In this case we will select the database properties fields that we need to monitor and get notified if the default values are changed.
@AutoShrink: – Auto Shrink of database is usually and preferably set to false. If the auto shrink property is set to true and total free space in database fall below the threshold value, SQL Server will start shrinking the database. In times this can happen during peak working hours and can cause blockings and performance issues. So as a best practice this option is always set to false.
@AutoUpdateStatisticsEnabled: – Auto update statistics option of database is set to true to make sure that the database statistics are always up to date. Although some time you may have to manually update the statistics also (even if this option is true) depending on the insert delete updates in the database.
@CompatibilityLevel: – To make full use of the new features available in SQL Server 2008, your database should be in SQL Server 2008 compatibility level i.e. 100. Although there are some applications that prefer to remain in previous version compatibility level to avoid costs linked to making necessary changes in code. What I believe is, there is no point moving to newer version if you are not willing to make use of the new features available.
@IsAccessible: – Is set to true to allow connections to database
@Owner: – As a best practice, owner of the database should always be ‘sa’ and not any individual user.
@ReadOnly: – If true then user cannot make any modifications to the database. (In the example I have considered that all database in production servers are set to read write mode. Although some exceptions might be there)
Now, after selecting all the fields and values click OK. You can select more or less fields as per your need.
4) Back to Create New Policy window. After selecting the newly created condition against ‘Check Condition’ option just makes sure that every option is checked if you want this policy for all databases.
Now, its time to select the evaluation mode for this policy. In this case you will have only two options either to run on demand or to schedule it via SQL Server agent job. Ideally there are four evaluation modes available for a policy but depending on the conditions that you have selected, these evaluation options will vary.
After selecting the Evaluation mode click OK. In this example we have selected the evaluation mode as ‘on demand’.
5) Now to evaluate the policy just navigate to Management -> Policy Management -> Policies. Just right click on the newly created policy and select Evaluate.
6) Once evaluation is complete, you will get the below screen with the evaluation results. For the databases where all properties are as per policy you will see green tick and for the once which deviate for the policy setting are marked by red cross.
To check the details of the policy results just click on View under Details column. A new screen will open with the expected values (policy settings) and the actual values. Thus it will help you to understand as which database properties need to be corrected to the expected values.
7) If you want to save the evaluation results, just click on the Export Results option. You can save this report in xml format and can view it later by opening it in any xml editor.
To Conclude, Policy based management is a very good feature introduced in SQL Server 2008. You can make use of these features to monitor/force policies as per your requirements. There are many more options available in Policy Based management that will help you to manage your database servers more easily and to enforce almost all the standards as per your company requirements.

Friday, March 2, 2012

Managing and Connecting to AlwaysOn Availability Groups


From the previous AlwaysOn Availability Group article, we provided a name for the availability group listener which is simply a unique DNS name as a Virtual Network Name (VNN) to direct read-write requests to the primary replica and read-only requests to the read-only secondary replica. In this way the client doesn’t need to know which physical server is hosting the primary replica and which is hosting the secondary replica for the read-only load.
A client connects to the current primary replica using the availability group listener for a read-write operation, for read-only it connects using the availability group listener but this time it also needs to provideApplicationIntent=ReadOnly in the connection string. In this way the read-only requests are routed to the readable secondary replica.

Connecting to The Primary Replica Using The Physical Server Name

Not only you can use an availability group listener to connect the replica/instance but if you know the physical server name, you can directly connect to it. If you expand Availability Groups node under AlwaysOn High Availability node, you will be shown the current instance is the primary replica (ARSHAD-PC in this example) whereas another instance (ARSHAD-LP) is the secondary replica as shown below:

Connecting To The Secondary Replica Using Physical Server Name

The way you can connect to primary replica using physical server name, you can also connect to secondary replica using the physical server name. If you expand Availability Groups node under AlwaysOn High Availability node, you can notice the current instance is the secondary replica (ARSHAD-LP) whereas another instance (ARSHAD-PC) is the primary replica as shown below:

Connecting To The Availability Group Using The Availability Group Listener

As shown below, you can see that I connected to availability group using the listener which directs connections to appropriate replica based on the type of connection I am making.

Managing And Monitoring Availability Groups

There are several ways to effectively manage and monitor availability groups, replicas and databases in SQL Server 2012 but the most efficient is the inbuilt dashboard which displays information in very a intuitive way for high-level monitoring and management. In addition, you can also use the newly introduced catalog views and dynamic management views, extended events as well as system performance counters.

Viewing The Availability Group information Dashboard

To the view availability group dashboard, connect to the server/replica (using physical server name or availability group listener), expand the nodes and then right-click on availability group node and click on Show Dashboard as shown below:
Description: Description: cid:image001.png@01CCCFBD.EA80F430
Clicking on the Show Dashboard as above will open the selected availability group dashboard. The dashboard displays primary replica and all the secondary replicas and its synchronization status along with overall health indicators as shown below. If there is any failure or any exception, the dashboard displays the messages or links for details message and what can be done to resolve the issues:
Description: cid:image005.png@01CCD058.DE58E910

Monitoring Health Events

Every events on the the availability groups are tracked by SQL Server which can be very helpful for troubleshooting. On the top right of the availability group dashboard you can see these two links, click View AlwaysOn Health Events link to display health events:
Description: cid:image009.png@01CCD05A.6B3E97C0
Clicking on View AlwaysOn Health Events will open a grid-view list for all the events tracked to date, you can sort these events by name or by timestamp by clicking on the column heading. Clicking/selecting on each event row will display the details on that particular event in the bottom section of page as shown below:
Description: cid:image011.png@01CCD05A.6B3E97C0
SQL Server 2012 has now two more SQL Server performance objects for performance monitoring:
  • SQLServer:Availability Replica – contains performance counters for reporting information about the availability replicas in availability groups.
  • SQLServer:Database Replica – contains performance counters for reporting information on the secondary databases on secondary replica of the availability group.
For those who prefer T-SQL, SQL Server 2012 also introduces several new catalog views and dynamic management views and extended events for managing and monitoring availability groups, replicas and databases, to learn more on these please check out msdn.

Manual Failover

You can configure your availability group for automatic failover or for manual failover. In case of automatic failover if the primary replica/instance does down the AlwaysOn availability group switches the role of primary replica/instance to another synchronous secondary replica.
You can also elect for manual failover. The failover will occur without data loss if both primary and secondary replicas are in synchronous commit mode and are in a healthy state, data loss will occur if the secondary replica is in asynchronous commit availability mode. To do a manual failover using the wizard, click on the Start Failover Wizard link on the top right corner of the availability group dashboard as shown below:
Description: cid:image009.png@01CCD05A.6B3E97C0
This will launch the failover wizard, the first screen of the wizard is the welcome screen so click Next to proceed:
Description: cid:image010.png@01CCD05A.6B3E97C0
On the Select New Primary Replica page of the Failover Availability Group wizard, you need to specify the secondary replica which will be the new primary replica after the failover, the screen will also display the failover readiness and availability mode (synchronous or asynchronous). Click next continue:
On the Connect to Replica page of the wizard, you need to connect to the secondary replica which you want to make the primary replica after failover, click on the Connect button to get connected to the secondary replica:
The Summary page of the Failover Availability Group wizard will display a summary on your selection and actions that wizard is going to perform. Click on Next to failover. You can also generate the failover T-SQL script by clicking on Script button at the bottom:
Clicking on Finish will start switching the role or performing the failover, you can monitor the progress and overall of status of operation as shown in screen below:
If I open the availability group dashboard again or refresh it, I can see role has now, my earlier primary replica (ARSHAD-PC) is now the secondary replica whereas my earlier secondary replica (ARSHAD-LP) is now the primary replica:
Please note, this example and demonstration is based on SQL Server 2012 RC0 and there might be slight changes in the RTM release; refer to BOL (Books online) or msdn for updated information.

Summary

In this article I discussed the AlwaysOn availability group feature of SQL Server 2012. AlwaysOn is a new High Availability (HA) and Disaster Recovery (DR) solution in SQL Server 2012 which improves high availability and protects data of your mission critical applications. AlwaysOn availability group is recommended over database mirroring since it combines the best of failover clustering and database mirroring and overcomes the several limitations imposed in failover clustering or database mirroring setup.
Source: