| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package icecast |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | prioSourceListeners = collectorapi.Priority + iota |
| 14 | ) |
| 15 | |
| 16 | var sourceChartsTmpl = collectorapi.Charts{ |
| 17 | sourceListenersChartTmpl.Copy(), |
| 18 | } |
| 19 | |
| 20 | var ( |
| 21 | sourceListenersChartTmpl = collectorapi.Chart{ |
| 22 | ID: "icecast_%s_listeners", |
| 23 | Title: "Icecast Listeners", |
| 24 | Units: "listeners", |
| 25 | Fam: "listeners", |
| 26 | Ctx: "icecast.listeners", |
| 27 | Type: collectorapi.Line, |
| 28 | Priority: prioSourceListeners, |
| 29 | Dims: collectorapi.Dims{ |
| 30 | {ID: "source_%s_listeners", Name: "listeners"}, |
| 31 | }, |
| 32 | } |
| 33 | ) |
| 34 | |
| 35 | func (c *Collector) addSourceCharts(name string) { |
| 36 | chart := sourceListenersChartTmpl.Copy() |
| 37 | |
| 38 | chart.ID = fmt.Sprintf(chart.ID, cleanSource(name)) |
| 39 | chart.Labels = []collectorapi.Label{ |
| 40 | {Key: "source", Value: name}, |
| 41 | } |
| 42 | for _, dim := range chart.Dims { |
| 43 | dim.ID = fmt.Sprintf(dim.ID, name) |
| 44 | } |
| 45 | |
| 46 | if err := c.Charts().Add(chart); err != nil { |
| 47 | c.Warning(err) |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | func (c *Collector) removeSourceCharts(name string) { |
| 53 | px := fmt.Sprintf("icecast_%s_", cleanSource(name)) |
| 54 | for _, chart := range *c.Charts() { |
| 55 | if strings.HasPrefix(chart.ID, px) { |
| 56 | chart.MarkRemove() |
| 57 | chart.MarkNotCreated() |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func cleanSource(name string) string { |
| 63 | r := strings.NewReplacer(" ", "_", ".", "_", ",", "_") |
| 64 | return r.Replace(name) |
| 65 | } |