-
Notifications
You must be signed in to change notification settings - Fork 28
/
register.go
65 lines (57 loc) · 1.19 KB
/
register.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
package gops_client
import (
"gops/gops-common"
"fmt"
"github.com/shirou/gopsutil/load"
"strconv"
"time"
"runtime"
"github.com/shirou/gopsutil/cpu"
)
const (
INTERVAL = 1 * time.Second
TTL int64 = 3
)
func NodePortInfo() {
t := time.NewTicker(INTERVAL)
for {
select {
case <-t.C:
prefix := "/register/nodes/" + common.GetConfig().Server.Host
port := strconv.Itoa(common.GetConfig().Server.Port)
if _, err := common.EtcdPutWithLeast(prefix, port, TTL); err != nil {
common.Logger().Print(err)
}
}
}
}
func NodeLoadInfo() {
cpus := runtime.NumCPU()
t := time.NewTicker(INTERVAL)
prefix := fmt.Sprintf(
"%s/%s/%s",
"/cron",
common.GetConfig().Server.Env,
common.GetConfig().Server.Host,
)
var (
loadStat *load.AvgStat
percent []float64
percent1 float64
loadP1 float64
loadP string
)
for {
select {
case <-t.C:
loadStat, _ = load.Avg()
percent, _ = cpu.Percent(1*time.Second, false)
percent1 = percent[0]
loadP1 = loadStat.Load1 / float64(cpus) * percent1
loadP = strconv.FormatFloat(loadP1, 'f', 2, 64)
if _, err := common.EtcdPutWithLeast(prefix, loadP, TTL); err != nil {
common.Logger().Print(err)
}
}
}
}