AD CPU Overload Datasource

AD_CPU_Overload.DataSource (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterPropertyBagSource Default

Overrideable Parameters:

IDParameterTypeSelector
Frequencyint$Config/Frequency$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<DataSourceModuleType ID="AD_CPU_Overload.DataSource" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element name="Frequency" type="xsd:int"/>
<xsd:element name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Frequency" Selector="$Config/Frequency$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" 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/AD_LSASS_CPU.vbs$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>AD_LSASS_CPU.vbs</Name>
<Contents><Script>
Option Explicit

'Event Constants
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

'Event ID Constants
Const EVENTID_SCRIPT_ERROR = 1000

Const SCRIPT_NAME = "AS_LSASS_CPU.vbs"

Dim colOSes, colProcessors, nCores, colProcesses, nProcTime, nAdjustedProcTime
Dim oAPI, oBag, oWMI, oProcessor, oProcess

Sub Main()
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()

On Error Resume Next

' Connect to WMI
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
If Err &lt;&gt; 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " &amp; SCRIPT_NAME &amp; _
" failed to connect to WMI. The error was: " &amp; GetErrorString(Err)
Else

' Get the processor information
Set colProcessors = oWMI.ExecQuery("Select NumberOfCores from Win32_Processor")
If Err &lt;&gt; 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " &amp; SCRIPT_NAME &amp; _
" failed query WMI. The error was: " &amp; GetErrorString(Err)
Else
' Get the number of cores in the system
nCores = 0
For Each oProcessor in colProcessors
nCores = nCores + oProcessor.NumberOfCores
Next

If Err &lt;&gt; 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " &amp; SCRIPT_NAME &amp; _
" failed to retrieve the number of CPU cores. The error was: " &amp; GetErrorString(Err)
Else
Set colProcesses = oWMI.ExecQuery("Select PercentProcessorTime from " _
&amp; "Win32_PerfFormattedData_PerfProc_Process Where name='lsass'")
If Err &lt;&gt; 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " &amp; SCRIPT_NAME &amp; _
" failed to query WMI for the LSASS utilization. The error was: " &amp; GetErrorString(Err)
Else
For Each oProcess in colProcesses
nProcTime = oProcess.PercentProcessorTime
Next
If Err &lt;&gt; 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " &amp; SCRIPT_NAME &amp; _
" failed to retrieve the LSASS utilization. The error was: " &amp; GetErrorString(Err)
Else
nAdjustedProcTime = CDbl(nProcTime) / CDbl(nCores)
If Err &lt;&gt; 0 Then
CreateEvent EVENTID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script " &amp; SCRIPT_NAME &amp; _
" failed to calculate the LSASS utilization. The error was: " &amp; GetErrorString(Err)
Else
Call oBag.AddValue("Value", CInt(nAdjustedProcTime))

Call oAPI.Return(oBag)
End If
End If
End If
End If
End If
End If
End Sub


'******************************************************************************
Sub CreateEvent(lEventID, lEventType, strMessage)
'
' Purpose: Create a new event.
'
' Parameters: lEventID - The numerical ID of the event to create
' lEventType - The numerical ID of the event type
' strMessage - The message to include in the event
'
oAPI.LogScriptEvent "AD CPU Overload", lEventID, lEventType, strMessage

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 &gt;= 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 = &amp;HFFFF0000
Const HiWord8007 = &amp;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 " &amp; (lErr And LoWordMask))

Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i &lt; 5)

strErr = strMessage
End If
End If
End If

GetErrorString = vbCrLf &amp; "The error returned was: '" &amp; strErr &amp; "' (0x" &amp; Hex(lErr) &amp; ")"
End Function



Call Main()
</Script></Contents>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>