Browse Source

HTTP alerting

Brendan Abolivier 7 years ago
parent
commit
cc9862f3d5
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 35 additions and 1 deletions
  1. 35
    1
      src/metrics-alerting/alert/http.go

+ 35
- 1
src/metrics-alerting/alert/http.go View File

1
 package alert
1
 package alert
2
 
2
 
3
 import (
3
 import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"net/http"
7
+
4
 	"metrics-alerting/config"
8
 	"metrics-alerting/config"
5
 )
9
 )
6
 
10
 
11
+type alertBody struct {
12
+	Key   string `json:"scriptKey"`
13
+	Value string `json:"value"`
14
+}
15
+
7
 func alertHttp(script config.Script, result interface{}) error {
16
 func alertHttp(script config.Script, result interface{}) error {
8
-	return nil
17
+	alert := alertBody{
18
+		Key:   script.Key,
19
+		Value: result,
20
+	}
21
+
22
+	body, err := json.Marshal(alert)
23
+	if err != nil {
24
+		return err
25
+	}
26
+
27
+	req, err := http.NewRequest("POST", script.Target, bytes.NewBuffer(body))
28
+	if err != nil {
29
+		return err
30
+	}
31
+	req.Header.Set("Content-Type", "application/json")
32
+
33
+	client := http.Client{}
34
+	resp, err := client.Do(req)
35
+
36
+	if resp.StatusCode != http.StatusOK {
37
+		return fmt.Errorf(
38
+			"target %s returned non-200 status code %d", script.Target, resp.StatusCode
39
+		)
40
+	}
41
+
42
+	return err
9
 }
43
 }