Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit b04c2b8

Browse files
committed
Added endpoints tor retrieve perf data - IIS and AD configuration required
1 parent 00ca4f0 commit b04c2b8

File tree

4 files changed

+352
-11
lines changed

4 files changed

+352
-11
lines changed
Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Web.Http;
5+
using System.Data.SqlClient;
6+
using System.Configuration;
7+
using System.Data;
8+
using System.Text;
9+
using Newtonsoft.Json;
10+
using System.Data.SqlTypes;
11+
using Microsoft.SqlServer.Server;
12+
using System.Security.Principal;
13+
14+
namespace SCOM_API.Controllers
15+
{
16+
17+
public class SCOMPerfController : ApiController
18+
{
19+
SqlConnection DWConnection = new SqlConnection();
20+
public SCOMPerfController()
21+
{
22+
System.Security.Principal.WindowsImpersonationContext impersonationContext;
23+
impersonationContext =
24+
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
25+
26+
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
27+
var SCOMDW = ConfigurationManager.AppSettings["ScomDWServer"];
28+
builder["Data Source"] = SCOMDW;
29+
builder["Integrated Security"] = "SSPI";
30+
builder["Initial Catalog"] = "OperationsManagerDW";
31+
32+
33+
34+
DWConnection.ConnectionString = builder.ConnectionString;
35+
}
36+
37+
private DataTable dataTable = new DataTable();
38+
39+
#region rawData
40+
41+
/// <summary>
42+
/// Get RAW performance data from a specific managedEntity and metric
43+
/// </summary>
44+
/// <example>
45+
/// API/Perf/5f2f477c-3b19-4ce8-b27a-eef59b9dc377?counterName=PercentMemoryUsed
46+
/// </example>
47+
/// <param name="managedEntityId">The guid of your managed entity, ie: windows computer</param>
48+
/// <param name="counterName">The performance counter you want to retrieve data from</param>
49+
/// <param name="startDate">Optionally add your start date. Data will be pulled between start and end dates</param>
50+
/// <param name="endDate">Optionally add your start date. Data will be pulled between start and end dates</param>
51+
52+
[HttpGet]
53+
[Route("API/Perf/{managedEntityId:Guid}")]
54+
public IHttpActionResult GetPerformanceData(Guid managedEntityId, string counterName, DateTime? startDate = null, DateTime? endDate = null)
55+
{
56+
using (WindowsImpersonationContext context = (WindowsIdentity.GetCurrent()).Impersonate())
57+
{
58+
if (managedEntityId == Guid.Empty && string.IsNullOrEmpty(counterName))
59+
{
60+
throw new HttpResponseException(Request
61+
.CreateResponse(HttpStatusCode.BadRequest));
62+
}
63+
64+
65+
else
66+
{
67+
68+
if (!endDate.HasValue)
69+
{
70+
endDate = DateTime.UtcNow;
71+
}
72+
73+
if (!startDate.HasValue)
74+
{
75+
startDate = (DateTime)SqlDateTime.MinValue;
76+
}
77+
78+
// Construct the actual DW sql query
79+
string sqlQuery = @"
80+
USE OperationsManagerDW
81+
SELECT DateTime, SampleValue, ObjectName, InstanceName, CounterName
82+
FROM
83+
Perf.vPerfRaw INNER JOIN
84+
vPerformanceRuleInstance ON Perf.vPerfRaw.PerformanceRuleInstanceRowId = vPerformanceRuleInstance.PerformanceRuleInstanceRowId INNER JOIN
85+
vPerformanceRule ON vPerformanceRuleInstance.RuleRowId = vPerformanceRule.RuleRowId INNER JOIN
86+
vRelationship ON Perf.vPerfRaw.ManagedEntityRowId = vRelationship.TargetManagedEntityRowId INNER JOIN
87+
vManagedEntity ON vRelationship.SourceManagedEntityRowId = vManagedEntity.ManagedEntityRowId
88+
WHERE ManagedEntityGuid = @entity
89+
AND vPerformanceRule.CounterName = @counter
90+
AND DateTime between @startDate and @endDate
91+
ORDER BY Perf.vPerfRaw.DateTime DESC";
92+
93+
try
94+
{
95+
96+
// Initiate command and add parameters
97+
98+
SqlCommand sqlCmd = new SqlCommand();
99+
sqlCmd.CommandType = CommandType.Text;
100+
SqlParameter counter = sqlCmd.Parameters.Add("@counter", SqlDbType.VarChar, 256);
101+
counter.Value = counterName;
102+
sqlCmd.Parameters.AddWithValue("@entity", managedEntityId);
103+
sqlCmd.Parameters.AddWithValue("@startDate", startDate);
104+
sqlCmd.Parameters.AddWithValue("@endDate", endDate);
105+
106+
sqlCmd.CommandText = sqlQuery;
107+
sqlCmd.Connection = DWConnection;
108+
109+
110+
// Connect SQL
111+
DWConnection.Open();
112+
// Fill datatable with result from SQL query
113+
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
114+
da.Fill(dataTable);
115+
116+
string jsonString = string.Empty;
117+
jsonString = JsonConvert.SerializeObject(dataTable, Formatting.Indented);
118+
119+
// Close connections and reurn data
120+
da.Dispose();
121+
DWConnection.Close();
122+
return Ok(jsonString);
123+
}
124+
125+
catch (Exception Ex)
126+
{
127+
128+
HttpResponseMessage exeption = new HttpResponseMessage(HttpStatusCode.InternalServerError);
129+
exeption.Content = new StringContent(Ex.ToString());
130+
throw new HttpResponseException(exeption);
131+
}
132+
}
133+
134+
}
135+
136+
}
137+
138+
#endregion
139+
140+
#region hourlyData
141+
142+
/// <summary>
143+
/// Get hourly performance data from a specific managedEntity and metric
144+
/// </summary>
145+
/// <example>
146+
/// API/Perf/5f2f477c-3b19-4ce8-b27a-eef59b9dc377?counterName=PercentMemoryUsed
147+
/// </example>
148+
/// <param name="managedEntityId">The guid of your managed entity, ie: windows computer</param>
149+
/// <param name="counterName">The performance counter you want to retrieve data from</param>
150+
/// <param name="startDate">Optionally add your start date. Data will be pulled between start and end dates</param>
151+
/// <param name="endDate">Optionally add your start date. Data will be pulled between start and end dates</param>
152+
153+
[HttpGet]
154+
[Route("API/Perf/Hourly/{managedEntityId:Guid}")]
155+
public IHttpActionResult GetHourlyPerformanceData(Guid managedEntityId, string counterName, DateTime? startDate = null, DateTime? endDate = null)
156+
{
157+
using (WindowsImpersonationContext context = (WindowsIdentity.GetCurrent()).Impersonate())
158+
{
159+
if (managedEntityId == Guid.Empty && string.IsNullOrEmpty(counterName))
160+
{
161+
throw new HttpResponseException(Request
162+
.CreateResponse(HttpStatusCode.BadRequest));
163+
}
164+
165+
166+
else
167+
{
168+
169+
if (!endDate.HasValue)
170+
{
171+
endDate = DateTime.UtcNow;
172+
}
173+
174+
if (!startDate.HasValue)
175+
{
176+
startDate = (DateTime)SqlDateTime.MinValue;
177+
}
178+
179+
// Construct the actual DW sql query
180+
string sqlQuery = @"
181+
USE OperationsManagerDW
182+
SELECT DateTime, MaxValue, AverageValue, MinValue, StandardDeviation, ObjectName, InstanceName, CounterName
183+
FROM
184+
Perf.vPerfHourly INNER JOIN
185+
vPerformanceRuleInstance ON Perf.vPerfHourly.PerformanceRuleInstanceRowId = vPerformanceRuleInstance.PerformanceRuleInstanceRowId INNER JOIN
186+
vPerformanceRule ON vPerformanceRuleInstance.RuleRowId = vPerformanceRule.RuleRowId INNER JOIN
187+
vRelationship ON Perf.vPerfHourly.ManagedEntityRowId = vRelationship.TargetManagedEntityRowId INNER JOIN
188+
vManagedEntity ON vRelationship.SourceManagedEntityRowId = vManagedEntity.ManagedEntityRowId
189+
WHERE ManagedEntityGuid = @entity
190+
AND vPerformanceRule.CounterName = @counter
191+
AND DateTime between @startDate and @endDate
192+
ORDER BY Perf.vPerfHourly.DateTime DESC";
193+
194+
try
195+
{
196+
// Initiate command and add parameters
197+
SqlCommand sqlCmd = new SqlCommand();
198+
sqlCmd.CommandType = CommandType.Text;
199+
SqlParameter counter = sqlCmd.Parameters.Add("@counter", SqlDbType.VarChar, 256);
200+
counter.Value = counterName;
201+
sqlCmd.Parameters.AddWithValue("@entity", managedEntityId);
202+
sqlCmd.Parameters.AddWithValue("@startDate", startDate);
203+
sqlCmd.Parameters.AddWithValue("@endDate", endDate);
204+
205+
sqlCmd.CommandText = sqlQuery;
206+
sqlCmd.Connection = DWConnection;
207+
208+
209+
// Connect SQL
210+
DWConnection.Open();
211+
// Fill datatable with result from SQL query
212+
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
213+
da.Fill(dataTable);
214+
215+
string jsonString = string.Empty;
216+
jsonString = JsonConvert.SerializeObject(dataTable, Formatting.Indented);
217+
218+
// Close connections and reurn data
219+
da.Dispose();
220+
DWConnection.Close();
221+
return Ok(jsonString);
222+
}
223+
224+
catch (Exception Ex)
225+
{
226+
227+
HttpResponseMessage exeption = new HttpResponseMessage(HttpStatusCode.InternalServerError);
228+
exeption.Content = new StringContent(Ex.ToString());
229+
throw new HttpResponseException(exeption);
230+
}
231+
}
232+
233+
}
234+
}
235+
#endregion
236+
237+
#region dailyData
238+
239+
/// <summary>
240+
/// Get daily aggragated performance data from a specific managedEntity and metric
241+
/// </summary>
242+
/// <example>
243+
/// API/Perf/5f2f477c-3b19-4ce8-b27a-eef59b9dc377?counterName=PercentMemoryUsed
244+
/// </example>
245+
/// <param name="managedEntityId">The guid of your managed entity, ie: windows computer</param>
246+
/// <param name="counterName">The performance counter you want to retrieve data from</param>
247+
/// <param name="startDate">Optionally add your start date. Data will be pulled between start and end dates</param>
248+
/// <param name="endDate">Optionally add your start date. Data will be pulled between start and end dates</param>
249+
250+
[HttpGet]
251+
[Route("API/Perf/Daily/{managedEntityId:Guid}")]
252+
public IHttpActionResult GetDailyPerformanceData(Guid managedEntityId, string counterName, DateTime? startDate = null, DateTime? endDate = null)
253+
{
254+
using (WindowsImpersonationContext context = (WindowsIdentity.GetCurrent()).Impersonate())
255+
{
256+
if (managedEntityId == Guid.Empty && string.IsNullOrEmpty(counterName))
257+
{
258+
throw new HttpResponseException(Request
259+
.CreateResponse(HttpStatusCode.BadRequest));
260+
}
261+
262+
263+
else
264+
{
265+
266+
if (!endDate.HasValue)
267+
{
268+
endDate = DateTime.UtcNow;
269+
}
270+
271+
if (!startDate.HasValue)
272+
{
273+
startDate = (DateTime)SqlDateTime.MinValue;
274+
}
275+
276+
// Construct the actual DW sql query
277+
string sqlQuery = @"
278+
USE OperationsManagerDW
279+
SELECT DateTime, MaxValue, AverageValue, MinValue, StandardDeviation, ObjectName, InstanceName, CounterName
280+
FROM
281+
Perf.vPerfDaily INNER JOIN
282+
vPerformanceRuleInstance ON Perf.vPerfDaily.PerformanceRuleInstanceRowId = vPerformanceRuleInstance.PerformanceRuleInstanceRowId INNER JOIN
283+
vPerformanceRule ON vPerformanceRuleInstance.RuleRowId = vPerformanceRule.RuleRowId INNER JOIN
284+
vRelationship ON Perf.vPerfDaily.ManagedEntityRowId = vRelationship.TargetManagedEntityRowId INNER JOIN
285+
vManagedEntity ON vRelationship.SourceManagedEntityRowId = vManagedEntity.ManagedEntityRowId
286+
WHERE ManagedEntityGuid = @entity
287+
AND vPerformanceRule.CounterName = @counter
288+
AND DateTime between @startDate and @endDate
289+
ORDER BY Perf.vPerfDaily.DateTime DESC";
290+
291+
try
292+
{
293+
// Initiate command and add parameters
294+
SqlCommand sqlCmd = new SqlCommand();
295+
sqlCmd.CommandType = CommandType.Text;
296+
SqlParameter counter = sqlCmd.Parameters.Add("@counter", SqlDbType.VarChar, 256);
297+
counter.Value = counterName;
298+
sqlCmd.Parameters.AddWithValue("@entity", managedEntityId);
299+
sqlCmd.Parameters.AddWithValue("@startDate", startDate);
300+
sqlCmd.Parameters.AddWithValue("@endDate", endDate);
301+
302+
sqlCmd.CommandText = sqlQuery;
303+
sqlCmd.Connection = DWConnection;
304+
305+
// Connect SQL
306+
DWConnection.Open();
307+
308+
if (DWConnection.State == ConnectionState.Closed)
309+
{
310+
throw new HttpResponseException(HttpStatusCode.ServiceUnavailable);
311+
}
312+
313+
314+
// Fill datatable with result from SQL query
315+
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
316+
da.Fill(dataTable);
317+
318+
string jsonString = string.Empty;
319+
jsonString = JsonConvert.SerializeObject(dataTable, Formatting.Indented);
320+
321+
// Close connections and reurn data
322+
da.Dispose();
323+
DWConnection.Close();
324+
return Ok(jsonString);
325+
}
326+
327+
catch (Exception Ex)
328+
{
329+
330+
HttpResponseMessage exeption = new HttpResponseMessage(HttpStatusCode.InternalServerError);
331+
exeption.Content = new StringContent(Ex.ToString());
332+
throw new HttpResponseException(exeption);
333+
}
334+
}
335+
336+
}
337+
}
338+
#endregion
339+
340+
}
341+
}
342+
//END

SCOM API/SCOM API.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@
119119
<Private>True</Private>
120120
</Reference>
121121
<Reference Include="Microsoft.EnterpriseManagement.Core, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
122-
<SpecificVersion>False</SpecificVersion>
123-
<HintPath>\\distr\users\aa98\PUBLIC\Microsoft.EnterpriseManagement.Core.dll</HintPath>
122+
<HintPath>..\packages\Unofficial.Microsoft.EnterpriseManagement.OperationsManager.7.0.5000\lib\net40\Microsoft.EnterpriseManagement.Core.dll</HintPath>
123+
<Private>True</Private>
124124
</Reference>
125125
<Reference Include="Microsoft.EnterpriseManagement.OperationsManager, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
126-
<SpecificVersion>False</SpecificVersion>
127-
<HintPath>\\distr\users\aa98\PUBLIC\Microsoft.EnterpriseManagement.OperationsManager.dll</HintPath>
126+
<HintPath>..\packages\Unofficial.Microsoft.EnterpriseManagement.OperationsManager.7.0.5000\lib\net40\Microsoft.EnterpriseManagement.OperationsManager.dll</HintPath>
127+
<Private>True</Private>
128128
</Reference>
129129
<Reference Include="Microsoft.EnterpriseManagement.Runtime, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
130-
<SpecificVersion>False</SpecificVersion>
131-
<HintPath>\\distr\users\aa98\PUBLIC\Microsoft.EnterpriseManagement.Runtime.dll</HintPath>
130+
<HintPath>..\packages\Unofficial.Microsoft.EnterpriseManagement.OperationsManager.7.0.5000\lib\net40\Microsoft.EnterpriseManagement.Runtime.dll</HintPath>
131+
<Private>True</Private>
132132
</Reference>
133133
<Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
134134
<HintPath>..\packages\Microsoft.Extensions.Configuration.Abstractions.1.0.0\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
@@ -270,6 +270,7 @@
270270
<Compile Include="Controllers\SCOMComputerController.cs" />
271271
<Compile Include="Controllers\SCOMAlertController.cs" />
272272
<Compile Include="Controllers\SCOMAgentController.cs" />
273+
<Compile Include="Controllers\SCOMPerfController.cs" />
273274
<Compile Include="Global.asax.cs">
274275
<DependentUpon>Global.asax</DependentUpon>
275276
</Compile>

SCOM API/Web.config

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<configuration>
77
<appSettings>
88
<add key="ScomSdkServer" value="localhost" />
9+
<add key="ScomDWServer" value="localhost" />
910
</appSettings>
1011
<system.web>
1112
<compilation debug="true" targetFramework="4.5.2" />
@@ -22,11 +23,7 @@
2223
<remove name="OPTIONSVerbHandler" />
2324
<remove name="TRACEVerbHandler" />
2425

25-
<add name="ApiURIs-ISAPI-Integrated-4.0"
26-
path="/API/*"
27-
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
28-
type="System.Web.Handlers.TransferRequestHandler"
29-
preCondition="integratedMode,runtimeVersionv4.0" />
26+
<add name="ApiURIs-ISAPI-Integrated-4.0" path="/API/*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
3027

3128
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
3229

0 commit comments

Comments
 (0)