-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
207 lines (171 loc) · 5.21 KB
/
check.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* nagopher - Library for writing Nagios plugins in Go
* Copyright (C) 2018-2019 Pascal Mathis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nagopher
import (
"fmt"
"reflect"
"sort"
)
// Check collects metrics (results) and performance data, which are being associated to one or more given contexts.
// Metrics are being collected by executing one or more resources using Resource.Probe(). Additional metadata without
// a specific type can be added using Check.SetMeta() / Check.GetMeta(). Collected results can be queried by using
// Check.State(), Check.Summary() or Check.VerboseSummary(), which use the configured summarizer instance.
type Check interface {
Run(warnings WarningCollection)
SetMeta(key string, value interface{})
GetMeta(key string, defaultValue interface{}) interface{}
AttachResources(resources ...Resource)
AttachContexts(contexts ...Context)
Name() string
PerfData() []PerfData
Contexts() []Context
Resources() []Resource
Results() ResultCollection
State() State
Summary() string
VerboseSummary() []string
}
type baseCheck struct {
name string
meta map[string]interface{}
contexts map[string]Context
resources map[*Resource]struct{}
performances []PerfData
results ResultCollection
summarizer Summarizer
}
// NewCheck instantiates a new Check object with the given name and summarizer
func NewCheck(name string, summarizer Summarizer) Check {
check := &baseCheck{
name: name,
summarizer: summarizer,
meta: make(map[string]interface{}),
contexts: make(map[string]Context),
resources: make(map[*Resource]struct{}),
results: NewResultCollection(),
}
return check
}
func (c *baseCheck) Run(warnings WarningCollection) {
c.results = NewResultCollection()
c.performances = []PerfData{}
for resource := range c.resources {
err := c.evaluateResource(warnings, *resource)
if err != nil {
c.results.Add(NewResult(
ResultState(StateUnknown()),
ResultResource(*resource), ResultHint(err.Error()),
))
}
}
sort.SliceStable(c.performances, func(a int, b int) bool {
return c.performances[a].Metric().Name() < c.performances[b].Metric().Name()
})
}
func (c *baseCheck) evaluateResource(warnings WarningCollection, resource Resource) error {
if err := resource.Setup(warnings); err != nil {
return err
}
metrics, err := resource.Probe(warnings)
if err != nil {
return err
}
if len(metrics) == 0 {
return fmt.Errorf("nagopher: resource [%s] did not return any metrics", reflect.TypeOf(resource))
}
for _, metric := range metrics {
context, ok := c.contexts[metric.ContextName()]
if !ok {
return fmt.Errorf("nagopher: missing context with name [%s]", metric.ContextName())
}
result := context.Evaluate(metric, resource)
c.results.Add(result)
perfData, err := context.Performance(metric, resource)
if err != nil {
return fmt.Errorf("nagopher: collecting performance data failed with [%s]", err.Error())
}
if performance, err := perfData.Get(); err == nil {
c.performances = append(c.performances, performance)
}
}
if err := resource.Teardown(warnings); err != nil {
return err
}
return nil
}
func (c *baseCheck) SetMeta(key string, value interface{}) {
c.meta[key] = value
}
func (c baseCheck) GetMeta(key string, defaultValue interface{}) interface{} {
if value, ok := c.meta[key]; ok {
return value
}
return defaultValue
}
func (c *baseCheck) AttachResources(resources ...Resource) {
for _, resource := range resources {
c.resources[&resource] = struct{}{}
}
}
func (c *baseCheck) AttachContexts(contexts ...Context) {
for _, context := range contexts {
c.contexts[context.Name()] = context
}
}
func (c baseCheck) Results() ResultCollection {
return c.results
}
func (c baseCheck) State() State {
state, err := c.results.MostSignificantState().Get()
if err == nil {
return state
}
return StateUnknown()
}
func (c baseCheck) Summary() string {
if c.results.Count() == 0 {
return c.summarizer.Empty()
}
if c.State() == StateOk() {
return c.summarizer.Ok(&c)
}
return c.summarizer.Problem(&c)
}
func (c baseCheck) VerboseSummary() []string {
return c.summarizer.Verbose(&c)
}
func (c baseCheck) Name() string {
return c.name
}
func (c baseCheck) PerfData() []PerfData {
return c.performances
}
func (c baseCheck) Contexts() []Context {
contexts := make([]Context, 0, len(c.contexts))
for _, context := range c.contexts {
contexts = append(contexts, context)
}
return contexts
}
func (c baseCheck) Resources() []Resource {
resources := make([]Resource, 0, len(c.resources))
for resource := range c.resources {
resources = append(resources, *resource)
}
return resources
}