-
Notifications
You must be signed in to change notification settings - Fork 1
/
host_metrics.go
162 lines (145 loc) · 4.59 KB
/
host_metrics.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
/*
* Copyright [2020] Sergey Kudasov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package loadgen
import (
"fmt"
"github.com/mackerelio/go-osstat/memory"
"github.com/mackerelio/go-osstat/network"
"time"
"github.com/mackerelio/go-osstat/cpu"
"github.com/rcrowley/go-metrics"
)
// GetCPU get user + system cpu used
func (m *HostMetrics) GetCPU() int64 {
before, err := cpu.Get()
if err != nil {
log.Info("[ OS Metrics ] failed to get cpu Metrics")
return 0
}
time.Sleep(time.Duration(1) * time.Second)
after, err := cpu.Get()
if err != nil {
log.Info("[ OS Metrics ] failed to get cpu Metrics")
return 0
}
total := float64(after.Total - before.Total)
// user + system
return int64(100 - float64(after.Idle-before.Idle)/total*100)
}
func (m *HostMetrics) SelectNetworkInterface(stats []network.Stats) *network.Stats {
for _, nd := range stats {
if nd.Name == m.networkInterface {
return &nd
}
}
log.Fatalf("no interface found, interface %s doesn't exist", m.networkInterface)
return nil
}
// GetNetwork get rx/tx for particular interface
func (m *HostMetrics) GetNetwork() (int64, int64) {
before, err := network.Get()
if err != nil {
log.Info("[ OS Metrics ] failed to get network Metrics")
return 0, 0
}
beforeData := m.SelectNetworkInterface(before)
time.Sleep(time.Duration(1) * time.Second)
after, err := network.Get()
if err != nil {
log.Info("[ OS Metrics ] failed to get network Metrics")
return 0, 0
}
afterData := m.SelectNetworkInterface(after)
totalRx := float64(afterData.RxBytes - beforeData.RxBytes)
totalTx := float64(afterData.TxBytes - beforeData.TxBytes)
return int64(totalRx), int64(totalTx)
}
// GetMem get all mem and swap used/free/total stats
func (m *HostMetrics) GetMem() *memory.Stats {
mem, err := memory.Get()
if err != nil {
log.Info("[ OS Metrics ] failed to get memory Metrics")
return nil
}
return mem
}
type HostMetrics struct {
hostPrefix string
graphiteUrl string
flushDuration time.Duration
networkInterface string
cpuUserSystemPercent metrics.Gauge
memTotal metrics.Gauge
memFree metrics.Gauge
memUsed metrics.Gauge
memCached metrics.Gauge
memSwapTotal metrics.Gauge
memSwapUsed metrics.Gauge
memSwapFree metrics.Gauge
rx metrics.Gauge
tx metrics.Gauge
}
// NewOsMetrics
func NewHostOSMetrics(hostPrefix string, graphiteUrl string, flushDurationSec int, networkInterface string) *HostMetrics {
return &HostMetrics{
hostPrefix: hostPrefix,
graphiteUrl: graphiteUrl,
flushDuration: time.Duration(flushDurationSec),
networkInterface: networkInterface,
cpuUserSystemPercent: RegisterGauge("cpu_used"),
memTotal: RegisterGauge("mem_total"),
memFree: RegisterGauge("mem_free"),
memUsed: RegisterGauge("mem_used"),
memCached: RegisterGauge("mem_cached"),
memSwapTotal: RegisterGauge("mem_swap_total"),
memSwapUsed: RegisterGauge("mem_swap_used"),
memSwapFree: RegisterGauge("mem_swap_free"),
rx: RegisterGauge(fmt.Sprintf("net_%s_rx", networkInterface)),
tx: RegisterGauge(fmt.Sprintf("net_%s_tx", networkInterface)),
}
}
// Watch updates generator host Metrics
func (m *HostMetrics) Watch(intervalSec int) {
go func() {
ticker := time.NewTicker(time.Duration(intervalSec) * time.Second)
for {
select {
case <-ticker.C:
cpuUserSystem := m.GetCPU()
m.cpuUserSystemPercent.Update(cpuUserSystem)
mem := m.GetMem()
m.memTotal.Update(int64(mem.Total))
m.memFree.Update(int64(mem.Free))
m.memUsed.Update(int64(mem.Used))
m.memCached.Update(int64(mem.Cached))
m.memSwapTotal.Update(int64(mem.SwapTotal))
m.memSwapUsed.Update(int64(mem.SwapUsed))
m.memSwapFree.Update(int64(mem.SwapFree))
rx, tx := m.GetNetwork()
m.rx.Update(rx)
m.tx.Update(tx)
}
}
}()
}
// RegisterGauge registers gauge metric to graphite
func RegisterGauge(name string) metrics.Gauge {
g := metrics.NewGauge()
if err := metrics.Register(name, g); err != nil {
log.Fatal(err)
}
return g
}