This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkubecat.go
97 lines (85 loc) · 2.28 KB
/
kubecat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"github.com/getsentry/raven-go"
"github.com/stevelacy/kubecat/modules"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
"time"
)
// Config for kubecat
type Config struct {
Reporters []modules.Reporter
}
var config Config
func main() {
raw, err := ioutil.ReadFile("./config.yaml")
if err != nil {
panic("unable to read ./config.yaml")
}
parsed := string(raw)
client, err := raven.New(os.Getenv("SENTRY_DSN"))
if err != nil {
panic("unable to connect to sentry")
}
client.SetEnvironment(os.Getenv("ENV"))
err = yaml.Unmarshal([]byte(parsed), &config)
if err != nil {
panic("unable to parse config yaml")
}
for _, reporter := range config.Reporters {
fmt.Printf("Loading reporter: %s with module: %s\n", reporter.Name, reporter.Module)
if strings.Contains(reporter.Options.URL, "env:") {
env := strings.Replace(reporter.Options.URL, "env:", "", 1)
reporter.Options.URL = strings.Replace(os.Getenv(env), "\n", "", -1)
}
runModule(reporter)
startChannel(reporter, client)
}
go forever()
select {}
}
func startChannel(reporter modules.Reporter, client *raven.Client) {
ticker := time.NewTicker(time.Duration(reporter.Interval) * time.Second)
go func() {
for _ = range ticker.C {
status := runModule(reporter)
if status.Error != "" {
notifySentry(client, reporter, status)
}
}
}()
}
func runModule(reporter modules.Reporter) modules.Status {
if reporter.Module == "http" {
status, _ := modules.HTTP(reporter)
return status
}
if reporter.Module == "Tile38" {
status, _ := modules.Tile38(reporter)
return status
}
if reporter.Module == "Redis" {
status, _ := modules.Redis(reporter)
return status
}
fmt.Printf("No module found for reporter '%s' -- %s\n", reporter.Name, reporter.Module)
return modules.Status{}
}
func notifySentry(client *raven.Client, reporter modules.Reporter, status modules.Status) {
fmt.Printf("reporting: %s, error: %s \n", reporter.Name, status.Error)
message := fmt.Sprintf("%s reporter: %s", status.Message, reporter.Name)
title := fmt.Sprintf("Error: %s is unreachable \n %s", reporter.Name, status.Error)
messages := map[string]string{
"message": message,
"url": reporter.Options.URL,
}
client.CaptureMessage(title, messages)
}
func forever() {
for {
time.Sleep(time.Second)
}
}