Kaynağa Gözat

Checks + structure for alerting

Brendan Abolivier 7 yıl önce
ebeveyn
işleme
621540254b
İmzalayan: Brendan Abolivier <contact@brendanabolivier.com> GPC anahtar kimliği: 8EF1500759F70623

+ 46
- 0
src/metrics-alerting/alert/alert.go Dosyayı Görüntüle

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 Dosyayı Görüntüle

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 Dosyayı Görüntüle

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 Dosyayı Görüntüle

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

+ 17
- 0
src/metrics-alerting/main.go Dosyayı Görüntüle

3
 import (
3
 import (
4
 	"flag"
4
 	"flag"
5
 	"fmt"
5
 	"fmt"
6
+	"log"
6
 
7
 
8
+	"metrics-alerting/alert"
7
 	"metrics-alerting/config"
9
 	"metrics-alerting/config"
8
 	"metrics-alerting/warp10"
10
 	"metrics-alerting/warp10"
9
 )
11
 )
22
 	}
24
 	}
23
 
25
 
24
 	for _, script := range cfg.Scripts {
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
 }