forked from paulstuart/influxsnmp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnmp.go
263 lines (244 loc) · 5.6 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/soniah/gosnmp"
)
type Control struct {
nameOid string
found map[string]string
labels map[string]string
usable map[string]struct{}
}
var (
errorSNMP int
nameOid = "1.3.6.1.2.1.31.1.1.1.1" // ifName
)
const (
maxOids = 60 // const in gosnmp
)
type pduValue struct {
name, column string
value interface{}
}
func getPoint(cfg *SnmpConfig, pdu gosnmp.SnmpPDU) *pduValue {
i := strings.LastIndex(pdu.Name, ".")
root := pdu.Name[1:i]
suffix := pdu.Name[i+1:]
col := cfg.labels[cfg.asOID[suffix]]
name, ok := oidToName[root]
if len(name) == 0 {
name, ok = oidToName[pdu.Name]
}
if verbose {
log.Println("ROOT:", root, "SUFFIX:", suffix, "COL:", col, "NAME:", "VALUE:", pdu.Value)
log.Println("OID TO NAME", name, col, pdu.Name, pdu.Value, oidToName)
}
if !ok {
log.Printf("Invalid oid: %s\n", pdu.Name)
return nil
}
if len(col) == 0 {
col = name
//log.Println("The col was empty for:", cfg.asOID[suffix])
//return nil // not an OID of interest
}
return &pduValue{name, col, pdu.Value}
}
func bulkPoint(cfg *SnmpConfig, pdu gosnmp.SnmpPDU) *pduValue {
i := strings.LastIndex(pdu.Name, ".")
root := pdu.Name[1:i]
suffix := pdu.Name[i+1:]
col := cfg.asOID[suffix]
name, ok := oidToName[root]
if len(name) == 0 {
name, ok = oidToName[pdu.Name]
}
if verbose {
log.Println("ROOT:", root, "SUFFIX:", suffix, "COL:", col, "NAME:", "VALUE:", pdu.Value)
log.Println("BULK OID TO NAME", name, col, pdu.Name, pdu.Value, oidToName)
}
if !ok {
log.Printf("Invalid oid: %s\n", pdu.Name)
return nil
}
if len(col) == 0 {
col = name
//log.Println("The col was empty for:", suffix)
//return nil // not an OID of interest
}
return &pduValue{name, col, pdu.Value}
}
func snmpStats(snmp *gosnmp.GoSNMP, cfg *SnmpConfig) error {
now := time.Now()
if cfg == nil {
log.Fatal("cfg is nil")
}
if cfg.Influx == nil {
log.Fatal("influx cfg is nil")
}
bps := cfg.Influx.BP()
// we can only get 'maxOids' worth of snmp requests at a time
for i := 0; i < len(cfg.oids); i += maxOids {
end := i + maxOids
if end > len(cfg.oids) {
end = len(cfg.oids)
}
cfg.incRequests()
pkt, err := snmp.Get(cfg.oids[i:end])
if err != nil {
errLog("SNMP (%s) get error: %s\n", cfg.Host, err)
cfg.incErrors()
cfg.LastError = now
return err
}
cfg.incGets()
if verbose {
log.Println("SNMP GET CNT:", len(pkt.Variables))
}
//log.Println("PKT", pkt)
for _, pdu := range pkt.Variables {
val := getPoint(cfg, pdu)
if val == nil {
continue
}
if val.value == nil {
continue
}
//log.Println("VAL", val)
pt := makePoint(cfg.Host, val, now)
//log.Println("PT", pt)
bps.Points = append(bps.Points, pt)
}
}
cfg.Influx.Send(bps)
return nil
}
func bulkStats(snmp *gosnmp.GoSNMP, cfg *SnmpConfig) error {
now := time.Now()
if cfg == nil {
log.Fatal("cfg is nil")
}
if cfg.Influx == nil {
log.Fatal("influx cfg is nil")
}
bps := cfg.Influx.BP()
addPacket := func(pdu gosnmp.SnmpPDU) error {
val := bulkPoint(cfg, pdu)
if val != nil && val.value != nil {
pt := makePoint(cfg.Host, val, now)
bps.Points = append(bps.Points, pt)
}
return nil
}
for i := 0; i < len(cfg.oids); i += 1 {
cfg.incRequests()
if err := snmp.BulkWalk(cfg.oids[i], addPacket); err != nil {
errLog("SNMP (%s) get error: %s\n", cfg.Host, err)
cfg.incErrors()
cfg.LastError = now
return err
}
}
cfg.Influx.Send(bps)
return nil
}
func printSnmpNames(c *SnmpConfig) {
client, err := snmpClient(c)
if err != nil {
fatal(err)
}
defer client.Conn.Close()
pdus, err := client.BulkWalkAll(nameOid)
if err != nil {
fatal("SNMP bulkwalk error", err)
}
for _, pdu := range pdus {
switch pdu.Type {
case gosnmp.OctetString:
fmt.Println(string(pdu.Value.([]byte)), pdu.Name)
}
}
}
func snmpClient(s *SnmpConfig) (*gosnmp.GoSNMP, error) {
client := &gosnmp.GoSNMP{
Target: s.Host,
Port: uint16(s.Port),
Community: s.Public,
Version: gosnmp.Version2c,
Timeout: time.Duration(s.Timeout) * time.Second,
Retries: s.Retries,
}
err := client.Connect()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
return client, err
}
func (s *SnmpConfig) DebugLog() *log.Logger {
name := filepath.Join(logDir, "debug_"+strings.Replace(s.Host, ".", "-", -1)+".log")
if l, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0664); err == nil {
return log.New(l, "", 0)
} else {
fmt.Fprintln(os.Stderr, err)
return nil
}
}
func (s *SnmpConfig) Gather(count int, wg *sync.WaitGroup) {
debug := false
client, err := snmpClient(s)
if err != nil {
fatal(err)
}
defer client.Conn.Close()
spew(strings.Join(s.oids, "\n"))
fn := snmpStats
if len(s.PortFile) == 0 {
fn = bulkStats
}
c := time.Tick(time.Duration(s.Freq) * time.Second)
for {
err := fn(client, s)
if count > 0 {
count--
if count == 0 {
break
}
}
// was seeing clients getting "wedged" -- so just restart
if err != nil {
errLog("snmp error - reloading snmp client: %s", err)
client.Conn.Close()
for {
if client, err = snmpClient(s); err == nil {
break
}
errLog("snmp client connect error: %s", err)
time.Sleep(time.Duration(s.Timeout) * time.Second)
}
}
// pause for interval period and have optional debug toggling
LOOP:
for {
select {
case <-c:
break LOOP
case debug := <-s.debugging:
log.Println("debugging:", debug)
if debug && client.Logger == nil {
client.Logger = s.DebugLog()
} else {
client.Logger = nil
}
case status := <-s.enabled:
status <- debug
}
}
}
wg.Done()
}