Detect Duplicate Relationships between Agents and Servers Diagnostic.

Microsoft.SystemCenter.2007.OpsMgrDB.DetectDuplicateRelationshipsforAgentsToServers.Diagnostic (Diagnostic)

Checks for duplicate relationships between agents and management servers.

Element properties:

TargetMicrosoft.SystemCenter.OpsMgrDBWatcher
MonitorMicrosoft.SystemCenter.OpsMgrDB.DetectDuplicateRelationshipsforAgentsToServers
RemotableTrue
Timeout300
Execute On StateError
CategoryMaintenance
Enabledfalse
AccessibilityPublic

Member Modules:

ID Module Type TypeId RunAs 
Probe ProbeAction Microsoft.SystemCenter.2007.OpsMgrDB.Generic.DS Default

Source Code:

<Diagnostic ID="Microsoft.SystemCenter.2007.OpsMgrDB.DetectDuplicateRelationshipsforAgentsToServers.Diagnostic" Accessibility="Public" Enabled="false" Target="SCLibrary!Microsoft.SystemCenter.OpsMgrDBWatcher" Monitor="Microsoft.SystemCenter.OpsMgrDB.DetectDuplicateRelationshipsforAgentsToServers" ExecuteOnState="Error" Remotable="true" Timeout="300">
<Category>Maintenance</Category>
<ProbeAction ID="Probe" TypeID="Microsoft.SystemCenter.2007.OpsMgrDB.Generic.DS">
<TimeoutSeconds>300</TimeoutSeconds>
<ScriptName>DetectDuplicateRelAgnToSrvDiagnostic.vbs</ScriptName>
<ScriptBody><Script>
'Copyright (c) Microsoft Corporation. All rights reserved.

'*************************************************************************
' $ScriptName: "GetSqlNativeClientComman" $
'
' Purpose: This script is used to get SQL native client Driver name.
'
' $File: GetSqlNativeClientComman.vbs $
'*************************************************************************


Option Explicit

Function GetSqlDriverName
Dim oReg
Const HKEY_LOCAL_MACHINE = &amp;H80000002
Const HKEY_CLASSES_ROOT = &amp;H80000000
Dim strComputer, strDriverPath, strKeyPath, intValue, strValueName,arrSubKeys,oSubkey,strNewKeyPath,intDotIndex, strDriverPathFromHKeyClassRoot
strComputer = "."
strDriverPath="SQLOLEDB"
Set oReg=GetObject("winmgmts:\\" &amp; _
strComputer &amp; "\root\default:StdRegProv")

strKeyPath = "CLSID\{5A23DE84-1D7B-4A16-8DED-B29C09CB648D}"
'strDriverPathFromHKeyClassRoot is used to check driver MSOLEDBSQL is installed or not. If found MSOLEDBSQL will be returned from function else strDriverPath="SQLOLEDB" will be used for further checking.
oReg.GetStringValue HKEY_CLASSES_ROOT, strKeyPath, "", strDriverPathFromHKeyClassRoot
If strDriverPathFromHKeyClassRoot = "MSOLEDBSQL" Then
GetSqlDriverName = strDriverPathFromHKeyClassRoot
Exit Function
End If

strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each oSubkey In arrSubKeys
intValue = InStr(oSubkey, "SQL Server Native Client")
if intValue &lt;&gt; 0 Then
strValueName = "Driver"
strNewKeyPath = strKeyPath&amp;"\"&amp;oSubkey
oReg.GetStringValue HKEY_LOCAL_MACHINE, strNewKeyPath, strValueName , strDriverPath
strDriverPath = Mid (strDriverPath, InStrRev(strDriverPath, "\")+1)
intDotIndex = InStr(strDriverPath, ".")
strDriverPath = Left(strDriverPath , intDotIndex-1)
end if
Next
GetSqlDriverName = strDriverPath
End Function

'Copyright (c) Microsoft Corporation. All rights reserved.

'*************************************************************************
' $ScriptName: "DetectDuplicateRelAgnToSrvDiagnostic" $
'
' Purpose: This script reports duplicates relationship between agents and servers.
'
' $File: DetectDuplicateRelAgnToSrvDiagnostic.vbs $
'*************************************************************************



'Declarations
Dim objCN,objRS,strQuery, intRecordsCount
Dim oArgs,oAPI,oBag
Dim strDBServer,strDatabase, strDriverName
Dim strRelationshipTypeName, strFullName, strLastModified, strDiscoverySource, strTimeGenerated

'Define local event constants
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

SetLocale("en-us")

'Create objects
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oArgs = WScript.Arguments

'Define parameters
strDBServer = oArgs(0)
strDatabase = oArgs(1)

Dim ObjError
Set ObjError = New Error

On Error Resume Next

'Set DB connection
Set objCN = CreateObject("ADODB.Connection")
strDriverName = GetSqlDriverName
objCN.Open "Provider=" &amp; strDriverName &amp;";Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" &amp; _
strDatabase &amp; ";Data Source=" &amp; strDBServer &amp; ""

ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then
Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue("Error", "Error Number: " &amp; ObjError.number &amp; " Error Details: " &amp; ObjError.Description)
Call oAPI.Return(oBag)
oAPI.LogScriptEvent "DetectDuplicateRelAgnToSrvDiagnostic.vbs",100,EVENT_TYPE_ERROR,"Script executed with Error Number: " &amp; ObjError.number &amp; " Error Details: " &amp; ObjError.Description
WScript.Quit
End If

strQuery = "SELECT RT.RelationshipTypeName,S.FullName ,R.LastModified ,Coalesce(D.DiscoveryName, '(system)') As DiscoverySource ,DSR.TimeGenerated " &amp; _
"FROM Relationship R (NoLock) " &amp; _
"JOIN RelationshipType RT (NoLock) ON RT.RelationshipTypeId = R.RelationshipTypeId " &amp; _
"JOIN BaseManagedEntity S (NoLock) ON R.SourceEntityId = S.BaseManagedEntityId " &amp; _
"JOIN DiscoverySourceToRelationship DSR (NoLock) ON DSR.RelationshipId = R.RelationshipId " &amp; _
"Left Outer Join DiscoverySource DS (NoLock) ON DS.DiscoverySourceId = DSR.DiscoverySourceId " &amp; _
"Left Outer Join Discovery D (NoLock) ON D.Discoveryid = DS.DiscoveryRuleId " &amp; _
"Where R.IsDeleted = 0 " &amp; _
"And RT.RelationshipTypeName IN ('Microsoft.SystemCenter.HealthServiceCommunication', 'Microsoft.SystemCenter.HealthServiceSecondaryCommunication') " &amp; _
"And R.RelationshipId NOT IN " &amp; _
"( " &amp; _
" SELECT RO.RelationshipId " &amp; _
" FROM " &amp; _
" ( " &amp; _
" SELECT RANK() OVER(PARTITION BY R1.SourceEntityId, R1.TargetEntityId ORDER BY R1.TimeAdded asc) as RowNumber, " &amp; _
" R1.RelationshipId " &amp; _
" FROM Relationship R1 (NoLock) " &amp; _
" JOIN RelationshipType RT1 (NoLock) " &amp; _
" ON RT1.RelationshipTypeId = R1.RelationshipTypeId " &amp; _
" WHERE RT1.RelationshipTypeName In ('Microsoft.SystemCenter.HealthServiceCommunication','Microsoft.SystemCenter.HealthServiceSecondaryCommunication') " &amp; _
" AND R1.IsDeleted = 0 " &amp; _
" Group By R1.RelationshipId,R1.SourceEntityId, R1.TargetEntityId, R1.TimeAdded " &amp; _
" ) As RO " &amp; _
" Where RO.RowNumber = 1 " &amp; _
") " &amp; _
"Order By R.LastModified Desc"

'Query DB
Set objRS = objCN.Execute(strQuery)

ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then
Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue("Error", "Error Number: " &amp; ObjError.number &amp; " Error Details: " &amp; ObjError.Description)
Call oAPI.Return(oBag)
oAPI.LogScriptEvent "DetectDuplicateRelAgnToSrvDiagnostic.vbs",100,EVENT_TYPE_ERROR,"Script executed with Error Number: " &amp; ObjError.number &amp; " Error Details: " &amp; ObjError.Description
WScript.Quit
End If



intRecordsCount = 0

Do While objRS.EOF = false
intRecordsCount = intRecordsCount + 1
strRelationshipTypeName = objRS.Fields(0).Value
strFullName = objRS.Fields(1).Value
strLastModified = objRS.Fields(2).Value
strDiscoverySource = objRS.Fields(3).Value
strTimeGenerated = objRS.Fields(4).Value

Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue("RelationshipTypeName", strRelationshipTypeName)
Call oBag.AddValue("FullName", strFullName)
Call oBag.AddValue("LastModified", strLastModified)
Call oBag.AddValue("DiscoverySource", strDiscoverySource)
Call oBag.AddValue("TimeGenerated", strTimeGenerated)
oAPI.AddItem(oBag)

objRS.MoveNext()
Loop

oAPI.LogScriptEvent "DetectDuplicateRelAgnToSrvDiagnostic.vbs",100,EVENT_TYPE_INFORMATION,"Script executed with Duplicate Records Count " &amp; CStr(intRecordsCount)

'Return property values
Call oAPI.ReturnItems()



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></ScriptBody>
<DatabaseServerName>$Target/Property[Type="SCLibrary!Microsoft.SystemCenter.OpsMgrDBWatcher"]/DatabaseServerName$</DatabaseServerName>
<DatabaseName>$Target/Property[Type="SCLibrary!Microsoft.SystemCenter.OpsMgrDBWatcher"]/DatabaseName$</DatabaseName>
<Threshold>0</Threshold>
</ProbeAction>
</Diagnostic>