forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_solaris.go
74 lines (64 loc) · 1.4 KB
/
load_solaris.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
//go:build solaris
// +build solaris
package load
import (
"bufio"
"bytes"
"context"
"strconv"
"strings"
)
func Avg() (*AvgStat, error) {
return AvgWithContext(context.Background())
}
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "unix:0:system_misc:avenrun_*")
if err != nil {
return nil, err
}
avg := &AvgStat{}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
flds := strings.Fields(scanner.Text())
if len(flds) < 2 {
continue
}
var tgt *float64
switch {
case strings.HasSuffix(flds[0], ":avenrun_1min"):
tgt = &avg.Load1
case strings.HasSuffix(flds[0], ":avenrun_5min"):
tgt = &avg.Load5
case strings.HasSuffix(flds[0], ":avenrun_15min"):
tgt = &avg.Load15
default:
continue
}
v, err := strconv.ParseInt(flds[1], 10, 64)
if err != nil {
return nil, err
}
*tgt = float64(v) / (1 << 8)
}
if err = scanner.Err(); err != nil {
return nil, err
}
return avg, nil
}
func Misc() (*MiscStat, error) {
return MiscWithContext(context.Background())
}
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
out, err := invoke.CommandWithContext(ctx, "ps", "-efo", "s")
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
ret := MiscStat{}
for _, l := range lines {
if l == "O" {
ret.ProcsRunning++
}
}
return &ret, nil
}