Pavleck.Com

ULTIMA RATIO REGUM

  • Flickr Photos

    www.flickr.com
  • Things

    www.flickr.com
    This is a Flickr badge showing public photos from Jeremy Pavleck. Make your own badge here.


  • Listening To

  • Twitter

  • I Support

    Bloggers' Rights at EFF

  • Spam Blocked

  • last.fm records

    1. Made of Bricks Made of Bricks
      Kate Nash
    2. Eazy Duz It Eazy Duz It
      Eazy-E
    3. Swass Swass
      Sir Mix-a-Lot
    4. Blue Horse Blue Horse
      The Be Good Tanyas
    5. The Greatest Hits: Why Try Harder The Greatest Hits: Why Try Harder
      Fatboy Slim
    6. Punk in Drublic Punk in Drublic
      NOFX

Useful Operations Manager 2007 SQL queries - prettified and PoShified

Posted by Jeremy on May 29th, 2008

Kevin Holman, who writes a lot about SCOM2007, posted an article some time ago with a bunch of extremely useful SQL queries to use with SCOM2007. Over several installments, I’ll take that file and show you some useful SCOM PoSh tools you can use to - including a set of cmdlets I wrote to help the process out.

First, let’s take the actual file itself. I’ve taken it and cleaned it up some, changing formats and fixing errors I found, and made it into an actual .SQL file you can use inside of SQL Studio.

Download the file, and save it somewhere useful. SQL Studio 2005 likes to save query files in My Documents\SQL Server Management Studio\Projects, and it’s the first place it goes when you select open, so that makes a great choice.

If you’re not used to SQL server, you might want to know how to use this file. When you open up the file, you’ll notice a ton of different queries - surely you don’t want to run them all at once1 - and you don’t have to. To run an individual query, simply highlight it and either hit F5, or click on the execute button.

Highlighted SQL QUery

That’s all there is to it. If you wanted to take it a step further, you could hardcode the database name and user before the table name (So instead of “from ManagedEntityGenericView” you’d alter it to (In my case, yours may be different) “from OperationsManager.dbo.ManagedEntityGenericView”. That way, you don’t even have to worry about switching which database to use before running it.

Next up, how to make these more useful to you on an every day basis. And if you’re not one to blindly download things, read on for the pasted SQL file.

  1. –======================================================================
  2. – SQL Source File — Created with SAPIEN Technologies PrimalScript 4.1
  3. – NAME: Useful_SCOM2007_SQL_Queries.sql
  4. – AUTHOR: Jeremy D. Pavleck , JPavleck@GMail.com
  5. – DATE  : 4/17/2008
  6. – COMMENT: Some useful SQL queries you can use in SCOM2007.
  7. – Taken verbatim from Kevin Holman’s OpsMgr Blog, and re-written to be SQL friendly.
  8. – URL: http://blogs.technet.com/kevinholman/archive/2007/10/18/useful-operations-manager-2007-sql-queries.aspx
  9. –======================================================================
  10.  
  11. – Find all managed servers that are down and not pingable
  12. SELECT bme.DisplayName, s.LastModified
  13. FROM state AS s, BaseManagedEntity AS bme
  14. WHERE s.basemanagedentityid = bme.basemanagedentityid AND s.monitorid
  15. IN (SELECT MonitorId FROM Monitor WHERE MonitorName = ‘Microsoft.SystemCenter.HealthService.ComputerDown’)
  16. AND s.Healthstate = ‘3′
  17. ORDER BY s.Lastmodified DESC
  18.  
  19. – Operational Database Version
  20. SELECT DBVersion FROM __MOMManagementGroupInfo__
  21.  
  22. – Find a computer name from it’s Health Service ID (guid from agent proxy alerts)
  23. SELECT id, path, fullname, displayname FROM ManagedEntityGenericView WHERE ID =
  24.  
  25. – ALERTS SECTION –
  26. ——————–
  27.  
  28. – Most common alerts, by alert count
  29. SELECT AlertStringName, AlertStringDescription, AlertParams, Name, SUM(1) AS AlertCount, SUM(RepeatCount+1) AS AlertCountWithRepeatCount
  30. FROM Alertview WITH (NOLOCK)
  31. WHERE ResolutionState = (0|255)
  32. GROUP BY AlertStringName, AlertStringDescription, AlertParams, Name
  33. ORDER BY AlertCount DESC
  34.  
  35. – TOP 10 common alerts
  36. SELECT Top(10) AlertStringName, AlertStringDescription, AlertParams, Name, SUM(1) AS AlertCount, SUM(RepeatCount+1) AS AlertCountWithRepeatCount
  37. FROM Alertview WITH (NOLOCK)
  38. WHERE ResolutionState = (0|255)
  39. GROUP BY AlertStringName, AlertStringDescription, AlertParams, Name
  40. ORDER BY AlertCount DESC
  41.  
  42. – Most common alerts, by repeat count
  43. SELECT AlertStringName, AlertStringDescription, AlertParams, Name, SUM(1) AS AlertCount, SUM(RepeatCount+1) AS AlertCountWithRepeatCount
  44. FROM Alertview WITH (NOLOCK)
  45. WHERE ResolutionState = (0|255)
  46. GROUP BY AlertStringName, AlertStringDescription, AlertParams, Name
  47. ORDER BY AlertCountWithRepeatCount DESC
  48.  
  49. – Number of alerts per day
  50. SELECT CONVERT(VARCHAR(20), TimeAdded, 101) AS DayAdded, COUNT(*) AS NumAlertsPerDay
  51. FROM Alert WITH (NOLOCK)
  52. GROUP BY CONVERT(VARCHAR(20), TimeAdded, 101)
  53. ORDER BY DayAdded DESC
  54.  
  55. – Number of alerts per day by resolution state
  56. SELECT
  57. CASE WHEN(GROUPING(CONVERT(VARCHAR(20), TimeAdded, 101)) = 1) THEN ‘All Days’ ELSE CONVERT(VARCHAR(20), TimeAdded, 101) END AS [Date],
  58. CASE WHEN(GROUPING(ResolutionState) = 1) THEN ‘All Resolution States’ ELSE CAST(ResolutionState AS VARCHAR(5)) END AS [ResolutionState],
  59. COUNT(*) AS NumAlerts
  60. FROM Alert WITH (NOLOCK)
  61. GROUP BY CONVERT(VARCHAR(20), TimeAdded, 101), ResolutionState WITH ROLLUP
  62. ORDER BY DATE DESC
  63.  
  64. – EVENTS SECTION –
  65. ——————–
  66.  
  67. – Most common events by day by count
  68. SELECT CASE WHEN(GROUPING(CONVERT(VARCHAR(20), TimeAdded, 101)) = 1)
  69. THEN ‘All Days’
  70. ELSE CONVERT(VARCHAR(20), TimeAdded, 101) END AS DayAdded,
  71. COUNT(*) AS NumEventsPerDay
  72. FROM EventAllView
  73. GROUP BY CONVERT(VARCHAR(20), TimeAdded, 101) WITH ROLLUP
  74. ORDER BY DayAdded DESC
  75.  
  76. – Most common events by event number
  77. SELECT Number, COUNT(*) AS "Number of Events"
  78. FROM EventView
  79. GROUP BY Number
  80. ORDER BY "Number of Events" DESC
  81.  
  82. – PERFORMANCE SECTION –
  83. ————————-
  84.  
  85. – Performance Insertions per day
  86. SELECT CASE WHEN(GROUPING(CONVERT(VARCHAR(20), TimeSampled, 101)) = 1)
  87. THEN ‘All Days’ ELSE CONVERT(VARCHAR(20), TimeSampled, 101)
  88. END AS DaySampled, COUNT(*) AS NumPerfPerDay
  89. FROM PerformanceDataAllView
  90. GROUP BY CONVERT(VARCHAR(20), TimeSampled, 101) WITH ROLLUP
  91. ORDER BY DaySampled DESC
  92.  
  93. – Most common performance insertions by perf object and counter name:
  94. SELECT pcv.objectname, pcv.countername, count (pcv.countername) AS total FROM performancedataallview AS pdv, performancecounterview AS pcv
  95. WHERE (pdv.performancesourceinternalid = pcv.performancesourceinternalid)
  96. GROUP BY pcv.objectname, pcv.countername
  97. ORDER BY count (pcv.countername) DESC
  98.  
  99. – Most common performance insertions by perf object name:
  100. SELECT pcv.objectname, count (pcv.countername) AS total FROM performancedataallview AS pdv, performancecounterview AS pcv
  101. WHERE (pdv.performancesourceinternalid = pcv.performancesourceinternalid)
  102. GROUP BY pcv.objectname
  103. ORDER BY count (pcv.countername) DESC
  104.  
  105. – Most common performance insertions by perf counter name:
  106. SELECT pcv.countername, count (pcv.countername) AS total FROM performancedataallview AS pdv, performancecounterview AS pcv
  107. WHERE (pdv.performancesourceinternalid = pcv.performancesourceinternalid)
  108. GROUP BY pcv.countername
  109. ORDER BY count (pcv.countername) DESC
  110.  
  111. – STATE SECTION –
  112. ——————-
  113.  
  114. – State changes per day:
  115. SELECT CASE WHEN(GROUPING(CONVERT(VARCHAR(20), TimeGenerated, 101)) = 1)
  116. THEN ‘All Days’ ELSE CONVERT(VARCHAR(20), TimeGenerated, 101)
  117. END AS DayGenerated, COUNT(*) AS NumEventsPerDay
  118. FROM StateChangeEvent WITH (NOLOCK)
  119. GROUP BY CONVERT(VARCHAR(20), TimeGenerated, 101) WITH ROLLUP
  120. ORDER BY DayGenerated DESC
  121.  
  122. – MANAGEMENT PACK INFO –
  123. ————————–
  124.  
  125. – To find all installed Management Packs and their version:
  126. SELECT MPName, MPFriendlyName, MPVersion, MPIsSealed
  127. FROM ManagementPack WITH(NOLOCK)
  128. ORDER BY MPName
  129.  
  130. – Rules per MP:
  131. SELECT mp.MPName, COUNT(*) AS RulesPerMP
  132. FROM Rules r
  133. INNER JOIN ManagementPack mp ON mp.ManagementPackID = r.ManagementPackID
  134. GROUP BY mp.MPName
  135. ORDER BY RulesPerMP DESC
  136.  
  137. – Rules per MP by category:
  138. SELECT mp.MPName, r.RuleCategory, COUNT(*) AS RulesPerMPPerCategory
  139. FROM Rules r
  140. INNER JOIN ManagementPack mp ON mp.ManagementPackID = r.ManagementPackID
  141. GROUP BY mp.MPName, r.RuleCategory
  142. ORDER BY RulesPerMPPerCategory DESC
  143.  
  144. – Monitors Per MP:
  145. SELECT mp.MPName, COUNT(*) AS MonitorsPerMPPerCategory
  146. FROM Monitor m
  147. INNER JOIN ManagementPack mp ON mp.ManagementPackID = m.ManagementPackID
  148. GROUP BY mp.MPName
  149. ORDER BY COUNT(*) DESC
  150.  
  151. – To find your Monitor by common name:
  152. SELECT * FROM Monitor
  153. INNER JOIN LocalizedText LT ON LT.ElementName = Monitor.MonitorName
  154. WHERE LTValue = ‘My Monitor Name’
  155.  
  156. – To find all Rules per MP that generate an alert:
  157. declare @mpid AS varchar(50)
  158. SELECT @mpid= managementpackid FROM managementpack WHERE
  159. mpName=‘Microsoft.BizTalk.Server.2006.Monitoring’
  160. SELECT rl.rulename,rl.ruleid,md.modulename FROM rules rl, module md
  161. WHERE md.managementpackid = @mpid
  162. AND rl.ruleid=md.parentid
  163. AND moduleconfiguration LIKE ‘%50%’
  164.  
  165. – To find all rules per MP with a given alert severity:
  166. declare @mpid AS varchar(50)
  167. SELECT @mpid= managementpackid FROM managementpack WHERE
  168. mpName=‘Microsoft.BizTalk.Server.2006.Monitoring’
  169. SELECT rl.rulename,rl.ruleid,md.modulename FROM rules rl, module md
  170. WHERE md.managementpackid = @mpid
  171. AND rl.ruleid=md.parentid
  172. AND moduleconfiguration LIKE ‘%2%’
  173.  
  174. – Number of instances of a type:  (Number of disks, computers, databases, etc that OpsMgr has discovered)
  175. SELECT mt.ManagedTypeID, mt.TypeName, COUNT(*) AS NumEntitiesByType
  176. FROM BaseManagedEntity bme WITH(NOLOCK)
  177.             LEFT JOIN ManagedType mt WITH(NOLOCK) ON mt.ManagedTypeID = bme.BaseManagedTypeID
  178. WHERE bme.IsDeleted = 0
  179. GROUP BY mt.ManagedTypeID, mt.TypeName
  180. ORDER BY COUNT(*) DESC
  181.  
  182. – Number of Views per Management Pack:
  183. SELECT mp.MPName, v.ViewVisible, COUNT(*) AS ViewsPerMP
  184. FROM [Views] v
  185.             INNER JOIN ManagementPack mp ON mp.ManagementPackID = v.ManagementPackID
  186. GROUP BY  mp.MPName, v.ViewVisible
  187. ORDER BY v.ViewVisible DESC, COUNT(*) DESC  
  188.  
  189. – Grooming:
  190. SELECT * FROM PartitionAndGroomingSettings WITH (NOLOCK)
  191.  
  192. – All managed computers count:
  193. SELECT COUNT(*) AS NumManagedComps FROM (
  194. SELECT bme2.BaseManagedEntityID
  195. FROM BaseManagedEntity bme WITH (NOLOCK)
  196.             INNER JOIN BaseManagedEntity bme2 WITH (NOLOCK) ON bme2.BaseManagedEntityID = bme.TopLevelHostEntityID
  197. WHERE bme2.IsDeleted = 0
  198.             AND bme2.IsDeleted = 0
  199.             AND bme2.BaseManagedTypeID = (SELECT TOP 1 ManagedTypeID FROM ManagedType WHERE TypeName = ‘microsoft.windows.computer’)
  200. GROUP BY bme2.BaseManagedEntityID
  201. ) AS Comps
  202.  
  203. – Classes available in the DB:
  204. SELECT * FROM ManagedType
  205.  
  206. – Classes available in the DB for Microsoft Windows type:
  207. SELECT * FROM ManagedType
  208. WHERE TypeName LIKE ‘Microsoft.Windows.%’  
  209.  
  210. – Every property of every class:
  211. SELECT * FROM MT_Computer  
  212.  
  213. – All instances of all types once discovered
  214. SELECT * FROM BaseManagedEntity
  215.  
  216. – To get the state of every instance of a particular monitor the following query can be run, (replace  with the name of the monitor):
  217. SELECT bme.FullName, bme.DisplayName, s.HealthState FROM state AS s, BaseManagedEntity AS bme
  218. WHERE s.basemanagedentityid = bme.basemanagedentityid
  219.         AND s.monitorid IN (SELECT MonitorId FROM Monitor WHERE MonitorName = )
  220.  
  221. – For example, this gets the state of the Microsoft.SQLServer.2005.DBEngine.ServiceMonitor for each instance of the SQL 2005 Database Engine class.
  222. SELECT bme.FullName, bme.DisplayName, s.HealthState
  223. FROM state AS s, BaseManagedEntity AS bme
  224. WHERE s.basemanagedentityid = bme.basemanagedentityid
  225.         AND s.monitorid IN (SELECT MonitorId FROM Monitor WHERE MonitorName = ‘Microsoft.SQLServer.2005.DBEngine.ServiceMonitor’)  
  226.  
  227. – 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:
  228. SELECT bme.FullName, bme.DisplayName, s.HealthState
  229. FROM state AS s, mt_managedcomputer AS mt, BaseManagedEntity AS bme
  230. WHERE s.basemanagedentityid = bme.basemanagedentityid
  231.         AND s.monitorid IN (SELECT MonitorId FROM Monitor WHERE MonitorName = ‘System.Health.EntityState’)  
  232.  
  233. – Rules are stored in a table named Rules. This table has columns linking rules to classes and Management Packs.
  234. – To find all rules in a Management Pack use the following query and substitute in the required Management Pack name:
  235. SELECT * FROM Rules WHERE ManagementPackID = (SELECT ManagementPackID FROM ManagementPack WHERE MPName = ‘Microsoft.Windows.Server.2003′)  
  236.  
  237. – To find all rules targeted at a given class use the following query and substitute in the required class name:
  238. SELECT * FROM Rules WHERE TargetManagedEntityType = (SELECT ManagedTypeId FROM ManagedType WHERE TypeName = ‘Microsoft.Windows.Computer’)  
  239.  
  240. – 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:
  241.  
  242. SELECT * FROM Alert WHERE ProblemID IN (SELECT MonitorId FROM Monitor WHERE MonitorName = ‘Microsoft.SQLServer.2005.DBEngine.ServiceMonitor’)
  243.  
  244. – 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_DBEngine is used to look for SQL alerts:
  245. SELECT * FROM Alert WHERE BaseManagedEntityID IN (SELECT BaseManagedEntityID FROM MT_DBEngine)
  246.  
  247. – To determine which table is currently being written to for event and performance data use the following query:
  248. SELECT * FROM PartitionTables WHERE IsCurrent = 1
  249.  
  250. – To retrieve events generated by a specific rule use the following query and substitute in the required rule ID:
  251. SELECT * FROM Event_00 WHERE RuleId = (SELECT RuleId FROM Rules WHERE RuleName = ‘Microsoft.Windows.Server.2003.OperatingSystem.CleanShutdown.Collection ‘)
  252.  
  253. – To retrieve all events generated by rules in a specific Management Pack the following query can be used where the Management Pack name is substituted with the required value:
  254. SELECT * FROM EventAllView
  255. WHERE RuleID IN (SELECT RuleId FROM Rules WHERE ManagementPackId =
  256.         (SELECT ManagementPackId FROM ManagementPack WHERE MPName = ‘Microsoft.Windows.Server.2003′))
  257.  
  258. – To retrieve all performance data for a given rule in a readable format use the following query:
  259. SELECT pc.ObjectName, pc.CounterName, ps.PerfmonInstanceName, pd.SampleValue, pd.TimeSampled
  260. FROM PerformanceDataAllView AS pd, PerformanceCounter AS pc, PerformanceSource AS ps
  261. WHERE pd.PerformanceSourceInternalId IN (SELECT PerformanceSourceInternalId FROM PerformanceSource
  262.         WHERE RuleId = (SELECT RuleId FROM Rules WHERE RuleName =‘ Microsoft.Windows.Server.2003.LogicalDisk.FreeSpace.Collection’))
  263.  
  264. – Information on existing User Roles:
  265. SELECT UserRoleName, IsSystem FROM userrole
  266.  
  267. – Grooming in the DataWarehouse:
  268. – Grooming no longer uses SQL agent jobs.  Grooming is handled by scheduled stored procedures, that run much more frequently, which provides less impact than in the previous version.
  269. – Default grooming for the DW for each dataset, to examine Data Warehouse grooming settings:
  270. SELECT AggregationIntervalDurationMinutes, BuildAggregationStoredProcedureName, GroomStoredProcedureName, MaxDataAgeDays, GroomingIntervalMinutes FROM StandardDatasetAggregation
  271.  
  272. – The first row is the interval in minutes.
  273. – NULL is raw data, 60 is hourly, and 1440 is daily.
  274. – The second and third row shows what data it is
  275. – MaxDataAgeDays has the retention period in days - this is the field to update if the administrator wants to lower the days of retention.
  276. – RAW alert – 400 days
  277. – RAW event – 100 days
  278. – RAW perf – 10 days (hourly and daily perf = 400 days)
  279. – RAW state – 180 days  (hourly and daily state = 400 days)
  280.  
  281. – AEM Queries (Data Warehouse):
  282.  
  283. – Default query to return all RAW AEM data:
  284. SELECT * FROM [CM].[vCMAemRaw] Rw
  285. INNER JOIN dbo.AemComputer Computer ON Computer.AemComputerRowID = Rw.AemComputerRowID
  286. INNER JOIN dbo.AemUser Usr ON Usr.AemUserRowId = Rw.AemUserRowId
  287. INNER JOIN dbo.AemErrorGroup EGrp ON Egrp.ErrorGroupRowId = Rw.ErrorGroupRowId
  288. INNER JOIN dbo.AemApplication App ON App.ApplicationRowId = Egrp.ApplicationRowId
  289.  
  290. – Count the raw crashes per day:
  291. SELECT CONVERT(char(10), DateTime, 101) AS "Crash Date (by Day)", COUNT(*) AS "Number of Crashes"
  292. FROM [CM].[vCMAemRaw]
  293. GROUP BY CONVERT(char(10), DateTime, 101)
  294. ORDER BY "Crash Date (by Day)" DESC
  295.  
  296. – Count the total number of raw crashes in the DW database:
  297. SELECT count(*) FROM CM.vCMAemRaw
  298.  
  299. – Default grooming for the DW for the AEM dataset:  (Aggregated data kept for 400 days, RAW 30 days by default)
  300. SELECT AggregationTypeID, BuildAggregationStoredProcedureName, GroomStoredProcedureName, MaxDataAgeDays, GroomingIntervalMinutes
  301. FROM StandardDatasetAggregation WHERE BuildAggregationStoredProcedureName = ‘AemAggregate’
  302.  
  303. – MISCELLANEOUS SECTION –
  304. —————————
  305.  
  306. – Simple query to display large tables, to determine what is taking up space in the database:
  307. SELECT so.name,
  308. 8 * Sum(CASE WHEN si.indid IN (0, 1) THEN si.reserved END) AS data_kb,
  309. Coalesce(8 * Sum(CASE WHEN si.indid NOT IN (0, 1, 255) THEN si.reserved END), 0) AS index_kb,
  310. Coalesce(8 * Sum(CASE WHEN si.indid IN (255) THEN si.reserved END), 0) AS blob_kb
  311. FROM dbo.sysobjects AS so JOIN dbo.sysindexes AS si ON (si.id = so.id)
  312. WHERE ‘U’ = so.type GROUP BY so.name  ORDER BY data_kb DESC
  313.  
  314. – Is SQL broker enabled?
  315. SELECT is_broker_enabled FROM sys.DATABASES WHERE name = ‘OperationsManager’
  316.  
  317. – How to identify your version of SQL server:
  318. SELECT  SERVERPROPERTY(‘productversion’), SERVERPROPERTY (‘productlevel’), SERVERPROPERTY (‘edition’)
  319.  
  320. – SQL 2005:
  321. – SQL Server 2005 RTM                    2005.90.1399
  322. – SQL Server 2005 SP1                     2005.90.2047
  323. – SQL Server 2005 SP1 plus 918222  2005.90.2153
  324. – SQL Server 2005 SP2                     2005.90.3042
  325.  
Footnotes listed in the above post:
  1. And don’t call me Shirley []

One Response to “Useful Operations Manager 2007 SQL queries - prettified and PoShified”

  1. Gravatar Pavleck.Net » Blog Archive » Useful SCOM SQL Queries Says:

    [...] essentially mirroring this from the original one on my blog, but since I’ve decided to transition to a tech blog and a non-tech blog, it seems [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>