| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux |
| 4 | |
| 5 | package systemdunits |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 13 | |
| 14 | "golang.org/x/text/cases" |
| 15 | "golang.org/x/text/language" |
| 16 | ) |
| 17 | |
| 18 | const ( |
| 19 | prioUnitState = collectorapi.Priority + iota |
| 20 | prioUnitFileState |
| 21 | ) |
| 22 | |
| 23 | func (c *Collector) addUnitCharts(name, typ string) { |
| 24 | chart := collectorapi.Chart{ |
| 25 | ID: "unit_%s_%s_state", |
| 26 | Title: "%s Unit State", |
| 27 | Units: "state", |
| 28 | Fam: "%s units", |
| 29 | Ctx: "systemd.%s_unit_state", |
| 30 | Priority: prioUnitState, |
| 31 | Labels: []collectorapi.Label{ |
| 32 | {Key: "unit_name", Value: name}, |
| 33 | }, |
| 34 | Dims: collectorapi.Dims{ |
| 35 | {ID: "unit_%s_%s_state_%s", Name: unitStateActive}, |
| 36 | {ID: "unit_%s_%s_state_%s", Name: unitStateInactive}, |
| 37 | {ID: "unit_%s_%s_state_%s", Name: unitStateActivating}, |
| 38 | {ID: "unit_%s_%s_state_%s", Name: unitStateDeactivating}, |
| 39 | {ID: "unit_%s_%s_state_%s", Name: unitStateFailed}, |
| 40 | }, |
| 41 | } |
| 42 | |
| 43 | chart.ID = fmt.Sprintf(chart.ID, name, typ) |
| 44 | chart.Title = fmt.Sprintf(chart.Title, cases.Title(language.English, cases.Compact).String(typ)) |
| 45 | chart.Fam = fmt.Sprintf(chart.Fam, typ) |
| 46 | chart.Ctx = fmt.Sprintf(chart.Ctx, typ) |
| 47 | |
| 48 | for _, d := range chart.Dims { |
| 49 | d.ID = fmt.Sprintf(d.ID, name, typ, d.Name) |
| 50 | } |
| 51 | |
| 52 | if err := c.Charts().Add(&chart); err != nil { |
| 53 | c.Warning(err) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func (c *Collector) removeUnitCharts(name, typ string) { |
| 58 | px := fmt.Sprintf("unit_%s_%s_", name, typ) |
| 59 | c.removeCharts(px) |
| 60 | } |
| 61 | |
| 62 | func (c *Collector) addUnitFileCharts(unitPath string) { |
| 63 | _, unitName := filepath.Split(unitPath) |
| 64 | unitType := strings.TrimPrefix(filepath.Ext(unitPath), ".") |
| 65 | |
| 66 | chart := collectorapi.Chart{ |
| 67 | ID: "unit_file_%s_state", |
| 68 | Title: "Unit File State", |
| 69 | Units: "state", |
| 70 | Fam: "unit files", |
| 71 | Ctx: "systemd.unit_file_state", |
| 72 | Type: collectorapi.Line, |
| 73 | Priority: prioUnitFileState, |
| 74 | Labels: []collectorapi.Label{ |
| 75 | {Key: "unit_file_name", Value: unitName}, |
| 76 | {Key: "unit_file_type", Value: unitType}, |
| 77 | }, |
| 78 | Dims: collectorapi.Dims{ |
| 79 | {ID: "unit_file_%s_state_enabled", Name: "enabled"}, |
| 80 | {ID: "unit_file_%s_state_enabled-runtime", Name: "enabled-runtime"}, |
| 81 | {ID: "unit_file_%s_state_linked", Name: "linked"}, |
| 82 | {ID: "unit_file_%s_state_linked-runtime", Name: "linked-runtime"}, |
| 83 | {ID: "unit_file_%s_state_alias", Name: "alias"}, |
| 84 | {ID: "unit_file_%s_state_masked", Name: "masked"}, |
| 85 | {ID: "unit_file_%s_state_masked-runtime", Name: "masked-runtime"}, |
| 86 | {ID: "unit_file_%s_state_static", Name: "static"}, |
| 87 | {ID: "unit_file_%s_state_disabled", Name: "disabled"}, |
| 88 | {ID: "unit_file_%s_state_indirect", Name: "indirect"}, |
| 89 | {ID: "unit_file_%s_state_generated", Name: "generated"}, |
| 90 | {ID: "unit_file_%s_state_transient", Name: "transient"}, |
| 91 | {ID: "unit_file_%s_state_bad", Name: "bad"}, |
| 92 | }, |
| 93 | } |
| 94 | |
| 95 | chart.ID = fmt.Sprintf(chart.ID, strings.ReplaceAll(unitPath, ".", "_")) |
| 96 | for _, dim := range chart.Dims { |
| 97 | dim.ID = fmt.Sprintf(dim.ID, unitPath) |
| 98 | } |
| 99 | |
| 100 | if err := c.Charts().Add(&chart); err != nil { |
| 101 | c.Warning(err) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func (c *Collector) removeUnitFileCharts(unitPath string) { |
| 106 | px := fmt.Sprintf("unit_file_%s_", strings.ReplaceAll(unitPath, ".", "_")) |
| 107 | c.removeCharts(px) |
| 108 | } |
| 109 | |
| 110 | func (c *Collector) removeCharts(prefix string) { |
| 111 | for _, chart := range *c.Charts() { |
| 112 | if strings.HasPrefix(chart.ID, prefix) { |
| 113 | chart.MarkRemove() |
| 114 | chart.MarkNotCreated() |
| 115 | } |
| 116 | } |
| 117 | } |