Brendan Abolivier 7 anni fa
parent
commit
cc9862f3d5
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 ha cambiato i file con 35 aggiunte e 1 eliminazioni
  1. 35
    1
      src/metrics-alerting/alert/http.go

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

@@ -1,9 +1,43 @@
1 1
 package alert
2 2
 
3 3
 import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"net/http"
7
+
4 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 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
 }