Skip to content

Commit

Permalink
check_network: fix list out of bounds crash
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 8, 2024
1 parent 09c89c2 commit b852d62
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/snclient/check_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func (l *CheckNetwork) Build() *CheckData {
func (l *CheckNetwork) Check(_ context.Context, snc *Agent, check *CheckData, _ []Argument) (*CheckResult, error) {
l.snc = snc

interfaceList, _ := net.Interfaces()
interfaceList, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("net.Interfaces: %s", err.Error())
}
IOList, err := net.IOCounters(true)
if err != nil {
return nil, fmt.Errorf("net.IOCounters: %s", err.Error())
Expand Down Expand Up @@ -103,15 +106,24 @@ func (l *CheckNetwork) Check(_ context.Context, snc *Agent, check *CheckData, _

recvRate, sentRate := l.getTrafficRates(int.Name)

total_received := uint64(0)

Check failure on line 109 in pkg/snclient/check_network.go

View workflow job for this annotation

GitHub Actions / test-linux

var-naming: don't use underscores in Go names; var total_received should be totalReceived (revive)
total_sent := uint64(0)

Check failure on line 110 in pkg/snclient/check_network.go

View workflow job for this annotation

GitHub Actions / test-linux

var-naming: don't use underscores in Go names; var total_sent should be totalSent (revive)
if len(IOList) <= intnr {
log.Debugf("failed to get interface counters for %s, only have %d io counters", int.Name, len(IOList))
} else {
total_received = IOList[intnr].BytesRecv
total_sent = IOList[intnr].BytesSent
}

entry := map[string]string{
"MAC": int.HardwareAddr,
"enabled": strconv.FormatBool(slices.Contains(int.Flags, "up")),
"name": int.Name,
"net_connection_id": int.Name,
"received": humanize.IBytes(uint64(recvRate)) + "/s",
"total_received": fmt.Sprintf("%d", IOList[intnr].BytesRecv),
"total_received": fmt.Sprintf("%d", total_received),
"sent": humanize.IBytes(uint64(sentRate)) + "/s",
"total_sent": fmt.Sprintf("%d", IOList[intnr].BytesSent),
"total_sent": fmt.Sprintf("%d", total_sent),
"total": fmt.Sprintf("%.2f", recvRate+sentRate),
"speed": fmt.Sprintf("%d", speed),
"flags": strings.Join(int.Flags, ","),
Expand All @@ -131,15 +143,15 @@ func (l *CheckNetwork) Check(_ context.Context, snc *Agent, check *CheckData, _
check.result.Metrics = append(check.result.Metrics, &CheckMetric{
ThresholdName: int.Name,
Name: fmt.Sprintf("%s_traffic_in", int.Name),
Value: IOList[intnr].BytesRecv,
Value: total_received,
Unit: "c",
Warning: check.warnThreshold,
Critical: check.critThreshold,
})
check.result.Metrics = append(check.result.Metrics, &CheckMetric{
ThresholdName: int.Name,
Name: fmt.Sprintf("%s_traffic_out", int.Name),
Value: IOList[intnr].BytesSent,
Value: total_sent,
Unit: "c",
Warning: check.warnThreshold,
Critical: check.critThreshold,
Expand Down

0 comments on commit b852d62

Please sign in to comment.