-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.go
246 lines (199 loc) · 5.71 KB
/
common.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/yaml.v2"
)
var (
knownMetricsDescriptionsBySection = map[string]map[string]string{
"resources": {
"failed": "How many resources were not successfully fixed",
"scheduled": "How many resources met any scheduling restrictions",
"changed": "How many resources were correctively changed",
"skipped": "How many resources were skipped, because of either tagging or scheduling restrictions",
"total": "The total number of resources being managed",
"failed_to_restart": "How many resources could not be restarted",
"restarted": "How many resources was restarted",
"out_of_sync": "How many resources were out of sync",
"corrective_change": "Number of corrected resources",
},
"events": {
"failure": "How many events were failed",
"success": "How many events were succeed",
"total": "Total number of events for last run",
},
"changes": {
"total": "Total number of changes in the transaction",
},
"time": {},
}
)
// PuppetYamlReportScraper defines a report scraper interface
type PuppetYamlReportScraper interface {
CollectMetrics(ch chan<- prometheus.Metric) error
}
type gaugeValueMap map[string]float64
type gaugeValuesBySectionsMap map[string]gaugeValueMap
type puppetYamlReportUnmarshaler interface {
yaml.Unmarshaler
}
type reportScraper struct {
namespace string
reportFilename string
disabledLockFilename string
info map[string]string
data gaugeValuesBySectionsMap
puppetVersion string
configTimestamp float64
}
func newPuppetReportScraper(namespace, reportFilename, disableLockFilename string) *reportScraper {
r := &reportScraper{
namespace: namespace,
reportFilename: reportFilename,
disabledLockFilename: disableLockFilename,
info: map[string]string{},
}
r.setInfo("report_filename", reportFilename)
if disableLockFilename != "" {
r.setInfo("disabled_lock_filename", disableLockFilename)
}
return r
}
func (r *reportScraper) collectMetrics(ch chan<- prometheus.Metric, u puppetYamlReportUnmarshaler) error {
if r.disabledLockFilename != "" {
agentDisabled, disabledMessage := r.processDisabledLock()
var disabledLockMetricValue float64
if agentDisabled {
disabledLockMetricValue = 1
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(r.namespace, "", "disabled"),
"Puppet state of agent disabled lock",
[]string{"disabled_message"},
nil,
),
prometheus.CounterValue,
disabledLockMetricValue,
[]string{disabledMessage}...,
)
}
metricsBySections, err := r.processReport(u)
if metricsBySections != nil {
for section, metrics := range metricsBySections {
for metricName, metricValue := range metrics {
help, ok := knownMetricsDescriptionsBySection[section][metricName]
if !ok {
help = metricName
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(r.namespace, section, metricName),
help,
nil,
nil,
),
prometheus.GaugeValue,
metricValue,
)
}
}
}
info := r.info
delete(info, "error")
if err != nil {
info["error"] = err.Error()
}
info["puppet_version"] = r.puppetVersion
var infoKeys, infoValues []string
for k, v := range info {
if v != "" {
infoKeys = append(infoKeys, k)
infoValues = append(infoValues, v)
}
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(r.namespace, "", "info"),
"Puppet configuration info in labels",
infoKeys,
nil,
),
prometheus.GaugeValue,
r.configTimestamp,
infoValues...,
)
return err
}
func (r *reportScraper) setMetricValue(section, name string, value float64) {
if _, ok := r.data[section]; !ok {
r.data[section] = make(gaugeValueMap)
}
r.data[section][name] = value
}
func (r *reportScraper) setInfo(key, value string) {
r.info[key] = value
}
func (r *reportScraper) setPuppetVersion(version string) {
r.puppetVersion = version
}
func (r *reportScraper) setConfigTimestamp(value float64) {
r.configTimestamp = value
}
type parseError struct {
filename string
error
}
func (err *parseError) Error() string {
return fmt.Sprintf("Error occured while trying to parse report file %q: %s", err.filename, err.error)
}
func (r *reportScraper) processDisabledLock() (bool, string) {
disabledLockContent, err := readFile(r.disabledLockFilename)
disabled := true
switch err.(type) {
case *notFoundError:
disabled = false
}
var d struct{ DisabledMessage string `json:"disabled_message"` }
if disabledLockContent != nil {
if err := json.Unmarshal(disabledLockContent, &d); err != nil {
return disabled, err.Error()
}
}
return disabled, d.DisabledMessage
}
func (r *reportScraper) processReport(u puppetYamlReportUnmarshaler) (gaugeValuesBySectionsMap, error) {
reportContent, err := readFile(r.reportFilename)
if err != nil {
return nil, err
}
r.data = make(gaugeValuesBySectionsMap)
err = yaml.Unmarshal(reportContent, u)
if err != nil {
return nil, &parseError{r.reportFilename, err}
}
return r.data, nil
}
type readError struct {
filename string
error
}
type notFoundError struct {
*readError
}
func (err *readError) Error() string {
return fmt.Sprintf("Error occured while trying to read report file %q: %s", err.filename, err.error)
}
func readFile(filename string) ([]byte, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, ¬FoundError{&readError{filename, err}}
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, &readError{filename, err}
}
return content, nil
}