1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00
shirou_gopsutil/net/net_windows.go

121 lines
3.4 KiB
Go
Raw Normal View History

2014-05-12 12:10:28 +09:00
// +build windows
2014-12-30 22:09:05 +09:00
package net
2014-05-12 12:10:28 +09:00
import (
2017-12-31 15:25:49 +09:00
"context"
2015-11-19 14:19:43 -07:00
"errors"
2014-05-16 11:33:35 +09:00
"net"
"os"
2015-10-20 00:04:57 +09:00
"github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows"
2014-05-12 12:10:28 +09:00
)
var (
modiphlpapi = windows.NewLazyDLL("iphlpapi.dll")
2016-04-01 21:34:39 +09:00
procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
)
const (
2014-08-15 20:05:26 +02:00
TCPTableBasicListener = iota
TCPTableBasicConnections
TCPTableBasicAll
TCPTableOwnerPIDListener
TCPTableOwnerPIDConnections
TCPTableOwnerPIDAll
TCPTableOwnerModuleListener
TCPTableOwnerModuleConnections
TCPTableOwnerModuleAll
)
func IOCounters(pernic bool) ([]IOCountersStat, error) {
2017-12-31 15:25:49 +09:00
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
2014-05-16 11:33:35 +09:00
ifs, err := net.Interfaces()
if err != nil {
return nil, err
}
var ret []IOCountersStat
2014-05-16 11:33:35 +09:00
for _, ifi := range ifs {
c := IOCountersStat{
Name: ifi.Name,
}
2014-05-16 11:33:35 +09:00
row := windows.MibIfRow{Index: uint32(ifi.Index)}
e := windows.GetIfEntry(&row)
if e != nil {
return nil, os.NewSyscallError("GetIfEntry", e)
2014-05-16 11:33:35 +09:00
}
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)
2014-05-16 11:33:35 +09:00
}
2015-01-01 21:29:25 +09:00
if pernic == false {
return getIOCountersAll(ret)
2015-01-01 21:29:25 +09:00
}
2014-05-16 11:33:35 +09:00
return ret, nil
}
// NetIOCountersByFile is an method which is added just a compatibility for linux.
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
2017-12-31 15:25:49 +09:00
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic)
}
// Return a list of network connections opened by a process
func Connections(kind string) ([]ConnectionStat, error) {
2017-12-31 15:25:49 +09:00
return ConnectionsWithContext(context.Background(), kind)
}
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
var ret []ConnectionStat
2016-04-01 21:34:39 +09:00
return ret, common.ErrNotImplementedError
}
// Return a list of network connections opened returning at most `max`
// connections for each running process.
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
2017-12-31 15:25:49 +09:00
return ConnectionsMaxWithContext(context.Background(), kind, max)
}
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
return []ConnectionStat{}, common.ErrNotImplementedError
}
func FilterCounters() ([]FilterStat, error) {
2017-12-31 15:25:49 +09:00
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for windows")
}
2015-11-19 14:19:43 -07:00
// NetProtoCounters returns network statistics for the entire system
// If protocols is empty then all protocols are returned, otherwise
// just the protocols in the list are returned.
// Not Implemented for Windows
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
2017-12-31 15:25:49 +09:00
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
2015-11-19 14:19:43 -07:00
return nil, errors.New("NetProtoCounters not implemented for windows")
}