forked from DataDog/datadog-agent
/
load.go
89 lines (75 loc) · 2.43 KB
/
load.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
// +build !windows
package system
import (
"fmt"
"github.com/shirou/gopsutil/load"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const loadCheckName = "load"
// For testing purpose
var loadAvg = load.Avg
// LoadCheck doesn't need additional fields
type LoadCheck struct {
core.CheckBase
nbCPU int32
}
// Run executes the check
func (c *LoadCheck) Run() error {
sender, err := aggregator.GetSender(c.ID())
if err != nil {
return err
}
avg, err := loadAvg()
if err != nil {
log.Errorf("system.LoadCheck: could not retrieve load stats: %s", err)
return err
}
sender.Gauge("system.load.1", avg.Load1, "", nil)
sender.Gauge("system.load.5", avg.Load5, "", nil)
sender.Gauge("system.load.15", avg.Load15, "", nil)
cpus := float64(c.nbCPU)
sender.Gauge("system.load.norm.1", avg.Load1/cpus, "", nil)
sender.Gauge("system.load.norm.5", avg.Load5/cpus, "", nil)
sender.Gauge("system.load.norm.15", avg.Load15/cpus, "", nil)
sender.Commit()
return nil
}
// Configure the CPU check
func (c *LoadCheck) Configure(data integration.Data, initConfig integration.Data, source string) error {
err := c.CommonConfigure(data, source)
if err != nil {
return err
}
// NOTE: This check is disabled on windows - so the following doesn't apply
// currently:
//
// This runs before the python checks, so we should be good, but cpuInfo()
// on windows initializes COM to the multithreaded model. Therefore,
// if a python check has run on this native windows thread prior and
// CoInitialized() the thread to a different model (ie. single-threaded)
// This will cause cpuInfo() to fail.
info, err := cpuInfo()
if err != nil {
return fmt.Errorf("system.LoadCheck: could not query CPU info - %v", err)
}
for _, i := range info {
c.nbCPU += i.Cores
}
return nil
}
func loadFactory() check.Check {
return &LoadCheck{
CheckBase: core.NewCheckBase(loadCheckName),
}
}
func init() {
core.RegisterCheck(loadCheckName, loadFactory)
}