-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcollectd-plugin.go
224 lines (198 loc) · 7.17 KB
/
collectd-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
package main
import (
"context"
"fmt"
"net"
"os"
"strconv"
"time"
"collectd.org/api"
"collectd.org/network"
sdkArgs "github.com/newrelic/infra-integrations-sdk/args"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/infra-integrations-sdk/integration"
"github.com/newrelic/infra-integrations-sdk/log"
"github.com/newrelic/newrelic-telemetry-sdk-go/telemetry"
)
type argumentList struct {
sdkArgs.DefaultArgumentList
Port int `default:"25826" help:"Port to listen on"`
Key string `default:"" help:"Insights Insert Key required for sending dimensional metrics"`
Interval string `default:"15s" help:"Interval to harvest dimensional metrics"`
Dim bool `default:"false" help:"Report output as dimensional metrics (true|false)"`
MetricApiUrl string `default:"https://metric-api.newrelic.com/metric/v1" help:"Metric API endpoint to use"`
}
const (
integrationName = "com.newrelic.collectd-plugin"
integrationVersion = "0.2.0"
)
var args argumentList
// CollectDReceiver implements the Writer interface. It wraps a channel to which metrics received are sent to
type CollectDReceiver struct {
metricChannel chan *api.ValueList
}
// Write captures the valueLists from collectd and places them on the channel
func (receiver *CollectDReceiver) Write(_ context.Context, valueLists *api.ValueList) error {
receiver.metricChannel <- valueLists
return nil
}
// Stateful nri- this nri never exits
func main() {
collectdIntegration, err := integration.New(integrationName, integrationVersion, integration.Args(&args))
fatalIfErr(err)
if args.All() || args.Metrics {
fatalIfErr(populateMetrics(collectdIntegration))
}
fatalIfErr(collectdIntegration.Publish())
}
func populateMetrics(integration *integration.Integration) error {
ListenAndWrite(integration)
return nil
}
// ListenAndWrite starts the collectd receiver, it never exits but listens constantly
func ListenAndWrite(integration *integration.Integration) {
var metricChannel = make(chan *api.ValueList)
writer := CollectDReceiver{metricChannel: metricChannel}
srv := &network.Server{
PasswordLookup: network.NewAuthFile("/etc/collectd/users"),
Addr: net.JoinHostPort("::", strconv.Itoa(args.Port)),
Writer: &writer,
}
if args.Dim {
go startDimMetricsProcessor(metricChannel)
} else {
go startInfraMetricProcessor(metricChannel, integration)
}
// blocks
log.Fatal(srv.ListenAndWrite(context.Background()))
}
func startDimMetricsProcessor(metricChannel chan *api.ValueList) {
t, _ := time.ParseDuration(args.Interval)
h, err := telemetry.NewHarvester(
telemetry.ConfigAPIKey(args.Key),
telemetry.ConfigHarvestPeriod(t),
telemetry.ConfigBasicErrorLogger(os.Stdout),
telemetry.ConfigMetricsURLOverride(args.MetricApiUrl),
)
if err != nil {
log.Fatal(err)
}
for {
valueLists := <-metricChannel
vl := valueLists
// for _, vl := range valueLists {
// println(vl.Identifier.Plugin + " " + vl.Identifier.PluginInstance + " " + vl.Identifier.Type + " " + vl.Identifier.TypeInstance)
metricName := fmt.Sprintf("%s.%s", vl.Identifier.Type, vl.Identifier.TypeInstance)
recordDimensionalMetric(vl, h, metricName)
// }
}
}
func recordDimensionalMetric(vl *api.ValueList, h *telemetry.Harvester, metricName string) {
for _, val := range vl.Values {
switch val.(type) {
case api.Counter:
newVal := val.(api.Counter)
h.RecordMetric(telemetry.Count{
Timestamp: time.Now(),
Value: float64(newVal),
Name: metricName,
Attributes: map[string]interface{}{
"Plugin": vl.Plugin,
"Entity": vl.PluginInstance,
"HostName": vl.Host,
},
})
case api.Gauge:
newVal := val.(api.Gauge)
h.RecordMetric(telemetry.Gauge{
Timestamp: time.Now(),
Value: float64(newVal),
Name: metricName,
Attributes: map[string]interface{}{
"Plugin": vl.Plugin,
"Entity": vl.PluginInstance,
"HostName": vl.Host,
},
})
case api.Derive:
newVal := val.(api.Derive)
h.RecordMetric(telemetry.Count{
Timestamp: time.Now(),
Value: float64(newVal),
Name: metricName,
Attributes: map[string]interface{}{
"Plugin": vl.Plugin,
"Entity": vl.PluginInstance,
"HostName": vl.Host,
},
})
default:
newVal := val.(api.Gauge)
h.RecordMetric(telemetry.Gauge{
Timestamp: time.Now(),
Value: float64(newVal),
Name: metricName,
Attributes: map[string]interface{}{
"Plugin": vl.Plugin,
"Entity": vl.PluginInstance,
"HostName": vl.Host,
},
})
}
}
}
func startInfraMetricProcessor(metricChannel chan *api.ValueList, integration *integration.Integration) {
// entity := integration.LocalEntity()
for {
valueLists := <-metricChannel
// Create a new ms (metricset) for each Identifier.Plugin+PluginInstance'
entity := integration.LocalEntity()
ms := entity.NewMetricSet("CollectdSample")
vl := valueLists
// for _, vl := range valueLists {
// println(vl.Identifier.Plugin + " " + vl.Identifier.PluginInstance + " " + vl.Identifier.Type + " " + vl.Identifier.TypeInstance)
metricName := ""
if vl.Identifier.PluginInstance != "" {
metricName = fmt.Sprintf("%s.%s.%s.%s", vl.Identifier.Plugin, vl.Identifier.PluginInstance, vl.Identifier.Type, vl.Identifier.TypeInstance)
} else {
metricName = fmt.Sprintf("%s.%s.%s", vl.Identifier.Plugin, vl.Identifier.Type, vl.Identifier.TypeInstance)
}
formatValues(vl, ms, metricName)
// }
fatalIfErr(integration.Publish())
}
}
func fatalIfErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func formatValues(vl *api.ValueList, ms *metric.Set, metricName string) {
// formatTime(vl.Time)
for _, val := range vl.Values {
switch v := val.(type) {
case api.Counter:
// fields[i+1] = fmt.Sprintf("%d", v)
ms.SetMetric(metricName, v, metric.GAUGE)
ms.SetMetric("Entity", vl.Host, metric.ATTRIBUTE)
case api.Gauge:
// fields[i+1] = fmt.Sprintf("%.15g", v)
ms.SetMetric(metricName, v, metric.GAUGE)
ms.SetMetric("Entity", vl.Host, metric.ATTRIBUTE)
case api.Derive:
// fields[i+1] = fmt.Sprintf("%d", v)
ms.SetMetric(metricName, v, metric.GAUGE)
ms.SetMetric("Entity", vl.Host, metric.ATTRIBUTE)
default:
ms.SetMetric(metricName, v, metric.ATTRIBUTE)
ms.SetMetric("Entity", vl.Host, metric.ATTRIBUTE)
// return "", fmt.Errorf("unexpected type %T", v)
}
}
}
func formatTime(t time.Time) string {
if t.IsZero() {
return "N"
}
return fmt.Sprintf("%.3f", float64(t.UnixNano())/1000000000.0)
}