forked from monicasarbu/topbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigar.go
284 lines (230 loc) · 5.82 KB
/
sigar.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"fmt"
"time"
"github.com/elastic/gosigar"
)
type SystemLoad struct {
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
}
type CpuTimes struct {
User uint64 `json:"user"`
UserPercent float64 `json:"user_p"`
Nice uint64 `json:"nice"`
System uint64 `json:"system"`
SystemPercent float64 `json:"system_p"`
Idle uint64 `json:"idle"`
IOWait uint64 `json:"iowait"`
Irq uint64 `json:"irq"`
SoftIrq uint64 `json:"softirq"`
Steal uint64 `json:"steal"`
}
type MemStat struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
UsedPercent float64 `json:"used_p"`
ActualUsed uint64 `json:"actual_used"`
ActualFree uint64 `json:"actual_free"`
}
type ProcMemStat struct {
Size uint64 `json:"size"`
Rss uint64 `json:"rss"`
RssPercent float64 `json:"rss_p"`
Share uint64 `json:"share"`
}
type ProcCpuTime struct {
User uint64 `json:"user"`
UserPercent float64 `json:"user_p"`
System uint64 `json:"system"`
Total uint64 `json:"total"`
Start string `json:"start_time"`
}
type Process struct {
Pid int `json:"pid"`
Ppid int `json:"ppid"`
Name string `json:"name"`
State string `json:"state"`
Mem *ProcMemStat `json:"mem"`
Cpu *ProcCpuTime `json:"cpu"`
ctime time.Time
}
type FileSystemStat struct {
DevName string `json:"device_name"`
Total uint64 `json:"total"`
Used uint64 `json:"used"`
UsedPercent float64 `json:"used_p"`
Free uint64 `json:"free"`
Avail uint64 `json:"avail"`
Files uint64 `json:"files"`
FreeFiles uint64 `json:"free_files"`
Mount string `json:"mount_point"`
ctime time.Time
}
func (f *FileSystemStat) String() string {
return fmt.Sprintf("device name: %s, total: %d, used %d, used pct %.2f, free: %d, avail: %d, files: %d, free files: %d, mount: %s",
f.DevName, f.Total, f.Used, f.UsedPercent, f.Free, f.Avail, f.Files, f.FreeFiles, f.Mount)
}
func (p *Process) String() string {
return fmt.Sprintf("pid: %d, ppid: %d, name: %s, state: %s, mem: %s, cpu: %s",
p.Pid, p.Ppid, p.Name, p.State, p.Mem.String(), p.Cpu.String())
}
func (m *ProcMemStat) String() string {
return fmt.Sprintf("%d size, %d rss, %d share", m.Size, m.Rss, m.Share)
}
func (t *ProcCpuTime) String() string {
return fmt.Sprintf("started at %s, %d total %.2f%%CPU, %d us, %d sys", t.Start, t.Total, t.UserPercent, t.User, t.System)
}
func (m *MemStat) String() string {
return fmt.Sprintf("%d total, %d used, %d actual used, %d free, %d actual free", m.Total, m.Used, m.ActualUsed,
m.Free, m.ActualFree)
}
func (t *SystemLoad) String() string {
return fmt.Sprintf("%.2f %.2f %.2f", t.Load1, t.Load5, t.Load15)
}
func (t *CpuTimes) String() string {
return fmt.Sprintf("%d user, %d system, %d nice, %d iddle, %d iowait, %d irq, %d softirq, %d steal",
t.User, t.System, t.Nice, t.Idle, t.IOWait, t.Irq, t.SoftIrq, t.Steal)
}
func GetSystemLoad() (*SystemLoad, error) {
concreteSigar := sigar.ConcreteSigar{}
avg, err := concreteSigar.GetLoadAverage()
if err != nil {
return nil, err
}
return &SystemLoad{
Load1: avg.One,
Load5: avg.Five,
Load15: avg.Fifteen,
}, nil
}
func GetCpuTimes() (*CpuTimes, error) {
cpu := sigar.Cpu{}
err := cpu.Get()
if err != nil {
return nil, err
}
return &CpuTimes{
User: cpu.User,
Nice: cpu.Nice,
System: cpu.Sys,
Idle: cpu.Idle,
IOWait: cpu.Wait,
Irq: cpu.Irq,
SoftIrq: cpu.SoftIrq,
Steal: cpu.Stolen,
}, nil
}
func GetMemory() (*MemStat, error) {
mem := sigar.Mem{}
err := mem.Get()
if err != nil {
return nil, err
}
return &MemStat{
Total: mem.Total,
Used: mem.Used,
Free: mem.Free,
ActualFree: mem.ActualFree,
ActualUsed: mem.ActualUsed,
}, nil
}
func GetSwap() (*MemStat, error) {
swap := sigar.Swap{}
err := swap.Get()
if err != nil {
return nil, err
}
return &MemStat{
Total: swap.Total,
Used: swap.Used,
Free: swap.Free,
}, nil
}
func Pids() ([]int, error) {
pids := sigar.ProcList{}
err := pids.Get()
if err != nil {
return nil, err
}
return pids.List, nil
}
func getProcState(b byte) string {
switch b {
case 'S':
return "sleeping"
case 'R':
return "running"
case 'D':
return "idle"
case 'T':
return "stopped"
case 'Z':
return "zombie"
}
return "unknown"
}
func GetProcess(pid int) (*Process, error) {
state := sigar.ProcState{}
mem := sigar.ProcMem{}
cpu := sigar.ProcTime{}
err := state.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting state info: %v", err)
}
err = mem.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting mem info: %v", err)
}
err = cpu.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting cpu info: %v", err)
}
proc := Process{
Pid: pid,
Ppid: state.Ppid,
Name: state.Name,
State: getProcState(byte(state.State)),
Mem: &ProcMemStat{
Size: mem.Size,
Rss: mem.Resident,
Share: mem.Share,
},
Cpu: &ProcCpuTime{
Start: cpu.FormatStartTime(),
Total: cpu.Total,
User: cpu.User,
System: cpu.Sys,
},
}
proc.ctime = time.Now()
return &proc, nil
}
func GetFileSystemList() ([]sigar.FileSystem, error) {
fss := sigar.FileSystemList{}
err := fss.Get()
if err != nil {
return nil, err
}
return fss.List, nil
}
func GetFileSystemStat(fs sigar.FileSystem) (*FileSystemStat, error) {
stat := sigar.FileSystemUsage{}
err := stat.Get(fs.DirName)
if err != nil {
return nil, err
}
filesystem := FileSystemStat{
DevName: fs.DevName,
Total: stat.Total,
Free: stat.Free,
Avail: stat.Avail,
Used: stat.Used,
Files: stat.Files,
FreeFiles: stat.FreeFiles,
Mount: fs.DirName,
}
return &filesystem, nil
}