-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutil.go
More file actions
188 lines (175 loc) · 4.64 KB
/
util.go
File metadata and controls
188 lines (175 loc) · 4.64 KB
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
// Copyright 2016 Paul Stuart. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
package snmputil
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/gosnmp/gosnmp"
)
// makeString converts ascii octets into a string
func makeString(bits []string) string {
chars := make([]byte, len(bits))
for i, bit := range bits {
n, _ := strconv.Atoi(bit)
chars[i] = byte(n)
}
return cleanString(chars)
}
// Octets converts a string of octets into an array of ints
func Octets(in string) []int {
bits := strings.Split(in, ".")
words := make([]int, 0, len(bits))
for _, b := range bits {
i, _ := strconv.Atoi(b)
words = append(words, i)
}
return words
}
// oidStrings converts ascii octets into an array of words
func oidStrings(in string) []string {
words := []string{}
bits := strings.Split(in, ".")
for i := 0; i < len(bits); i++ {
cnt, _ := strconv.Atoi(bits[i])
end := i + cnt + 1
if i > len(bits) || i >= end {
break
}
if end > len(bits) {
end = len(bits)
}
words = append(words, makeString(bits[i+1:end]))
i += cnt
}
return words
}
// cleanString creates a printable string
func cleanString(in []byte) string {
r := bytes.Runes(in)
acc := make([]rune, 0, len(r))
for _, c := range r {
if strconv.IsPrint(c) {
acc = append(acc, c)
}
}
return string(acc)
}
// dateTime converts snmp datetime octets into time.Time
func dateTime(pdu gosnmp.SnmpPDU) (interface{}, error) {
d := pdu.Value.([]byte)
offset := 0
switch len(d) {
case 8:
case 11:
offset = (int(d[9]) * 3600) + (int(d[10]) * 60)
if string(d[8]) == "-" {
offset = -offset
}
default:
return time.Time{}, fmt.Errorf("invalid octet length:%d", len(d))
}
year := int(d[0])<<8 + int(d[1])
month := time.Month(d[2])
nano := int(d[7]) * 1024
loc := time.FixedZone("UTC", offset)
return time.Date(year, month, int(d[3]), int(d[4]), int(d[5]), int(d[6]), nano, loc), nil
}
// pduType verifies and normalizes the pdu data
func pduType(pdu gosnmp.SnmpPDU) (interface{}, error) {
switch pdu.Type {
case gosnmp.Integer, gosnmp.Gauge32, gosnmp.TimeTicks, gosnmp.Uinteger32:
case gosnmp.IPAddress, gosnmp.ObjectIdentifier:
// ensuring counters are correctly cast allows for processing on type
// e.g., if it's a uint64 it's a counter so calculate rate change on it
case gosnmp.Counter32:
switch pdu.Value.(type) {
case uint:
return uint32(pdu.Value.(uint)), nil
case uint32:
return uint32(pdu.Value.(uint32)), nil
case uint64:
return uint32(pdu.Value.(uint64)), nil
case int:
return uint32(pdu.Value.(int)), nil
case int32:
return uint32(pdu.Value.(int32)), nil
case int64:
return uint32(pdu.Value.(int64)), nil
default:
return pdu.Value, fmt.Errorf("invalid Counter32 type:%T pdu.Value:%v\n", pdu.Value, pdu.Value)
}
case gosnmp.Counter64:
switch pdu.Value.(type) {
case uint:
return uint64(pdu.Value.(uint)), nil
case uint32:
return uint64(pdu.Value.(uint32)), nil
case uint64:
return uint64(pdu.Value.(uint64)), nil
case int:
return uint64(pdu.Value.(int)), nil
case int32:
return uint64(pdu.Value.(int32)), nil
case int64:
return uint64(pdu.Value.(int64)), nil
default:
return pdu.Value, fmt.Errorf("invalid Counter64 type:%T pdu.Value:%v\n", pdu.Value, pdu.Value)
}
case gosnmp.OctetString:
s := cleanString([]byte(pdu.Value.([]uint8)))
// sometimes numbers are encoded as strings
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f, nil
}
if i, err := strconv.ParseInt(s, 0, 64); err == nil {
return i, nil
}
return s, nil
default:
return pdu.Value, fmt.Errorf("unsupported type: %x (%T), pdu.Value: %v\n", pdu.Type, pdu.Value, pdu.Value)
}
return pdu.Value, nil
}
// getOID returns the OID representing name
func getOID(oid string) (string, error) {
if strings.HasPrefix(oid, ".") {
return oid, nil
}
mu.Lock()
defer mu.Unlock()
fixed, ok := lookupOID[oid]
if !ok {
return oid, fmt.Errorf("no OID found for %s", oid)
}
return fixed, nil
}
// regexpFilter returns a function that filters results based on name
// the function returns true if name is not valid
func regexpFilter(regexps []string, keep bool) (func(string) bool, error) {
if len(regexps) == 0 {
return func(name string) bool {
return false
}, nil
}
filterNames := []*regexp.Regexp{}
for _, n := range regexps {
re, err := regexp.Compile(n)
if err != nil {
return nil, fmt.Errorf("filter pattern: %s: %w", n, err)
}
filterNames = append(filterNames, re)
}
return func(name string) bool {
for _, r := range filterNames {
if r.MatchString(name) {
return !keep
}
}
return keep
}, nil
}