Skip to content

Commit 0ba45a6

Browse files
committed
reworking debug level logging for snmp
1 parent c1d5926 commit 0ba45a6

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

pkg/inputs/snmp/metadata/interface_metadata.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ type InterfaceMetadata struct {
5454
log logger.ContextL
5555
mibs map[string]*kt.Mib
5656
interfaceOids *orderedmap.OrderedMap
57+
debug bool
5758
}
5859

59-
func NewInterfaceMetadata(interfaceMetadataMibs map[string]*kt.Mib, log logger.ContextL) *InterfaceMetadata {
60+
func NewInterfaceMetadata(interfaceMetadataMibs map[string]*kt.Mib, log logger.ContextL, debug bool) *InterfaceMetadata {
6061
mibs := map[string]*kt.Mib{}
6162
m := orderedmap.NewOrderedMap()
6263

@@ -65,7 +66,6 @@ func NewInterfaceMetadata(interfaceMetadataMibs map[string]*kt.Mib, log logger.C
6566
m.Set(el.Key, el.Value)
6667
}
6768

68-
6969
if len(interfaceMetadataMibs) > 0 {
7070
oids := getFromCustomMap(interfaceMetadataMibs)
7171
for _, oid := range oids {
@@ -88,6 +88,7 @@ func NewInterfaceMetadata(interfaceMetadataMibs map[string]*kt.Mib, log logger.C
8888

8989
return &InterfaceMetadata{
9090
log: log,
91+
debug: debug,
9192
mibs: mibs,
9293
interfaceOids: m,
9394
}
@@ -116,7 +117,9 @@ func (im *InterfaceMetadata) Poll(ctx context.Context, conf *kt.SnmpDeviceConfig
116117

117118
for _, variable := range results {
118119
parts := strings.Split(variable.Name, oidVal)
119-
im.log.Debugf("SNMP %v:%+v", variable.Name, variable.Value)
120+
if im.debug {
121+
im.log.Debugf("SNMP %v:%+v", variable.Name, variable.Value)
122+
}
120123
if len(parts) != 2 || len(parts[1]) == 0 {
121124
continue
122125
}
@@ -382,7 +385,9 @@ func (im *InterfaceMetadata) UpdateForHuawei(server *gosnmp.GoSNMP, d *kt.Device
382385
id2snmp := map[string]string{}
383386
for _, variable := range results {
384387
prts := strings.Split(variable.Name, SNMP_HUAWEI_ID_MAP_OID)
385-
im.log.Debugf("[API] ", "SNMP %+v:%+v", variable.Name, variable.Value)
388+
if im.debug {
389+
im.log.Debugf("[API] ", "SNMP %+v:%+v", variable.Name, variable.Value)
390+
}
386391
if len(prts) == 2 {
387392
// variable.Name looks like this: .<oid>.<intVal> (.1.3.6.1.2.1.31.1.1.1.10.219)
388393
oidIdx := prts[1][1:]

pkg/inputs/snmp/metadata/poll.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpD
103103
log: log,
104104
server: server,
105105
interval: DEFAULT_INTERVAL,
106-
interfaceMetadata: NewInterfaceMetadata(interfaceMetadataMibs, log),
106+
interfaceMetadata: NewInterfaceMetadata(interfaceMetadataMibs, log, conf.Debug),
107107
deviceMetadata: NewDeviceMetadata(deviceMetadataMibs, gconf, conf, metrics, log),
108108
jchfChan: jchfChan,
109109
metrics: metrics,

pkg/inputs/snmp/util/adaptor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ func InitSNMP(device *kt.SnmpDeviceConfig, connectTimeout time.Duration, retries
154154
if device.Debug {
155155
server.Logger = gosnmp.NewLogger(logWrapper{
156156
print: func(v ...interface{}) {
157-
log.Debugf("GoSNMP: [hostname=" + device.DeviceName + "]" + fmt.Sprint(v...))
157+
log.Debugf("GoSNMP: [hostname=" + device.DeviceName + "] " + fmt.Sprint(v...))
158158
},
159159
printf: func(format string, v ...interface{}) {
160-
log.Debugf("GoSNMP: [hostname="+device.DeviceName+"]"+format, v...)
160+
log.Debugf("GoSNMP: [hostname="+device.DeviceName+"] "+format, v...)
161161
},
162162
})
163163
}

pkg/inputs/snmp/util/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ func WalkOID(ctx context.Context, device *kt.SnmpDeviceConfig, oid string, serve
177177
if i > 0 {
178178
log.Infof("%s SNMP retry %d on OID %s succeeded", logName, i, oid)
179179
}
180-
log.Debugf("(%s) polling %s using %s took %v. attempt %d of %d.", device.DeviceIP, oid, runtime.FuncForPC(reflect.ValueOf(try.walk).Pointer()).Name(), time.Now().Sub(st), i, len(tries))
180+
if device.Debug {
181+
log.Debugf("(%s) polling %s using %s took %v. attempt %d of %d.", device.DeviceIP, oid, runtime.FuncForPC(reflect.ValueOf(try.walk).Pointer()).Name(), time.Now().Sub(st), i, len(tries))
182+
}
181183
return results, nil
182184
}
183185

0 commit comments

Comments
 (0)