SQL Server Real-time Monitoring using WMI classes: Part -1

Who is online?  135 guests and 0 members
home  »  blogs  »  Abhay Chaudhary  »  SQL Server Real-time Monitoring using WMI classes: Part -1
  Rate This Blog Entry:  register  or  login

Author

Abhay_c Abhay Chaudhary (Member since: 11/19/2011 7:36:26 AM)

View Abhay Chaudhary 's profile

Comments (27)

AhmadOsama
2/18/2012 7:00:16 PM Ahmad Osama said:

Hey Abhay - nice one and a coincedence too..was working on the same topic for my next blog post..  Smile 

by
Abhay_c
2/20/2012 9:26:59 AM Abhay Chaudhary said:

Thanks Ahmad

Kind Regards

Abhay

by
Ashwin
2/20/2012 9:41:31 AM Ashwin said:

Hi Abhay,

This is a nice one. I have tried out many different things to capture blocking info when it happens and finally after lots of try had to run a job every minute which checks whether blocking is there for more than 1 minute and email the DBA's with the snapshot of sysprocesses. But your trick sounds better. I will surely try this in our environment. Just thinking if you blogged this 3-4 months earlier , I would have saved lots of my time trying different things which never worked ;)

Ashwin

by
Abhay_c
2/20/2012 9:53:46 AM Abhay Chaudhary said:

Thanks Ashwin ..Let me know if you get stuck anywhere ...

Kind Regards

Abhay

by
Ravikanth
2/20/2012 11:07:12 AM Ravikanth said:

Great article on the T-SQL way of doing this. I am not an expert (not even a beginner) in the SQL space. S, this TSQL code looks very intimidating to me. I prefer doing the same in PowerShell. It is equally powerful and lets me do it outside of the box I am monitoring and remotely. PowerShell eventing lets you take advantage of this and you can event run scripts remotely as a response to an event.

For example, Get-WMIObject -Namespace root\Microsoft\SqlServer\ServerEvents\SQLEXPRESS -Query "SELECT * FROM meta_class WHERE (__THIS ISA '__Event')" all the SQL event classes for the Express Instance I have on my laptop. Now, using the event classes and Register-WMIEvent cmdlet, we can easily create a monitoring solution that responds to an event and does something. 

As I said, I am not an expert in SQL area. So, I don't really know why I have to use T-SQL as compared to PowerShell for WMI event monitoring. Just sharing my 2 cents! I will see if I can quickly blog a part of your solution in PowerShell.

Ravi

by
Abhay_c
2/20/2012 11:56:46 AM Abhay Chaudhary said:

Thanks Ravi . You are bang on the target .In my next blog I will be discussing on using different SQL Server related and OS related (using simple WQL) classes to create any kind of alert we want, through SQL Server .This will use the TargetMachine, ISA ,etc clauses ... I have infact a monitoring solution ready (written in VB .Net ) and working that we use to deploy these scripts on standalone and centralized monitoring instances .This becomes even more fruitful if we use it through an instance that is used only for monitoring other instances on different servers(i.e. Centralized monitoring instance).

Instead of using SQL Server we can use (I can use actually ) VB scripts to do the same .However, integrating it with SQL had the benefit of not only capturing the alert but also take the actions based on this like running jobs that captures the data in the table , sending mail using database mail (rather than using CDO objetcs ) .SO this basically gave me all the options at one place .Will try to cover as much as I can .

Waiting eagerly for the powershell blog(s) :) .

Kind Regards

Abhay

by
Ravikanth
2/20/2012 12:05:48 PM Ravikanth said:

Aah, I see. Nice to know your plans. Using PowerShell 2.0, you can do everything in one place. Send-MailMessage and other cmdlets make it possible!

by
Nitin
2/20/2012 3:35:18 PM Nitin said:

Nice post

In order to obtain deadlock graph by whatever means, youll need to have one of the traceflag 1204 or 1205 or 1224 turned on.

by
Abhay_c
2/20/2012 3:48:24 PM Abhay Chaudhary said:

Incorrect :) ........You can use deadlock_graph class .There is no need to use any traceflag ...Wait for my future blogs :) ..

Kind Regards

Abhay

by
Ankit Shah
2/20/2012 4:08:57 PM Ankit Shah said:

Hi ..

Thanks for posting wonderful script to detect deadlocks .I am working on this script . 

getting one error when the job starts run 

"Unable to start execution of step 1 (reason: Variable WMI(HostName) not found).  The step failed."

 

Any Idead about this ?

by
kin
2/21/2012 9:16:28 PM kin said:

Nice article. I have designed the same solution for my new company last year that has both Blocking and Deadlock detection in place.

The beauty of this solution is that you do not have to enable any deadlock trace flags... 

Just 2 cents from my side, in sp_add_alert, you are better off adding a delay for e.g 5 mins (300 Secs)  or whatever fits in your environment by using @delay_between_responses=300. This way you dont receive unnecessay emails Smile

HTH,

Kin

by
Abhay_c
2/22/2012 4:01:19 AM Abhay Chaudhary said:

Thanks Kin ....In this solution you dont need to use the delay_between_responses option because 1) I have kept the blocking threshold to 5 seconds , and 2) I have kept the duration to less than 8 seconds .Now what happens is that internally the blocking setection thread wakes up every 5 seconds and + some time that it might take for the overhead .This is the reason I have kept the extra buffer of 3 seconds in case the server is busy or slow.So anyone will get only ONE alert for ONE blocking ....these 2 values can always be modified ...

Initially I was wanting to use the delay_between_responses .But that will send you redundant blocking mails (chances are less) + during the 5 minutes wait (300 seconds) if any blocking occurs you might not get the response .... So ideally it will wake up every 5 minutes to deliver the blocking information if at that time its happeneing ..whereas this solution will send you mail for every blocking for sure ..

Kind Regards

Abhay

by
Abhay_c
2/22/2012 4:03:06 AM Abhay Chaudhary said:

If you read my second post , I have mentioned the deadlock example as well .And yes , you are right ; there is no need of any traceflag ..

Kind Regards

Abhay

by
Jorge
3/1/2012 7:52:42 PM Jorge said:

Hi there: Getting same msg that was already reported

Unable to start execution of step 1 (reason: Variable WMI(SQLInstance) not found).  The step failed

by
Abhay_c
3/2/2012 4:11:48 AM Abhay Chaudhary said:

1  : Have you enabled the token replacement policy ?

2: What is the SQL Sevrer version and build ?

3:Did you try again after restarting SQL Server agent ...

by
kin
3/21/2012 9:11:18 PM kin said:

Abhay,

one more thing ...

SELECT * FROM BLOCKED_PROCESS_REPORT Where Duration <=8000000

should it not be >=  as the duration is the amount of time (in milliseconds) that the process was blocked.

So you what all the spids that are blocking for more than certain value.

HTH,

Kin

by
Abhay_c
3/22/2012 6:46:12 AM Abhay Chaudhary said:

Kin ,

The whole idea was to capture the blocking which is greater than 5 seconds.This is controlled by keeping the blocked process threshold value to 5 in sysconfigurations via sp_configure .The reason I have kept the duration of 8 seconds is because I only want this blocking to be captured ONCE and not more than that .If you set this value to 10 or 11seconds then there is a very high chance of getting redundant blocking alerts .You can test this yourself .The reason of keeping it higher than sp_configure values is this : you may have blocking happening but to detect that blocking there is a background SPID that needs to wake up every 5 seconds (due sp_configure setting ) ..I have seen that sometimes Blocking is detected a bit late due to the fact that blocking capturing spid initiated might be after the 1st or 2nd second of actual blocking .But since 5+2 is 7 we are still with in the range as we have kept it to 8 second .This way the chances of missing a blocking alert is almost NIL ..

HTH

Kind regards

Abhay

by
Jan Arnoldus
4/14/2012 12:43:46 AM Jan Arnoldus said:

Hi Abhay,

Thanks for sharing this piece of great work. Just a recommendation:

@server is derived from SERVERPROPERTY('ComputerNamePhysicalNetBIOS'). What would happen if I run it on a sql cluster? I set it up on the active node of a a sql cluster checked the alert's properties. It shows the physical hostname of the active node. Now if there is a failover to the passive node, the name space wouldn't be correct. I don't have a lab sql cluster to test and thought you might have an answer.

Thanks.

 

 

by
Jan Arnoldus
4/14/2012 12:45:57 AM Jan Arnoldus said:

Hi Abhay,

 

Thanks for sharing this piece of great work. Just a recommendation:

 

@server is derived from SERVERPROPERTY('ComputerNamePhysicalNetBIOS'). 

 

What would happen if I run it on a sql cluster? 

 

I set it up on the active node of a a sql cluster checked the alert's properties. 

It shows the physical hostname of the active node. Now if there is a failover to 

the passive node, the name space wouldn't be correct. I don't have a lab sql 

cluster to test and thought you might have an answer.

 

Thanks.

by
Abhay_c
4/15/2012 3:40:12 PM Abhay Chaudhary said:

HI Jan, You have a valid point ...I need to test this on a cluster ...But we can add a check for this in the code (should'nt be a problem) ...IN the mean time run it on both the nodes and when the instance fails over the alert will still work ..

Regards

Abhay

by
Abhay_c
4/15/2012 3:42:23 PM Abhay Chaudhary said:

Try using MachineName rather than netbiosname.This should work ....

Regards

Abhay

by
Moin
6/4/2012 5:42:50 AM Moin said:

Hi Abhay, Thanks for one more beautiful post. I am a regular follower of your blogs and also attended recent webcast on transactional replication you have conducted.

 

I see this post very promising to the DBAs. I would like to know the impact of these WMI alerts on production machines. Do we need to take care of anything from that point of view. please advise.

by
Abhay_c
6/4/2012 6:03:01 AM Abhay Chaudhary said:

Moin , 

Thanks for your kind words ...

In my environment ,I wrapped several such WMI scripts + Reports + Capacity mgt reports with a VB .net frontend tool .I can say that one of the client is monitoring around 60+ instances through a centralized SQL Server with 4GB RAM , 1 Quad core CPU and 50 GB Storage .I normally take this as a benchmark..The resource consumprion should be much less as comparision to the third party tools .

Kind Regards

Abhay

by
Sachin
6/4/2012 6:54:24 PM Sachin said:

Just out of curiosity why do all this and maintain such a heavy script ?

Cant we just enable block thresold using sp_configure and create a event notification which will trigger a mail if a block occurs.Same thing can be done for deadlocks also.Hardly 4 to 5 line code.

by
Abhay_c
6/5/2012 3:56:25 AM Abhay Chaudhary said:

Sachin,

It makes sense to just create an alert on the event and get notified .I have already mentioned if certain part of the code is not needed it can be comented out .However , I as an admin would never want an email alone stating that there is a deadlock or blocking .I would always prefer to know more about who is causing blocking , who are the head blokers , What are the resources like page , extent , row , table ,etc participating in blocking .Is the stats of the participating tables out of date or have less sampling .Are the indexes of the participating tables fragemnted ...Once we know that there is blocking , we will do all these things to find out the root cause and that is the reason that I have added all these options here for the community members .Its up to them what to pick and what not to pick ..for example ,In our enviornment where people talks in TBs , we cannot afford to find the index fragemntation and hence I dont use it ..

Kind Regards

Abhay

by
Sachin
6/5/2012 5:02:32 AM Sachin said:

I am definately not trying to say here that only a notification(email) would suffice.We can do more than that.The whole default blocked process report can be sent out in a mail through event notification which gives you more than enough information to deep dive the causes of blocking.

Between never heard that index fragementation also causes blocking.Yes it does hamper performance but never saw/heard a direct relationship beetwen blocking and index fragementation.

by
Abhay_c
6/5/2012 5:36:25 AM Abhay Chaudhary said:

I think a ping pong has started which is not useful..Can you give me your email address so that I can send you a detailed mail to address your concerns.

Kind Regards

Abhay

by

Leave a comment

Name:
  *  
Email:  (your email is kept secure)
 *  
Website:

example: "http://www.SQLServerGeeks.com"

 
Type the characters:
 *
 
   

Training on Microsoft Products & Technologies

Email Subscriptions

   Get the Most Recent Blogs in your inbox
Blogs RSS Feed

Abhay Chaudhary's latest blog posts

Blogs RSS Feed

Latest community blog posts

  • This is an update to the DB-Migrate ( http://www.sqlservergeeks.com/blogs/AhmadOsama/personal/797/db-migrate-a-powershell-module-to-migrate-databases ) powershell module to migrate database between sq...
  • This blog is part of the series The TSQL Classes: Stored Procedure The links to previous post are given below. http://sqlservergeeks.com/blogs/AhmadOsama/personal/762/the-tsql-classes-stored-procedure...
  • Reporting With SSRS Part 1 : In this serires of blogs, i will try to introduce you with some of the key features of the tool which will help you get on board to become a proficient report developer.If...
  • Hi Folks, I am sure that all of you would be aware about the role of Identity column in a table, i.e., it is column which keeps on incrementing without supplying the value explicitly during insertion....
  • Recently I wrote powershell scripts to move databases, logins and SQL Agent Jobs between instances. Another one I wrote was to fix orphan users. I have now combined all of these different functions in...
  • Hi SQL Geeks, Here are the blog posts by Ahmad Osama for the month of April. We would expect more of his informative blogs this month. Happy Learning! If you like our blogs do like us on Facebook at h...
  • This blog is part of the series The TSQL Classes: Stored Procedure The link to previous post are given below. http://sqlservergeeks.com/blogs/AhmadOsama/personal/762/the-tsql-classes-stored-procedures...
  • As DBAs we are always challenged of unexpected size growth of log or data files and need to shrink files based on available free space in each file. Most of the times we are stuck and do a lot of scri...
  • One of the major migration activities is to fix orphan users. Though, it can be easily fixed by sp_change_users_login procedure, however what if you need to do this for multiple servers. A powershell ...
  • This blog is part of the series The TSQL Classes: Stored Procedure. http://www.sqlservergeeks.com/blogs/AhmadOsama/personal/764/the-tsql-classes-stored-procedures-nested-proc-recursion-and-plan-cachin...