forked from yyyeerbo/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_stats.go
117 lines (108 loc) · 2.92 KB
/
pod_stats.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
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
pb "github.com/kata-containers/agent/protocols/grpc"
)
const (
VMSTAT = "/proc/vmstat"
STAT = "/proc/stat"
SCHEDSTAT = "/proc/schedstat"
MEMINFO = "/proc/meminfo"
LOADAVG = "/proc/loadavg"
DISKSTATS = "/proc/diskstats"
NET_DEV = "/proc/net/dev"
NET_SNMP = "/proc/net/snmp"
APACHERT = "/tmp/apachert.mmap"
TCP = "/proc/net/tcp"
NETSTAT = "/proc/net/netstat"
SOCKSTAT = "/proc/net/sockstat"
BUDDYINFO = "/proc/buddyinfo"
PROCSTAT = "/sys/fs/cgroup/cpuacct/cpuacct.proc_stat"
MEMCGSTAT = "/sys/fs/cgroup/memory/memory.stat"
MEMCGFAILCNT = "/sys/fs/cgroup/memory/memory.failcnt"
CGTASKS = "/sys/fs/cgroup/freezer/tasks"
SOFTIRQS = "/proc/softirqs"
INTERRUPT = "/proc/interrupts"
)
func GetFileContent1MBAsStringLines(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
agentLog.WithError(err).Errorf("open file: %v ", filePath)
return nil, err
}
defer file.Close()
var chunk []byte
buffer := make([]byte, 1024*1024)
n, err := file.Read(buffer)
if err != nil && err != io.EOF {
agentLog.WithError(err).Errorf("read file: %v ", filePath)
return nil, err
}
chunk = append(chunk, buffer[:n]...)
s := string(chunk)
result := []string{}
for _, lineStr := range strings.Split(s, "\n") {
lineStr = strings.TrimSpace(lineStr)
result = append(result, lineStr)
}
return result, nil
}
func getPodBuddyStats() (map[uint16]*pb.PodBuddyStats, error) {
strLines, err := GetFileContent1MBAsStringLines(BUDDYINFO)
if err != nil {
return nil, err
}
pbs := make(map[uint16]*pb.PodBuddyStats)
for _, eachLine := range strLines {
if strings.HasPrefix(eachLine, "Node") {
fields := strings.Fields(eachLine)
node, err := strconv.Atoi(strings.Trim(fields[1], ","))
if err != nil {
return nil, err
}
tn := uint16(node)
statInt32Values := []int32{}
for _, each := range fields[4:] {
intValue, err := strconv.Atoi(each)
if err != nil {
return nil, err
}
statInt32Values = append(statInt32Values, int32(intValue))
}
if len(statInt32Values) < 11 {
return nil, fmt.Errorf("invalid buddyinfo")
}
if _, ok := pbs[tn]; !ok {
pbs[tn] = &pb.PodBuddyStats{
Order0: 0,
Order1: 0,
Order2: 0,
Order3: 0,
Order4: 0,
Order5: 0,
Order6: 0,
Order7: 0,
Order8: 0,
Order9: 0,
Order10: 0,
}
}
pbs[tn].Order0 += statInt32Values[0]
pbs[tn].Order1 += statInt32Values[1]
pbs[tn].Order2 += statInt32Values[2]
pbs[tn].Order3 += statInt32Values[3]
pbs[tn].Order4 += statInt32Values[4]
pbs[tn].Order5 += statInt32Values[5]
pbs[tn].Order6 += statInt32Values[6]
pbs[tn].Order7 += statInt32Values[7]
pbs[tn].Order8 += statInt32Values[8]
pbs[tn].Order9 += statInt32Values[9]
pbs[tn].Order10 += statInt32Values[10]
}
}
return pbs, err
}