Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Monitor Performance Counters using PowerShell in SQL Server Agent Job Step

$
0
0

By: Pablo Echeverria || Related Tips:More >Monitoring

Problem

You may relate to this scenario: a user calls-in reporting slowness in the SQL Server database and you hurry up to your monitoring software/custom stored procedures, only to find out there have not been issues at all and the SQL Server queries are returning quickly, so you’re left wondering where did the slowness occur?

And what about the following error messages that are reported in the error log?

A significant part of SQL Server process memory has been paged out link . SQL Server has encountered x occurrence(s) of I/O requests taking longer than 15 seconds to complete link .

As you may already know, there are multiple hardware components that can fail or be misconfigured, and that could be the reason for the slowness, so you need to include them as part of your monitoring strategy as suggested here and here . Or you can deploy a monitoring software as suggested here .

But is there a way to know if there are issues right now, without having to install monitoring software that you have not tested yet, and without having to install/configure it in all your database instances when you have a lot?

Solution

You can create a multi-server job that you can deploy quickly in all the servers in your farm, but you need to be aware that the command “Get-Counter” is unavailable from SQLPS.exe when you run a PowerShell type job step, so you need to use the solution describedhere to be able to get the performance counters information.

Note that there is a lot of advice about what counters you need to measure and various thresholds (i.e. thislink), so in the script below is a list of the counters and thresholds that make more sense to me.

Also, note that this will only help you determine if there’s any trend, in no way this will be a substitute for a monitoring tool that is more comprehensive.

The script works as follows:

The SQL Server Agent job is scheduled to run every 15 minutes. At that moment, the script will gather the performance counters every second for 15 seconds, which can be modified in the variables $sampleInterval and $maxSamples . The results are averaged and rounded to 2 decimals. There are exclusion lists maintained at the script level (so we don’t need to maintain an exclusion list locally on each instance). This is to prevent your email from being flooded with never-ending alerts if there’s a known issue that is still being addressed. This can be modified in the variables $perfMonExclusions, $perfMonExclusionsProcessor, $perfMonExclusionsMemory, $perfMonExclusionsDisk and $perfMonExclusionsNetwork . The performance counters are: VM-related: As suggested in this link , and we search in the available perf counters if these are available so it won’t throw an error if they don’t. \VM Processor(_Total)\% Processor Time: must be lower than 80% \VM Processor(_Total)\CPU Stolen Time: must be lower than 40 milliseconds \VM Memory\Memory Ballooned in MB: this memory can’t be used \VM Memory\Memory Swapped in MB: this memory can’t be used SQL Server specific: Because they vary from SQL Server version as suggested in this link , we search if these are available so it won’t throw an error if they don’t. \SQLServer:Buffer Manager\total pages: used to get the memory used by SQL Server (prior to 2012) \SQLServer:Memory Node(000)\Total Node Memory (KB): used to get the memory used by SQL Server \SQLServer:Buffer Manager\Page life expectancy The ratio between the memory used by SQL Server and page life expectancy as suggested in this link must be lower than 20 MB/sec. Processor \System\Processor Queue Length: must be lower than the number of cores \Processor(_Total)\% Processor Time: must be lower than 80% \Processor(_Total)\% Privileged Time: must be lower than 80% \Process(*)\% Processor Time: used to display the process consuming most CPU Memory \Memory\Available Bytes: must be greater than 10% of total memory \Process(*)\Working Set: used to display the process consuming most memory \Memory\Cache Bytes \Memory\Pool Nonpaged Bytes The sum of the 4 above is the total memory \Paging File(_Total)\% Usage: must be lower than 90% \Memory\Pages/sec: must be lower than 25 Disk: as suggested in thislink \PhysicalDisk(*)\Avg. Disk sec/Read: must be lower than 15 milliseconds \PhysicalDisk(*)\Avg. Disk sec/Write: must be lower than 15 milliseconds \PhysicalDisk(*)\Disk Bytes/sec: must be greater than 200 MB \PhysicalDisk(*)\Current Disk Queue Length Network \Network Interface(*)\Current Bandwidth \Network Interface(*)\Bytes Received/sec: must be lower than 80% bandwidth \Network Interface(*)\Bytes Sent/sec: must be lower than 80% bandwidth \Network Interface(*)\Output Queue Length: must be lower than 2 \Network Interface(*)\Packet Outbound Errors: must be zero \Network Interface(*)\Packet Received Errors: must be zero I/O (file, network and device) \Process(*)\IO Data Bytes/sec: used to display the process consuming most I/O The results are sent by email. The parameters can be modified in the variables $reportBody , $mailServer , $mailFrom , $mailTo , $mailSubject and $reportHeader . Script

Here is the complete script that must be placed in a SQL Server Agent job step of type PowerShell:

$ErrorActionPreference = "Stop"
$server = "$(ESCAPE_DQUOTE(SRVR))"
$sampleInterval = 1
$maxSamples = 15
$numberOfHeaderRowsToSkip = 1
$date = Get-Date
$reportBody =

Viewing all articles
Browse latest Browse all 3160

Trending Articles