Guest
Public paste!

ziembor

By: a guest | Apr 13th, 2009 | Syntax: SQL | Size: 3.82 KB | Hits: 1,434 | Expires: Never
Copy text to clipboard
  1.  
  2. DECLARE @StartDate datetime
  3. DECLARE @EndDate datetime
  4. DECLARE @StartDateMAX datetime
  5. -- comment bellow if You will use as parameter
  6. DECLARE @FullName nvarchar(255)
  7.  
  8. DECLARE @CounterIDs TABLE(ID INT, ObjectName NVARCHAR(60), CounterName NVARCHAR(60))
  9.  
  10. SELECT      @StartDateMAX =  max(vPerf.DateTime)  FROM OperationsManagerDW.Perf.vPerfRaw AS vPerf
  11. SET @StartDate = ISNULL(@StartDate, cast(cast(cast(DATEADD(DAY,-1,@StartDateMAX) AS int) AS float) AS datetime));
  12. SET @EndDate = ISNULL(@EndDate, cast(cast(cast(DATEADD(DAY,0,GetDate()) AS int) AS float) AS datetime));
  13.  
  14. SET @FullName = ISNULL(@FullName,'Microsoft.SystemCenter.AllComputersGroup');
  15.  
  16. INSERT INTO @CounterIDs
  17. SELECT RuleRowId, ObjectName, CounterName
  18. --  FROM OperationsManagerDW.dbo.vPerformanceRule vPR  where objectName in ('LogicalDisk', 'Network Interface', 'System','Processor','Memory') and
  19. FROM OperationsManagerDW.dbo.vPerformanceRule vPR  WHERE objectName IN ('LogicalDisk') AND
  20. ((counterName = '% Free Space' ) OR (counterName = 'Free Megabytes'))
  21.  
  22. DECLARE @ManagedEntityRowId INT
  23. SELECT @ManagedEntityRowId = ManagedEntityRowId
  24. FROM OperationsManagerDW.dbo.vManagedEntity
  25. WHERE FullName = @FullName
  26.  
  27. DECLARE @MERIOBJ nvarchar(255)
  28. SET @MERIOBJ='<Data><Objects><Object Use="Containment">' + CAST(@ManagedEntityRowId AS VARCHAR(5)) + '</Object></Objects></Data>'
  29.  
  30. DECLARE @ManagedEntity TABLE(ManagedEntityRowId int)
  31.  
  32. DECLARE @FROM DATETIME
  33. DECLARE @TO DATETIME
  34. SET @TO = GETDATE();
  35. SET @FROM = DATEADD(MONTH, -1, @TO)
  36.  
  37. INSERT INTO @ManagedEntity
  38. EXEC [OperationsManagerDW].[dbo].[Microsoft_SystemCenter_DataWarehouse_Report_Library_ReportObjectListParse]
  39. @FROM,
  40. @TO,
  41. @MERIOBJ
  42.  
  43.  
  44. DECLARE @CounterResults TABLE([DateTime] datetime,AverageValue int,Path nvarchar(255),InstanceName nvarchar(255),ObjectName nvarchar(255),CounterName nvarchar(255))
  45.  
  46. INSERT INTO @CounterResults
  47. SELECT
  48. max(vPerf.DateTime) AS DateTime,
  49. max(vPerf.SampleValue) AS AverageValue,
  50. vME.Path,
  51. vPRI.InstanceName,
  52. C.ObjectName,
  53. C.CounterName
  54.  
  55. FROM OperationsManagerDW.Perf.vPerfRaw AS vPerf
  56. INNER JOIN
  57. OperationsManagerDW.dbo.vPerformanceRuleInstance vPRI ON vPRI.PerformanceRuleInstanceRowId = vPerf.PerformanceRuleInstanceRowId
  58. INNER JOIN
  59. OperationsManagerDW.dbo.vManagedEntity vME ON vPerf.ManagedEntityRowId = vME.ManagedEntityRowId
  60. INNER JOIN
  61. @CounterIDs C ON vPRI.RuleRowId = C.ID
  62. WHERE
  63. vPerf.DateTime > @StartDate
  64. AND
  65. vPerf.DateTime < @EndDate
  66. AND
  67. vME.Path IN (SELECT DISTINCT
  68. vManagedEntity.Path
  69. FROM OperationsManagerDW.Perf.vPerfRaw AS vPerf
  70. INNER JOIN
  71. OperationsManagerDW.dbo.vPerformanceRuleInstance ON vPerformanceRuleInstance.PerformanceRuleInstanceRowId = vPerf.PerformanceRuleInstanceRowId
  72. INNER JOIN
  73. OperationsManagerDW.dbo.vManagedEntity ON vPerf.ManagedEntityRowId = vManagedEntity.ManagedEntityRowId
  74. INNER JOIN
  75. @ManagedEntity ME ON vManagedEntity.ManagedEntityRowId = ME.ManagedEntityRowId
  76. )
  77. GROUP BY vME.Path,
  78. vPRI.InstanceName,
  79. C.ObjectName,
  80. C.CounterName
  81.  
  82. ORDER BY DateTime
  83.  
  84. DECLARE @ProcFreeSpaceResults TABLE([DateTime] datetime,ProcFreeSpace int,Path nvarchar(255),InstanceName nvarchar(255),CounterName nvarchar(255))
  85. INSERT INTO @ProcFreeSpaceResults
  86. SELECT DateTime,AverageValue AS ProcFreeSpace,Path,InstanceName,CounterName FROM @CounterResults WHERE CounterName = '% Free Space'
  87.  
  88.  
  89. DECLARE @FreeMegabytesResults TABLE([DateTime] datetime,FreeMegabytes int,Path nvarchar(255),InstanceName nvarchar(255),CounterName nvarchar(255))
  90. INSERT INTO @FreeMegabytesResults
  91. SELECT DateTime,AverageValue AS FreeMegabytes,Path,InstanceName,CounterName FROM @CounterResults WHERE CounterName = 'Free Megabytes'
  92.  
  93. SELECT m.Path AS Computer,m.InstanceName AS LogicalDisk,FreeMegabytes,ProcFreeSpace,m.DateTime FROM @ProcFreeSpaceResults F ,@FreeMegabytesResults m WHERE
  94. m.Path=F.Path AND
  95. m.InstanceName=f.InstanceName
  96. ORDER BY computer,LogicalDisk