<DataSourceModuleType ID="Microsoft.Windows.Server.2012.AD.Performance.Atq.AvgThreads.Monitor.DataSource" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Frequency" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WarnThreshold" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ErrorThreshold" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="NumSamples" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MaxPoolThreads" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Frequency" Selector="$Config/Frequency$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="WarnThreshold" Selector="$Config/WarnThreshold$" ParameterType="int"/>
<OverrideableParameter ID="ErrorThreshold" Selector="$Config/ErrorThreshold$" ParameterType="int"/>
<OverrideableParameter ID="NumSamples" Selector="$Config/NumSamples$" ParameterType="int"/>
<OverrideableParameter ID="MaxPoolThreads" Selector="$Config/MaxPoolThreads$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterPropertyBagSource">
<IntervalSeconds>$Config/Frequency$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//nologo $file/Atq_Thread_Usage.vbs$ $Config/ErrorThreshold$ $Config/WarnThreshold$ $Config/NumSamples$ $Config/MaxPoolThreads$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>Atq_Thread_Usage.vbs</Name>
<Contents><Script>'*************************************************************************
' Script Name - ATQ Thread Usage Monitor
'
' Purpose - Monitors the ATQ thread usage - LDAP and Other vs. Total
'
' (c) Copyright 2015, Microsoft Corporation, All Rights Reserved
' Proprietary and confidential to Microsoft Corporation
'*************************************************************************
Option Explicit
SetLocale("en-us")
Sub Main()
Dim oParams, oBag, sError, oAPI, nErrorThreshold, nWarningThreshold, nIntervals, nTotalUsage, nAverageUsage, nCount
Dim nMaxPoolThreads
Set oParams = WScript.Arguments
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
if oParams.Count < 4 then
sError = "The number of command line arguments is incorrect: " & vbCrLf & _
"Expected: 4" & vbCrLf & _
"Actual: " & oParams.Count
Err.Clear
Dim oWMI,oPerfInstance, oItem, nLogicalProcessors
' Connect to WMI
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
' Get the Win32_Processor class and find the number of logical processors
Set oPerfInstance = oWMI.ExecQuery("Select * From Win32_Processor")
If Err <> 0 Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Failure to retrieve the CPU Configuration via WMI: " & GetErrorString(Err)
Call oAPI.Return(oBag)
Exit Sub
End If
For Each oItem in oPerfInstance
nLogicalProcessors = oItem.NumberOfLogicalProcessors
Next
Dim nTotalThreadsAllowed
nTotalThreadsAllowed = nLogicalProcessors * nMaxPoolThreads
Err.Clear
On Error Resume Next
' Gather the performance data over the number of intervals specified
For nCount = 0 to nIntervals - 1
' Re-connect to WMI on each iteration
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
' Get the current snapshot
set oPerfInstance = oWMI.ExecQuery("Select * From Win32_PerfFormattedData_NTDS_NTDS")
If Err <> 0 Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Failure to retrieve the raw performance data for NTDS via WMI: " & GetErrorString(Err)
Call oAPI.Return(oBag)
Exit Sub
End If
For Each oItem in oPerfInstance
nAverageUsage = CDbl((oItem.ATQThreadsLDAP + oItem.ATQThreadsOther) / nTotalThreadsAllowed)
Next
' Add the usage to the total
nTotalUsage = nTotalUsage + nAverageUsage
' Wait one second before grabbing the next snapshot
Wscript.Sleep(1000)
next
' Calculate the average usage over the number of intervals
nAverageUsage = CDbl(nTotalUsage / nIntervals)
' Update the monitor based on the usage vs. threshold.
' If we're over the threshold, add the relevant data to be bubbled up into the alert body.
If nAverageUsage > nErrorThreshold Then
sError = "The ATQ Thread pool utilization is currently at: " & FormatNumber(nAverageUsage,2) & "%" & vbCrLf & _
"Intervals: " & nIntervals & vbCrLf & _
"Threshold: " & nErrorThreshold
oBag.AddValue "State", "BAD"
oBag.AddValue "Value", nAverageUsage
oBag.AddValue "ErrorString", sError
ElseIf nAverageUsage > nWarningThreshold Then
sError = "The ATQ Thread pool utilization is currently at: " & FormatNumber(nAverageUsage,2) & "%" & vbCrLf & _
"Intervals: " & nIntervals & vbCrLf & _
"Threshold: " & nWarningThreshold
oBag.AddValue "State", "WARN"
oBag.AddValue "Value", nAverageUsage
oBag.AddValue "ErrorString", sError
Else
oBag.AddValue "State", "GOOD"
oBag.AddValue "Value", nAverageUsage
End If
Call oAPI.Return(oBag)
End Sub
'******************************************************************************
Function GetErrorString(oErr)
'
' Purpose: Attempts to find the description for an error if an error with
' no description is passed in.
'
' Parameters: oErr, the error object
'
' Return: String, the description for the error. (Includes the error code.)
'
Dim lErr, strErr
lErr = oErr
strErr = oErr.Description
On Error Resume Next
If 0 >= Len(strErr) Then
' If we don't have an error description, then check to see if the error
' is a 0x8007xxxx error. If it is, then look it up.
Const ErrorMask = &HFFFF0000
Const HiWord8007 = &H80070000
Const LoWordMask = 65535 ' This is equivalent to 0x0000FFFF
If (lErr And ErrorMask) = HiWord8007 Then
' Attempt to use 'net helpmsg' to get a description for the error.
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If Err = 0 Then
Dim oExec
Set oExec = oShell.Exec("net helpmsg " & (lErr And LoWordMask))
Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i < 5)
strErr = strMessage
End If
End If
End If
GetErrorString = vbCrLf & "The error returned was: '" & strErr & "' (0x" & Hex(lErr) & ")"
End Function