forked from circonus-labs/circonus-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
120 lines (103 loc) · 3.06 KB
/
collector.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
// Copyright © 2017 Circonus, Inc. <support@circonus.com>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// +build linux
package procfs
import (
"time"
"github.com/circonus-labs/circonus-agent/internal/builtins/collector"
cgm "github.com/circonus-labs/circonus-gometrics"
"github.com/pkg/errors"
)
// Define stubs to satisfy the collector.Collector interface.
//
// The individual wmi collector implementations must override Collect and Flush.
//
// ID and Inventory are generic and do not need to be overriden unless the
// collector implementation requires it.
// Collect returns collector metrics
func (c *pfscommon) Collect() error {
c.Lock()
defer c.Unlock()
return collector.ErrNotImplemented
}
// Flush returns last metrics collected
func (c *pfscommon) Flush() cgm.Metrics {
c.Lock()
defer c.Unlock()
if c.lastMetrics == nil {
c.lastMetrics = cgm.Metrics{}
}
return c.lastMetrics
}
// ID returns the id of the instance
func (c *pfscommon) ID() string {
c.Lock()
defer c.Unlock()
return c.id
}
// Inventory returns collector stats for /inventory endpoint
func (c *pfscommon) Inventory() collector.InventoryStats {
c.Lock()
defer c.Unlock()
return collector.InventoryStats{
ID: c.id,
LastRunStart: c.lastStart.Format(time.RFC3339Nano),
LastRunEnd: c.lastEnd.Format(time.RFC3339Nano),
LastRunDuration: c.lastRunDuration.String(),
LastError: c.lastError,
}
}
// cleanName is used to clean the metric name
func (c *pfscommon) cleanName(name string) string {
// metric names are not dynamic for linux procfs - reintroduce cleaner if
// procfs sources used return dirty dynamic names.
//
// return c.metricNameRegex.ReplaceAllString(name, c.metricNameChar)
return name
}
// addMetric to internal buffer if metric is active
func (c *pfscommon) addMetric(metrics *cgm.Metrics, prefix string, mname, mtype string, mval interface{}) error {
if metrics == nil {
return errors.New("invalid metric submission")
}
if mname == "" {
return errors.New("invalid metric, no name")
}
if mtype == "" {
return errors.New("invalid metric, no type")
}
// cleanup the raw metric name, if needed
mname = c.cleanName(mname)
// check status of cleaned metric name
active, found := c.metricStatus[mname]
if (found && active) || (!found && c.metricDefaultActive) {
metricName := mname
if prefix != "" {
metricName = prefix + metricNameSeparator + mname
}
(*metrics)[metricName] = cgm.Metric{Type: mtype, Value: mval}
return nil
}
return errors.Errorf("metric (%s) not active", mname)
}
// setStatus is used in Collect to set the collector status
func (c *pfscommon) setStatus(metrics cgm.Metrics, err error) {
c.Lock()
if err == nil {
c.lastError = ""
c.lastMetrics = metrics
} else {
c.lastError = err.Error()
// on error, ensure metrics are reset
// do not keep returning a stale set of metrics
c.lastMetrics = cgm.Metrics{}
}
c.lastEnd = time.Now()
if !c.lastStart.IsZero() {
c.lastRunDuration = time.Since(c.lastStart)
}
c.running = false
c.Unlock()
}