Fix NetIOCounter windows interface behavior

addresses a few things:

- Windows has a concept of both a network "interface" and an "adapter"
- These are almost always a one-to-one relationship, though there can be
esoteric instances where they are not.
- I believe the gopsutil NetIOCounters function should only return on a
per-interface level, since this is the behavior on linux/darwin.

Previously, the plugin was basically ignoring the actual interfaces
returned from net.Interfaces(). Instead, it was looping over the net
adapters for each interface, somewhat uselessly.

FWIW, the code for getAdapterList() doesn't exist in the Go standard lib
anymore.

closes #245
pull/275/head
Cameron Sparr 9 years ago
parent 6f43e5d707
commit 5af5f08785

@ -7,7 +7,6 @@ import (
"net" "net"
"os" "os"
"syscall" "syscall"
"unsafe"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
) )
@ -35,22 +34,14 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ai, err := getAdapterList()
if err != nil {
return nil, err
}
var ret []IOCountersStat var ret []IOCountersStat
for _, ifi := range ifs { for _, ifi := range ifs {
name := ifi.Name
for ; ai != nil; ai = ai.Next {
name = common.BytePtrToString(&ai.Description[0])
c := IOCountersStat{ c := IOCountersStat{
Name: name, Name: ifi.Name,
} }
row := syscall.MibIfRow{Index: ai.Index} row := syscall.MibIfRow{Index: uint32(ifi.Index)}
e := syscall.GetIfEntry(&row) e := syscall.GetIfEntry(&row)
if e != nil { if e != nil {
return nil, os.NewSyscallError("GetIfEntry", e) return nil, os.NewSyscallError("GetIfEntry", e)
@ -66,7 +57,6 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
ret = append(ret, c) ret = append(ret, c)
} }
}
if pernic == false { if pernic == false {
return getIOCountersAll(ret) return getIOCountersAll(ret)
@ -86,23 +76,6 @@ func Connections(kind string) ([]ConnectionStat, error) {
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
// borrowed from src/pkg/net/interface_windows.go
func getAdapterList() (*syscall.IpAdapterInfo, error) {
b := make([]byte, 1000)
l := uint32(len(b))
a := (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
err := syscall.GetAdaptersInfo(a, &l)
if err == syscall.ERROR_BUFFER_OVERFLOW {
b = make([]byte, l)
a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
err = syscall.GetAdaptersInfo(a, &l)
}
if err != nil {
return nil, os.NewSyscallError("GetAdaptersInfo", err)
}
return a, nil
}
func FilterCounters() ([]FilterStat, error) { func FilterCounters() ([]FilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for windows") return nil, errors.New("NetFilterCounters not implemented for windows")
} }

Loading…
Cancel
Save