-
Notifications
You must be signed in to change notification settings - Fork 462
/
inspector_api.go
150 lines (130 loc) · 3.4 KB
/
inspector_api.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
package node
import (
"os"
"runtime"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/repo"
sysi "github.com/whyrusleeping/go-sysinfo"
)
type IInspector interface {
Runtime() *RuntimeInfo
Memory() (*MemoryInfo, error)
Config() *config.Config
Disk() (*DiskInfo, error)
FilecoinVersion() string
Environment() *EnvironmentInfo
}
var _ IInspector = &inspector{}
// NewInspectorAPI returns a `Inspector` used to inspect the venus node.
func NewInspectorAPI(r repo.Repo) IInspector {
return &inspector{
repo: r,
}
}
// Inspector contains information used to inspect the venus node.
type inspector struct {
repo repo.Repo
}
// AllInspectorInfo contains all information the inspector can gather.
type AllInspectorInfo struct {
Config *config.Config
Runtime *RuntimeInfo
Environment *EnvironmentInfo
Disk *DiskInfo
Memory *MemoryInfo
FilecoinVersion string
}
// RuntimeInfo contains information about the golang runtime.
type RuntimeInfo struct {
OS string
Arch string
Version string
Compiler string
NumProc int
GoMaxProcs int
NumGoRoutines int
NumCGoCalls int64
}
// EnvironmentInfo contains information about the environment filecoin is running in.
type EnvironmentInfo struct {
VENUSAPI string `json:"VENUS_API"`
VENUSPath string `json:"VENUS_PATH"`
GoPath string `json:"GOPATH"`
}
// DiskInfo contains information about disk usage and type.
type DiskInfo struct {
Free uint64
Total uint64
FSType string
}
// MemoryInfo contains information about memory usage.
type MemoryInfo struct {
Swap uint64
Virtual uint64
}
// Runtime returns infrormation about the golang runtime.
func (g *inspector) Runtime() *RuntimeInfo {
return &RuntimeInfo{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Version: runtime.Version(),
Compiler: runtime.Compiler,
NumProc: runtime.NumCPU(),
GoMaxProcs: runtime.GOMAXPROCS(0),
NumGoRoutines: runtime.NumGoroutine(),
NumCGoCalls: runtime.NumCgoCall(),
}
}
// Environment returns information about the environment filecoin is running in.
func (g *inspector) Environment() *EnvironmentInfo {
return &EnvironmentInfo{
VENUSAPI: os.Getenv("VENUS_API"),
VENUSPath: os.Getenv("VENUS_PATH"),
GoPath: os.Getenv("GOPATH"),
}
}
// Disk return information about filesystem the filecoin nodes repo is on.
func (g *inspector) Disk() (*DiskInfo, error) {
fsr, ok := g.repo.(*repo.FSRepo)
if !ok {
// we are using a in memory repo
return &DiskInfo{
Free: 0,
Total: 0,
FSType: "0",
}, nil
}
p, err := fsr.Path()
if err != nil {
return nil, err
}
dinfo, err := sysi.DiskUsage(p)
if err != nil {
return nil, err
}
return &DiskInfo{
Free: dinfo.Free,
Total: dinfo.Total,
FSType: dinfo.FsType,
}, nil
}
// Memory return information about system meory usage.
func (g *inspector) Memory() (*MemoryInfo, error) {
meminfo, err := sysi.MemoryInfo()
if err != nil {
return nil, err
}
return &MemoryInfo{
Swap: meminfo.Swap,
Virtual: meminfo.Used,
}, nil
}
// configModule return the current config values of the filecoin node.
func (g *inspector) Config() *config.Config {
return g.repo.Config()
}
// FilecoinVersion returns the version of venus.
func (g *inspector) FilecoinVersion() string {
return constants.UserVersion()
}