-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysinfo.go
100 lines (91 loc) · 2.27 KB
/
sysinfo.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
// Copyright 2021 冯立强 fenglq@tingyun.com. All rights reserved.
package tingyun3
import (
"errors"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
)
type cpuInfo struct {
User uint64
Kernel uint64
Idle uint64
IoWait uint64
}
func (i *cpuInfo) ProcessUse() uint64 {
return i.User + i.Kernel
}
func (i *cpuInfo) FullUse() uint64 {
return i.User + i.Kernel + i.Idle + i.IoWait
}
type sysInfo struct {
cpuProcess cpuInfo
cpuSystem cpuInfo
vmSize uint64
vmRss uint64
Threads int
FdSize int
err error
}
func (s *sysInfo) Init() *sysInfo {
s.err = nil
if runtime.GOOS != "linux" {
s.err = errors.New("not linux")
return nil
}
s.FdSize, s.err = elementCount("/proc/self/fd/")
s.Threads, s.err = elementCount("/proc/self/task/")
bytes, err := ioutil.ReadFile("/proc/self/stat")
if err != nil {
s.err = err
return nil
}
array := strings.Split(string(bytes), " ")
if len(array) < 15 {
s.err = errors.New("Unknown or UnSupport linux version")
return nil
}
s.cpuProcess.User, _ = strconv.ParseUint(array[13], 10, 64)
s.cpuProcess.Kernel, _ = strconv.ParseUint(array[14], 10, 64)
bytes, err = ioutil.ReadFile("/proc/stat")
if err != nil {
s.err = err
return nil
}
array = strings.Split(strings.Split(string(bytes), "\n")[0], " ")
s.cpuSystem.User, _ = strconv.ParseUint(array[1], 10, 64)
s.cpuSystem.Kernel, _ = strconv.ParseUint(array[3], 10, 64)
s.cpuSystem.Idle, _ = strconv.ParseUint(array[4], 10, 64)
s.cpuSystem.IoWait, _ = strconv.ParseUint(array[5], 10, 64)
bytes, err = ioutil.ReadFile("/proc/self/status")
if err != nil {
s.err = err
return nil
}
array = strings.Split(string(bytes), "\n")
for _, it := range array {
a := strings.Split(it, ":")
if a[0] == "VmSize" {
valarray := strings.Split(strings.Trim(strings.Trim(a[1], "\t"), " "), " ")
s.vmSize, _ = strconv.ParseUint(valarray[0], 10, 64)
} else if a[0] == "VmRSS" {
valarray := strings.Split(strings.Trim(strings.Trim(a[1], "\t"), " "), " ")
s.vmRss, _ = strconv.ParseUint(valarray[0], 10, 64)
}
}
return s
}
func elementCount(dir string) (int, error) {
file, err := os.Open(dir)
if err != nil {
return -1, err
}
defer file.Close()
names, err := file.Readdirnames(0)
if err != nil {
return -1, err
}
return len(names), nil
}