-
Notifications
You must be signed in to change notification settings - Fork 14
/
monitor.go
83 lines (72 loc) · 1.81 KB
/
monitor.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
package master
import (
"fmt"
"runtime"
"time"
"github.com/gobench-io/gobench/pse"
)
// Snapshot this
var numCores int
var maxProcs int
func init() {
numCores = runtime.NumCPU()
maxProcs = runtime.GOMAXPROCS(0)
}
// Varz outputs master information at /varz
type Varz struct {
ID string `json:"server_id"`
Version string `json:"version"`
GitCommit string `json:"git_commit,omitempty"`
GoVersion string `json:"go"`
Start time.Time `json:"start"`
Now time.Time `json:"now"`
Uptime string `json:"uptime"`
Mem uint64 `json:"mem"`
Cores int `json:"cores"`
MaxProcs int `json:"gomaxprocs"`
CPU float64 `json:"cpu"`
}
// Varz returns a Varz struct containing the server information.
func (m *Master) Varz() (*Varz, error) {
varz := &Varz{
ID: m.id,
Version: m.version,
GitCommit: m.gitCommit,
GoVersion: m.goVersion,
Start: m.start,
Now: time.Now(),
Cores: numCores,
MaxProcs: maxProcs,
}
var pcpu float64
var mem uint64
if err := pse.ProcUsage(&pcpu, &mem); err != nil {
return varz, err
}
varz.CPU = pcpu
varz.Mem = mem
varz.Uptime = myUptime(varz.Now.Sub(varz.Start))
return varz, nil
}
// myUptime returns duration in year-day-hour-min-second format
// inspired by nats-server
func myUptime(d time.Duration) string {
tsecs := d / time.Second
tmins := tsecs / 60
thrs := tmins / 60
tdays := thrs / 24
tyrs := tdays / 365
if tyrs > 0 {
return fmt.Sprintf("%dy%dd%dh%dm%ds", tyrs, tdays%365, thrs%24, tmins%60, tsecs%60)
}
if tdays > 0 {
return fmt.Sprintf("%dd%dh%dm%ds", tdays, thrs%24, tmins%60, tsecs%60)
}
if thrs > 0 {
return fmt.Sprintf("%dh%dm%ds", thrs, tmins%60, tsecs%60)
}
if tmins > 0 {
return fmt.Sprintf("%dm%ds", tmins, tsecs%60)
}
return fmt.Sprintf("%ds", tsecs)
}