BizTalk Application Artifact Run-Time State Monitoring Module

Microsoft.BizTalk.Server.2016.Monitor.ArtifactSuspendedInstances.DataSource (DataSourceModuleType)

This module provides data for monitoring the run-time state of BizTalk application artifacts--specifically, the number of suspended instances per artifact.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsMicrosoft.BizTalk.ProbeAccount
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterPropertyBagSource Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval SecondsThis is the interval (in seconds) at which the script associated with the module is run.
StartDateTimestring$Config/StartDateTime$Start DateThis is the date from which suspended instances are taken into account. Along with end date this forms a window between which all suspended instances for an artifact are taken into account for health computation.
EndDateTimestring$Config/EndDateTime$End DateThis is the date till which suspended instances are taken into account. Along with start date this forms a window between which all suspended instances for an artifact are taken into account for health computation.
SuccessLimitint$Config/SuccessLimit$Success LimitMonitor state is success if the number of suspended instances is less than or equal to this value.
WarningLimitint$Config/WarningLimit$Warning LimitMonitor state is warning if the number of suspended instances is greater than success limit and less than or equal to this value. Monitor state is error if the number of suspended instances is greater than this value.
LogSuccessEventstring$Config/LogSuccessEvent$Log Success EventAn event is logged based on successful completion of the script associated with the module when value is set to 'true'.
TimeoutSecondsint$Config/TimeoutSeconds$Timeout SecondsThis is the timeout (in seconds) after which execution of the script associated with the module is terminated if not yet completed.

Source Code:

<DataSourceModuleType ID="Microsoft.BizTalk.Server.2016.Monitor.ArtifactSuspendedInstances.DataSource" Accessibility="Public" RunAs="Microsoft.BizTalk.ProbeAccount" Batching="false">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:integer"/>
<xsd:element name="ApplicationID" type="xsd:integer"/>
<xsd:element name="ArtifactType" type="xsd:string"/>
<xsd:element name="StartDateTime" type="xsd:string"/>
<xsd:element name="EndDateTime" type="xsd:string"/>
<xsd:element name="SuccessLimit" type="xsd:integer"/>
<xsd:element name="WarningLimit" type="xsd:integer"/>
<xsd:element name="LogSuccessEvent" type="xsd:boolean"/>
<xsd:element name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="StartDateTime" Selector="$Config/StartDateTime$" ParameterType="string"/>
<OverrideableParameter ID="EndDateTime" Selector="$Config/EndDateTime$" ParameterType="string"/>
<OverrideableParameter ID="SuccessLimit" Selector="$Config/SuccessLimit$" ParameterType="int"/>
<OverrideableParameter ID="WarningLimit" Selector="$Config/WarningLimit$" ParameterType="int"/>
<OverrideableParameter ID="LogSuccessEvent" Selector="$Config/LogSuccessEvent$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterPropertyBagSource">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//nologo $file/BizTalkArtifactSuspendedInstancesMonitor.vbs$ "$Config/ApplicationID$" "$Config/ArtifactType$" "$Config/StartDateTime$" "$Config/EndDateTime$" "$Config/SuccessLimit$" "$Config/WarningLimit$" "$Config/LogSuccessEvent$" "$Target/Host/Host/Property[Type="Microsoft.BizTalk.Server.2016.BizTalkGroup"]/MgmtDbServerName$" "$Target/Host/Host/Property[Type="Microsoft.BizTalk.Server.2016.BizTalkGroup"]/MgmtDbName$"</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>BizTalkArtifactSuspendedInstancesMonitor.vbs</Name>
<Contents><Script>
'Copyright (c) Microsoft Corporation. All rights reserved

'This script generates monitoring data for BizTalk application artifact runtime state
'based on number of suspended instances per artifact. Each artifact will be in one of
'the three monitoring states - success, warning, error.

Option Explicit

Const MGMTDB_CONNECT_FAILURE = -1
Const MGMTDB_QUERY_FAILURE = -2
Const MSGBOXDB_CONNECT_FAILURE = -3
Const MSGBOXDB_QUERY_FAILURE = -4
'Event Constants
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4
'Other constants
Const SCRIPT_NAME = "BizTalk Server Suspended Instances Monitor"
' Event ID Constants
Const EVENTID_SUCCESS = 99
Const EVENTID_SCRIPT_ERROR = 1000

Dim oAPI, oBagState
Dim oParams, ApplicationID, ArtifactType, StartDateTime, EndDateTime, SuccessLimit, WarningLimit, bLogSuccessEvent
Dim strQuery, strArtifactCategory, MgmtDbName, MgmtDbServerName
Dim strMonitorStatus, strErrorDetail
Dim dtStart, strMessage

dtStart = Now

Set oParams = WScript.Arguments
If oParams.Count &lt; 9 then
Wscript.Quit -1
End if

ApplicationID = CInt(oParams(0))
ArtifactType = oParams(1)
StartDateTime = oParams(2)
EndDateTime = oParams(3)
SuccessLimit = CInt(oParams(4))
WarningLimit = CInt(oParams(5))
bLogSuccessEvent = CBool(oParams(6))
MgmtDbServerName = oParams(7)
MgmtDbName = oParams(8)

'warning limit should always be &gt;= success limit
if WarningLimit &lt; SuccessLimit then
Wscript.Quit -1
End if

if ArtifactType = "" then
ArtifactType = "0"
end if
if StartDateTime = "" then
StartDateTime = #1/1/1753#
end if
if EndDateTime = "" then
EndDateTime = Now()
end if
strMonitorStatus = "0"
strErrorDetail = ""

Set oAPI = CreateObject("Mom.ScriptAPI")
Set oBagState = oAPI.CreatePropertyBag()
Dim SPDictionary
Set SPDictionary = CreateObject("Scripting.Dictionary")

Call GetMonitorConfig()
Call GetSuspendedInstances()
GetMonitorStatus

Sub GetMonitorStatus
Dim key, count
for each key in SPDictionary.Keys
count = CInt(SPDictionary.Item(key))
if count &lt;= SuccessLimit then
oBagState.AddValue key &amp; "/State", 2
strMessage = strMessage &amp; " , " &amp; key &amp; "/State:2 " &amp; count
else
if count &lt;= WarningLimit then
oBagState.AddValue key &amp; "/State", 1
strMessage = strMessage &amp; " , " &amp; key &amp; "/State:1 " &amp; count
else
oBagState.AddValue key &amp; "/State", 0
strMessage = strMessage &amp; " , " &amp; key &amp; "/State:0 " &amp; count
end if
end if
Next

If bLogSuccessEvent Then
CreateEvent EVENTID_SUCCESS, EVENT_TYPE_INFORMATION, strMessage
strMessage = "The script '" &amp; SCRIPT_NAME &amp; " : " &amp; strArtifactCategory &amp; "' completed successfully in " &amp; DateDiff("s", dtStart, Now) &amp; " seconds."
CreateEvent EVENTID_SUCCESS, EVENT_TYPE_INFORMATION, strMessage
End If

oAPI.AddItem oBagState
Call oAPI.ReturnItems
End Sub

Function GetSuspendedInstances()
Dim defaultConnStr
defaultConnStr = "Server=" &amp; MgmtDbServerName &amp; ";Database=" &amp; MgmtDbName &amp; ";Trusted_Connection=yes"
Dim objResults1, objResults2

Dim ObjError
Set ObjError = New Error

Dim cnADOConnection
Set cnADOConnection = MomCreateObject("ADODB.Connection")
cnADOConnection.Provider = "SQLNCLI11"
cnADOConnection.ConnectionTimeout = 15

ObjError.Clear
On Error Resume Next
cnADOConnection.Open defaultConnStr
ObjError.Save
On Error Goto 0
If 0 &lt;&gt; Err.number then
GetSuspendedInstances = MGMTDB_CONNECT_FAILURE
Exit Function
End If

ObjError.Clear
On Error Resume Next
set objResults1 = cnADOConnection.Execute("select DBServerName, DBName from adm_MessageBox with (NoLock)")
ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then
GetSuspendedInstances = MGMTDB_QUERY_FAILURE
If (objResults1 &lt;&gt; null) Then objResults1.Close
Exit Function
End If

ObjError.Clear
On Error Resume Next
set objResults2 = cnADOConnection.Execute(strQuery)
ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then
GetSuspendedInstances = MGMTDB_QUERY_FAILURE
If (objResults1 &lt;&gt; null) Then objResults1.Close
If (objResults2 &lt;&gt; null) Then objResults2.Close
Exit Function
End If

Dim params
params = Array()
ReDim params(33)
params(0) = 5
params(1) = 0
params(2) = "{00000000-0000-0000-0000-000000000000}"
params(3) = null
params(4) = 0
params(5) = null
params(6) = 0
params(7) = null
params(8) = 0
params(9) = null
params(10) = 1
params(11) = 36
params(12) = 1
params(13) = null
params(14) = 0
params(15) = null
params(16) = null
params(17) = StartDateTime
params(18) = EndDateTime
params(19) = null
params(20) = 0
params(21) = null
params(22) = 0
params(23) = null
params(24) = 0
params(25) = StartDateTime
params(26) = EndDateTime
params(27) = null
params(28) = 0
params(29) = 0
params(30) = 0
params(31) = 50
params(32) = "{00000000-0000-0000-0000-000000000000}"
params(33) = 0

Dim command
Dim cnADOMsgBoxConnection
Set cnADOMsgBoxConnection = MomCreateObject("ADODB.Connection")
cnADOMsgBoxConnection.Provider = "SQLNCLI11"
cnADOMsgBoxConnection.ConnectionTimeout = 15

Do While Not objResults1.EOF
defaultConnStr = "Server=" &amp; CStr(objResults1(0)) &amp; ";Database=" &amp; CStr(objResults1(1)) &amp; ";Trusted_Connection=yes"

ObjError.Clear
On Error Resume Next
cnADOMsgBoxConnection.Open defaultConnStr
ObjError.Save
On Error Goto 0
If 0 &lt;&gt; Err.number then
If (objResults1 &lt;&gt; null) Then objResults1.Close
If (objResults2 &lt;&gt; null) Then objResults2.Close
GetSuspendedInstances = MSGBOXDB_CONNECT_FAILURE
Exit Function
End If

Do While Not objResults2.EOF
params(9) = CStr(objResults2(0))

set command = CreateObject("ADODB.Command")
command.ActiveConnection = cnADOMsgBoxConnection
command.commandText = "ops_OperateOnInstances"
command.commandType = 4
command.Parameters.Refresh
dim param, i
For i = 0 To UBound(params)
command.Parameters(i+1).Value = params(i)
Next
Dim rs
set rs = command.Execute
If Not (SPDictionary.Exists(CStr(objResults2(1)))) Then
SPDictionary.Add CStr(objResults2(1)), CInt(rs(0))
else
SPDictionary.Item(CStr(objResults2(1))) = SPDictionary.Item(CStr(objResults2(1))) + CInt(rs(0))
End If
rs.Close
objResults2.MoveNext
Loop
cnADOMsgBoxConnection.Close
objResults1.MoveNext
Loop

cnADOConnection.Close
End Function

Function GetMonitorConfig()
Select Case ArtifactType
Case "0" 'All application artifacts
strQuery = "select orc.uidGUID as GUID, orc.nvcFullName+N', '+asm.nvcName+N', '+asm.nvcVersion+N', '+asm.nvcCulture+N', '+asm.nvcPublicKeyToken as Name from bts_orchestration orc with (NoLock) " &amp; _
"inner join bts_item itm with (NoLock) on orc.nItemID = itm.id " &amp; _
"inner join bts_assembly asm with (NoLock) on asm.nID = itm.AssemblyId " &amp; _
"left join adm_Host hs with (NoLock) on orc.nAdminHostID = hs.Id " &amp; _
"where asm.nApplicationID = " &amp; ApplicationID &amp; _
"union " &amp; _
"select uidGUID as GUID, nvcName as Name from bts_sendport with (NoLock) where nApplicationID = " &amp; ApplicationID &amp; _
"union " &amp; _
"select uidGUID as GUID, nvcName as Name from bts_receiveport with (NoLock) where nApplicationID = " &amp; ApplicationID
strArtifactCategory = "All"
Case "1" 'Orchestration
if ApplicationID = -1 then
strQuery = "select orc.uidGUID as GUID, orc.nvcFullName+N', '+asm.nvcName+N', '+asm.nvcVersion+N', '+asm.nvcCulture+N', '+asm.nvcPublicKeyToken as Name from bts_orchestration orc with (NoLock) " &amp; _
"inner join bts_item itm with (NoLock) on orc.nItemID = itm.id " &amp; _
"inner join bts_assembly asm with (NoLock) on asm.nID = itm.AssemblyId " &amp; _
"left join adm_Host hs with (NoLock) on orc.nAdminHostID = hs.Id"
else
strQuery = "select orc.uidGUID as GUID, orc.nvcFullName+N', '+asm.nvcName+N', '+asm.nvcVersion+N', '+asm.nvcCulture+N', '+asm.nvcPublicKeyToken as Name from bts_orchestration orc with (NoLock) " &amp; _
"inner join bts_item itm with (NoLock) on orc.nItemID = itm.id " &amp; _
"inner join bts_assembly asm with (NoLock) on asm.nID = itm.AssemblyId " &amp; _
"left join adm_Host hs with (NoLock) on orc.nAdminHostID = hs.Id " &amp; _
"where asm.nApplicationID = " &amp; ApplicationID
end if
strArtifactCategory = "Orchestration"
Case "2" 'Send Port
if ApplicationID = -1 then
strQuery = "select uidGUID as GUID, nvcName as Name from bts_sendport with (NoLock)"
else
strQuery = "select uidGUID as GUID, nvcName as Name from bts_sendport with (NoLock) where nApplicationID = " &amp; ApplicationID
end if
strArtifactCategory = "Send Port"
Case "3" 'Receive Port
if ApplicationID = -1 then
strQuery = "select uidGUID as GUID, nvcName as Name from bts_receiveport with (NoLock)"
else
strQuery = "select uidGUID as GUID, nvcName as Name from bts_receiveport with (NoLock) where nApplicationID = " &amp; ApplicationID
end if
strArtifactCategory = "Receive Port"
End Select
End Function

Sub CreateEvent(lEventID, lEventType, strMessage)
oAPI.LogScriptEvent SCRIPT_NAME, lEventID, lEventType, strMessage
End Sub

Function GetWMICollection(TargetComputerToUse, strNamespace, strQuery)
Dim WbemSrv, WbemObjectSet
Set WbemSrv = Getobject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; TargetComputerToUse &amp; strNamespace)
Set WbemObjectSet = WbemSrv.ExecQuery(strQuery)
Set GetWMICollection = WbemObjectSet
End Function

Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error

On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error Goto 0

If oError.Number &lt;&gt; 0 Then ThrowScriptError "Unable to create automation object '" &amp; sProgramId &amp; "'", oError
End Function

Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
</Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>