-
Notifications
You must be signed in to change notification settings - Fork 83
/
snmp.go
63 lines (53 loc) · 1.05 KB
/
snmp.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"
"io"
"io/ioutil"
"strconv"
"strings"
"github.com/toolkits/file"
)
func Snmp(title string) (ret map[string]int64, err error) {
ret = make(map[string]int64)
var contents []byte
contents, err = ioutil.ReadFile(Root() + "/proc/net/snmp")
if err != nil {
return
}
reader := bufio.NewReader(bytes.NewBuffer(contents))
for {
var bs []byte
bs, err = file.ReadLine(reader)
if err == io.EOF {
err = nil
break
} else if err != nil {
return
}
line := string(bs)
idx := strings.Index(line, ":")
if idx < 0 {
continue
}
prefix := strings.TrimSpace(line[:idx])
if prefix == title {
ths := strings.Fields(strings.TrimSpace(line[idx+1:]))
// the next line must be values
bs, err = file.ReadLine(reader)
if err != nil {
return
}
valLine := string(bs)
tds := strings.Fields(strings.TrimSpace(valLine[idx+1:]))
for i := 0; i < len(ths); i++ {
ret[ths[i]], err = strconv.ParseInt(tds[i], 10, 64)
if err != nil {
return
}
}
return
}
}
return
}