Skip to content

Commit

Permalink
Merge pull request #329 from gwos/GROUNDWORK-2644_snmp
Browse files Browse the repository at this point in the history
GROUNDWORK-2644 snmp: fix cache logic
  • Loading branch information
dppattison authored Oct 3, 2023
2 parents 47bd46a + e8a84fb commit 9311a83
Show file tree
Hide file tree
Showing 9 changed files with 2,310 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.19
go-version: 1.21
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go: '1.19'
go: '1.21'
skip-dirs:
- vendor
- bin
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# NOTE:
# https://stackoverflow.com/questions/36279253/go-compiled-binary-wont-run-in-an-alpine-docker-container-on-ubuntu-host
#
FROM golang:1.19-bullseye AS build-libtransit
FROM golang:1.21-bullseye AS build-libtransit
ARG TRAVIS_TAG=
ENV TRAVIS_TAG=${TRAVIS_TAG:-master}
WORKDIR /go/src/
Expand All @@ -21,7 +21,7 @@ RUN make clean && make \
FROM scratch AS export-libtransit
COPY --from=build-libtransit /go/src/build /

FROM golang:1.19-alpine AS build
FROM golang:1.21-alpine AS build
ARG TRAVIS_TAG=
ENV TRAVIS_TAG=${TRAVIS_TAG:-master}
WORKDIR /go/src/
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,5 @@ For more info see package `config` and tests.

## Run golangci-lint locally:

$ golangci-lint --config ./.golangci.yaml run ./... --deadline=2m
$ go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2
$ ~/go/bin/golangci-lint --config ./.golangci.yaml run ./... --deadline=2m
11 changes: 7 additions & 4 deletions connectors/snmp-connector/clients/snmpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ var AvailableMetrics = map[string]*SnmpMetric{
Description: "The number of outbound packets which were chosen to be discarded" +
" even though no errors had been detected to prevent their being transmitted."},

BytesIn: {Key: "bytesIn", Mib: "ifInOctets", Oid: "1.3.6.1.2.1.2.2.1.10", Name: "Inbound Bytes",
IfInOctets: {Key: "bytesIn", Mib: "ifInOctets", Oid: "1.3.6.1.2.1.2.2.1.10", Name: "Inbound Bytes",
Description: "The total number of octets received on the interface, including framing characters."},
BytesOut: {Key: "bytesOut", Mib: "ifOutOctets", Oid: "1.3.6.1.2.1.2.2.1.16", Name: "Outbound Bytes",
IfOutOctets: {Key: "bytesOut", Mib: "ifOutOctets", Oid: "1.3.6.1.2.1.2.2.1.16", Name: "Outbound Bytes",
Description: "The total number of octets transmitted out of the interface, including framing characters."},
BytesInX64: {Key: "bytesInX64", Mib: "ifHCInOctets", Oid: "1.3.6.1.2.1.31.1.1.1.6", Name: "Inbound Bytes 64-bit",
IfHCInOctets: {Key: "bytesInX64", Mib: "ifHCInOctets", Oid: "1.3.6.1.2.1.31.1.1.1.6", Name: "Inbound Bytes 64-bit",
Description: "The total number of octets received on the interface, including framing characters." +
"This object is a 64-bit version of ifInOctets."},
BytesOutX64: {Key: "bytesOutX64", Mib: "ifHCOutOctets", Oid: "1.3.6.1.2.1.31.1.1.1.10", Name: "Outbound Bytes 64-bit",
IfHCOutOctets: {Key: "bytesOutX64", Mib: "ifHCOutOctets", Oid: "1.3.6.1.2.1.31.1.1.1.10", Name: "Outbound Bytes 64-bit",
Description: "The total number of octets transmitted out of the interface, including framing characters. " +
"This object is a 64-bit version of ifOutOctets."},
}
Expand Down Expand Up @@ -288,6 +288,9 @@ func getSnmpData(mib string, goSnmp *snmp.GoSNMP) (*SnmpMetricData, error) {
case uint:
val.Value = int64(v)
log.Info().Msgf("*** parsed value for %s: %d", val.Name, val.Value)
case uint64:
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)
}
Expand Down
41 changes: 27 additions & 14 deletions connectors/snmp-connector/snmpConnectorModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func (state *MonitoringState) Init() {
}

func (state *MonitoringState) retrieveMonitoredResources(metricDefinitions map[string]transit.MetricDefinition) []transit.MonitoredResource {
mResources := make([]transit.MonitoredResource, len(state.devices))
i := 0
mResources := make([]transit.MonitoredResource, 0, len(state.devices))
for _, device := range state.devices {
mServices := device.retrieveMonitoredServices(metricDefinitions)
mResource, err := connectors.CreateResource(device.Name, mServices)
Expand All @@ -58,21 +57,26 @@ func (state *MonitoringState) retrieveMonitoredResources(metricDefinitions map[s
continue
}
mResource.Status = calculateHostStatus(device.LastOK)
mResources[i] = *mResource
i++
mResources = append(mResources, *mResource)
}

// log.Debug().
// Interface("devices", state.devices).
// Interface("metricDefinitions", metricDefinitions).
// Interface("mResources", mResources).
// Msg("__ retrieveMonitoredResources")

return mResources
}

func (device *DeviceExt) retrieveMonitoredServices(metricDefinitions map[string]transit.MetricDefinition) []transit.MonitoredService {
mServices := make([]transit.MonitoredService, len(device.Interfaces))
mServices := make([]transit.MonitoredService, 0, len(device.Interfaces))

if metricDefinitions == nil {
return mServices
}

timestamp := transit.NewTimestamp()
i := 0
for _, iFace := range device.Interfaces {
var bytesInPrev, bytesOutPrev, bytesInX64Prev, bytesOutX64Prev int64 = -1, -1, -1, -1
if val, ok := previousValueCache.Get(fmt.Sprintf("%s:%s:%s", device.Name, iFace.Name, clients.IfInOctets)); ok {
Expand All @@ -90,7 +94,7 @@ func (device *DeviceExt) retrieveMonitoredServices(metricDefinitions map[string]

var metricsBuilder []connectors.MetricBuilder
for mib, metric := range iFace.Metrics {
if metricDefinition, has := metricDefinitions[metric.Key]; has {
if metricDefinition, has := metricDefinitions[metric.Mib]; has {
var unitType transit.UnitType
var value interface{}

Expand All @@ -117,15 +121,25 @@ func (device *DeviceExt) retrieveMonitoredServices(metricDefinitions map[string]
Value: nil,
}

ck := fmt.Sprintf("%s:%s:%s", device.Name, iFace.Name, mib)
isDelta, isPreviousPresent, valueToSet := calculateValue(metricDefinition.MetricType, unitType,
fmt.Sprintf("%s:%s:%s", device.Name, iFace.Name, mib), value)
ck, value)

if !isDelta || (isDelta && isPreviousPresent) {
metricBuilder.Value = valueToSet
metricsBuilder = append(metricsBuilder, metricBuilder)
}

previousValueCache.SetDefault(ck, metric.Value)

// log.Debug().
// Interface("_ck", ck).
// Interface("_isDelta", isDelta).
// Interface("_isPreviousPresent", isPreviousPresent).
// Interface("_valueToSet", valueToSet).
// Interface("metricsBuilder", metricsBuilder).
// Msg("__ ck")
}
previousValueCache.SetDefault(mib, metric.Value)
}

for key := range clients.NonMibMetrics {
Expand Down Expand Up @@ -163,9 +177,8 @@ func (device *DeviceExt) retrieveMonitoredServices(metricDefinitions map[string]
mService.LastPluginOutput = "Interface Operational State is UP, Administrative state is UP"
case -1:
}
mServices[i] = *mService
mServices = append(mServices, *mService)
}
i++
}

return mServices
Expand All @@ -189,11 +202,11 @@ func calculateValue(metricKind transit.MetricKind, unitType transit.UnitType,
previousValueCache.SetDefault(metricName, currentValue.(int64))
currentValue = currentValue.(int64) - previousValue.(int64)
}
return true, true, currentValue
return true, true, currentValue.(int64)
}
return true, false, currentValue
return true, false, currentValue.(int64)
}
return false, false, currentValue
return false, false, currentValue.(int64)
}

func calculateBytesPerSecond(metricName string, metricDefinition transit.MetricDefinition, current, currentX64, previous,
Expand Down
Loading

0 comments on commit 9311a83

Please sign in to comment.