/
command.go
640 lines (565 loc) · 18.1 KB
/
command.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
628
629
630
631
632
633
634
635
636
637
638
639
640
package command
import (
"crypto/sha1"
"encoding/json"
"fmt"
"math"
"os"
"time"
"github.com/Songmu/retry"
"github.com/mackerelio/mackerel-agent/agent"
"github.com/mackerelio/mackerel-agent/checks"
"github.com/mackerelio/mackerel-agent/config"
"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/mackerel"
"github.com/mackerelio/mackerel-agent/metrics"
"github.com/mackerelio/mackerel-agent/spec"
)
var logger = logging.GetLogger("command")
var metricsInterval = 60 * time.Second
var retryNum uint = 20
var retryInterval = 3 * time.Second
// prepareHost collects specs of the host and sends them to Mackerel server.
// A unique host-id is returned by the server if one is not specified.
func prepareHost(conf *config.Config, api *mackerel.API) (*mackerel.Host, error) {
doRetry := func(f func() error) {
retry.Retry(retryNum, retryInterval, f)
}
filterErrorForRetry := func(err error) error {
if err != nil {
logger.Warningf("%s", err.Error())
}
if apiErr, ok := err.(*mackerel.Error); ok && apiErr.IsClientError() {
// don't retry when client error (APIKey error etc.) occurred
return nil
}
return err
}
hostname, meta, interfaces, customIdentifier, lastErr := collectHostSpecs()
if lastErr != nil {
return nil, fmt.Errorf("error while collecting host specs: %s", lastErr.Error())
}
var result *mackerel.Host
if hostID, err := conf.LoadHostID(); err != nil { // create
if customIdentifier != "" {
retry.Retry(3, 2*time.Second, func() error {
result, lastErr = api.FindHostByCustomIdentifier(customIdentifier)
return filterErrorForRetry(lastErr)
})
if result != nil {
hostID = result.ID
}
}
if result == nil {
logger.Debugf("Registering new host on mackerel...")
doRetry(func() error {
hostID, lastErr = api.CreateHost(mackerel.HostSpec{
Name: hostname,
Meta: meta,
Interfaces: interfaces,
RoleFullnames: conf.Roles,
DisplayName: conf.DisplayName,
CustomIdentifier: customIdentifier,
})
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
return nil, fmt.Errorf("Failed to register this host: %s", lastErr.Error())
}
doRetry(func() error {
result, lastErr = api.FindHost(hostID)
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
return nil, fmt.Errorf("Failed to find this host on mackerel: %s", lastErr.Error())
}
}
} else { // check the hostID is valid or not
doRetry(func() error {
result, lastErr = api.FindHost(hostID)
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
if fsStorage, ok := conf.HostIDStorage.(*config.FileSystemHostIDStorage); ok {
return nil, fmt.Errorf("Failed to find this host on mackerel (You may want to delete file \"%s\" to register this host to an another organization): %s", fsStorage.HostIDFile(), lastErr.Error())
}
return nil, fmt.Errorf("Failed to find this host on mackerel: %s", lastErr.Error())
}
}
hostSt := conf.HostStatus.OnStart
if hostSt != "" && hostSt != result.Status {
doRetry(func() error {
lastErr = api.UpdateHostStatus(result.ID, hostSt)
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
return nil, fmt.Errorf("Failed to set default host status: %s, %s", hostSt, lastErr.Error())
}
}
lastErr = conf.SaveHostID(result.ID)
if lastErr != nil {
return nil, fmt.Errorf("Failed to save host ID: %s", lastErr.Error())
}
return result, nil
}
// prepareCustomIdentiferHosts collects the host information based on the
// configuration of the custom_identifier fields.
func prepareCustomIdentiferHosts(conf *config.Config, api *mackerel.API) map[string]*mackerel.Host {
customIdentifierHosts := make(map[string]*mackerel.Host)
customIdentifiers := make(map[string]bool) // use a map to make them unique
for _, pluginConfigs := range conf.Plugin {
for _, pluginConfig := range pluginConfigs {
if pluginConfig.CustomIdentifier != nil {
customIdentifiers[*pluginConfig.CustomIdentifier] = true
}
}
}
for customIdentifier := range customIdentifiers {
host, err := api.FindHostByCustomIdentifier(customIdentifier)
if err != nil {
logger.Warningf("Failed to retrieve the host of custom_identifier: %s, %s", customIdentifier, err)
continue
}
customIdentifierHosts[customIdentifier] = host
}
return customIdentifierHosts
}
// Interval between each updating host specs.
var specsUpdateInterval = 1 * time.Hour
func delayByHost(host *mackerel.Host) int {
s := sha1.Sum([]byte(host.ID))
return int(s[len(s)-1]) % int(config.PostMetricsInterval.Seconds())
}
// Context context object
type Context struct {
Agent *agent.Agent
Config *config.Config
Host *mackerel.Host
API *mackerel.API
CustomIdentifierHosts map[string]*mackerel.Host
}
type postValue struct {
values []*mackerel.CreatingMetricsValue
retryCnt int
}
func newPostValue(values []*mackerel.CreatingMetricsValue) *postValue {
return &postValue{values, 0}
}
type loopState uint8
const (
loopStateFirst loopState = iota
loopStateDefault
loopStateQueued
loopStateHadError
loopStateTerminating
)
func loop(c *Context, termCh chan struct{}) error {
quit := make(chan struct{})
defer close(quit) // broadcast terminating
// Periodically update host specs.
go updateHostSpecsLoop(c, quit)
postQueue := make(chan *postValue, c.Config.Connection.PostMetricsBufferSize)
go enqueueLoop(c, postQueue, quit)
postDelaySeconds := delayByHost(c.Host)
initialDelay := postDelaySeconds / 2
logger.Debugf("wait %d seconds before initial posting.", initialDelay)
select {
case <-termCh:
return nil
case <-time.After(time.Duration(initialDelay) * time.Second):
c.Agent.InitPluginGenerators(c.API)
}
termMetricsCh := make(chan struct{})
var termCheckerCh chan struct{}
hasChecks := len(c.Agent.Checkers) > 0
if hasChecks {
termCheckerCh = make(chan struct{})
}
// fan-out termCh
go func() {
for range termCh {
termMetricsCh <- struct{}{}
if termCheckerCh != nil {
termCheckerCh <- struct{}{}
}
}
}()
if hasChecks {
go runCheckersLoop(c, termCheckerCh, quit)
}
lState := loopStateFirst
for {
select {
case <-termMetricsCh:
if lState == loopStateTerminating {
return fmt.Errorf("received terminate instruction again. force return")
}
lState = loopStateTerminating
if len(postQueue) <= 0 {
return nil
}
case v := <-postQueue:
origPostValues := [](*postValue){v}
if len(postQueue) > 0 {
// Bulk posting. However at most "two" metrics are to be posted, so postQueue isn't always empty yet.
logger.Debugf("Merging datapoints with next queued ones")
nextValues := <-postQueue
origPostValues = append(origPostValues, nextValues)
}
delaySeconds := 0
switch lState {
case loopStateFirst: // request immediately to create graph defs of host
// nop
case loopStateQueued:
delaySeconds = c.Config.Connection.PostMetricsDequeueDelaySeconds
case loopStateHadError:
// TODO: better interval calculation. exponential backoff or so.
delaySeconds = c.Config.Connection.PostMetricsRetryDelaySeconds
case loopStateTerminating:
// dequeue and post every one second when terminating.
delaySeconds = 1
default:
// Sending data at every 0 second from all hosts causes request flooding.
// To prevent flooding, this loop sleeps for some seconds
// which is specific to the ID of the host running agent on.
// The sleep second is up to 60s (to be exact up to `config.Postmetricsinterval.Seconds()`.
elapsedSeconds := int(time.Now().Unix() % int64(config.PostMetricsInterval.Seconds()))
if postDelaySeconds > elapsedSeconds {
delaySeconds = postDelaySeconds - elapsedSeconds
}
}
// determine next loopState before sleeping
if lState != loopStateTerminating {
if len(postQueue) > 0 {
lState = loopStateQueued
} else {
lState = loopStateDefault
}
}
logger.Debugf("Sleep %d seconds before posting.", delaySeconds)
select {
case <-time.After(time.Duration(delaySeconds) * time.Second):
// nop
case <-termMetricsCh:
if lState == loopStateTerminating {
return fmt.Errorf("received terminate instruction again. force return")
}
lState = loopStateTerminating
}
postValues := [](*mackerel.CreatingMetricsValue){}
for _, v := range origPostValues {
postValues = append(postValues, v.values...)
}
err := c.API.PostMetricsValues(postValues)
if err != nil {
logger.Errorf("Failed to post metrics value (will retry): %s", err.Error())
if lState != loopStateTerminating {
lState = loopStateHadError
}
go func() {
for _, v := range origPostValues {
v.retryCnt++
// It is difficult to distinguish the error is server error or data error.
// So, if retryCnt exceeded the configured limit, postValue is considered invalid and abandoned.
if v.retryCnt > c.Config.Connection.PostMetricsRetryMax {
json, err := json.Marshal(v.values)
if err != nil {
logger.Errorf("Something wrong with post values. marshaling failed.")
} else {
logger.Errorf("Post values may be invalid and abandoned: %s", string(json))
}
continue
}
postQueue <- v
}
}()
continue
}
logger.Debugf("Posting metrics succeeded.")
if lState == loopStateTerminating && len(postQueue) <= 0 {
return nil
}
}
}
}
func updateHostSpecsLoop(c *Context, quit chan struct{}) {
for {
c.UpdateHostSpecs()
select {
case <-quit:
return
case <-time.After(specsUpdateInterval):
// nop
}
}
}
func enqueueLoop(c *Context, postQueue chan *postValue, quit chan struct{}) {
metricsResult := c.Agent.Watch()
for {
select {
case <-quit:
return
case result := <-metricsResult:
created := float64(result.Created.Unix())
creatingValues := [](*mackerel.CreatingMetricsValue){}
for _, values := range result.Values {
hostID := c.Host.ID
if values.CustomIdentifier != nil {
if host, ok := c.CustomIdentifierHosts[*values.CustomIdentifier]; ok {
hostID = host.ID
} else {
continue
}
}
for name, value := range values.Values {
if math.IsNaN(value) || math.IsInf(value, 0) {
logger.Warningf("Invalid value: hostID = %s, name = %s, value = %f\n is not sent.", hostID, name, value)
continue
}
creatingValues = append(
creatingValues,
&mackerel.CreatingMetricsValue{
HostID: hostID,
Name: name,
Time: created,
Value: value,
},
)
}
}
logger.Debugf("Enqueuing task to post metrics.")
postQueue <- newPostValue(creatingValues)
}
}
}
func runChecker(checker *checks.Checker, checkReportCh chan *checks.Report, reportImmediateCh chan struct{}, quit <-chan struct{}) {
lastStatus := checks.StatusUndefined
lastMessage := ""
interval := checker.Interval()
nextInterval := time.Duration(0)
nextTime := time.Now()
for {
select {
case <-time.After(nextInterval):
report := checker.Check()
logger.Debugf("checker %q: report=%v", checker.Name, report)
// It is possible that `now` is much bigger than `nextTime` because of
// laptop sleep mode or any reason.
now := time.Now()
nextInterval = interval - (now.Sub(nextTime) % interval)
nextTime = now.Add(nextInterval)
if report.Status == checks.StatusOK && report.Status == lastStatus && report.Message == lastMessage {
// Do not report if nothing has changed
continue
}
checkReportCh <- report
// If status has changed, send it immediately
// but if the status was OK and it's first invocation of a check, do not
if report.Status != lastStatus && !(report.Status == checks.StatusOK && lastStatus == checks.StatusUndefined) {
logger.Debugf("checker %q: status has changed %v -> %v: send it immediately", checker.Name, lastStatus, report.Status)
reportImmediateCh <- struct{}{}
}
lastStatus = report.Status
lastMessage = report.Message
case <-quit:
return
}
}
}
// runCheckersLoop generates "checker" goroutines
// which run for each checker commands and one for HTTP POSTing
// the reports to Mackerel API.
func runCheckersLoop(c *Context, termCheckerCh <-chan struct{}, quit <-chan struct{}) {
checkReportCh := make(chan *checks.Report)
reportImmediateCh := make(chan struct{})
for _, checker := range c.Agent.Checkers {
go runChecker(checker, checkReportCh, reportImmediateCh, quit)
}
exit := false
for !exit {
select {
case <-time.After(1 * time.Minute):
case <-termCheckerCh:
logger.Debugf("received 'term' chan")
exit = true
case <-reportImmediateCh:
logger.Debugf("received 'immediate' chan")
}
reports := []*checks.Report{}
DrainCheckReport:
for {
select {
case report := <-checkReportCh:
reports = append(reports, report)
default:
break DrainCheckReport
}
}
if len(reports) == 0 {
continue
}
for i, report := range reports {
logger.Debugf("reports[%d]: %#v", i, report)
}
err := c.API.ReportCheckMonitors(c.Host.ID, reports)
if err != nil {
logger.Errorf("ReportCheckMonitors: %s", err)
// queue back the reports
go func() {
for _, report := range reports {
logger.Debugf("queue back report: %#v", report)
checkReportCh <- report
}
}()
}
}
}
// collectHostSpecs collects host specs (correspond to "name", "meta", "interfaces" and "customIdentifier" fields in API v0)
func collectHostSpecs() (string, map[string]interface{}, []spec.NetInterface, string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", nil, nil, "", fmt.Errorf("failed to obtain hostname: %s", err.Error())
}
specGens := specGenerators()
cGen := spec.SuggestCloudGenerator()
if cGen != nil {
specGens = append(specGens, cGen)
}
meta := spec.Collect(specGens)
var customIdentifier string
if cGen != nil {
customIdentifier, err = cGen.SuggestCustomIdentifier()
if err != nil {
logger.Warningf("Error while suggesting custom identifier. err: %s", err.Error())
}
}
interfaces, err := interfaceGenerator().Generate()
if err != nil {
return "", nil, nil, "", fmt.Errorf("failed to collect interfaces: %s", err.Error())
}
return hostname, meta, interfaces, customIdentifier, nil
}
// UpdateHostSpecs updates the host information that is already registered on Mackerel.
func (c *Context) UpdateHostSpecs() {
logger.Debugf("Updating host specs...")
hostname, meta, interfaces, customIdentifier, err := collectHostSpecs()
if err != nil {
logger.Errorf("While collecting host specs: %s", err)
return
}
err = c.API.UpdateHost(c.Host.ID, mackerel.HostSpec{
Name: hostname,
Meta: meta,
Interfaces: interfaces,
RoleFullnames: c.Config.Roles,
Checks: c.Config.CheckNames(),
DisplayName: c.Config.DisplayName,
CustomIdentifier: customIdentifier,
})
if err != nil {
logger.Errorf("Error while updating host specs: %s", err)
} else {
logger.Debugf("Host specs sent.")
}
}
// Prepare sets up API and registers the host data to the Mackerel server.
// Use returned values to call Run().
func Prepare(conf *config.Config) (*Context, error) {
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose)
if err != nil {
return nil, fmt.Errorf("Failed to prepare an api: %s", err.Error())
}
host, err := prepareHost(conf, api)
if err != nil {
return nil, fmt.Errorf("Failed to prepare host: %s", err.Error())
}
return &Context{
Agent: NewAgent(conf),
Config: conf,
Host: host,
API: api,
CustomIdentifierHosts: prepareCustomIdentiferHosts(conf, api),
}, nil
}
// RunOnce collects specs and metrics, then output them to stdout.
func RunOnce(conf *config.Config) error {
graphdefs, hostSpec, metrics, err := runOncePayload(conf)
if err != nil {
return err
}
json, err := json.Marshal(map[string]interface{}{
"host": hostSpec,
"metrics": metrics,
})
if err != nil {
logger.Warningf("Error while marshaling graphdefs: err = %s, graphdefs = %s.", err.Error(), graphdefs)
return err
}
fmt.Println(string(json))
return nil
}
func runOncePayload(conf *config.Config) ([]mackerel.CreateGraphDefsPayload, *mackerel.HostSpec, *agent.MetricsResult, error) {
hostname, meta, interfaces, customIdentifier, err := collectHostSpecs()
if err != nil {
logger.Errorf("While collecting host specs: %s", err)
return nil, nil, nil, err
}
origInterval := metricsInterval
metricsInterval = 1 * time.Second
defer func() {
metricsInterval = origInterval
}()
ag := NewAgent(conf)
graphdefs := ag.CollectGraphDefsOfPlugins()
metrics := ag.CollectMetrics(time.Now())
return graphdefs, &mackerel.HostSpec{
Name: hostname,
Meta: meta,
Interfaces: interfaces,
RoleFullnames: conf.Roles,
Checks: conf.CheckNames(),
DisplayName: conf.DisplayName,
CustomIdentifier: customIdentifier,
}, metrics, nil
}
// NewAgent creates a new instance of agent.Agent from its configuration conf.
func NewAgent(conf *config.Config) *agent.Agent {
return &agent.Agent{
MetricsGenerators: prepareGenerators(conf),
PluginGenerators: pluginGenerators(conf),
Checkers: createCheckers(conf),
}
}
// Run starts the main metric collecting logic and this function will never return.
func Run(c *Context, termCh chan struct{}) error {
logger.Infof("Start: apibase = %s, hostName = %s, hostID = %s", c.Config.Apibase, c.Host.Name, c.Host.ID)
err := loop(c, termCh)
if err == nil && c.Config.HostStatus.OnStop != "" {
// TODO error handling. support retire(?)
e := c.API.UpdateHostStatus(c.Host.ID, c.Config.HostStatus.OnStop)
if e != nil {
logger.Errorf("Failed update host status on stop: %s", e)
}
}
return err
}
func createCheckers(conf *config.Config) []*checks.Checker {
checkers := []*checks.Checker{}
for name, pluginConfig := range conf.Plugin["checks"] {
checker := &checks.Checker{
Name: name,
Config: pluginConfig,
}
logger.Debugf("Checker created: %v", checker)
checkers = append(checkers, checker)
}
return checkers
}
func prepareGenerators(conf *config.Config) []metrics.Generator {
diagnostic := conf.Diagnostic
generators := metricsGenerators(conf)
if diagnostic {
generators = append(generators, &metrics.AgentGenerator{})
}
return generators
}