forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.go
60 lines (51 loc) · 1.62 KB
/
data.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
package healthcheck
import (
"strings"
dc "github.com/fsouza/go-dockerclient"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/docker"
)
func eventsMapping(containers []dc.APIContainers, m *MetricSet) []common.MapStr {
var events []common.MapStr
for _, container := range containers {
event := eventMapping(&container, m)
if event != nil {
events = append(events, event)
}
}
return events
}
func eventMapping(cont *dc.APIContainers, m *MetricSet) common.MapStr {
if !hasHealthCheck(cont.Status) {
return nil
}
container, err := m.dockerClient.InspectContainer(cont.ID)
if err != nil {
logp.Err("Error inpsecting container %v: %v", cont.ID, err)
return nil
}
lastEvent := len(container.State.Health.Log) - 1
// Checks if a healthcheck already happened
if lastEvent < 0 {
return nil
}
return common.MapStr{
mb.ModuleData: common.MapStr{
"container": docker.NewContainer(cont).ToMapStr(),
},
"status": container.State.Health.Status,
"failingstreak": container.State.Health.FailingStreak,
"event": common.MapStr{
"start_date": common.Time(container.State.Health.Log[lastEvent].Start),
"end_date": common.Time(container.State.Health.Log[lastEvent].End),
"exit_code": container.State.Health.Log[lastEvent].ExitCode,
"output": container.State.Health.Log[lastEvent].Output,
},
}
}
// hasHealthCheck detects if healthcheck is available for container
func hasHealthCheck(status string) bool {
return strings.Contains(status, "(") && strings.Contains(status, ")")
}