-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
57 lines (50 loc) · 1.51 KB
/
utils.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
package hpmib
import (
"fmt"
"strconv"
"strings"
"github.com/soniah/gosnmp"
)
// traverseTable traverses the table with the provided columns by issuing multiple GETNEXT
// requests to the SNMP agent until one or more results are returned that are outside of the
// tablespace.
func traverseTable(client *gosnmp.GoSNMP, columns OIDList) ([][]string, error) {
table := [][]string{}
for i := range table {
table[i] = make([]string, 0, len(columns))
}
rootOIDs := columns.Strings()
currentOIDs := columns.Strings()
traverse:
for {
res, err := client.GetNext(currentOIDs)
if err != nil {
return [][]string{}, nil
}
row := []string{}
currentOIDs = []string{}
for i, v := range res.Variables {
currentOIDs = append(currentOIDs, v.Name)
if !strings.HasPrefix(v.Name, "."+rootOIDs[i]) {
break traverse
}
switch v.Type {
case gosnmp.OctetString:
row = append(row, string(v.Value.([]byte)))
case gosnmp.Integer:
row = append(row, strconv.Itoa(v.Value.(int)))
default:
return [][]string{}, fmt.Errorf("unknown variable type encountered for OID %s", v.Name)
}
}
if len(row) != len(columns) {
return [][]string{}, fmt.Errorf("expected number of columns in the row %d to equal the number of columns %d in the table", len(row), len(columns))
}
table = append(table, row)
}
return table, nil
}
// prettifyString removes redundant whitespace characters from a string.
func prettifyString(s string) string {
return strings.TrimSpace(strings.Join(strings.Fields(s), " "))
}