forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemstat_linux.go
163 lines (141 loc) · 3.32 KB
/
systemstat_linux.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
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2013 Phillip Bond
// Licensed under the MIT License
// see file LICENSE
// +build linux
package systemstat
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"strconv"
"strings"
"syscall"
"time"
)
func getUptime(procfile string) (uptime UptimeSample) {
// read in whole uptime file with cpu usage information ;"/proc/uptime"
contents, err := ioutil.ReadFile(procfile)
uptime.Time = time.Now()
if err != nil {
return
}
reader := bufio.NewReader(bytes.NewBuffer(contents))
line, _, err := reader.ReadLine()
fields := strings.Fields(string(line))
val, numerr := strconv.ParseFloat(fields[0], 64)
if numerr != nil {
return
}
uptime.Uptime = val
return
}
func getLoadAvgSample(procfile string) (samp LoadAvgSample) {
// read in whole loadavg file with cpu usage information ;"/proc/loadavg"
contents, err := ioutil.ReadFile(procfile)
samp.Time = time.Now()
if err != nil {
return
}
reader := bufio.NewReader(bytes.NewBuffer(contents))
line, _, err := reader.ReadLine()
fields := strings.Fields(string(line))
for i := 0; i < 3; i++ {
val, numerr := strconv.ParseFloat(fields[i], 64)
if numerr != nil {
return
}
switch i {
case 0:
samp.One = val
case 1:
samp.Five = val
case 2:
samp.Fifteen = val
}
}
return
}
func getMemSample(procfile string) (samp MemSample) {
want := map[string]bool{
"Buffers:": true,
"Cached:": true,
"MemTotal:": true,
"MemFree:": true,
"MemUsed:": true,
"SwapTotal:": true,
"SwapFree:": true,
"SwapUsed:": true}
// read in whole meminfo file with cpu usage information ;"/proc/meminfo"
contents, err := ioutil.ReadFile(procfile)
samp.Time = time.Now()
if err != nil {
return
}
reader := bufio.NewReader(bytes.NewBuffer(contents))
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
fields := strings.Fields(string(line))
fieldName := fields[0]
_, ok := want[fieldName]
if ok && len(fields) == 3 {
val, numerr := strconv.ParseUint(fields[1], 10, 64)
if numerr != nil {
return
}
switch fieldName {
case "Buffers:":
samp.Buffers = val
case "Cached:":
samp.Cached = val
case "MemTotal:":
samp.MemTotal = val
case "MemFree:":
samp.MemFree = val
case "SwapTotal:":
samp.SwapTotal = val
case "SwapFree:":
samp.SwapFree = val
}
}
}
samp.MemUsed = samp.MemTotal - samp.MemFree
samp.SwapUsed = samp.SwapTotal - samp.SwapFree
return
}
func getProcCPUSample() (s ProcCPUSample) {
var processInfo syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &processInfo)
s.Time = time.Now()
s.ProcMemUsedK = int64(processInfo.Maxrss)
s.User = float64(processInfo.Utime.Usec)/1000000 + float64(processInfo.Utime.Sec)
s.System = float64(processInfo.Stime.Usec)/1000000 + float64(processInfo.Stime.Sec)
s.Total = s.User + s.System
return
}
func getCPUSample(procfile string) (samp CPUSample) {
// read in whole proc file with cpu usage information ; "/proc/stat"
contents, err := ioutil.ReadFile(procfile)
samp.Time = time.Now()
if err != nil {
return
}
reader := bufio.NewReader(bytes.NewBuffer(contents))
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
fields := strings.Fields(string(line))
if len(fields) > 0 {
fieldName := fields[0]
if fieldName == "cpu" {
parseCPUFields(fields, &samp)
}
}
}
return
}