/
monitor_cpu.go
96 lines (85 loc) · 2.22 KB
/
monitor_cpu.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
package someta
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/shirou/gopsutil/cpu"
)
func init() {
registerMonitor("cpu", NewCPUMonitor)
}
// CPUMetadata encapsulates what it says
type CPUMetadata struct {
Timestamp time.Time `json:"timestamp"`
CPU map[string]float64 `json:"cpuidle"`
}
// NewCPUMonitor creates and returns a new CPUMonitor
func NewCPUMonitor() MetadataGenerator {
return new(CPUMonitor)
}
// CPUMonitor collects cpu usage metadata
type CPUMonitor struct {
Monitor
Data []CPUMetadata `json:"data"`
}
// DefaultConfig returns a default config or nil if no default
func (c *CPUMonitor) DefaultConfig() *MonitorConf {
conf := &MonitorConf{Kind: "cpu",
Interval: 1 * time.Second}
return conf
}
// CheckConfig does some basic sanity checking on the configuration
func (c *CPUMonitor) CheckConfig(name string, conf MonitorConf) {
if conf.Interval < time.Second*1 {
log.Fatalf("%s: interval %v too short", name, conf.Interval)
}
if len(conf.Device) > 0 {
log.Fatalf("%s: device config inappropriate", name)
}
}
// Init initializes a CPUMonitor
func (c *CPUMonitor) Init(name string, verbose bool, defaultInterval time.Duration, config MonitorConf) error {
c.CheckConfig(name, config)
c.baseInit(name, verbose, defaultInterval)
return nil
}
// Run runs the cpu monitor; this should be invoked in a goroutine
func (c *CPUMonitor) Run(ctx context.Context) error {
ticker := time.NewTicker(c.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
if c.verbose {
fmt.Printf("%s stopping\n", c.Name)
}
return nil
case t := <-ticker.C:
cpuval, err := cpu.Percent(0, true)
if err != nil {
log.Printf("%s: %v\n", c.Name, err)
} else {
if c.verbose {
log.Printf("%s: %v\n", c.Name, cpuval)
}
cpuidle := make(map[string]float64)
for i, pval := range cpuval {
cpuidle[fmt.Sprintf("cpu%d_idle", i)] = 100.0 - pval
}
c.mutex.Lock()
c.Data = append(c.Data, CPUMetadata{t, cpuidle})
c.mutex.Unlock()
}
}
}
}
// Flush will write any current metadata to the writer
func (c *CPUMonitor) Flush(encoder *json.Encoder) error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := encoder.Encode(c)
c.Data = nil
return err
}