微软SCOM管理中最有用的SQL查询(三)

SCOM的管理包(Package)通常包含应用程序和服务的监视设置。 将管理包导入到管理组后,System Center - Operations Manager 会立即根据由管理包定义的默认配置和阈值开始监视对象。

每个管理包可能包含以下一项或所有部件:

监视器,定向代理以跟踪管理组件的不同部件的状态。

规则,定向代理以收集性能和发现数据,发送警报和事件等等。

任务,定义代理或控制台可以执行的活动。

知识,提供文字建议以帮助操作员诊断和解决问题。

视图,为监视和管理此组件提供自定义的用户界面。

报表,定义报告此管理组件相关信息的特定方式。

对象发现,识别要监视的对象。

运行方式配置文件,允许你在不同计算机上使用不同帐户运行不同的规则、任务、监视器或发现。

管理包是SCOM监控的核心,以下SQL查询可以帮助管理员对Package进行快速查询和统计信息。

SCOM管理包Rules:

要查找给定规则 ID 名称的通用规则名称:

 SELECT DisplayName from RuleView where name = 'Microsoft.SystemCenter.GenericNTPerfMapperModule.FailedExecution.Alert' --Rules per MP: SELECT mp.MPName, COUNT(*) As RulesPerMP FROM Rules r INNER JOIN ManagementPack mp ON mp.ManagementPackID = r.ManagementPackID GROUP BY mp.MPName ORDER BY RulesPerMP DESC --Rules per MP by category: SELECT mp.MPName, r.RuleCategory, COUNT(*) As RulesPerMPPerCategory FROM Rules r INNER JOIN ManagementPack mp ON mp.ManagementPackID = r.ManagementPackID GROUP BY mp.MPName, r.RuleCategory ORDER BY RulesPerMPPerCategory DESC --To find all rules per MP with a given alert severity: declare @mpid as varchar(50) select @mpid= managementpackid from managementpack where mpName='Microsoft.SystemCenter.2007' select rl.rulename,rl.ruleid,md.modulename from rules rl, module md where md.managementpackid = @mpid and rl.ruleid=md.parentid and moduleconfiguration like '%2%' --Rules are stored in a table named Rules. This table has columns linking rules to classes and Management Packs. --To find all rules in a Management Pack use the following query and substitute in the required Management Pack name: SELECT * FROM Rules WHERE ManagementPackID = (SELECT ManagementPackID from ManagementPack WHERE MPName = 'Microsoft.SystemCenter.2007') --To find all rules targeted at a given class use the following query and substitute in the required class name: SELECT * FROM Rules WHERE TargetManagedEntityType = (SELECT ManagedTypeId FROM ManagedType WHERE TypeName = 'Microsoft.Windows.Computer') --Rules by Class targeted and enabled by default excluding perf collection and discovery rules SELECT mtv.Name AS 'ClassName', mtv.DisplayName, COUNT(*) AS 'COUNT' FROM RuleView rv JOIN ManagedTypeView mtv ON rv.TargetMonitoringClassId = mtv.Id WHERE mtv.LanguageCode = 'ENU' AND rv.LanguageCode = 'ENU' AND rv.Enabled NOT IN (0) AND rv.Category NOT IN ('PerformanceCollection','Discovery') GROUP BY mtv.Name, mtv.DisplayName ORDER BY COUNT DESC

监控管理包信息查询:

SELECT mp.MPName, COUNT(*) As MonitorsPerMPPerCategory FROM Monitor m INNER JOIN ManagementPack mp ON mp.ManagementPackID = m.ManagementPackID GROUP BY mp.MPName ORDER BY COUNT(*) Desc --To find your Monitor by common name: select * from Monitor m Inner join LocalizedText LT on LT.ElementName = m.MonitorName where LTValue = ‘Monitor Common Name’ --To find your Monitor by ID name: select * from Monitor m Inner join LocalizedText LT on LT.ElementName = m.MonitorName where m.monitorname = 'your Monitor ID name' --To find all monitors targeted at a specific class: SELECT * FROM monitor WHERE TargetManagedEntityType = (SELECT ManagedTypeId FROM ManagedType WHERE TypeName = 'Microsoft.Windows.Computer') --Unit Monitors by Class targeted that are configured to alert and monitor enabled by default SELECT mtv.Name AS 'ClassName', mtv.DisplayName, COUNT(*) AS 'COUNT' FROM MonitorView mv JOIN ManagedTypeView mtv ON mv.TargetMonitoringClassId = mtv.Id WHERE mtv.LanguageCode = 'ENU' AND mv.LanguageCode = 'ENU' AND mv.IsUnitMonitor = 1 AND mv.AlertMessage IS NOT NULL AND mv.Enabled NOT IN (0) GROUP BY mtv.Name, mtv.DisplayName ORDER BY COUNT DESC

Groups Section:

要查找给定组的所有成员(更改下面的组名称)

select TargetObjectDisplayName as 'Group Members' from RelationshipGenericView where isDeleted=0 AND SourceObjectDisplayName = 'All Windows Computers' ORDER BY TargetObjectDisplayName --Find find the entity data on all members of a given group (change the group name below): SELECT bme.* FROM BaseManagedEntity bme INNER JOIN RelationshipGenericView rgv WITH(NOLOCK) ON bme.basemanagedentityid = rgv.TargetObjectId WHERE bme.IsDeleted = '0' AND rgv.SourceObjectDisplayName = 'All Windows Computers' ORDER BY bme.displayname --To find all groups for a given computer/object (change “computername” in the query below): SELECT SourceObjectDisplayName AS 'Group' FROM RelationshipGenericView WHERE TargetObjectDisplayName like ('%sql2a.opsmgr.net%') AND (SourceObjectDisplayName IN (SELECT ManagedEntityGenericView.DisplayName FROM ManagedEntityGenericView INNER JOIN (SELECT BaseManagedEntityId FROM BaseManagedEntity WITH (NOLOCK) WHERE (BaseManagedEntityId = TopLevelHostEntityId) AND (BaseManagedEntityId NOT IN (SELECT R.TargetEntityId FROM Relationship AS R WITH (NOLOCK) INNER JOIN dbo.fn_ContainmentRelationshipTypes() AS CRT ON R.RelationshipTypeId = CRT.RelationshipTypeId WHERE (R.IsDeleted = 0)))) AS GetTopLevelEntities ON GetTopLevelEntities.BaseManagedEntityId = ManagedEntityGenericView.Id INNER JOIN (SELECT DISTINCT BaseManagedEntityId FROM TypedManagedEntity WITH (NOLOCK) WHERE (ManagedTypeId IN (SELECT DerivedManagedTypeId FROM dbo.fn_DerivedManagedTypes(dbo.fn_ManagedTypeId_Group()) AS fn_DerivedManagedTypes_1))) AS GetOnlyGroups ON GetOnlyGroups.BaseManagedEntityId = ManagedEntityGenericView.Id)) ORDER BY 'Group'

管理包和实例空间查询:

 查找所有已安装的管理包及其版本

SELECT Name AS 'ManagementPackID', FriendlyName, DisplayName, Version, Sealed, LastModified, TimeCreated FROM ManagementPackView WHERE LanguageCode = 'ENU' OR LanguageCode IS NULL ORDER BY DisplayName --Number of Views per Management Pack: SELECT mp.MPName, v.ViewVisible, COUNT(*) As ViewsPerMP FROM [Views] v INNER JOIN ManagementPack mp ON mp.ManagementPackID = v.ManagementPackID GROUP BY mp.MPName, v.ViewVisible ORDER BY v.ViewVisible DESC, COUNT(*) Desc --How to gather all the views in the database, their ID, MP location, and view type: select vv.id as 'View Id', vv.displayname as 'View DisplayName', vv.name as 'View Name', vtv.DisplayName as 'ViewType', mpv.FriendlyName as 'MP Name' from ViewsView vv inner join managementpackview mpv on mpv.id = vv.managementpackid inner join viewtypeview vtv on vtv.id = vv.monitoringviewtypeid -- where mpv.FriendlyName like '%default%' -- where vv.displayname like '%operating%' order by mpv.FriendlyName, vv.displayname --Classes available in the DB: SELECT count(*) FROM ManagedType --Total BaseManagedEntities SELECT count(*) FROM BaseManagedEntity --To get the state of every instance of a particular monitor the following query can be run, (replace with the name of the monitor): SELECT bme.FullName, bme.DisplayName, s.HealthState FROM state AS s, BaseManagedEntity as bme WHERE s.basemanagedentityid = bme.basemanagedentityid AND s.monitorid IN (SELECT Id FROM MonitorView WHERE DisplayName = 'Health Service Heartbeat Failure') --For example, this gets the state of the Microsoft.SQLServer.2012.DBEngine.ServiceMonitor for each instance of the SQL 2012 Database Engine class. SELECT bme.FullName, bme.DisplayName, s.HealthState FROM state AS s, BaseManagedEntity as bme WHERE s.basemanagedentityid = bme.basemanagedentityid AND s.monitorid IN (SELECT MonitorId FROM Monitor WHERE MonitorName = 'Microsoft.SQLServer.2012.DBEngine.ServiceMonitor') --To find the overall state of any object in OpsMgr the following query should be used to return the state of the System.EntityState monitor: SELECT bme.FullName, bme.DisplayName, s.HealthState FROM state AS s, BaseManagedEntity as bme WHERE s.basemanagedentityid = bme.basemanagedentityid AND s.monitorid IN (SELECT MonitorId FROM Monitor WHERE MonitorName = 'System.Health.EntityState') --The Alert table contains all alerts currently open in OpsMgr. This includes resolved alerts until they are groomed out of the database. To get all alerts across all instances of a given monitor use the following query and substitute in the required monitor name: SELECT * FROM Alert WHERE ProblemID IN (SELECT MonitorId FROM Monitor WHERE MonitorName = 'Microsoft.SQLServer.2012.DBEngine.ServiceMonitor') --To retrieve all alerts for all instances of a specific class use the following query and substitute in the required table name, in this example MT_Microsoft$SQLServer$2012$DBEngine is used to look for SQL alerts: SELECT * FROM Alert WHERE BaseManagedEntityID IN (SELECT BaseManagedEntityID from MT_Microsoft$SQLServer$2012$DBEngine) --To determine which table is currently being written to for event and performance data use the following query: SELECT * FROM PartitionTables WHERE IsCurrent = 1 --Number of instances of a type: (Number of disks, computers, databases, etc that OpsMgr has discovered) SELECT mt.TypeName, COUNT(*) AS NumEntitiesByType FROM BaseManagedEntity bme WITH(NOLOCK) LEFT JOIN ManagedType mt WITH(NOLOCK) ON mt.ManagedTypeID = bme.BaseManagedTypeID WHERE bme.IsDeleted = 0 GROUP BY mt.TypeName ORDER BY COUNT(*) DESC --To retrieve all performance data for a given rule in a readable format use the following query: (change the r.RuleName value – get list from Rules Table) SELECT bme.Path, pc.ObjectName, pc.CounterName, ps.PerfmonInstanceName, pdav.SampleValue, pdav.TimeSampled FROM PerformanceDataAllView AS pdav with (NOLOCK) INNER JOIN PerformanceSource ps on pdav.PerformanceSourceInternalId = ps.PerformanceSourceInternalId INNER JOIN PerformanceCounter pc on ps.PerformanceCounterId = pc.PerformanceCounterId INNER JOIN Rules r on ps.RuleId = r.RuleId INNER JOIN BaseManagedEntity bme on ps.BaseManagedEntityID = bme.BaseManagedEntityID WHERE r.RuleName = 'Microsoft.Windows.Server.6.2.LogicalDisk.FreeSpace.Collection' GROUP BY PerfmonInstanceName, ObjectName, CounterName, SampleValue, TimeSampled, bme.path ORDER BY bme.path, PerfmonInstanceName, TimeSampled --To determine what discoveries are still associated with a computer – helpful in finding old stale computer objects in the console that are no longer agent managed, or desired. select BME.FullName, DS.DiscoveryRuleID, D.DiscoveryName from typedmanagedentity TME Join BaseManagedEntity BME ON TME.BaseManagedEntityId = BME.BaseManagedEntityId JOIN DiscoverySourceToTypedManagedEntity DSTME ON TME.TypedManagedEntityID = DSTME.TypedManagedEntityID JOIN DiscoverySource DS ON DS.DiscoverySourceID = DSTME.DiscoverySourceID JOIN Discovery D ON DS.DiscoveryRuleID=D.DiscoveryID Where BME.Fullname like '%SQL2A%' --To dump out all the rules and monitors that have overrides, and display the context and instance of the override: select rv.DisplayName as WorkFlowName, OverrideName, mo.Value as OverrideValue, mt.TypeName as OverrideScope, bme.DisplayName as InstanceName, bme.Path as InstancePath, mpv.DisplayName as ORMPName, mo.LastModified as LastModified from ModuleOverride mo inner join managementpackview mpv on mpv.Id = mo.ManagementPackId inner join ruleview rv on rv.Id = mo.ParentId inner join ManagedType mt on mt.managedtypeid = mo.TypeContext left join BaseManagedEntity bme on bme.BaseManagedEntityId = mo.InstanceContext Where mpv.Sealed = 0 UNION ALL select mv.DisplayName as WorkFlowName, OverrideName, mto.Value as OverrideValue, mt.TypeName as OverrideScope, bme.DisplayName as InstanceName, bme.Path as InstancePath, mpv.DisplayName as ORMPName, mto.LastModified as LastModified from MonitorOverride mto inner join managementpackview mpv on mpv.Id = mto.ManagementPackId inner join monitorview mv on mv.Id = mto.MonitorId inner join ManagedType mt on mt.managedtypeid = mto.TypeContext left join BaseManagedEntity bme on bme.BaseManagedEntityId = mto.InstanceContext Where mpv.Sealed = 0 Order By mpv.DisplayName

以上查询可以帮助管理员对已经安装的管理包进行快速查询,尤其在体量大机器数量多的生产环境中,方便管理员对整体管理包的跟踪,统计。

你可能感兴趣的:(微软SCOM管理中最有用的SQL查询(三))