master
go 91 lines 2.23 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package monit
4
5 import (
6 "fmt"
7 "strings"
8
9 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
10 )
11
12 const (
13 prioServiceCheckStatus = collectorapi.Priority + iota
14 prioUptime
15 )
16
17 var baseCharts = collectorapi.Charts{
18 uptimeChart.Copy(),
19 }
20
21 var (
22 uptimeChart = collectorapi.Chart{
23 ID: "uptime",
24 Title: "Uptime",
25 Units: "seconds",
26 Fam: "uptime",
27 Ctx: "monit.uptime",
28 Priority: prioUptime,
29 Dims: collectorapi.Dims{
30 {ID: "uptime"},
31 },
32 }
33 )
34
35 var serviceCheckChartsTmpl = collectorapi.Charts{
36 serviceCheckStatusChartTmpl.Copy(),
37 }
38
39 var (
40 serviceCheckStatusChartTmpl = collectorapi.Chart{
41 ID: "service_check_type_%s_name_%s_status",
42 Title: "Service Check Status",
43 Units: "status",
44 Fam: "service status",
45 Ctx: "monit.service_check_status",
46 Priority: prioServiceCheckStatus,
47 Dims: collectorapi.Dims{
48 {ID: "service_check_type_%s_name_%s_status_ok", Name: "ok"},
49 {ID: "service_check_type_%s_name_%s_status_error", Name: "error"},
50 {ID: "service_check_type_%s_name_%s_status_initializing", Name: "initializing"},
51 {ID: "service_check_type_%s_name_%s_status_not_monitored", Name: "not_monitored"},
52 },
53 }
54 )
55
56 func (c *Collector) addServiceCheckCharts(svc statusServiceCheck, srv *statusServer) {
57 charts := serviceCheckChartsTmpl.Copy()
58
59 for _, chart := range *charts {
60 chart.ID = cleanChartId(fmt.Sprintf(chart.ID, svc.svcType(), svc.Name))
61 chart.Labels = []collectorapi.Label{
62 {Key: "server_hostname", Value: srv.LocalHostname},
63 {Key: "service_check_name", Value: svc.Name},
64 {Key: "service_check_type", Value: svc.svcType()},
65 }
66 for _, dim := range chart.Dims {
67 dim.ID = fmt.Sprintf(dim.ID, svc.svcType(), svc.Name)
68 }
69 }
70
71 if err := c.Charts().Add(*charts...); err != nil {
72 c.Warning(err)
73 }
74 }
75
76 func (c *Collector) removeServiceCharts(svc statusServiceCheck) {
77 px := fmt.Sprintf("service_check_type_%s_name_%s_", svc.svcType(), svc.Name)
78 px = cleanChartId(px)
79
80 for _, chart := range *c.Charts() {
81 if strings.HasPrefix(chart.ID, px) {
82 chart.MarkRemove()
83 chart.MarkNotCreated()
84 }
85 }
86 }
87
88 func cleanChartId(s string) string {
89 r := strings.NewReplacer(" ", "_", ".", "_", ",", "_")
90 return r.Replace(s)
91 }