From 3ff690d1d81c8dca36ddef66ec13b4542551c2f5 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Thu, 1 Jan 2015 21:54:45 +0900 Subject: [PATCH] make single network interface about NetIOCounters on darwin and freebsd --- net/net_darwin.go | 9 +++++++++ net/net_freebsd.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/net/net_darwin.go b/net/net_darwin.go index bde740b5e..2c4be7661 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -6,6 +6,8 @@ import ( "os/exec" "strconv" "strings" + + "github.com/shirou/gopsutil/common" ) func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { @@ -16,6 +18,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { lines := strings.Split(string(out), "\n") ret := make([]NetIOCountersStat, 0, len(lines)-1) + exists := make([]string, 0, len(ret)) for _, line := range lines { values := strings.Fields(line) @@ -23,6 +26,12 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { // skip first line continue } + if common.StringContains(exists, values[0]) { + // skip if already get + continue + } + exists = append(exists, values[0]) + base := 1 // sometimes Address is ommitted if len(values) < 11 { diff --git a/net/net_freebsd.go b/net/net_freebsd.go index f1d44fe70..b9e14286b 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -6,6 +6,8 @@ import ( "os/exec" "strconv" "strings" + + "github.com/shirou/gopsutil/common" ) func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { @@ -16,12 +18,19 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { lines := strings.Split(string(out), "\n") ret := make([]NetIOCountersStat, 0, len(lines)-1) + exists := make([]string, 0, len(ret)) for _, line := range lines { values := strings.Fields(line) if len(values) < 1 || values[0] == "Name" { continue } + if common.StringContains(exists, values[0]) { + // skip if already get + continue + } + exists = append(exists, values[0]) + base := 1 // sometimes Address is ommitted if len(values) < 13 {