11package de .gdata .mobilelab .alertmanagercallback ;
22
3- import com .fasterxml .jackson .databind .ObjectMapper ;
4-
53import org .graylog2 .plugin .alarms .AlertCondition ;
64import org .graylog2 .plugin .alarms .callbacks .AlarmCallback ;
75import org .graylog2 .plugin .alarms .callbacks .AlarmCallbackConfigurationException ;
1412import org .graylog2 .plugin .configuration .fields .TextField ;
1513import org .graylog2 .plugin .streams .Stream ;
1614
17- import java .io .BufferedReader ;
18- import java .io .IOException ;
19- import java .io .InputStreamReader ;
20- import java .io .OutputStream ;
21- import java .net .HttpURLConnection ;
2215import java .net .URI ;
2316import java .net .URISyntaxException ;
24- import java .net .URL ;
25- import java .nio .charset .StandardCharsets ;
2617import java .util .Map ;
2718
2819public class AlertManagerAlarmCallback implements AlarmCallback {
2920
3021 private Configuration configuration ;
31- private ObjectMapper objectMapper ;
22+ private AlertManagerPostRequestSender alertManagerPostRequestSender ;
3223
3324 @ Override
3425 public void initialize (Configuration config ) throws AlarmCallbackConfigurationException {
3526 configuration = config ;
36- if (objectMapper == null ) {
37- objectMapper = new ObjectMapper ();
38- }
27+ alertManagerPostRequestSender = new AlertManagerPostRequestSender (config .getString ("alertmanager_api_url" ));
3928 }
4029
4130 @ Override
@@ -46,60 +35,7 @@ public void call(Stream stream, AlertCondition.CheckResult result) throws AlarmC
4635 .withStream (stream )
4736 .build ();
4837
49- Object [] wrapper = new Object [1 ];
50- wrapper [0 ] = alertManagerPayload ;
51-
52- final String alertManagerApiUrl = configuration .getString ("alertmanager_api_url" );
53- try {
54- String responseAsString = postForResponseAsString (alertManagerApiUrl , objectMapper .writeValueAsString (wrapper ));
55- AlertManagerResponse alertManagerResponse = objectMapper .readValue (responseAsString , AlertManagerResponse .class );
56- if (!AlertManagerResponse .STATUS_SUCCESS .equals (alertManagerResponse .getStatus ())) {
57- throw new AlarmCallbackException ("Response from AlertManager for Alert failed. Response-Status: '"
58- + alertManagerResponse .getStatus () + "'." );
59- }
60- } catch (Exception e ) {
61- throw new AlarmCallbackException ("Could not send Alert to AlertManager (" + alertManagerApiUrl + ")." , e );
62- }
63- }
64-
65- /**
66- * Sends the POST-request to the given targetUrl with given payload as body.
67- *
68- * @param targetUrl the target url of POST-request
69- * @param payload the payload (JSON body)
70- * @return the response
71- * @throws IOException if request fails
72- */
73- private String postForResponseAsString (String targetUrl , String payload ) throws IOException {
74- URL apiUrl = new URL (targetUrl );
75- HttpURLConnection connection = (HttpURLConnection ) apiUrl .openConnection ();
76- connection .setDoInput (true );
77- connection .setDoOutput (true );
78-
79- connection .setRequestProperty ("Content-Type" , "application/json;" );
80- connection .setRequestProperty ("Accept" , "application/json,text/plain" );
81- connection .setRequestProperty ("Method" , "POST" );
82- OutputStream os = connection .getOutputStream ();
83- os .write (payload .getBytes (StandardCharsets .UTF_8 ));
84- os .close ();
85-
86- StringBuilder sb = new StringBuilder ();
87- int HttpResult = connection .getResponseCode ();
88- if (HttpResult == HttpURLConnection .HTTP_OK ) {
89- BufferedReader br = new BufferedReader (new InputStreamReader (connection .getInputStream (), StandardCharsets .UTF_8 ));
90-
91- String line ;
92- while ((line = br .readLine ()) != null ) {
93- sb .append (line ).append ("\n " );
94- }
95- br .close ();
96- connection .disconnect ();
97- return sb .toString ();
98- } else {
99- throw new IOException ("Could not get a valid response for POST-request to '" + targetUrl
100- + "'. ResponseCode: " + connection .getResponseCode ()
101- + " ResponseMessage: '" + connection .getResponseMessage () + "'." );
102- }
38+ alertManagerPostRequestSender .sendPostRequestToAlertManager (alertManagerPayload );
10339 }
10440
10541 @ Override
0 commit comments