-
Notifications
You must be signed in to change notification settings - Fork 83
/
ss_s.go
63 lines (55 loc) · 1.29 KB
/
ss_s.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
package nux
import (
"bufio"
"bytes"
"github.com/toolkits/file"
"github.com/toolkits/sys"
"strconv"
"strings"
)
func SocketStatSummary() (m map[string]uint64, err error) {
m = make(map[string]uint64)
var bs []byte
bs, err = sys.CmdOutBytes("sh", "-c", "ss -s")
if err != nil {
return
}
reader := bufio.NewReader(bytes.NewBuffer(bs))
// ignore the first line
line, e := file.ReadLine(reader)
if e != nil {
return m, e
}
for {
line, err = file.ReadLine(reader)
if err != nil {
return
}
lineStr := string(line)
if strings.HasPrefix(lineStr, "TCP") {
left := strings.Index(lineStr, "(")
right := strings.Index(lineStr, ")")
if left < 0 || right < 0 {
continue
}
content := lineStr[left+1 : right]
arr := strings.Split(content, ", ")
for _, val := range arr {
fields := strings.Fields(val)
if fields[0] == "timewait" {
timewait_arr := strings.Split(fields[1], "/")
m["timewait"], _ = strconv.ParseUint(timewait_arr[0], 10, 64)
if len(timewait_arr) > 1 {
m["slabinfo.timewait"], _ = strconv.ParseUint(timewait_arr[1], 10, 64)
} else {
m["slabinfo.timewait"] = 0
}
continue
}
m[fields[0]], _ = strconv.ParseUint(fields[1], 10, 64)
}
return
}
}
return
}