瀏覽代碼

Checks + structure for alerting

Brendan Abolivier 7 年之前
父節點
當前提交
621540254b
簽署人: Brendan Abolivier <contact@brendanabolivier.com> GPG 金鑰 ID: 8EF1500759F70623

+ 46
- 0
src/metrics-alerting/alert/alert.go 查看文件

@@ -0,0 +1,46 @@
1
+package alert
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"metrics-alerting/config"
7
+	"metrics-alerting/warp10"
8
+)
9
+
10
+func ProcessNumber(client warp10.Warp10Client, script config.Script) error {
11
+	value, err := client.ReadNumber(script.Script)
12
+	if err != nil {
13
+		return err
14
+	}
15
+
16
+	if value < script.Threshold {
17
+		// Nothing to alert about
18
+		return nil
19
+	}
20
+
21
+	return alert(script, value)
22
+}
23
+
24
+func ProcessBool(client warp10.Warp10Client, script config.Script) error {
25
+	value, err := client.ReadBool(script.Script)
26
+	if err != nil {
27
+		return err
28
+	}
29
+
30
+	if value {
31
+		return nil
32
+	}
33
+
34
+	return alert(script, value)
35
+}
36
+
37
+func alert(script config.Script, result interface{}) error {
38
+	switch script.Action {
39
+	case "http":
40
+		return alertHttp(script, result)
41
+	case "email":
42
+		return alertEmail(script, result)
43
+	default:
44
+		return fmt.Errorf("invalid action type: %s", script.Action)
45
+	}
46
+}

+ 9
- 0
src/metrics-alerting/alert/email.go 查看文件

@@ -0,0 +1,9 @@
1
+package alert
2
+
3
+import (
4
+	"metrics-alerting/config"
5
+)
6
+
7
+func alertEmail(script config.Script, result interface{}) error {
8
+	return nil
9
+}

+ 9
- 0
src/metrics-alerting/alert/http.go 查看文件

@@ -0,0 +1,9 @@
1
+package alert
2
+
3
+import (
4
+	"metrics-alerting/config"
5
+)
6
+
7
+func alertHttp(script config.Script, result interface{}) error {
8
+	return nil
9
+}

+ 1
- 1
src/metrics-alerting/config/config.go 查看文件

@@ -14,7 +14,7 @@ type Script struct {
14 14
 	// The type of the value returned by the script
15 15
 	Type string `yaml:"type"`
16 16
 	// Value above which an action is required
17
-	Threshold string `yaml:"threshold,omitempty"`
17
+	Threshold float64 `yaml:"threshold,omitempty"`
18 18
 	// The action to take (either "http" or "email")
19 19
 	Action string `yaml:"action"`
20 20
 	// The action's target

+ 17
- 0
src/metrics-alerting/main.go 查看文件

@@ -3,7 +3,9 @@ package main
3 3
 import (
4 4
 	"flag"
5 5
 	"fmt"
6
+	"log"
6 7
 
8
+	"metrics-alerting/alert"
7 9
 	"metrics-alerting/config"
8 10
 	"metrics-alerting/warp10"
9 11
 )
@@ -22,5 +24,20 @@ func main() {
22 24
 	}
23 25
 
24 26
 	for _, script := range cfg.Scripts {
27
+		var err error
28
+		switch script.Type {
29
+		case "number":
30
+			err = alert.ProcessNumber(client, script)
31
+			break
32
+		case "bool":
33
+			err = alert.ProcessBool(client, script)
34
+			break
35
+		default:
36
+			err = fmt.Errorf("invalid return type: %s", script.Type)
37
+		}
38
+
39
+		if err != nil {
40
+			log.Fatal(err)
41
+		}
25 42
 	}
26 43
 }