master
go 68 lines 1.77 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package nagios
4
5 import (
6 "strings"
7
8 "github.com/netdata/netdata/go/plugins/pkg/metrix"
9 )
10
11 const (
12 metricJobStateOK = "ok"
13 metricJobStateWarning = "warning"
14 metricJobStateCritical = "critical"
15 metricJobStateUnknown = "unknown"
16 metricJobStateTimeout = "timeout"
17 metricJobStatePaused = "paused"
18 metricStateRetry = "retry"
19 )
20
21 var jobExecutionStateNames = []string{
22 metricJobStateOK,
23 metricJobStateWarning,
24 metricJobStateCritical,
25 metricJobStateUnknown,
26 metricJobStateTimeout,
27 metricJobStatePaused,
28 metricStateRetry,
29 }
30
31 // projectJobExecutionState maps runtime state into the public metric surface.
32 func projectJobExecutionState(state string, retrying bool) metrix.StateSetPoint {
33 normalized := normalizeJobStateForMetric(state)
34 actives := []string{normalized}
35 if retrying && normalized != metricJobStatePaused {
36 actives = append(actives, metricStateRetry)
37 }
38 return stateSetPoint(jobExecutionStateNames, actives...)
39 }
40
41 // projectPerfThresholdAlertState maps routed threshold state into the alertable duplicate.
42 func projectPerfThresholdAlertState(state string, retrying bool) metrix.StateSetPoint {
43 if state == "" {
44 return perfThresholdAlertStatePoint()
45 }
46 actives := []string{state}
47 if retrying {
48 actives = append(actives, perfThresholdStateRetry)
49 }
50 return perfThresholdAlertStatePoint(actives...)
51 }
52
53 func normalizeJobStateForMetric(state string) string {
54 switch strings.ToUpper(strings.TrimSpace(state)) {
55 case nagiosStateOK:
56 return metricJobStateOK
57 case nagiosStateWarning:
58 return metricJobStateWarning
59 case nagiosStateCritical:
60 return metricJobStateCritical
61 case jobStateTimeout:
62 return metricJobStateTimeout
63 case jobStatePaused:
64 return metricJobStatePaused
65 default:
66 return metricJobStateUnknown
67 }
68 }