-
Notifications
You must be signed in to change notification settings - Fork 3
/
sysinfo.go
367 lines (304 loc) · 8.5 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)
type SysInfo struct {
Uname *Utsname `json:"uname"` // utsname struct
LongBit int `json:"long_bit"` // getconf LONG_BIT
Uptime time.Duration `json:"uptime"` // time since boot
Loadavg LoadAverage `json:"loadavg"` // 1, 5, and 15 minute load averages
Mem MemStat `json:"ram"` // memory stat (total/free/buffers/cached) in kB
Swap SwapStat `json:"swap"` // swap stat (total/free) in kB
Users []LoggedUser `json:"users"` // logged-in users from /var/run/utmp
Disks []BlockDev `json:"disks"` // some disks stats
}
type Utsname struct {
Sysname string `json:"sysname"`
Nodename string `json:"nodename"`
Release string `json:"release"`
Version string `json:"version"`
Machine string `json:"machine"`
Domainname string `json:"domain"`
}
type LoadAverage struct {
One float64 `json:"1m"`
Five float64 `json:"5m"`
Fifteen float64 `json:"15m"`
}
type MemStat struct {
Total uint64 `json:"total"`
Free uint64 `json:"free"`
Buffers uint64 `json:"buffers"`
Cached uint64 `json:"cached"`
FreeTotal uint64 `json:"free_total"`
}
type SwapStat struct {
Total uint64 `json:"total"`
Free uint64 `json:"free"`
}
type LoggedUser struct {
Name string `json:"name"`
Device string `json:"device"`
Host string `json:"host"`
LoginTime int64 `json:"login_time"`
}
type BlockDev struct {
Path string `json:"name"`
IsMounted bool `json:"is_mounted"`
Mountpoint string `json:"mountpoint"`
SizeTotal int64 `json:"size_total"` // in kB
SizeUsed int64 `json:"size_used"` // in kB
SizeAvail int64 `json:"size_avail"` // in kB
}
// Values for Utmp.Type field
type Utype int16
// Type for ut_exit, below
const (
Empty Utype = iota // record does not contain valid info (formerly known as UT_UNKNOWN on Linux)
RunLevel = iota // change in system run-level (see init(8))
BootTime = iota // time of system boot (in ut_tv)
NewTime = iota // time after system clock change (in ut_tv)
OldTime = iota // time before system clock change (in ut_tv)
InitProcess = iota // process spawned by init(8)
LoginProcess = iota // session leader process for user login
UserProcess = iota // normal process
DeadProcess = iota // terminated process
Accounting = iota // not implemented
LineSize = 32
NameSize = 32
HostSize = 256
)
type ExitStatus struct {
Termination int16 `json:"termination"` // process termination status
Exit int16 `json:"exit"` // process exit status
}
type TimeVal struct {
Sec int32 `json:"seconds"`
Usec int32 `json:"microseconds"`
}
// http://man7.org/linux/man-pages/man5/utmp.5.html
type Utmp struct {
Type Utype // type of record
_ int16 // padding because Go doesn't 4-byte align
Pid int32 // PID of login process
Device [LineSize]byte // device name of tty - "/dev/"
Id [4]byte // terminal name suffix or inittab(5) ID
User [NameSize]byte // username
Host [HostSize]byte // hostname for remote login or kernel version for run-level messages
Exit ExitStatus // exit status of a process marked as DeadProcess; not used by Linux init(1)
Session int32 // session ID (getsid(2)), used for windowing
Time TimeVal // time entry was made
Addr [4]int32 // internet address of remote host; IPv4 address uses just Addr[0]
Unused [20]byte // reserved for future use
}
func GetSystemInfo(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
st := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(st); err != nil {
cResp <- &Response{nil, tag, err}
return
}
sinfo := &SysInfo{}
sinfo.Uptime = time.Duration(st.Uptime)
// float64(1<<SI_LOAD_SHIFT) == 65536.0
scale := 65536.0
sinfo.Loadavg.One = float64(st.Loads[0]) / scale
sinfo.Loadavg.Five = float64(st.Loads[1]) / scale
sinfo.Loadavg.Fifteen = float64(st.Loads[2]) / scale
if err := getMemInfo(&sinfo.Mem); err != nil {
cResp <- &Response{nil, tag, err}
return
}
unit := uint64(st.Unit) * 1024 // kB
sinfo.Swap.Total = uint64(st.Totalswap) / unit
sinfo.Swap.Free = uint64(st.Freeswap) / unit
sinfo.LongBit = getLongBit()
switch u, err := getUname(); {
case err == nil:
sinfo.Uname = u
default:
cResp <- &Response{nil, tag, err}
return
}
switch u, err := getLoggedUsers(); {
case err == nil:
sinfo.Users = u
default:
cResp <- &Response{nil, tag, err}
return
}
switch d, err := getDisksInfo(); {
case err == nil:
sinfo.Disks = d
default:
cResp <- &Response{nil, tag, err}
return
}
cResp <- &Response{sinfo, tag, nil}
}
func getLongBit() int {
out, err := exec.Command("/usr/bin/getconf", "LONG_BIT").Output()
if err != nil {
fmt.Println(err)
return 0
}
i, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
return 0
}
return i
}
func getUname() (*Utsname, error) {
u := syscall.Utsname{}
if err := syscall.Uname(&u); err != nil {
return nil, err
}
toString := func(f [65]int8) string {
out := make([]byte, 0, 64)
for _, v := range f[:] {
if v == 0 {
break
}
out = append(out, uint8(v))
}
return string(out)
}
uname := Utsname{
Sysname: toString(u.Sysname),
Nodename: toString(u.Nodename),
Release: toString(u.Release),
Version: toString(u.Version),
Machine: toString(u.Machine),
Domainname: toString(u.Domainname),
}
return &uname, nil
}
func getLoggedUsers() ([]LoggedUser, error) {
f, err := os.Open("/var/run/utmp")
if err != nil {
return nil, err
}
defer f.Close()
users := []LoggedUser{}
for {
entry := new(Utmp)
err := binary.Read(f, binary.LittleEndian, entry)
if err != nil && err != io.EOF {
continue
}
if err == io.EOF {
break
}
if entry.Type != UserProcess {
continue
}
u := LoggedUser{
Name: string(bytes.Trim(entry.User[:], "\u0000")),
Device: string(bytes.Trim(entry.Device[:], "\u0000")),
Host: string(bytes.Trim(entry.Host[:], "\u0000")),
LoginTime: time.Unix(int64(entry.Time.Sec), int64(entry.Time.Usec)).Unix(),
}
if u.Name != "" && u.Host != "" {
users = append(users, u)
}
}
return users, nil
}
func getDisksInfo() ([]BlockDev, error) {
dir, err := ioutil.ReadDir("/sys/block")
if err != nil {
return nil, err
}
mounts, err := parseMounts()
if err != nil {
return nil, err
}
disks := []BlockDev{}
for _, file := range dir {
// Checking whether device is physical device
if _, err := os.Stat(filepath.Join("/sys/block", file.Name(), "device")); err != nil {
continue
}
d := BlockDev{
Path: filepath.Join("/dev", file.Name()),
}
if _, ok := mounts[d.Path]; ok {
d.IsMounted = true
d.Mountpoint = mounts[d.Path]
st := syscall.Statfs_t{}
if err := syscall.Statfs(d.Mountpoint, &st); err != nil {
return nil, err
}
d.SizeTotal = (int64(st.Bsize) * int64(st.Blocks)) / 1024
d.SizeUsed = d.SizeTotal - (int64(st.Bsize)*int64(st.Bfree))/1024
d.SizeAvail = (int64(st.Bsize) * int64(st.Bavail)) / 1024
}
disks = append(disks, d)
}
return disks, nil
}
func getMemInfo(mi *MemStat) error {
f, err := os.Open("/proc/meminfo")
if err != nil {
return err
}
defer f.Close()
s := bufio.NewScanner(f)
// number of fields we're interested in
n := 4
for s.Scan() && n > 0 {
switch {
case bytes.HasPrefix(s.Bytes(), []byte(`MemTotal:`)):
_, err = fmt.Sscanf(s.Text(), "MemTotal:%d", &mi.Total)
case bytes.HasPrefix(s.Bytes(), []byte(`MemFree:`)):
_, err = fmt.Sscanf(s.Text(), "MemFree:%d", &mi.Free)
case bytes.HasPrefix(s.Bytes(), []byte(`Buffers:`)):
_, err = fmt.Sscanf(s.Text(), "Buffers:%d", &mi.Buffers)
case bytes.HasPrefix(s.Bytes(), []byte(`Cached:`)):
_, err = fmt.Sscanf(s.Text(), "Cached:%d", &mi.Cached)
default:
continue
}
if err != nil {
return err
}
n--
}
if err = s.Err(); err != nil {
return err
}
mi.FreeTotal = mi.Free + mi.Buffers + mi.Cached
return nil
}
func parseMounts() (map[string]string, error) {
f, err := os.Open("/proc/self/mounts")
if err != nil {
return nil, err
}
defer f.Close()
mounts := make(map[string]string)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 2 {
continue
}
mounts[fields[0]] = fields[1]
}
if err = scanner.Err(); err != nil {
return nil, err
}
return mounts, nil
}