-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmackerel-plugin.go
503 lines (450 loc) · 12.3 KB
/
mackerel-plugin.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package mackerelplugin
import (
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"math"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/mackerelio/golib/pluginutil"
)
// Metrics represents definition of a metric
type Metrics struct {
Name string `json:"name"`
Label string `json:"label"`
Diff bool `json:"-"`
Type string `json:"-"`
Stacked bool `json:"stacked"`
Scale float64 `json:"-"`
AbsoluteName bool `json:"-"`
}
// Graphs represents definition of a graph
type Graphs struct {
Label string `json:"label"`
Unit string `json:"unit"`
Metrics []Metrics `json:"metrics"`
}
// MetricValues represents a collection of metric values and its timestamp
type MetricValues struct {
Values map[string]interface{}
Timestamp time.Time
}
// Plugin is old interface of mackerel-plugin
type Plugin interface {
FetchMetrics() (map[string]interface{}, error)
GraphDefinition() map[string]Graphs
}
// PluginWithPrefix is recommended interface
type PluginWithPrefix interface {
Plugin
MetricKeyPrefix() string
}
// MackerelPlugin is for mackerel-agent-plugin
type MackerelPlugin struct {
Plugin
Tempfile string
diff *bool
}
// NewMackerelPlugin returns new MackerelPlugin struct
func NewMackerelPlugin(plugin Plugin) MackerelPlugin {
mp := MackerelPlugin{Plugin: plugin}
return mp
}
func (h *MackerelPlugin) hasDiff() bool {
if h.diff == nil {
diff := false
h.diff = &diff
DiffCheck:
for _, graph := range h.GraphDefinition() {
for _, metric := range graph.Metrics {
if metric.Diff {
*h.diff = true
break DiffCheck
}
}
}
}
return *h.diff
}
func (h *MackerelPlugin) printValue(w io.Writer, key string, value interface{}, now time.Time) {
switch v := value.(type) {
case uint32:
fmt.Fprintf(w, "%s\t%d\t%d\n", key, v, now.Unix())
case uint64:
fmt.Fprintf(w, "%s\t%d\t%d\n", key, v, now.Unix())
case float64:
if math.IsNaN(value.(float64)) || math.IsInf(v, 0) {
log.Printf("Invalid value: key = %s, value = %f\n", key, value)
} else {
fmt.Fprintf(w, "%s\t%f\t%d\n", key, v, now.Unix())
}
}
}
// FetchLastValues retrieves the last recorded metric value
// if there is the graph-def that is set Diff to true in the result of h.GraphDefinition().
func (h *MackerelPlugin) FetchLastValues() (metricValues MetricValues, err error) {
if !h.hasDiff() {
return
}
f, err := os.Open(h.tempfilename())
if err != nil {
if os.IsNotExist(err) {
return metricValues, nil
}
return
}
defer f.Close()
decoder := json.NewDecoder(f)
err = decoder.Decode(&metricValues.Values)
if err != nil {
return
}
switch v := metricValues.Values["_lastTime"].(type) {
case float64:
metricValues.Timestamp = time.Unix(int64(v), 0)
case int64:
metricValues.Timestamp = time.Unix(v, 0)
}
return
}
var errStateUpdated = errors.New("state was recently updated")
func (h *MackerelPlugin) fetchLastValuesSafe(now time.Time) (metricValues MetricValues, err error) {
m, err := h.FetchLastValues()
if err != nil {
return m, err
}
if now.Sub(m.Timestamp) < time.Second {
return m, errStateUpdated
}
return m, nil
}
func (h *MackerelPlugin) saveValues(metricValues MetricValues) error {
if !h.hasDiff() {
return nil
}
fname := h.tempfilename()
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
// Since Go 1.15 strconv.ParseFloat returns +Inf if it couldn't parse a string.
// But JSON does not accept invalid numbers, such as +Inf, -Inf or NaN.
// We perhaps have some plugins that is affected above change,
// so saveState should clear invalid numbers in the values before saving it.
for k, v := range metricValues.Values {
f, ok := v.(float64)
if !ok {
continue
}
if math.IsInf(f, 0) || math.IsNaN(f) {
delete(metricValues.Values, k)
}
}
metricValues.Values["_lastTime"] = metricValues.Timestamp.Unix()
encoder := json.NewEncoder(f)
err = encoder.Encode(metricValues.Values)
if err != nil {
return err
}
return nil
}
func (h *MackerelPlugin) calcDiff(value float64, now time.Time, lastValue float64, lastTime time.Time) (float64, error) {
diffTime := now.Unix() - lastTime.Unix()
if diffTime > 600 {
return 0, errors.New("too long duration")
}
diff := (value - lastValue) * 60 / float64(diffTime)
if lastValue <= value {
return diff, nil
}
return 0.0, errors.New("counter seems to be reset")
}
func (h *MackerelPlugin) calcDiffUint32(value uint32, now time.Time, lastValue uint32, lastTime time.Time, lastDiff float64) (float64, error) {
diffTime := now.Unix() - lastTime.Unix()
if diffTime > 600 {
return 0, errors.New("too long duration")
}
diff := float64((value-lastValue)*60) / float64(diffTime)
if lastValue <= value || diff < lastDiff*10 {
return diff, nil
}
return 0.0, errors.New("counter seems to be reset")
}
func (h *MackerelPlugin) calcDiffUint64(value uint64, now time.Time, lastValue uint64, lastTime time.Time, lastDiff float64) (float64, error) {
diffTime := now.Unix() - lastTime.Unix()
if diffTime > 600 {
return 0, errors.New("too long duration")
}
diff := float64((value-lastValue)*60) / float64(diffTime)
if lastValue <= value || diff < lastDiff*10 {
return diff, nil
}
return 0.0, errors.New("counter seems to be reset")
}
func (h *MackerelPlugin) tempfilename() string {
if h.Tempfile == "" {
h.Tempfile = h.generateTempfilePath(os.Args)
}
return h.Tempfile
}
var tempfileSanitizeReg = regexp.MustCompile(`[^A-Za-z0-9_.-]`)
// SetTempfileByBasename sets Tempfile under proper directory with specified basename.
func (h *MackerelPlugin) SetTempfileByBasename(base string) {
h.Tempfile = filepath.Join(pluginutil.PluginWorkDir(), base)
}
func (h *MackerelPlugin) generateTempfilePath(args []string) string {
commandPath := args[0]
var prefix string
if p, ok := h.Plugin.(PluginWithPrefix); ok {
prefix = p.MetricKeyPrefix()
} else {
name := filepath.Base(commandPath)
prefix = strings.TrimPrefix(tempfileSanitizeReg.ReplaceAllString(name, "_"), "mackerel-plugin-")
}
filename := fmt.Sprintf(
"mackerel-plugin-%s-%x",
prefix,
// When command-line options are different, mostly different metrics.
// e.g. `-host` and `-port` options for mackerel-plugin-mysql
sha1.Sum([]byte(strings.Join(args[1:], " "))),
)
return filepath.Join(pluginutil.PluginWorkDir(), filename)
}
const (
metricTypeUint32 = "uint32"
metricTypeUint64 = "uint64"
// metricTypeFloat = "float64"
)
func (h *MackerelPlugin) formatValues(prefix string, metric Metrics, metricValues MetricValues, lastMetricValues MetricValues) {
name := metric.Name
if metric.AbsoluteName && len(prefix) > 0 {
name = prefix + "." + name
}
value, ok := metricValues.Values[name]
if !ok || value == nil {
return
}
var err error
if v, ok := value.(string); ok {
switch metric.Type {
case metricTypeUint32:
value, err = strconv.ParseUint(v, 10, 32)
case metricTypeUint64:
value, err = strconv.ParseUint(v, 10, 64)
default:
value, err = strconv.ParseFloat(v, 64)
}
}
if err != nil {
// For keeping compatibility, if each above statement occurred the error,
// then the value is set to 0 and continue.
log.Println("Parsing a value: ", err)
}
if metric.Diff {
_, ok := lastMetricValues.Values[name]
if ok {
var lastDiff float64
if lastMetricValues.Values[".last_diff."+name] != nil {
lastDiff = toFloat64(lastMetricValues.Values[".last_diff."+name])
}
var err error
switch metric.Type {
case metricTypeUint32:
value, err = h.calcDiffUint32(toUint32(value), metricValues.Timestamp, toUint32(lastMetricValues.Values[name]), lastMetricValues.Timestamp, lastDiff)
case metricTypeUint64:
value, err = h.calcDiffUint64(toUint64(value), metricValues.Timestamp, toUint64(lastMetricValues.Values[name]), lastMetricValues.Timestamp, lastDiff)
default:
value, err = h.calcDiff(toFloat64(value), metricValues.Timestamp, toFloat64(lastMetricValues.Values[name]), lastMetricValues.Timestamp)
}
if err != nil {
log.Println("OutputValues: ", err)
return
}
metricValues.Values[".last_diff."+name] = value
} else {
log.Printf("%s does not exist at last fetch\n", name)
return
}
}
if metric.Scale != 0 {
switch metric.Type {
case metricTypeUint32:
value = toUint32(value) * uint32(metric.Scale)
case metricTypeUint64:
value = toUint64(value) * uint64(metric.Scale)
default:
value = toFloat64(value) * metric.Scale
}
}
metricNames := []string{}
if p, ok := h.Plugin.(PluginWithPrefix); ok {
metricNames = append(metricNames, p.MetricKeyPrefix())
}
if len(prefix) > 0 {
metricNames = append(metricNames, prefix)
}
metricNames = append(metricNames, metric.Name)
h.printValue(os.Stdout, strings.Join(metricNames, "."), value, metricValues.Timestamp)
}
func (h *MackerelPlugin) formatValuesWithWildcard(prefix string, metric Metrics, metricValues MetricValues, lastMetricValues MetricValues) {
regexpStr := `\A` + prefix + "." + metric.Name
regexpStr = strings.Replace(regexpStr, ".", "\\.", -1)
regexpStr = strings.Replace(regexpStr, "*", "[-a-zA-Z0-9_]+", -1)
regexpStr = strings.Replace(regexpStr, "#", "[-a-zA-Z0-9_]+", -1)
re, err := regexp.Compile(regexpStr)
if err != nil {
log.Fatalln("Failed to compile regexp: ", err)
}
for k := range metricValues.Values {
if re.MatchString(k) {
metricEach := metric
metricEach.Name = k
h.formatValues("", metricEach, metricValues, lastMetricValues)
}
}
}
// Run the plugin
func (h *MackerelPlugin) Run() {
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
h.OutputDefinitions()
} else {
h.OutputValues()
}
}
// OutputValues output the metrics
func (h *MackerelPlugin) OutputValues() {
stat, err := h.FetchMetrics()
if err != nil {
log.Fatalln("OutputValues: ", err)
}
metricValues := MetricValues{Values: stat, Timestamp: time.Now()}
lastMetricValues, err := h.fetchLastValuesSafe(metricValues.Timestamp)
if err != nil {
if err == errStateUpdated {
log.Println("OutputValues: ", err)
return
}
log.Println("FetchLastValues (ignore):", err)
}
for key, graph := range h.GraphDefinition() {
for _, metric := range graph.Metrics {
if strings.ContainsAny(key+metric.Name, "*#") {
h.formatValuesWithWildcard(key, metric, metricValues, lastMetricValues)
} else {
h.formatValues(key, metric, metricValues, lastMetricValues)
}
}
}
err = h.saveValues(metricValues)
if err != nil {
log.Fatalln("saveValues: ", err)
}
}
// GraphDef represents graph definitions
type GraphDef struct {
Graphs map[string]Graphs `json:"graphs"`
}
func title(s string) string {
r := strings.NewReplacer(".", " ", "_", " ")
return cases.Title(language.Und, cases.NoLower).String(r.Replace(s))
}
// OutputDefinitions outputs graph definitions
func (h *MackerelPlugin) OutputDefinitions() {
fmt.Println("# mackerel-agent-plugin")
graphs := make(map[string]Graphs)
for key, graph := range h.GraphDefinition() {
g := graph
k := key
if p, ok := h.Plugin.(PluginWithPrefix); ok {
prefix := p.MetricKeyPrefix()
if k == "" {
k = prefix
} else {
k = prefix + "." + k
}
}
if g.Label == "" {
g.Label = title(k)
}
metrics := []Metrics{}
for _, v := range g.Metrics {
if v.Label == "" {
v.Label = title(v.Name)
}
metrics = append(metrics, v)
}
g.Metrics = metrics
graphs[k] = g
}
var graphdef GraphDef
graphdef.Graphs = graphs
b, err := json.Marshal(graphdef)
if err != nil {
log.Fatalln("OutputDefinitions: ", err)
}
fmt.Println(string(b))
}
func toUint32(value interface{}) uint32 {
switch v := value.(type) {
case uint32:
return v
case uint64:
return uint32(v)
case float64:
return uint32(v)
case string:
n, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return 0
}
return uint32(n)
default:
return 0
}
}
func toUint64(value interface{}) uint64 {
switch v := value.(type) {
case uint32:
return uint64(v)
case uint64:
return v
case float64:
return uint64(v)
case string:
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return 0
}
return n
default:
return 0
}
}
func toFloat64(value interface{}) float64 {
switch v := value.(type) {
case uint32:
return float64(v)
case uint64:
return float64(v)
case float64:
return v
case string:
n, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0
}
return n
default:
return 0
}
}