Skip to content

Commit

Permalink
GROUNDWORK-2644 snmp: fix cache logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Sumkin committed Oct 2, 2023
1 parent b5527a3 commit 9020afb
Show file tree
Hide file tree
Showing 2 changed files with 1,312 additions and 13 deletions.
24 changes: 11 additions & 13 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,19 @@ func (state *MonitoringState) retrieveMonitoredResources(metricDefinitions map[s
continue
}
mResource.Status = calculateHostStatus(device.LastOK)
mResources[i] = *mResource
i++
mResources = append(mResources, *mResource)
}
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 Down Expand Up @@ -117,15 +114,17 @@ 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)
}
previousValueCache.SetDefault(mib, metric.Value)
}

for key := range clients.NonMibMetrics {
Expand Down Expand Up @@ -163,9 +162,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 +187,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 9020afb

Please sign in to comment.