summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/docker/charts.go
blob: 2dd26c0e34bcde51dafe6c2479191271b84929a5 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: GPL-3.0-or-later

package docker

import (
	"fmt"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
)

const (
	prioContainersState = module.Priority + iota
	prioContainersHealthy

	prioContainerState
	prioContainerHealthStatus
	prioContainerWritableLayerSize

	prioImagesCount
	prioImagesSize
)

var summaryCharts = module.Charts{
	containersStateChart.Copy(),
	containersHealthyChart.Copy(),

	imagesCountChart.Copy(),
	imagesSizeChart.Copy(),
}

var (
	containersStateChart = module.Chart{
		ID:       "containers_state",
		Title:    "Total number of Docker containers in various states",
		Units:    "containers",
		Fam:      "containers",
		Ctx:      "docker.containers_state",
		Priority: prioContainersState,
		Type:     module.Stacked,
		Dims: module.Dims{
			{ID: "containers_state_running", Name: "running"},
			{ID: "containers_state_paused", Name: "paused"},
			{ID: "containers_state_exited", Name: "exited"},
		},
	}
	containersHealthyChart = module.Chart{
		ID:       "healthy_containers",
		Title:    "Total number of Docker containers in various health states",
		Units:    "containers",
		Fam:      "containers",
		Ctx:      "docker.containers_health_status",
		Priority: prioContainersHealthy,
		Dims: module.Dims{
			{ID: "containers_health_status_healthy", Name: "healthy"},
			{ID: "containers_health_status_unhealthy", Name: "unhealthy"},
			{ID: "containers_health_status_not_running_unhealthy", Name: "not_running_unhealthy"},
			{ID: "containers_health_status_starting", Name: "starting"},
			{ID: "containers_health_status_none", Name: "no_healthcheck"},
		},
	}
)

var (
	imagesCountChart = module.Chart{
		ID:       "images_count",
		Title:    "Total number of Docker images in various states",
		Units:    "images",
		Fam:      "images",
		Ctx:      "docker.images",
		Priority: prioImagesCount,
		Type:     module.Stacked,
		Dims: module.Dims{
			{ID: "images_active", Name: "active"},
			{ID: "images_dangling", Name: "dangling"},
		},
	}
	imagesSizeChart = module.Chart{
		ID:       "images_size",
		Title:    "Total size of all Docker images",
		Units:    "bytes",
		Fam:      "images",
		Ctx:      "docker.images_size",
		Priority: prioImagesSize,
		Dims: module.Dims{
			{ID: "images_size", Name: "size"},
		},
	}
)

var (
	containerChartsTmpl = module.Charts{
		containerStateChartTmpl.Copy(),
		containerHealthStatusChartTmpl.Copy(),
		containerWritableLayerSizeChartTmpl.Copy(),
	}

	containerStateChartTmpl = module.Chart{
		ID:       "container_%s_state",
		Title:    "Docker container state",
		Units:    "state",
		Fam:      "containers",
		Ctx:      "docker.container_state",
		Priority: prioContainerState,
		Dims: module.Dims{
			{ID: "container_%s_state_running", Name: "running"},
			{ID: "container_%s_state_paused", Name: "paused"},
			{ID: "container_%s_state_exited", Name: "exited"},
			{ID: "container_%s_state_created", Name: "created"},
			{ID: "container_%s_state_restarting", Name: "restarting"},
			{ID: "container_%s_state_removing", Name: "removing"},
			{ID: "container_%s_state_dead", Name: "dead"},
		},
	}
	containerHealthStatusChartTmpl = module.Chart{
		ID:       "container_%s_health_status",
		Title:    "Docker container health status",
		Units:    "status",
		Fam:      "containers",
		Ctx:      "docker.container_health_status",
		Priority: prioContainerHealthStatus,
		Dims: module.Dims{
			{ID: "container_%s_health_status_healthy", Name: "healthy"},
			{ID: "container_%s_health_status_unhealthy", Name: "unhealthy"},
			{ID: "container_%s_health_status_not_running_unhealthy", Name: "not_running_unhealthy"},
			{ID: "container_%s_health_status_starting", Name: "starting"},
			{ID: "container_%s_health_status_none", Name: "no_healthcheck"},
		},
	}
	containerWritableLayerSizeChartTmpl = module.Chart{
		ID:       "container_%s_writable_layer_size",
		Title:    "Docker container writable layer size",
		Units:    "bytes",
		Fam:      "containers",
		Ctx:      "docker.container_writeable_layer_size",
		Priority: prioContainerWritableLayerSize,
		Dims: module.Dims{
			{ID: "container_%s_size_rw", Name: "writable_layer"},
		},
	}
)

func (d *Docker) addContainerCharts(name, image string) {
	charts := containerChartsTmpl.Copy()
	if !d.CollectContainerSize {
		_ = charts.Remove(containerWritableLayerSizeChartTmpl.ID)
	}

	for _, chart := range *charts {
		chart.ID = fmt.Sprintf(chart.ID, name)
		chart.Labels = []module.Label{
			{Key: "container_name", Value: name},
			{Key: "image", Value: image},
		}
		for _, dim := range chart.Dims {
			dim.ID = fmt.Sprintf(dim.ID, name)
		}
	}

	if err := d.Charts().Add(*charts...); err != nil {
		d.Warning(err)
	}
}

func (d *Docker) removeContainerCharts(name string) {
	px := fmt.Sprintf("container_%s", name)

	for _, chart := range *d.Charts() {
		if strings.HasPrefix(chart.ID, px) {
			chart.MarkRemove()
			chart.MarkNotCreated()
		}
	}
}