-
Notifications
You must be signed in to change notification settings - Fork 13
/
circonus.go
627 lines (558 loc) · 18.3 KB
/
circonus.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package circonus
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"github.com/circonus-labs/circonus-unified-agent/config"
"github.com/circonus-labs/circonus-unified-agent/cua"
"github.com/circonus-labs/circonus-unified-agent/internal/release"
"github.com/circonus-labs/circonus-unified-agent/models"
"github.com/circonus-labs/go-apiclient"
"github.com/circonus-labs/go-trapcheck"
"github.com/circonus-labs/go-trapmetrics"
"github.com/shirou/gopsutil/v3/host"
)
var ch *Circonus
type Circonus struct {
sync.Mutex
logger cua.Logger
brokerTLSConfigs map[string]*tls.Config
circCfg *config.CirconusConfig
apiCfg *apiclient.Config
brokerCIDrx string
globalTags trapmetrics.Tags
ready bool
}
type MetricMeta struct {
PluginID string // plugin id or name (e.g. snmp, ping, etc.)
InstanceID string // plugin instance id (all plugins require an instance_id setting)
MetricGroupID string // metric group id (some plugins produce multiple "metric groups")
ProjectID string // metric tag project_id (stackdriver_circonus)
}
type MetricDestConfig struct {
DebugAPI *bool // allow override of api debugging per output
TraceMetrics *string // allow override of metric tracing per output
APIToken string // allow override of api token for a specific plugin (dm input or circonus output)
Broker string // allow override of broker for a specific plugin (dm input or circonus output)
Hostname string // allow override of hostname for a specific plugin (dm input or circonus output)
MetricMeta MetricMeta
}
// Logshim is for api and traps - it uses the info level and
// agent debug logging are controlled independently
type Logshim struct {
logh cua.Logger
prefix string
debugAPI bool
}
func (l Logshim) Printf(fmt string, args ...interface{}) {
if strings.Contains(fmt, "[DEBUG]") {
// for retryablehttp (it only logs using Printf, and everything is DEBUG)
if l.debugAPI {
l.logh.Infof(l.prefix+": "+fmt, args...)
}
} else {
l.logh.Infof(l.prefix+": "+fmt, args...)
}
}
func (l Logshim) Debugf(fmt string, args ...interface{}) {
l.logh.Debugf(l.prefix+": "+fmt, args...)
}
func (l Logshim) Infof(fmt string, args ...interface{}) {
l.logh.Infof(l.prefix+": "+fmt, args...)
}
func (l Logshim) Warnf(fmt string, args ...interface{}) {
l.logh.Warnf(l.prefix+": "+fmt, args...)
}
func (l Logshim) Errorf(fmt string, args ...interface{}) {
l.logh.Errorf(l.prefix+": "+fmt, args...)
}
func Initialize(cfg *config.CirconusConfig, err error) error {
if ch != nil {
return nil // already initialized
}
if err != nil {
return err
}
if cfg == nil {
return fmt.Errorf("circonus metric destination management module: invalid circonus config (nil)")
}
c := &Circonus{
circCfg: cfg,
brokerTLSConfigs: make(map[string]*tls.Config),
globalTags: make(trapmetrics.Tags, 0),
brokerCIDrx: `^/broker/[0-9]+$`,
}
if c.circCfg.APIToken == "" {
return fmt.Errorf("circonus metric destination management module: unable to initialize, API Token is required")
}
if c.circCfg.APIApp == "" {
c.circCfg.APIApp = "circonus-unified-agent"
}
c.apiCfg = &apiclient.Config{
TokenKey: c.circCfg.APIToken,
TokenApp: c.circCfg.APIApp,
MaxRetries: 4,
MinRetryDelay: "10s", // for race where api returns 500 but check is created,
MaxRetryDelay: "20s", // if retry is to fast a duplicate check is created...
}
if c.circCfg.APIURL != "" {
c.apiCfg.URL = c.circCfg.APIURL
}
if c.circCfg.APITLSCA != "" {
cp := x509.NewCertPool()
cert, err := os.ReadFile(c.circCfg.APITLSCA)
if err != nil {
return fmt.Errorf("circonus metric destination management module: unable to load api ca file (%s): %w", c.circCfg.APITLSCA, err)
}
if !cp.AppendCertsFromPEM(cert) {
return fmt.Errorf("circonus metric destination management module: unable to parse api ca file (%s): %w", c.circCfg.APITLSCA, err)
}
c.apiCfg.CACert = cp
}
if c.circCfg.CacheConfigs && c.circCfg.CacheDir == "" {
return fmt.Errorf("circonus metric destination management module: cache_configs on, cache_dir not set")
}
if c.circCfg.CacheConfigs && c.circCfg.CacheDir != "" {
info, err := os.Stat(c.circCfg.CacheDir)
if err != nil {
return fmt.Errorf("circonus metric destination management module: cache_dir (%s): %w", c.circCfg.CacheDir, err)
} else if !info.IsDir() {
return fmt.Errorf("circonus metric destination management module: cache_dir (%s): not a directory", c.circCfg.CacheDir)
}
}
// if len(c.circCfg.CheckSearchTags) == 0 {
// _, an := filepath.Split(os.Args[0])
// c.circCfg.CheckSearchTags = []string{"_service:" + an}
// }
if c.circCfg.Hostname == "" {
hn, err := os.Hostname()
if err != nil || hn == "" {
hn = "unknown"
}
c.circCfg.Hostname = hn
}
c.logger = models.NewLogger("agent", "circ_metric_dest_mgr", "")
c.ready = true
ch = c
return nil
}
func Ready() bool {
if ch == nil {
return false
}
return ch.ready
}
func AddGlobalTags(tags map[string]string) {
if ch == nil {
return
}
for k, v := range tags {
if k != "" && v != "" {
ch.globalTags = append(ch.globalTags, trapmetrics.Tag{Category: k, Value: v})
}
}
}
func GetGlobalTags() trapmetrics.Tags {
return ch.globalTags
}
// getAPIClient returns a Circonus API client or an error
func getAPIClient(opts *MetricDestConfig) (*apiclient.API, error) {
if ch == nil {
return nil, fmt.Errorf("circonus metric destination management module: module not initialized")
}
if !ch.ready {
return nil, fmt.Errorf("circonus metric destination management module: invalid agent circonus config")
}
cfg := *ch.apiCfg
if opts != nil {
// only option which may currently be overridden is the api key
if opts.APIToken != "" {
cfg.TokenKey = opts.APIToken
}
}
client, err := apiclient.New(&cfg)
if err != nil {
return nil, fmt.Errorf("circonus metric destination management module: unable to initialize circonus api client: %w", err)
}
return client, nil
}
// createCheck retrieves, finds, or creates a Check bundle in Circonus and returns a trap check or an error
func createCheck(cfg *trapcheck.Config) (*trapcheck.TrapCheck, error) {
if ch == nil {
return nil, fmt.Errorf("circonus metric destination management module: module not initialized")
}
if !ch.ready {
return nil, fmt.Errorf("circonus metric destination management module: invalid agent circonus config")
}
tc, err := trapcheck.New(cfg)
if err != nil {
return nil, fmt.Errorf("circonus metric destination management module: creating trap check: %w", err)
}
return tc, nil
}
// createMetrics creates an instance of trap metrics and returns it or an error
func createMetrics(cfg *trapmetrics.Config) (*trapmetrics.TrapMetrics, error) {
if ch == nil {
return nil, fmt.Errorf("circonus metric destination management module: module not initialized")
}
if !ch.ready {
return nil, fmt.Errorf("circonus metric destination management module: invalid agent circonus config")
}
tm, err := trapmetrics.New(cfg)
if err != nil {
return nil, fmt.Errorf("circonus metric destination management module: creating trap metrics: %w", err)
}
return tm, nil
}
// NewMetricDestination will find/retrieve/create a new circonus check bundle and add it to a trap metrics instance to be
// used as a metric destination.
// pluginID = id/name (e.g. inputs.cpu would be cpu, inputs.snmp would be snmp)
// instanceID = instance_id setting from the config
// metricGroupID = group of metrics from the plugin (some offer multiple)
// hostname = used in the display name and target of the check
// logger = an instance of cua logger (already configured for the plugin requesting the metric destination)
func NewMetricDestination(opts *MetricDestConfig, logger cua.Logger) (*trapmetrics.TrapMetrics, error) {
if ch == nil {
return nil, fmt.Errorf("circonus metric destination management module: module not initialized")
}
if !ch.ready {
return nil, fmt.Errorf("circonus metric destination management module: invalid agent circonus config")
}
// serialize, don't want too many checks being created simultaneously - api rate limits, overwhelm broker, duplicate checks, etc.
ch.Lock()
defer ch.Unlock()
pluginID := opts.MetricMeta.PluginID
instanceID := opts.MetricMeta.InstanceID
metricGroupID := opts.MetricMeta.MetricGroupID
projectID := opts.MetricMeta.ProjectID
hostname := opts.Hostname
destKey := opts.MetricMeta.Key()
if hostname == "" && ch.circCfg.Hostname != "" {
hostname = ch.circCfg.Hostname
}
debugCheckSet := false
debugAPI := ch.circCfg.DebugAPI
traceMetrics := ch.circCfg.TraceMetrics
if opts.DebugAPI != nil {
debugAPI = *opts.DebugAPI
}
if opts.TraceMetrics != nil {
traceMetrics = *opts.TraceMetrics
}
bundle := loadCheckConfig(destKey)
if bundle != nil {
// NOTE: api call debug won't be set on existing checks unless they are cached.
// submission debugging will work as the flags will be set after the
// check bundle is retrieved (found) via the api. so, the initial api calls
// to find the check would not be printed in the log.
checkUUID := bundle.CheckUUIDs[0]
if settings, found := ch.circCfg.DebugChecks[checkUUID]; found {
options := strings.Split(settings, ",")
if len(options) != 2 {
ch.logger.Warnf("debug_checks invalid settings (%s): %s", checkUUID, settings)
}
traceMetrics = options[1]
debug, err := strconv.ParseBool(options[0])
if err != nil {
ch.logger.Warnf("debug_checks invalid setting (%s) (debugapi:%s): %s", checkUUID, options[0], err)
} else {
debugAPI = debug
}
ch.logger.Infof("set debug:%t trace:%s on check %s", debugAPI, traceMetrics, checkUUID)
debugCheckSet = true // don't try to parse the setting again later
}
}
checkType := []string{"httptrap", "cua", pluginID}
if metricGroupID != "" {
checkType = append(checkType, metricGroupID)
}
checkType = append(checkType, runtime.GOOS)
var checkDisplayName []string
switch pluginID {
case "stackdriver_circonus":
checkDisplayName = []string{"GCP"}
if instanceID != "" && instanceID != pluginID {
checkDisplayName = append(checkDisplayName, instanceID)
}
if projectID != "" {
checkDisplayName = append(checkDisplayName, projectID)
}
if metricGroupID != "" {
checkDisplayName = append(checkDisplayName, gcpMetricGroupLookup(metricGroupID))
}
default:
checkDisplayName = []string{hostname, pluginID}
if instanceID != "" && instanceID != pluginID {
checkDisplayName = append(checkDisplayName, instanceID)
}
checkDisplayName = append(checkDisplayName, "("+runtime.GOOS+")")
}
// tags used to SEARCH for a specific check
searchTags := make([]string, 0)
if len(ch.circCfg.CheckSearchTags) > 0 {
for _, tag := range ch.circCfg.CheckSearchTags {
if tag != "" {
searchTags = append(searchTags, tag)
}
}
}
if !strings.Contains(strings.Join(searchTags, ","), "_instance_id") {
searchTags = append(searchTags, "_instance_id:"+strings.ToLower(instanceID))
}
// additional tags to ADD to a check (metadata, DESCRIBE a check)
checkTags := []string{
"_plugin_id:" + pluginID,
"_instance_id:" + strings.ToLower(instanceID),
"_service:" + release.NAME,
}
if metricGroupID != "" {
checkTags = append(checkTags, "_metric_group:"+metricGroupID)
}
if pluginID == "host" {
checkTags = append(checkTags, getOSCheckTags()...)
}
checkTarget := hostname
instanceLogger := &Logshim{
logh: logger,
prefix: destKey,
debugAPI: debugAPI,
}
// API client
circAPI, err := getAPIClient(opts)
if err != nil {
return nil, err
}
circAPI.Log = instanceLogger
circAPI.Debug = debugAPI
// Trap Check
tc := &trapcheck.Config{
Client: circAPI,
Logger: instanceLogger,
CheckSearchTags: searchTags,
TraceMetrics: traceMetrics,
}
var cc *apiclient.CheckBundle
var tch *trapcheck.TrapCheck
switch {
case bundle != nil && ch.circCfg.CacheNoVerify: // use cached check bundle and don't verify by pulling from API again
if tlscfg, ok := ch.brokerTLSConfigs[bundle.Brokers[0]]; ok {
tc.SubmitTLSConfig = tlscfg.Clone()
}
var err error
tch, err = trapcheck.NewFromCheckBundle(tc, bundle)
if err != nil {
return nil, err
}
if tc.SubmitTLSConfig == nil {
t, err := tch.GetBrokerTLSConfig()
if err != nil {
return nil, fmt.Errorf("circonus metric destination management module: unable to get broker tls config: %w", err)
}
if t != nil {
ch.brokerTLSConfigs[bundle.Brokers[0]] = t.Clone()
} else {
// note: err==nil and t==nil means public broker (api.circonus.com) or using http: as the schema
ch.brokerTLSConfigs[bundle.Brokers[0]] = t
}
}
case bundle != nil: // cached bundle, use the cid
cc = &apiclient.CheckBundle{
CID: bundle.CID,
Tags: checkTags,
}
if len(bundle.Brokers) > 0 {
if tlscfg, ok := ch.brokerTLSConfigs[bundle.Brokers[0]]; ok {
tc.SubmitTLSConfig = tlscfg.Clone()
}
}
default: // find/create check bundle
cc = &apiclient.CheckBundle{
Type: strings.Join(checkType, ":"),
DisplayName: strings.Join(checkDisplayName, " "),
Target: checkTarget,
Tags: checkTags,
}
if opts.Broker != "" {
cc.Brokers = []string{opts.Broker}
} else if ch.circCfg.Broker != "" {
cc.Brokers = []string{ch.circCfg.Broker}
}
if len(cc.Brokers) > 0 {
// fixup config supplied broker CID if needed
bid := cc.Brokers[0]
if !strings.HasPrefix(bid, "/broker/") {
bid = "/broker/" + bid
matched, err := regexp.MatchString(ch.brokerCIDrx, bid)
if err != nil {
return nil, err
}
if !matched {
return nil, fmt.Errorf("invalid broker cid (%s): %w", bid, err)
}
cc.Brokers[0] = bid
}
if tlscfg, ok := ch.brokerTLSConfigs[cc.Brokers[0]]; ok {
tc.SubmitTLSConfig = tlscfg.Clone()
}
}
}
if tch == nil {
var err error
tc.CheckConfig = cc
logger.Debug("find/create check using API")
tch, err = createCheck(tc)
if err != nil {
return nil, err
}
}
if bundle == nil { // it wasn't loaded from cache
b, err := tch.GetCheckBundle()
if err != nil {
return nil, fmt.Errorf("circonus metric destination management module: unable to get check bundle: %w", err)
}
bundle = &b
saveCheckConfig(destKey, bundle)
}
if pluginID == "host" {
if b, err := updateCheckTags(circAPI, bundle, checkTags, logger); err != nil {
logger.Warnf("circonus metric destination management moudle: updating check %s", err)
} else if b != nil {
saveCheckConfig(destKey, b)
}
}
// if checks are going to a non-public trap
// cache the brokerTLS to use for other checks
// so that the api isn't hit for evevry check to pull the broker
if _, ok := ch.brokerTLSConfigs[bundle.Brokers[0]]; !ok {
t, err := tch.GetBrokerTLSConfig()
if err != nil {
return nil, fmt.Errorf("circonus metric destination management module: unable to get broker tls config: %w", err)
}
if t != nil {
ch.brokerTLSConfigs[bundle.Brokers[0]] = t.Clone()
} else {
// note: err==nil and t==nil means public broker (api.circonus.com) or using http: as the schema
ch.brokerTLSConfigs[bundle.Brokers[0]] = t
}
}
// Trap Metrics
tm := &trapmetrics.Config{
Trap: tch,
Logger: instanceLogger,
}
metrics, err := createMetrics(tm)
if err != nil {
return nil, err
}
if bundle != nil && !debugCheckSet {
// non-nil bundle, and check specific debugging hasn't already been set
// e.g. check wasn't cached, and was "found" during check initialization
checkUUID := bundle.CheckUUIDs[0]
if settings, found := ch.circCfg.DebugChecks[checkUUID]; found {
options := strings.Split(settings, ",")
if len(options) != 2 {
ch.logger.Warnf("debug_checks invalid settings (%s): %s", checkUUID, settings)
}
traceMetrics = options[1]
debug, err := strconv.ParseBool(options[0])
if err != nil {
ch.logger.Warnf("debug_checks invalid setting (%s) (debugapi:%s): %s", checkUUID, options[0], err)
} else {
debugAPI = debug
}
circAPI.Debug = debugAPI
instanceLogger.debugAPI = debugAPI
_, _ = tch.TraceMetrics(traceMetrics)
ch.logger.Infof("set debug:%t trace:%s on check %s", debugAPI, traceMetrics, checkUUID)
}
}
return metrics, nil
}
func getOSCheckTags() []string {
ri := release.GetInfo()
checkTags := []string{
"_os:" + runtime.GOOS,
"_agent:" + ri.Name + "-" + strings.ToLower(ri.Version),
}
hi, err := host.Info()
if err != nil {
return checkTags
}
// hi.OS is runtime.GOOS, which is added above
// if hi.OS != "" {
// checkTags = append(checkTags, "_os:"+hi.OS)
// }
if hi.Platform != "" {
checkTags = append(checkTags, "_platform:"+strings.ToLower(hi.Platform))
}
if hi.PlatformFamily != "" {
checkTags = append(checkTags, "_platform_family:"+strings.ToLower(hi.PlatformFamily))
}
if hi.PlatformVersion != "" {
checkTags = append(checkTags, "_platform_version:"+strings.ToLower(hi.PlatformVersion))
}
if hi.KernelVersion != "" {
checkTags = append(checkTags, "_kernel_version:"+strings.ToLower(hi.KernelVersion))
}
if hi.KernelArch != "" {
checkTags = append(checkTags, "_kernel_arch:"+strings.ToLower(hi.KernelArch))
}
if hi.VirtualizationSystem != "" {
checkTags = append(checkTags, "_virt_sys:"+strings.ToLower(hi.VirtualizationSystem))
}
if hi.VirtualizationRole != "" {
checkTags = append(checkTags, "_virt_role:"+strings.ToLower(hi.VirtualizationRole))
}
return checkTags
}
func updateCheckTags(client *apiclient.API, bundle *apiclient.CheckBundle, tags []string, logger cua.Logger) (*apiclient.CheckBundle, error) {
update := false
for _, ostag := range tags {
found := false
ostagParts := strings.SplitN(ostag, ":", 2)
for j, ctag := range bundle.Tags {
if ostag == ctag {
found = true
break
}
ctagParts := strings.SplitN(ctag, ":", 2)
if len(ostagParts) == len(ctagParts) {
if ostagParts[0] == ctagParts[0] {
if ostagParts[1] != ctagParts[1] {
logger.Warnf("modifying tag: new: %v old: %v", ostagParts, ctagParts)
bundle.Tags[j] = ostag
update = true // but force update since we're modifying a tag
found = true
break
}
}
}
}
if !found {
logger.Warnf("adding missing tag: %s curr: %v", ostag, bundle.Tags)
bundle.Tags = append(bundle.Tags, ostag)
update = true
}
}
if update {
b, err := client.UpdateCheckBundle(bundle)
if err != nil {
return nil, err
}
return b, nil
}
return nil, nil
}
func (m MetricMeta) Key() string {
return fmt.Sprintf("%s:%s:%s:%s",
m.PluginID,
m.InstanceID,
m.MetricGroupID,
m.ProjectID)
}