Skip to content

Commit

Permalink
GROUNDWORK-3736 improve SNMP log
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Sumkin committed Nov 28, 2024
1 parent fdfb210 commit af4329d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
66 changes: 29 additions & 37 deletions connectors/snmp/clients/nediClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,18 @@ func (client *NediClient) GetDevices() ([]Device, error) {

path, err := client.getConnectionString(tableDevices, "", "")
if err != nil {
log.Err(err).Msg("could not get NeDi connection string")
return nil, errors.New("failed to get NeDi connection string")
msg := "could not get NeDi connection string"
log.Err(err).Msg(msg)
return nil, errors.New(msg)
}

log.Debug().Msgf("performing NeDi Get Devices request: %s", *path)
r, err := executeGet(*path)
if err != nil {
log.Err(err).
Str("request", *path).
Msg("could not execute NeDi request")
return nil, errors.New("failed to execute NeDi request")
msg := "could not execute NeDi request"
log.Err(err).Str("request", *path).Msg(msg)
return nil, errors.New(msg)
}
log.Debug().Bytes("response", r).Msg("NeDi Get Devices response")
log.Debug().Str("request", *path).Bytes("response", r).Msg("NeDi Get Devices response")

return parseDevices(r, monitored)
}
Expand All @@ -100,19 +99,18 @@ func (client *NediClient) GetDeviceInterfaces(device string) ([]Interface, error
query := colDevice + " = " + device
path, err := client.getConnectionString(tableInterfaces, query, "")
if err != nil {
log.Err(err).Msg("could not get NeDi connection string")
return nil, errors.New("failed to get NeDi connection string")
msg := "could not get NeDi connection string"
log.Err(err).Msg(msg)
return nil, errors.New(msg)
}

log.Debug().Msgf("performing NeDi Get Interfaces request: %s", *path)
r, err := executeGet(*path)
if err != nil {
log.Err(err).
Str("request", *path).
Msg("could not execute NeDi request")
return nil, errors.New("failed to execute NeDi request")
msg := "could not execute NeDi request"
log.Err(err).Str("request", *path).Msg(msg)
return nil, errors.New(msg)
}
log.Debug().Bytes("response", r).Msg("NeDi Get Interfaces response")
log.Debug().Str("request", *path).Bytes("response", r).Msg("NeDi Get Interfaces response")

return parseInterfaces(r), nil
}
Expand Down Expand Up @@ -232,46 +230,41 @@ func parseInterfaces(response []byte) []Interface {
}

func parseResponse(bytes []byte) []map[string]interface{} {
log.Debug().
Bytes("response", bytes).
Msg("parsing NeDi response")

var response []interface{}
if err := json.Unmarshal(bytes, &response); err != nil {
log.Err(err).Bytes("response", bytes).Msg("could not parse NeDi response")
return nil
}

var res []map[string]interface{}
dbg := log.Debug().Bytes("response", bytes)
res := make([]map[string]interface{}, len(response)-1)
skip := 0
for i, r := range response {
log.Debug().
Interface("obj", r).
Msg("parsing NeDi response object")
if i == 0 {
log.Debug().Msg("skipping system information")
// log.Debug().Msg("skipping system information")
continue
}
switch r := r.(type) {
case map[string]interface{}:
res = append(res, r)
default:
log.Warn().
Interface("obj", r).
Msgf("skipping response object of unsupported type %T", r)
skip++
continue
}
}
log.Debug().
Interface("res", res).
Msg("parsing NeDi response completed")
if skip > 0 {
dbg.Int("skipped parts of unsupported type", skip)
}
dbg.Interface("res", res).
Msg("parsing NeDi response")

return res
}

func executeGet(url string) ([]byte, error) {
s, r, err := clients.SendRequest(http.MethodGet, url, nil, nil, nil)
if err != nil || s != 200 || r == nil {
log.Error().
Err(err).
log.Err(err).
Int("status", s).
Bytes("response", r).
Msg("could not send request")
Expand Down Expand Up @@ -312,19 +305,18 @@ func int2ip(val int) string {
func (client *NediClient) getMonitoredDevices() (map[string]Monitoring, error) {
path, err := client.getConnectionString(tableMonitoring, "", "")
if err != nil {
msg := "failed to get NeDi monitoring connection string"
msg := "could not get NeDi monitoring connection string"
log.Err(err).Msg(msg)
return nil, errors.New(msg)
}

log.Debug().Msgf("performing NeDi Get Monitoring request: %s", *path)
response, err := executeGet(*path)
if err != nil {
msg := "could not execute NeDi monitoring request"
log.Err(err).Msg(msg)
log.Err(err).Str("request", *path).Msg(msg)
return nil, errors.New(msg)
}
log.Debug().Bytes("response", response).Msg("NeDi Get Monitoring response")
log.Debug().Str("request", *path).Bytes("response", response).Msg("NeDi Get Monitoring response")

monitors := make(map[string]Monitoring)
for _, fields := range parseResponse(response) {
Expand Down
4 changes: 2 additions & 2 deletions connectors/snmp/clients/snmpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func getSnmpData(mib string, goSnmp *snmp.GoSNMP) (*SnmpMetricData, error) {
data.SnmpMetric = *snmpMetric

walkHandler := func(dataUnit snmp.SnmpPDU) error {
log.Info().Msgf("-- walk Handler: data unit name: '%s', value: '%s'",
log.Info().Msgf("-- walk Handler: data unit name: '%s', value: '%v'",
dataUnit.Name, dataUnit.Value)
var val SnmpValue
val.Name = dataUnit.Name
Expand All @@ -292,7 +292,7 @@ func getSnmpData(mib string, goSnmp *snmp.GoSNMP) (*SnmpMetricData, error) {
val.Value = int64(v)
log.Info().Msgf("*** parsed value for %s: %d", val.Name, val.Value)
default:
log.Warn().Msgf("value '%s' of unsupported type for %s", v, dataUnit.Name)
log.Warn().Msgf("value '%v' of unsupported type for %s", v, dataUnit.Name)
}
data.Values = append(data.Values, val)
return nil
Expand Down

0 comments on commit af4329d

Please sign in to comment.