/
AlertsController.java
339 lines (286 loc) · 14.4 KB
/
AlertsController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Copyright 2012-2015 TORCH GmbH, 2015 Graylog, Inc.
*
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*
*/
package controllers;
import com.google.inject.Inject;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.models.*;
import org.graylog2.restclient.models.alerts.Alert;
import org.graylog2.restclient.models.alerts.AlertCondition;
import org.graylog2.restclient.models.alerts.AlertConditionService;
import org.graylog2.restclient.models.api.requests.alerts.CreateAlertConditionRequest;
import org.graylog2.rest.models.alarmcallbacks.responses.AvailableAlarmCallbackSummaryResponse;
import play.mvc.BodyParser;
import play.mvc.Result;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @author Lennart Koopmann <lennart@torch.sh>
*/
public class AlertsController extends AuthenticatedController {
@Inject
private StreamService streamService;
@Inject
private UserService userService;
@Inject
private AlertConditionService alertConditionService;
@Inject
private AlarmCallbackService alarmCallbackService;
@Inject
private NodeService nodeService;
public Result index(String streamId) {
try {
Stream stream = streamService.get(streamId);
List<AlertCondition> alertConditions = alertConditionService.allOfStream(stream);
List<Alert> alerts = stream.getAlerts();
long totalAlerts = stream.getTotalAlerts();
StringBuilder users = new StringBuilder();
users.append("[");
List<User> userList = userService.allExceptAdmin();
int i = 0;
for(User user : userList){
users.append("\"").append(user.getName()).append("\"");
if(i != userList.size()-1) {
users.append(",");
}
i++;
}
users.append("]");
Map<String, AvailableAlarmCallbackSummaryResponse> availableAlarmCallbacks = alarmCallbackService.available(streamId);
List<AlarmCallback> alarmCallbacks = alarmCallbackService.all(streamId);
return ok(views.html.alerts.manage.render(
currentUser(),
stream,
alertConditions,
totalAlerts,
alerts,
users.toString(),
availableAlarmCallbacks,
alarmCallbacks,
nodeService.loadMasterNode()
));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not fetch stream. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public Result addTypeMessageCount(String streamId) {
Map<String,String> form = flattenFormUrlEncoded(request().body().asFormUrlEncoded());
if(!checkParam("grace", form) || !checkParam("time", form)
|| !checkParam("threshold", form)
|| !checkParam("threshold_type", form)
|| !checkParam("backlog", form)) {
flash("error", "Could not add alert condition: Missing parameters.");
return redirect(routes.AlertsController.index(streamId));
}
try {
Stream stream = streamService.get(streamId);
CreateAlertConditionRequest request = new CreateAlertConditionRequest();
request.type = "message_count";
request.parameters.put("grace", Integer.parseInt(form.get("grace")));
request.parameters.put("time", Integer.parseInt(form.get("time")));
request.parameters.put("threshold", Integer.parseInt(form.get("threshold")));
request.parameters.put("threshold_type", form.get("threshold_type"));
request.parameters.put("backlog", Integer.parseInt(form.get("backlog")));
stream.addAlertCondition(request);
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not create alert condition. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
flash("success", "Added alert condition.");
return redirect(routes.AlertsController.index(streamId));
}
@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public Result addTypeFieldValue(String streamId) {
Map<String,String> form = flattenFormUrlEncoded(request().body().asFormUrlEncoded());
if(!checkParam("grace", form) || !checkParam("time", form)
|| !checkParam("threshold", form)
|| !checkParam("threshold_type", form)
|| !checkParam("field", form)
|| !checkParam("type", form)
|| !checkParam("backlog", form)) {
flash("error", "Could not add alert condition: Missing parameters.");
return redirect(routes.AlertsController.index(streamId));
}
try {
Stream stream = streamService.get(streamId);
CreateAlertConditionRequest request = new CreateAlertConditionRequest();
request.type = "field_value";
request.parameters.put("grace", Integer.parseInt(form.get("grace")));
request.parameters.put("time", Integer.parseInt(form.get("time")));
request.parameters.put("threshold", Double.parseDouble(form.get("threshold")));
request.parameters.put("threshold_type", form.get("threshold_type"));
request.parameters.put("type", form.get("type"));
request.parameters.put("field", form.get("field"));
request.parameters.put("backlog", Integer.parseInt(form.get("backlog")));
stream.addAlertCondition(request);
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not create alert condition. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
flash("success", "Added alert condition.");
return redirect(routes.AlertsController.index(streamId));
}
@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public Result updateCondition(String streamId, String conditionId) {
Map<String,String> form = flattenFormUrlEncoded(request().body().asFormUrlEncoded());
for (String key : form.keySet()) {
if (!checkParam(key, form)) {
flash("error", "Could not add alert condition: Missing parameters.");
return redirect(routes.AlertsController.index(streamId));
}
}
try {
Stream stream = streamService.get(streamId);
CreateAlertConditionRequest request = new CreateAlertConditionRequest();
for (Map.Entry<String, String> entry : form.entrySet()) {
try {
request.parameters.put(entry.getKey(), Integer.parseInt(entry.getValue()));
} catch (Exception e1) {
try {
request.parameters.put(entry.getKey(), Double.parseDouble(entry.getValue()));
} catch (Exception e2) {
request.parameters.put(entry.getKey(), entry.getValue());
}
}
}
alertConditionService.update(stream, conditionId, request);
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not create alert condition. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
flash("success", "Updated alert condition.");
return redirect(routes.AlertsController.index(streamId));
}
public Result removeCondition(String streamId, String conditionId) {
try {
Stream stream = streamService.get(streamId);
alertConditionService.delete(stream, conditionId);
flash("success", "Deleted alert condition.");
return redirect(routes.AlertsController.index(streamId));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not fetch stream. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public Result addReceiverUser(String streamId) {
Map<String,String> form = flattenFormUrlEncoded(request().body().asFormUrlEncoded());
String username = form.get("username");
if (username == null || username.trim().isEmpty()) {
flash("error", "No username provided.");
return redirect(routes.AlertsController.index(streamId));
}
try {
User receiver = userService.load(username);
if (receiver == null) {
flash("error", "Could not add alert receiver: Unknown user.");
return redirect(routes.AlertsController.index(streamId));
}
Stream stream = streamService.get(streamId);
stream.addAlertReceiver(receiver);
flash("success", "Added alert receiver.");
return redirect(routes.AlertsController.index(streamId));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not add alert receiver: We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
public Result removeReceiverUser(String streamId, String username) {
if (username == null || username.trim().isEmpty()) {
flash("error", "No username provided.");
return redirect(routes.AlertsController.index(streamId));
}
try {
User receiver = userService.load(username);
if (receiver == null) {
flash("error", "Could not remove alert receiver: Unknown user.");
return redirect(routes.AlertsController.index(streamId));
}
Stream stream = streamService.get(streamId);
stream.removeAlertReceiver(receiver);
flash("success", "Removed alert receiver.");
return redirect(routes.AlertsController.index(streamId));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not add alert receiver: We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public Result addReceiverEmail(String streamId) {
Map<String,String> form = flattenFormUrlEncoded(request().body().asFormUrlEncoded());
String email = form.get("email");
try {
Stream stream = streamService.get(streamId);
stream.addAlertReceiver(email);
flash("success", "Added alert receiver.");
return redirect(routes.AlertsController.index(streamId));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not add alert receiver: We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
public Result removeReceiverEmail(String streamId, String email) {
try {
Stream stream = streamService.get(streamId);
stream.removeAlertReceiver(email);
flash("success", "Removed alert receiver.");
return redirect(routes.AlertsController.index(streamId));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
String message = "Could not add alert receiver: We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));
}
}
public Result sendDummyAlert(String streamId) {
try {
streamService.sendDummyAlert(streamId);
flash("success", "Sent dummy alert to all subscribers.");
return redirect(routes.AlertsController.index(streamId));
} catch (IOException e) {
return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
} catch (APIException e) {
flash("error", "Unable to send dummy alert, check server log for details: " + e.getMessage());
return redirect(routes.AlertsController.index(streamId));
}
}
private boolean checkParam(String key, Map<String,String> form) {
return form.containsKey(key) && !form.get(key).isEmpty();
}
}