-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
240 lines (222 loc) · 5.98 KB
/
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package mikrus
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
type Stats struct {
Memory Memory `json:"memory"`
DiskSpace DiskSpace `json:"disk_space"`
Uptime Uptime `json:"uptime"`
Processes []ProcessInfo `json:"processes"`
}
type Memory struct {
Total int `json:"total"`
Used int `json:"used"`
Free int `json:"free"`
Shared int `json:"shared"`
Cache int `json:"cache"`
Available int `json:"available"`
SwapTotal int `json:"swap_total"`
SwapUsed int `json:"swap_used"`
SwapFree int `json:"swap_free"`
}
func ParseMemoryUsage(s string) (Memory, error) {
var (
total, used, free, shared, cache, available int
swapTotal, swapUsed, swapFree int
)
table := strings.Split(s, "\n")
if len(table) < 3 {
return Memory{}, errors.New("parsing `free` command output")
}
memory := strings.TrimSpace(table[1])
swap := strings.TrimSpace(table[2])
_, err := fmt.Sscanf(memory, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &cache, &available)
if err != nil {
return Memory{}, fmt.Errorf("incorrect input data for `free` command: %w", err)
}
_, err = fmt.Sscanf(swap, "Swap: %d %d %d", &swapTotal, &swapUsed, &swapFree)
if err != nil {
return Memory{}, fmt.Errorf("incorrect input data for `free` command: %w", err)
}
return Memory{
Total: total,
Used: used,
Free: free,
Shared: shared,
Cache: cache,
Available: available,
SwapFree: swapFree,
SwapUsed: swapUsed,
SwapTotal: swapTotal,
}, nil
}
type DiskSpace struct {
Filesystem string `json:"filesystem"`
Size string `json:"size"`
Used string `json:"used"`
Available string `json:"available"`
Usage string `json:"usage"`
MountedOn string `json:"mounted_on"`
}
type ProcessInfo struct {
User string
PID uint64
CPUPercent float64
MemoryPercent float64
VirtualMemorySize uint64
ResidentSetSize uint64
TTY string
State string
Start string
CPUTime string
Command string
}
// Format: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
var psRE = regexp.MustCompile(`^(\w+) +(\d+) +(\d+\.\d+) +(\d+\.\d+) +(\d+) +(\d+) +(.) +(\w+) +([\d:]+) +([\d:]+) +(.+)$`)
func ParsePS(s string) ([]ProcessInfo, error) {
lines := strings.Split(s, "\n")
if len(lines) < 2 {
return nil, fmt.Errorf("invalid input line: %s", s)
}
list := make([]ProcessInfo, 0, len(lines)-1)
const (
USER = iota + 1
PID
CPUPERCENT
MEMPERCENT
VSZ
RSS
TTY
STAT
START
TIME
COMMAND
)
for _, line := range lines[1 : len(lines)-1] {
matches := psRE.FindStringSubmatch(line)
if len(matches) != 12 {
return nil, fmt.Errorf("parsing %q", line)
}
pid, err := strconv.ParseUint(matches[PID], 10, 64)
if err != nil {
return nil, err
}
cpuPercent, err := strconv.ParseFloat(matches[CPUPERCENT], 64)
if err != nil {
return nil, err
}
memPercent, err := strconv.ParseFloat(matches[MEMPERCENT], 64)
if err != nil {
return nil, err
}
vsz, err := strconv.ParseUint(matches[VSZ], 10, 64)
if err != nil {
return nil, err
}
rss, err := strconv.ParseUint(matches[RSS], 10, 64)
if err != nil {
return nil, err
}
list = append(list, ProcessInfo{
User: matches[USER],
PID: pid,
CPUPercent: cpuPercent,
MemoryPercent: memPercent,
VirtualMemorySize: vsz,
ResidentSetSize: rss,
TTY: matches[TTY],
State: matches[STAT],
Start: matches[START],
CPUTime: matches[TIME],
Command: matches[COMMAND],
})
}
return list, nil
}
func ParseDiskSpace(s string) (DiskSpace, error) {
var fileSystem, size, used, avail, usage, mountedOn string
table := strings.Split(s, "\n")
if len(table) < 2 {
return DiskSpace{}, errors.New("parsing `df` command output")
}
values := strings.TrimSpace(table[1])
_, err := fmt.Sscanf(values, "%s %s %s %s %s %s", &fileSystem, &size, &used, &avail, &usage, &mountedOn)
if err != nil {
return DiskSpace{}, fmt.Errorf("incorrect input data for `df` command: %w", err)
}
return DiskSpace{
Filesystem: fileSystem,
Size: size,
Used: used,
Available: avail,
Usage: usage,
MountedOn: mountedOn,
}, nil
}
type Uptime struct {
Time string `json:"time"`
Uptime time.Duration `json:"days_up"`
Users int `json:"users_logged_in"`
CPUload1min float64 `json:"load_average_1_min"`
CPUload5min float64 `json:"load_average_5_min"`
CPUload15min float64 `json:"load_average_15_min"`
}
var uptimeRE = regexp.MustCompile(`up (\d+) days, +(\d+):(\d\d), +(\d+) users, +load average: (\d+\.\d\d), (\d+\.\d\d), (\d+\.\d\d)`)
func ParseUptime(s string) (Uptime, error) {
matches := uptimeRE.FindStringSubmatch(s)
if len(matches) != 8 {
return Uptime{}, fmt.Errorf("parsing input %q", s)
}
const (
UPDAYS = iota + 1
UPHOURS
UPMINUTES
USERS
LOAD1MIN
LOAD5MIN
LOAD15MIN
)
upDays, err := strconv.Atoi(matches[UPDAYS])
if err != nil {
return Uptime{}, err
}
upHours, err := strconv.Atoi(matches[UPHOURS])
if err != nil {
return Uptime{}, err
}
upMinutes, err := strconv.Atoi(matches[UPMINUTES])
if err != nil {
return Uptime{}, err
}
up := 24 * time.Hour * time.Duration(upDays)
up += time.Duration(upHours) * time.Hour
up += time.Duration(upMinutes) * time.Minute
users, err := strconv.Atoi(matches[USERS])
if err != nil {
return Uptime{}, err
}
load1min, err := strconv.ParseFloat(matches[LOAD1MIN], 64)
if err != nil {
return Uptime{}, err
}
load5min, err := strconv.ParseFloat(matches[LOAD5MIN], 64)
if err != nil {
return Uptime{}, err
}
load15min, err := strconv.ParseFloat(matches[LOAD15MIN], 64)
if err != nil {
return Uptime{}, err
}
return Uptime{
Uptime: up,
Users: users,
CPUload1min: load1min,
CPUload5min: load5min,
CPUload15min: load15min,
}, nil
}