forked from supherman/riemann-docker-health
/
riemann-docker-health.go
executable file
·115 lines (97 loc) · 2.45 KB
/
riemann-docker-health.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"fmt"
"github.com/amir/raidman"
"github.com/supherman/riemann-docker-health/docker"
"github.com/supherman/riemann-docker-health/docker/cpu"
"github.com/supherman/riemann-docker-health/docker/mem"
"log"
"os"
"time"
)
var (
cpuWarning = flag.Int("cpu_warning", 50, "CPU warning threshold")
cpuCritical = flag.Int("cpu_critical", 90, "CPU critical threshold")
memWarning = flag.Int("memory_warning", 50, "Memory warning threshold")
memCritical = flag.Int("memory_critical", 90, "Memory critical threshold")
host = flag.String("host", "127.0.0.1", "Riemann host (default: 127.0.0.1)")
port = flag.String("port", "5555", "Riemann port (default: 5555")
)
type Threshold struct {
warning int
critical int
}
func Hostname() string {
hostname, err := os.Hostname()
if err != nil {
log.Println(err)
}
return hostname
}
func Alert(event *raidman.Event) {
c, err := raidman.Dial("tcp", *host+":"+*port)
if err != nil {
panic(err)
}
err = c.Send(event)
if err != nil {
panic(err)
}
c.Close()
}
func ComputeState(metric int, threshold *Threshold) string {
switch {
case metric >= threshold.critical:
return "critical"
case metric >= threshold.warning && metric < threshold.critical:
return "warning"
}
return "ok"
}
func AlertCPU(container string, threshold *Threshold) {
containerCpu, _ := cpu.GetUsage(container)
metric := int(containerCpu)
state := ComputeState(metric, threshold)
var cpuEvent = &raidman.Event{
State: state,
Service: "cpu",
Metric: metric,
Ttl: 10,
Host: fmt.Sprintf("%s %s", Hostname(), container),
}
Alert(cpuEvent)
}
func AlertMemory(container string, threshold *Threshold) {
containerMem, _ := mem.GetPercentage(container)
metric := int(containerMem)
state := ComputeState(metric, threshold)
var memEvent = &raidman.Event{
State: state,
Service: "memory",
Metric: metric,
Ttl: 10,
Host: fmt.Sprintf("%s %s", Hostname(), container),
}
Alert(memEvent)
}
func main() {
fmt.Println("Initializing monitoring agent...")
flag.Parse()
cpuThreshold := Threshold{*cpuWarning, *cpuCritical}
memThreshold := Threshold{*memWarning, *memCritical}
go cpu.Monitor()
tick := time.NewTicker(1 * time.Second)
for {
<-tick.C
containers, err := docker.ListContainers()
if err != nil {
log.Println(err)
continue
}
for _, container := range containers {
AlertCPU(container, &cpuThreshold)
AlertMemory(container, &memThreshold)
}
}
}