forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snmp.go
245 lines (230 loc) · 5.89 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package collectors
import (
"fmt"
"io"
"math/big"
"reflect"
"strconv"
"strings"
"bosun.org/_third_party/github.com/mjibson/snmp"
"bosun.org/cmd/scollector/conf"
"bosun.org/metadata"
"bosun.org/opentsdb"
)
var builtInSNMPs = map[string]func(cfg conf.SNMP){"ifaces": SNMPIfaces, "cisco": SNMPCisco, "bridge": SNMPBridge}
func SNMP(cfg conf.SNMP, mibs map[string]conf.MIB) error {
if cfg.Host == "" {
return fmt.Errorf("empty SNMP hostname")
}
if cfg.Community == "" {
return fmt.Errorf("empty SNMP community")
}
if len(cfg.MIBs) == 0 {
cfg.MIBs = []string{"ifaces", "cisco", "bridge"}
}
for _, m := range cfg.MIBs {
mib, ok := mibs[m]
if ok {
collectors = append(collectors, &IntervalCollector{
F: func() (opentsdb.MultiDataPoint, error) {
return GenericSnmp(cfg, mib)
},
name: fmt.Sprintf("snmp-generic-%s-%s", cfg.Host, m),
})
} else {
if f, ok := builtInSNMPs[m]; ok {
f(cfg)
} else {
return fmt.Errorf("unknown MIB \"%s\" specified", m)
}
}
}
return nil
}
// snmp_subtree takes an oid and returns all data exactly one level below it. It
// produces an error if there is more than one level below.
func snmp_subtree(host, community, oid string) (map[string]interface{}, error) {
rows, err := snmp.Walk(host, community, oid)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
for rows.Next() {
key := ""
var a interface{}
switch oid {
case ifHCInBroadcastPkts:
a = new(big.Int)
id, err := rows.Scan(&a)
if err != nil {
return nil, err
}
switch t := id.(type) {
case int:
key = fmt.Sprint(t)
default:
return nil, fmt.Errorf("snmp subtree: only one level allowed")
}
default:
id, err := rows.Scan(&a)
if err != nil {
return nil, err
}
switch t := id.(type) {
case int:
key = fmt.Sprint(t)
case []int:
key = snmpOidArrayToString(t)
default:
return nil, fmt.Errorf("Unknown key type: %s", reflect.TypeOf(id).String())
}
}
m[key] = a
}
if err := rows.Err(); err != nil && err != io.EOF {
return nil, err
}
return m, nil
}
// recombine an oid array to a dot-delimited string
func snmpOidArrayToString(path []int) string {
s := make([]string, len(path))
for i := range path {
s[i] = fmt.Sprint(path[i])
}
return strings.Join(s, ".")
}
func snmp_oid(host, community, oid string) (*big.Int, error) {
v := new(big.Int)
err := snmp.Get(host, community, oid, &v)
return v, err
}
func snmp_convertToFloat(v interface{}) (float64, error) {
switch val := v.(type) {
case int:
return float64(val), nil
case int32:
return float64(val), nil
case int64:
return float64(val), nil
case float64:
return val, nil
case *big.Int:
return float64(val.Int64()), nil
case string:
return strconv.ParseFloat(val, 64)
case []uint8:
return strconv.ParseFloat(string(val), 64)
default:
return 0, fmt.Errorf("Cannot convert type %s to float64", reflect.TypeOf(v))
}
}
func combineOids(oid, base string) string {
if oid != "" && oid[0] == '.' {
return base + oid
}
return oid
}
func GenericSnmp(cfg conf.SNMP, mib conf.MIB) (opentsdb.MultiDataPoint, error) {
md := opentsdb.MultiDataPoint{}
baseOid := mib.BaseOid
rateUnitTags := func(m conf.MIBMetric) (r metadata.RateType, u metadata.Unit, t opentsdb.TagSet, err error) {
if r = metadata.RateType(m.RateType); r == "" {
r = metadata.Gauge
}
if u = metadata.Unit(m.Unit); u == "" {
u = metadata.None
}
if m.Tags == "" {
t = make(opentsdb.TagSet)
} else {
t, err = opentsdb.ParseTags(m.Tags)
if err != nil {
return "", "", nil, err
}
}
t["host"] = cfg.Host
return
}
for _, metric := range mib.Metrics {
rate, unit, tagset, err := rateUnitTags(metric)
if err != nil {
return md, err
}
v, err := snmp_oid(cfg.Host, cfg.Community, combineOids(metric.Oid, baseOid))
if err != nil && metric.FallbackOid != "" {
v, err = snmp_oid(cfg.Host, cfg.Community, combineOids(metric.FallbackOid, baseOid))
}
if err != nil {
return md, err
}
val, err := snmp_convertToFloat(v)
if err != nil {
return md, err
}
if metric.Scale != 0 {
val = val * metric.Scale
}
Add(&md, metric.Metric, val, tagset, rate, unit, metric.Description)
}
for _, tree := range mib.Trees {
treeOid := combineOids(tree.BaseOid, baseOid)
tagCache := make(map[string]map[string]interface{}) // tag key to map of values
for _, tag := range tree.Tags {
if tag.Oid == "idx" {
continue
}
vals, err := snmp_subtree(cfg.Host, cfg.Community, combineOids(tag.Oid, treeOid))
if err != nil {
return md, err
}
tagCache[tag.Key] = vals
}
for _, metric := range tree.Metrics {
rate, unit, tagset, err := rateUnitTags(metric)
if err != nil {
return md, err
}
nodes, err := snmp_subtree(cfg.Host, cfg.Community, combineOids(metric.Oid, treeOid))
if err != nil && metric.FallbackOid != "" {
nodes, err = snmp_subtree(cfg.Host, cfg.Community, combineOids(metric.FallbackOid, treeOid))
}
if err != nil {
return md, err
}
// check all lengths
for k, list := range tagCache {
if len(list) != len(nodes) {
return md, fmt.Errorf("snmp tree for tag key %s, and metric %s do not have same length", k, metric.Metric)
}
}
for i, v := range nodes {
for _, tag := range tree.Tags {
var tagVal interface{}
if tag.Oid == "idx" {
tagVal = i
} else {
var ok bool
tagVal, ok = tagCache[tag.Key][i]
if !ok {
return md, fmt.Errorf("tree for tag %s has no entry for metric %s index %s", tag.Key, metric.Metric, i)
}
}
if byteSlice, ok := tagVal.([]byte); ok {
tagVal = string(byteSlice)
}
tagset[tag.Key] = fmt.Sprint(tagVal)
}
val, err := snmp_convertToFloat(v)
if err != nil {
return md, err
}
if metric.Scale != 0 {
val = val * metric.Scale
}
Add(&md, metric.Metric, val, tagset, rate, unit, metric.Description)
}
}
}
return md, nil
}