AppFabric Monitoring Database Discovery

Microsoft.ApplicationServer.MonitoringStoreConnection.Discovery (Discovery)

This discovery discovers connections to SQL monitoring databases that are currently in use by AppFabric managed applications on the machine.


This is accomplished by reading application configuration files.

Knowledge Base article:

Summary

This discovery discovers connections to SQL monitoring databases that are currently in use by AppFabric managed applications on the machine.

This is accomplished by reading application configuration files.

Element properties:

TargetMicrosoft.ApplicationServer.EventCollectorService
EnabledTrue
Frequency14700
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:
Discovered relationships and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterDiscoveryDataSource Microsoft.ApplicationServer.StoreReaderAccount

Source Code:

<Discovery ID="Microsoft.ApplicationServer.MonitoringStoreConnection.Discovery" Enabled="true" Target="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.EventCollectorService" ConfirmDelivery="true" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Service">
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
<DiscoveryClass TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Stores">
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Stores" PropertyID="StoresName"/>
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
<DiscoveryClass TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStore">
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStore" PropertyID="DBIdentity"/>
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Store" PropertyID="ConnectionStringHash"/>
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Store" PropertyID="Provider"/>
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Store" PropertyID="DataSource"/>
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
<DiscoveryClass TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectedConnection">
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectedConnection" PropertyID="DBIdentity"/>
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreConnection" PropertyID="ConnectionStringName"/>
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreConnection" PropertyID="Provider"/>
<Property TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreConnection" PropertyID="ConnectionStringHash"/>
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.EventCollectorServiceHostsMonitoringStoreConnection"/>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.ServiceContainsStores"/>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoresContainsMonitoringStore"/>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreContainsMonitoringStoreConnectedConnection"/>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreContainsMonitoringStoreConnection"/>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectionReferencesMonitoringStore"/>
<DiscoveryRelationship TypeID="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectedConnectionReferencesMonitoringStore"/>
</DiscoveryTypes>
<DataSource ID="DS" RunAs="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreReaderAccount" TypeID="System!System.CommandExecuterDiscoveryDataSource">
<IntervalSeconds>14700</IntervalSeconds>
<ApplicationName>%windir%\system32\windowspowershell\v1.0\powershell.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>-Command ".\Create-MonitoringStoreConnectionInfo.ps1" </CommandLine>
<TimeoutSeconds>600</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>Create-MonitoringStoreConnectionInfo.ps1</Name>
<Contents><Script>
function Get-CollectorConfiguration
{
Param([string]$ecServiceName)
$DefaultRuntimeVersion = "4.0"
$MonitoringSectionPath = "microsoft.applicationServer/monitoring"
$CollectorsSectionName = "collectors"

[Microsoft.Web.Administration.ConfigurationElement]$collectorConfiguration = $null

$serverManager = new-object Microsoft.Web.Administration.ServerManager
[Microsoft.Web.Administration.Configuration]$rootWebConfiguration = $serverManager.GetWebConfiguration($null);


$rootWebConfiguration.SetMetadata("defaultManagedRuntimeVersion", $DefaultRuntimeVersion);

trap [Exception]
{
#defaults to .NET 2.0 apps
continue
}

[Microsoft.Web.Administration.ConfigurationSection]$applicationServerSection = $rootWebConfiguration.GetSection($MonitoringSectionPath);
[Microsoft.Web.Administration.ConfigurationElementCollection]$collectorsCollection = $applicationServerSection.GetCollection($CollectorsSectionName);

foreach ($collectorElement in $collectorsCollection)
{
[string]$currentCollectorName = $collectorElement.GetAttributeValue("name");
if ($currentCollectorName.CompareTo($ecServiceName) -eq 0)
{
$collectorConfiguration = $collectorElement;
break;
}
}

return $collectorConfiguration
}

function NormalizeScopePath
{
Param([string] $scope)
[string]$normalizedScope = $scope;
if (![string]::IsNullOrEmpty($scope))
{
# Trim whitespaces, leading and trailing '/' if any
$normalizedScope = $scope.Trim();
if ($normalizedScope[0] -eq '/')
{
$normalizedScope = $normalizedScope.Substring(1);
}
[int]$length = $normalizedScope.Length;
if (($length -ge 1) -and ($normalizedScope[$length - 1] -eq '/'))
{
$normalizedScope = $normalizedScope.Substring(0, $length - 1);
}
}

return $normalizedScope;
}

function Get-UniqueConnectionInfoObjects
{
Param([string]$siteName, [string]$applicationPath)
$connectionInfoObjects = $null

if ([string]::IsNullOrEmpty($siteName))
{
$connectionInfoObjects = Get-ASApplication | Get-ASAppMonitoring | Group-Object ConnectionString -ErrorAction SilentlyContinue
}
else
{
$connectionInfoObjects = Get-ASApplication -SiteName $siteName -VirtualPath $applicationPath | Get-ASAppMonitoring | Group-Object ConnectionString -ErrorAction SilentlyContinue
}

return $connectionInfoObjects;
}

function Get-SHA1Hash
{
param([string]$stringValueToHash)
$hashAlgorithm = new-object System.Security.Cryptography.SHA1Managed
$computedHashBytes = $hashAlgorithm.ComputeHash($([Char[]]$stringValueToHash.ToUpperInvariant()));

$stringBuilder = New-Object System.Text.StringBuilder
$computedHashBytes | Foreach-Object { [void] $stringBuilder.Append($_.ToString("X2")) }
return $stringBuilder.ToString()
}

function Create-MonitoringStoreConnectionObjects
{
Param([string]$connectionString, [string]$provider, [string]$connectionStringName, $discovery, $dbInfoCache)

# Create App Service (singleton object)
$sourceService = $discovery.CreateClassInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Service']$")

# Create Stores object
$sourceDBServers = $discovery.CreateClassInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Stores']$")
$sourceDBServers.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Stores']/StoresName$", 'AppFabric Databases')
$discovery.AddInstance($sourceDBServers)

# Create App Service contains Stores relation
$relation = $discovery.CreateRelationshipInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.ServiceContainsStores']$")
$relation.Source = $sourceService
$relation.Target = $sourceDBServers
$discovery.AddInstance($relation)

# Create Store Connection object
$dbConnection = $null
$isMonitoringStoreConnectedConnection = $false
$hashedConnectionString = Get-SHA1Hash -stringValueToHash $connectionString
if($dbInfoCache -ne $null -and $dbInfoCache.Contains($hashedConnectionString) -and
$dbInfoCache[$hashedConnectionString].DbIdentity -ne $null)
{
$isMonitoringStoreConnectedConnection = $true;
}

if ($isMonitoringStoreConnectedConnection)
{
$dbConnection = $discovery.CreateClassInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectedConnection']$")
}
else
{
$dbConnection = $discovery.CreateClassInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnection']$")
}

$dbConnection.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreConnection']/ConnectionStringHash$", $hashedConnectionString)
$dbConnection.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreConnection']/ConnectionStringName$", $connectionStringName)
$dbConnection.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoreConnection']/Provider$", $provider)
$dbConnection.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", '$Target/Host/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$')
$dbConnection.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.LocalService']/ServiceName$", '$Target/Property[Type="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.LocalService"]/ServiceName$')
$dbConnection.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $connectionStringName)

if($isMonitoringStoreConnectedConnection)
{
$dbInfo = $dbInfoCache[$hashedConnectionString]
[string]$provider = $dbInfo.Provider
[string]$dbIdentity = $dbInfo.DbIdentity
[string]$dbDataSource = $dbInfo.DataSource
[string]$dbDatabaseName = $dbInfo.Database

#Add DBIdentity key to connection object
$dbConnection.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectedConnection']/DBIdentity$", $dbIdentity)
$discovery.AddInstance($dbConnection)

# Create MonitoringStore object
$discoveredDatabase = $discovery.CreateClassInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStore']$")
$discoveredDatabase.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Store']/ConnectionStringHash$", $hashedConnectionString)
$discoveredDatabase.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Store']/Provider$", $provider)
$discoveredDatabase.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.Store']/DataSource$", $dbDataSource)
$discoveredDatabase.AddProperty("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStore']/DBIdentity$", $dbIdentity)
$discoveredDatabase.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $dbDatabaseName)
$discovery.AddInstance($discoveredDatabase)

# Add relation Store's contains MonitoringStore relation
$relation = $discovery.CreateRelationshipInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.StoresContainsMonitoringStore']$")
$relation.Source = $sourceDBServers
$relation.Target = $discoveredDatabase
$discovery.AddInstance($relation)

# Create MonitoringStore Contains MonitoringStoreConnection relation
if ($isMonitoringStoreConnectedConnection)
{
$relation = $discovery.CreateRelationshipInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreContainsMonitoringStoreConnectedConnection']$")
}
else
{
$relation = $discovery.CreateRelationshipInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreContainsMonitoringStoreConnection']$")
}
$relation.Source = $discoveredDatabase
$relation.Target = $dbConnection
$discovery.AddInstance($relation)

# Create MonitoringStoreConnection References MonitoringStore relation

if ($isMonitoringStoreConnectedConnection)
{
$relation = $discovery.CreateRelationshipInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectedConnectionReferencesMonitoringStore']$")
}
else
{
$relation = $discovery.CreateRelationshipInstance("$MPElement[Name='MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.MonitoringStoreConnectionReferencesMonitoringStore']$")
}

$relation.Source = $dbConnection
$relation.Target = $discoveredDatabase
$discovery.AddInstance($relation)
}
else
{
$discovery.AddInstance($dbConnection)
}
}

[string]$DbInfoCacheFileName = "$env:ALLUSERSPROFILE\Microsoft AppFabric Management Pack\DbInfoCache.xml"
[Hashtable]$existingDbInfoCache = $null
if(test-path $DbInfoCacheFileName)
{
$existingDbInfoCache = Import-Clixml -Path $DbInfoCacheFileName
}

# Create a new discovery data packet -- this assumes that the client SDK is available, which it is on all OpsMgr agents
$oAPI = new-object -comObject "MOM.ScriptAPI"
$Discovery = $oAPI.CreateDiscoveryData(0, '$MPElement$', '$Target/Id$')

#$oAPI.LogScriptEvent("Create-MonitoringStoreConnectionInfo.ps1", 123, 4, "Init")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
if ((Get-Command -Module ApplicationServer) -eq $null)
{
Import-Module ApplicationServer # Application Server Extensions for .NET 4
}

$ScopesName = "scopes"
$ScopePathAttributeName = "path"

$collectorName = '$Target/Property[Type="MicrosoftApplicationServerLibrary!Microsoft.ApplicationServer.EventCollectorService"]/CollectorName$'

[Microsoft.Web.Administration.ConfigurationElement]$collectorConfiguration = Get-CollectorConfiguration -ecServiceName $collectorName
[Microsoft.Web.Administration.ConfigurationElementCollection]$scopeConfigurationElements = $collectorConfiguration.GetCollection($ScopesName);

if ($scopeConfigurationElements -eq $null -or $scopeConfigurationElements.Count -eq 0)
{
$connectionInfoObjects = Get-UniqueConnectionInfoObjects -siteName $null -applicationPath $null
foreach($connectionInfoObject in $connectionInfoObjects)
{
Create-MonitoringStoreConnectionObjects -connectionString $connectionInfoObject.Name -provider $connectionInfoObject.Group[0].ProviderName -connectionStringName $connectionInfoObject.Group[0].ConnectionStringName -discovery $Discovery -dbInfoCache $existingDbInfoCache
}
}
else
{
foreach ($scopeElement in $scopeConfigurationElements)
{
[string]$scopePath = $scopeElement.GetAttributeValue($ScopePathAttributeName)
[string]$normalizedScope = NormalizeScopePath -scope $scopePath
if ([string]::IsNullOrEmpty($normalizedScope))
{
continue
}

$SiteName = $null
$ApplicationPath = [string]::Empty
# Split SiteName and Virtual Path
[int]$siteNameEndIndex = $normalizedScope.IndexOf('/');
if ($siteNameEndIndex -ne -1)
{
$SiteName = $normalizedScope.Substring(0, $siteNameEndIndex)
}
else
{
$SiteName = $normalizedScope
}

if (($siteNameEndIndex -ne -1) -and ($siteNameEndIndex -lt $normalizedScope.Length))
{
$ApplicationPath = $normalizedScope.Substring($siteNameEndIndex)
}

$connectionInfoObjects = Get-UniqueConnectionInfoObjects -siteName $SiteName -applicationPath $ApplicationPath
foreach($connectionInfoObject in $connectionInfoObjects)
{
Create-MonitoringStoreConnectionObjects -connectionString $connectionInfoObject.Name -provider $connectionInfoObject.Group[0].ProviderName -connectionStringName $connectionInfoObject.Group[0].ConnectionStringName -discovery $Discovery
}

}
}

# Return the discovery data by calling the variable
$oAPI.Return($Discovery)
return

trap
{
if ($oAPI -ne $null)
{
foreach($errorDetail in $Error)
{
$oAPI.LogScriptEvent("Create-MonitoringStoreConnectionInfo.ps1", 100, 1, $errorDetail.Exception)
}

$oAPI.Return($Discovery)
}

return
}

</Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</DataSource>
</Discovery>