Skip to content

Commit

Permalink
Merge pull request #4 from jedipunkz/fix/ioutil
Browse files Browse the repository at this point in the history
fix ioutil deprecated warn
  • Loading branch information
jedipunkz authored Apr 15, 2023
2 parents 80da27f + a731986 commit e4827cb
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions internal/collector/net_collector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package collector

import (
"io/ioutil"
"io"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -52,12 +53,12 @@ func (collector *NetCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (collector *NetCollector) Collect(ch chan<- prometheus.Metric) {
data, err := ioutil.ReadFile("/proc/net/dev")
data, err := getNetSample()
if err != nil {
return
}

lines := strings.Split(string(data), "\n")
lines := strings.Split(data, "\n")

for i, line := range lines {
if i < 2 {
Expand Down Expand Up @@ -98,3 +99,19 @@ func (collector *NetCollector) Collect(ch chan<- prometheus.Metric) {
collector.lastTransmitPackets[interfaceName] = transmitPackets
}
}

func getNetSample() (string, error) {
file, err := os.Open("/proc/net/dev")
if err != nil {
return "", err
}
defer file.Close()

var sb strings.Builder
_, err = io.Copy(&sb, file)
if err != nil {
return "", err
}

return sb.String(), nil
}

0 comments on commit e4827cb

Please sign in to comment.