提交 855ec85d 编写于 作者: W WAKAYAMA Shirou

implement net_io_counters on Windows.

上级 d5a476f6
...@@ -9,6 +9,7 @@ package gopsutil ...@@ -9,6 +9,7 @@ package gopsutil
import ( import (
"bufio" "bufio"
"os" "os"
"reflect"
"strconv" "strconv"
"strings" "strings"
) )
...@@ -77,3 +78,26 @@ func stringContains(target []string, src string) bool { ...@@ -77,3 +78,26 @@ func stringContains(target []string, src string) bool {
} }
return false return false
} }
// get struct attributes.
// This method is used only for debugging platform dependent code.
func attributes(m interface{}) map[string]reflect.Type {
typ := reflect.TypeOf(m)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
attrs := make(map[string]reflect.Type)
if typ.Kind() != reflect.Struct {
return nil
}
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
attrs[p.Name] = p.Type
}
}
return attrs
}
...@@ -4,6 +4,7 @@ package gopsutil ...@@ -4,6 +4,7 @@ package gopsutil
import ( import (
"syscall" "syscall"
"unsafe"
) )
var ( var (
...@@ -18,3 +19,13 @@ type FILETIME struct { ...@@ -18,3 +19,13 @@ type FILETIME struct {
DwLowDateTime uint32 DwLowDateTime uint32
DwHighDateTime uint32 DwHighDateTime uint32
} }
// borrowed from net/interface_windows.go
func bytePtrToString(p *uint8) string {
a := (*[10000]uint8)(unsafe.Pointer(p))
i := 0
for a[i] != 0 {
i++
}
return string(a[:i])
}
...@@ -3,9 +3,65 @@ ...@@ -3,9 +3,65 @@
package gopsutil package gopsutil
import ( import (
"errors" "net"
"os"
"syscall"
"unsafe"
) )
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
return nil, errors.New("not implemented yet") ifs, err := net.Interfaces()
if err != nil {
return nil, err
}
ai, err := getAdapterList()
if err != nil {
return nil, err
}
var ret []NetIOCountersStat
for _, ifi := range ifs {
name := ifi.Name
for ; ai != nil; ai = ai.Next {
name = bytePtrToString(&ai.Description[0])
c := NetIOCountersStat{
Name: name,
}
row := syscall.MibIfRow{Index: ai.Index}
e := syscall.GetIfEntry(&row)
if e != nil {
return nil, os.NewSyscallError("GetIfEntry", e)
}
c.BytesSent = uint64(row.OutOctets)
c.BytesRecv = uint64(row.InOctets)
c.PacketsSent = uint64(row.OutUcastPkts)
c.PacketsRecv = uint64(row.InUcastPkts)
c.Errin = uint64(row.InErrors)
c.Errout = uint64(row.OutErrors)
c.Dropin = uint64(row.InDiscards)
c.Dropout = uint64(row.OutDiscards)
ret = append(ret, c)
}
}
return ret, nil
}
// 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
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册