统计Wait_Queue

 

function  funhelp()
{
$helpText =@ "
NAME:Collect_dm_os_wait_stats.ps1
This script Collect Wait and Queue information from DMV sys.dm_os_wait_stats
This is a hardcode script , you don't need to specify any parameter. Please modify 'computerList' if you want to add more computers..
Make sure you have a Disk_database in the host where this script runs. And the two tables dm_os_wait_stats_xxx exist in the database.
SYNTAX:
Collect_dm_os_wait_stats.ps1
" @
Write
- Host  $helpText   - ForegroundColor Green

}


function  GetData([string] $servername , $tableName , $orderbyColumn )
{
# get statistics from target instance
  $sqlStatement   =   @ "
select top 20
       @@SERVERNAME
      ,[wait_type]
      ,[waiting_tasks_count]
      ,[wait_time_ms]
      ,[max_wait_time_ms]
      ,[signal_wait_time_ms]
from sys.dm_os_wait_stats
order by $orderbyColumn;
" @
$sqlStatement
# DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);

    
$SqlConnection   =  New - Object System.Data.SqlClient.SqlConnection 
    
$SqlConnection .ConnectionString  =   " Server=$servername;Initial Catalog=master;Trusted_Connection=True; "
    
$SqlCmd   = New - Object System.Data.SqlClient.SqlCommand
    
$SqlCmd .Connection = $SqlConnection
    
$SqlCmd .CommandText = $sqlStatement
    
$da =  New - Object System.Data.SqlClient.SqlDataAdapter
    
$da .SelectCommand = $SqlCmd
    
$ds = New - Object system.Data.DataSet
    
$da .Fill( $ds )


    
# purge into database
    SaveToDisk  $ds .Tables[ 0 $tableName
    
}

function  CleanWaitStats([string] $servername )
{
 
$sqlStatement   =   @ "
DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR)
" @

    
$SqlConnection   =  New - Object System.Data.SqlClient.SqlConnection 
    
$SqlConnection .ConnectionString  =   " Server=$servername;Initial Catalog=master;Integrated Security=SSPI; "
    
$SqlCmd   = New - Object System.Data.SqlClient.SqlCommand 
    
$SqlCmd .Connection = $SqlConnection
    
$SqlCmd .CommandText = $sqlStatement

                
    
$SqlConnection .Open()
    
$effectCount = $SqlCmd .ExecuteNonQuery()
    
$SqlConnection .Close()
    
}


function  SaveToDisk( $table , $tableName )
{
    
$SqlConnection   =  New - Object System.Data.SqlClient.SqlConnection 
    
$SqlConnection .ConnectionString  =   " Server=.;Initial Catalog=disk_database;Trusted_Connection=True; "
    
    
    
$sbc   =  New - Object System.Data.SqlClient.SqlBulkCopy( $SqlConnection )
    
$SqlConnection .Open()
    
$sbc .DestinationTableName  =   $tableName
    
    
$sbc .WriteToServer( $table )
    
" done "     
}



funhelp

$computerList = " db-back-read-05 " , " db-back-read-06 "

foreach ( $computer   in   $computerList )
{
    Write
- Host  " Collect data against $computer... "
    GetData 
$computer   " dm_os_wait_stats_waittime "   " wait_time_ms desc "
    GetData 
$computer   " dm_os_wait_stats_taskcounter "   " waiting_tasks_count desc "
    
    
# clear
    CleanWaitStats  $computer
}

# defination of dm_os_wait_stats_xxxx
#
CREATE TABLE [dbo].[dm_os_wait_stats_xxx](
#
    [serverName] [nvarchar](50) ,
#
    [wait_type] [nvarchar](60) NOT NULL,
#
    [waiting_tasks_count] [bigint] NOT NULL,
#
    [wait_time_ms] [bigint] NOT NULL,
#
    [max_wait_time_ms] [bigint] NOT NULL,
#
    [signal_wait_time_ms] [bigint] NOT NULL,
#
    [createDate] datetime  not null default getdate(),
#
    primary key (servername,wait_type,createDate)
#
) ON [PRIMARY]

 

你可能感兴趣的:(Queue)