|
| 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 |
0 commit comments