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

Commit 992b173

Browse files
committed
added alerts by mon.object, update to all alert properties
Breaking changes in endpoints
1 parent d4075b8 commit 992b173

4 files changed

Lines changed: 185 additions & 20 deletions

File tree

SCOM API/App_Start/WebApiConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public static void Register(HttpConfiguration config)
1313
// Web API routes
1414
config.MapHttpAttributeRoutes();
1515

16+
1617
config.Routes.MapHttpRoute(
1718
name: "System Center Operations Manager API",
1819
routeTemplate: "API/{controller}/{action}/{id}",
1920
defaults: new { id = RouteParameter.Optional }
2021
);
2122

23+
2224
// Web API configuration and services
2325
config.Filters.Add(new AuthorizeAttribute());
2426

SCOM API/Controllers/SCOMAlertController.cs

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Collections.ObjectModel;
1515
using Swashbuckle.AspNetCore.SwaggerGen;
1616
using System.Web.Http.Description;
17+
using SCOM_API.Models;
1718

1819
namespace SCOM_API.Controllers
1920
{
@@ -79,7 +80,7 @@ public IList<MonitoringAlert> GetAlertById(Guid Id)
7980
/// <param name="ComputerName">FQDN of the windows computer</param>
8081
/// <param name="IncClosed">if true closed alert is also returned. Default is 'false'</param>
8182
[HttpGet]
82-
[Route("Alerts/{ComputerName}")]
83+
[Route("Alerts/Computer/{ComputerName}")]
8384
public IList<MonitoringAlert> GetAlertByComputerName(string ComputerName, bool? IncClosed = false)
8485
{
8586
if (string.IsNullOrEmpty(ComputerName))
@@ -93,7 +94,7 @@ public IList<MonitoringAlert> GetAlertByComputerName(string ComputerName, bool?
9394
{
9495
//Set alert criteria
9596
var Criteria = string.Format("MonitoringObjectPath = '{0}'", ComputerName);
96-
///Get alerts
97+
//Get alerts
9798
MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria(Criteria);
9899
var Alert = mg.OperationalData.GetMonitoringAlerts(alertCriteria, default(DateTime));
99100

@@ -113,31 +114,74 @@ public IList<MonitoringAlert> GetAlertByComputerName(string ComputerName, bool?
113114
}
114115

115116
/// <summary>
116-
/// Updates the specified alert.
117+
/// Get all alerts related to the specific monitoring object.
117118
/// </summary>
118-
/// <param name="ResolutionState">If specified as 255 (closed) and alert is raised by monitor. The corresponding monitor will be reset</param>
119-
/// <param name="TicketId">set if you want to update alert with a ticket id</param>
120-
/// <param name="Id">the alert GUID</param>
119+
/// <param name="MonitoringObjectId">SCOM GUID of the monitoring object</param>
120+
/// <param name="IncClosed">if true closed alert is also returned. Default is 'false'</param>
121+
[HttpGet]
122+
[Route("Alerts/MonitoringObject/{MonitoringObjectId:Guid}")]
123+
public IList<MonitoringAlert> GetAlertByMonitoringObjectId(Guid MonitoringObjectId, bool? IncClosed = false)
124+
{
125+
if (MonitoringObjectId == Guid.Empty)
126+
{
127+
throw new HttpResponseException(Request
128+
.CreateResponse(HttpStatusCode.BadRequest));
129+
}
130+
131+
//If include closed alerts
132+
if (IncClosed == true)
133+
{
134+
//Set alert criteria
135+
var Criteria = string.Format("MonitoringObjectId = '{0}'", MonitoringObjectId);
136+
///Get alerts
137+
MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria(Criteria);
138+
var Alert = mg.OperationalData.GetMonitoringAlerts(alertCriteria, default(DateTime));
139+
140+
return Alert;
141+
}
142+
143+
else
144+
{
145+
//set alert criteria
146+
var Criteria = string.Format("MonitoringObjectId = '{0}' AND ResolutionState = 0", MonitoringObjectId);
147+
//Get alerts
148+
MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria(Criteria);
149+
var Alert = mg.OperationalData.GetMonitoringAlerts(alertCriteria, default(DateTime));
150+
151+
return Alert;
152+
}
153+
}
154+
155+
/// <summary>
156+
/// Updates the specific alert.
157+
/// </summary>
158+
/// <param name="Properties">Json object to update alert properties.
159+
/// All set properties are available.
160+
/// For more information please see technet documentation "http://bit.ly/2zblZLh"</param>
161+
/// <param name="Id">Specify alert guid you want to update</param>
121162
[HttpPut]
122163
[ResponseType(typeof(IEnumerable<MonitoringAlert>))]
123-
[Route("Alerts")]
124-
public IList<MonitoringAlert> UpdateAlertById(Guid Id, byte ResolutionState, string TicketId = "")
164+
[Route("Alerts/{Id:Guid}")]
165+
public IList<MonitoringAlert> UpdateAlertById([FromUri()]Guid Id, [FromBody()] SCOMAlertUpdateModel Properties)
125166
{
126-
if (string.IsNullOrEmpty(ResolutionState.ToString()))
167+
if (Id == Guid.Empty)
127168
{
128169
throw new HttpResponseException(Request
129170
.CreateResponse(HttpStatusCode.BadRequest));
130171
}
131172

173+
// Declare variables
174+
var ResolutionState = Properties.resolutionState;
132175

133176
//alert criteria
134177
var Criteria = string.Format("Id = '{0}'", Id);
135178
MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria(Criteria);
136179
var alerts = mg.OperationalData.GetMonitoringAlerts(alertCriteria, default(DateTime));
180+
137181
foreach (MonitoringAlert a in alerts)
138182
{
139183
//If resolution state is set to closed and alert is raised by a monitor. Reset the monitor
140-
if (a.IsMonitorAlert & ResolutionState.ToString() == "255")
184+
if (a.IsMonitorAlert & ResolutionState == "255")
141185
{
142186
//Get object and monitor that raised the alert
143187
Guid monitoringObjectId = a.MonitoringObjectId;
@@ -146,27 +190,59 @@ public IList<MonitoringAlert> UpdateAlertById(Guid Id, byte ResolutionState, str
146190
var monObject = mg.EntityObjects.GetObject<MonitoringObject>(monitoringObjectId, ObjectQueryOptions.Default);
147191
//reset the monitor to 'close' the alert
148192
monObject.ResetMonitoringState(monitor);
149-
193+
150194
}
195+
196+
151197
else
152198
{
153-
154-
if (string.IsNullOrWhiteSpace(TicketId))
199+
200+
if (string.IsNullOrEmpty(ResolutionState))
155201
{
156-
a.ResolutionState = ResolutionState;
157-
string comment = "Changed resolution state (API)";
202+
a.TfsWorkItemId = Properties.tfsWorkItemId;
203+
a.TfsWorkItemOwner = Properties.tfsWorkItemOwner;
204+
a.Owner = Properties.owner;
205+
a.CustomField1 = Properties.customField1;
206+
a.CustomField2 = Properties.customField2;
207+
a.CustomField3 = Properties.customField3;
208+
a.CustomField4 = Properties.customField4;
209+
a.CustomField5 = Properties.customField5;
210+
a.CustomField6 = Properties.customField6;
211+
a.CustomField7 = Properties.customField7;
212+
a.CustomField8 = Properties.customField8;
213+
a.CustomField9 = Properties.customField9;
214+
a.CustomField10 = Properties.customField10;
215+
216+
string comment = "Alert properties updated";
158217
a.Update(comment);
159218

160219
}
161-
//If ticket id is specified.
220+
//If resolution state is specified
162221
else
163222
{
164-
a.ResolutionState = ResolutionState;
165-
a.TicketId = TicketId;
166-
string comment = "Changed resolution state and ticket id (API)";
223+
a.TfsWorkItemId = Properties.tfsWorkItemId;
224+
a.TfsWorkItemOwner = Properties.tfsWorkItemOwner;
225+
a.Owner = Properties.owner;
226+
a.CustomField1 = Properties.customField1;
227+
a.CustomField2 = Properties.customField2;
228+
a.CustomField3 = Properties.customField3;
229+
a.CustomField4 = Properties.customField4;
230+
a.CustomField5 = Properties.customField5;
231+
a.CustomField6 = Properties.customField6;
232+
a.CustomField7 = Properties.customField7;
233+
a.CustomField8 = Properties.customField8;
234+
a.CustomField9 = Properties.customField9;
235+
a.CustomField10 = Properties.customField10;
236+
237+
//Convert string resolution state to byte type
238+
var Res = Convert.ToByte(ResolutionState);
239+
a.ResolutionState = Res;
240+
a.TicketId = Properties.ticketId;
241+
var comments = $"Updated and changed resolution state: {ResolutionState}";
242+
string comment = comments;
167243
a.Update(comment);
168244
}
169-
245+
170246
}
171247

172248
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Web;
6+
7+
namespace SCOM_API.Models
8+
{
9+
public class SCOMAlertUpdateModel
10+
{
11+
/// <summary>
12+
/// Alert resolution state
13+
/// </summary>
14+
public string resolutionState { get; set; }
15+
16+
/// <summary>
17+
/// Connected ticket id
18+
/// </summary>
19+
public string ticketId { get; set; }
20+
21+
/// <summary>
22+
/// TfsWorkItemId
23+
/// </summary>
24+
public string tfsWorkItemId { get; set; }
25+
26+
/// <summary>
27+
/// TfsWorkItemOwner
28+
/// </summary>
29+
public string tfsWorkItemOwner { get; set; }
30+
31+
/// <summary>
32+
/// Alert owner
33+
/// </summary>
34+
public string owner { get; set; }
35+
36+
/// <summary>
37+
/// Alert custom field 1
38+
/// </summary>
39+
public string customField1 { get; set; }
40+
41+
/// <summary>
42+
/// Alert custom field 2
43+
/// </summary>
44+
public string customField2 { get; set; }
45+
46+
/// <summary>
47+
/// Alert custom field 3
48+
/// </summary>
49+
public string customField3 { get; set; }
50+
51+
/// <summary>
52+
/// Alert custom field 4
53+
/// </summary>
54+
public string customField4 { get; set; }
55+
56+
/// <summary>
57+
/// Alert custom field 5
58+
/// </summary>
59+
public string customField5 { get; set; }
60+
61+
/// <summary>
62+
/// Alert custom field 6
63+
/// </summary>
64+
public string customField6 { get; set; }
65+
66+
/// <summary>
67+
/// Alert custom field 7
68+
/// </summary>
69+
public string customField7 { get; set; }
70+
71+
/// <summary>
72+
/// Alert custom field 8
73+
/// </summary>
74+
public string customField8 { get; set; }
75+
76+
/// <summary>
77+
/// Alert custom field 9
78+
/// </summary>
79+
public string customField9 { get; set; }
80+
81+
/// <summary>
82+
/// Alert custom field 10
83+
/// </summary>
84+
public string customField10 { get; set; }
85+
}
86+
}

SCOM API/SCOM API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
<Compile Include="Global.asax.cs">
274274
<DependentUpon>Global.asax</DependentUpon>
275275
</Compile>
276+
<Compile Include="Models\SCOMAlertUpdateModel.cs" />
276277
<Compile Include="Models\SCOMComputerModelDetailed.cs" />
277278
<Compile Include="Models\SCOMObjectScheduleMaintenanceModel.cs" />
278279
<Compile Include="Models\SCOMObjectMaintenanceModel.cs" />

0 commit comments

Comments
 (0)