summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/systemdunits/charts.go
blob: 18d8838fbc6ad097c45b3c88cbe6e7d23da167e3 (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
// SPDX-License-Identifier: GPL-3.0-or-later

//go:build linux
// +build linux

package systemdunits

import (
	"fmt"
	"path/filepath"
	"strings"

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

	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

const (
	prioUnitState = module.Priority + iota
	prioUnitFileState
)

func (s *SystemdUnits) addUnitCharts(name, typ string) {
	chart := module.Chart{
		ID:       "unit_%s_%s_state",
		Title:    "%s Unit State",
		Units:    "state",
		Fam:      "%s units",
		Ctx:      "systemd.%s_unit_state",
		Priority: prioUnitState,
		Labels: []module.Label{
			{Key: "unit_name", Value: name},
		},
		Dims: module.Dims{
			{ID: "unit_%s_%s_state_%s", Name: unitStateActive},
			{ID: "unit_%s_%s_state_%s", Name: unitStateInactive},
			{ID: "unit_%s_%s_state_%s", Name: unitStateActivating},
			{ID: "unit_%s_%s_state_%s", Name: unitStateDeactivating},
			{ID: "unit_%s_%s_state_%s", Name: unitStateFailed},
		},
	}

	chart.ID = fmt.Sprintf(chart.ID, name, typ)
	chart.Title = fmt.Sprintf(chart.Title, cases.Title(language.English, cases.Compact).String(typ))
	chart.Fam = fmt.Sprintf(chart.Fam, typ)
	chart.Ctx = fmt.Sprintf(chart.Ctx, typ)

	for _, d := range chart.Dims {
		d.ID = fmt.Sprintf(d.ID, name, typ, d.Name)
	}

	if err := s.Charts().Add(&chart); err != nil {
		s.Warning(err)
	}
}

func (s *SystemdUnits) removeUnitCharts(name, typ string) {
	px := fmt.Sprintf("unit_%s_%s_", name, typ)
	s.removeCharts(px)
}

func (s *SystemdUnits) addUnitFileCharts(unitPath string) {
	_, unitName := filepath.Split(unitPath)
	unitType := strings.TrimPrefix(filepath.Ext(unitPath), ".")

	chart := module.Chart{
		ID:       "unit_file_%s_state",
		Title:    "Unit File State",
		Units:    "state",
		Fam:      "unit files",
		Ctx:      "systemd.unit_file_state",
		Type:     module.Line,
		Priority: prioUnitFileState,
		Labels: []module.Label{
			{Key: "unit_file_name", Value: unitName},
			{Key: "unit_file_type", Value: unitType},
		},
		Dims: module.Dims{
			{ID: "unit_file_%s_state_enabled", Name: "enabled"},
			{ID: "unit_file_%s_state_enabled-runtime", Name: "enabled-runtime"},
			{ID: "unit_file_%s_state_linked", Name: "linked"},
			{ID: "unit_file_%s_state_linked-runtime", Name: "linked-runtime"},
			{ID: "unit_file_%s_state_alias", Name: "alias"},
			{ID: "unit_file_%s_state_masked", Name: "masked"},
			{ID: "unit_file_%s_state_masked-runtime", Name: "masked-runtime"},
			{ID: "unit_file_%s_state_static", Name: "static"},
			{ID: "unit_file_%s_state_disabled", Name: "disabled"},
			{ID: "unit_file_%s_state_indirect", Name: "indirect"},
			{ID: "unit_file_%s_state_generated", Name: "generated"},
			{ID: "unit_file_%s_state_transient", Name: "transient"},
			{ID: "unit_file_%s_state_bad", Name: "bad"},
		},
	}

	chart.ID = fmt.Sprintf(chart.ID, strings.ReplaceAll(unitPath, ".", "_"))
	for _, dim := range chart.Dims {
		dim.ID = fmt.Sprintf(dim.ID, unitPath)
	}

	if err := s.Charts().Add(&chart); err != nil {
		s.Warning(err)
	}
}

func (s *SystemdUnits) removeUnitFileCharts(unitPath string) {
	px := fmt.Sprintf("unit_file_%s_", strings.ReplaceAll(unitPath, ".", "_"))
	s.removeCharts(px)
}

func (s *SystemdUnits) removeCharts(prefix string) {
	for _, chart := range *s.Charts() {
		if strings.HasPrefix(chart.ID, prefix) {
			chart.MarkRemove()
			chart.MarkNotCreated()
		}
	}
}