From d680853370327f092359df9b2b62797255cefefc Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Thu, 21 Jan 2016 17:31:05 +0900 Subject: [PATCH] net,process[linux]: add process.NetIOCounter and net.NetIOCounterByFile. --- net/net_darwin.go | 9 +++++++++ net/net_freebsd.go | 9 +++++++++ net/net_linux.go | 4 ++++ net/net_windows.go | 9 +++++++++ process/process_darwin.go | 4 ++++ process/process_freebsd.go | 4 ++++ process/process_linux.go | 5 +++++ process/process_windows.go | 4 ++++ 8 files changed, 48 insertions(+) diff --git a/net/net_darwin.go b/net/net_darwin.go index 547286b..51257fb 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -92,6 +92,15 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { return ret, nil } +// NetIOCountersByFile is an method which is added just a compatibility for linux. +func NetIOCountersByFile(pernic bool, filename string) ([]NetIOCountersStat, error) { + return NetIOCounters(pernic) +} + +func NetFilterCounters() ([]NetFilterStat, error) { + return nil, errors.New("NetFilterCounters not implemented for darwin") +} + // 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. diff --git a/net/net_freebsd.go b/net/net_freebsd.go index 1b0375c..08957ae 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -86,6 +86,15 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { return ret, nil } +// NetIOCountersByFile is an method which is added just a compatibility for linux. +func NetIOCountersByFile(pernic bool, filename string) ([]NetIOCountersStat, error) { + return NetIOCounters(pernic) +} + +func NetFilterCounters() ([]NetFilterStat, error) { + return nil, errors.New("NetFilterCounters not implemented for freebsd") +} + // 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. diff --git a/net/net_linux.go b/net/net_linux.go index b15e03b..e2e217d 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -17,6 +17,10 @@ import ( // separately. func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { filename := common.HostProc("net/dev") + return NetIOCountersByFile(pernic, filename) +} + +func NetIOCountersByFile(pernic bool, filename string) ([]NetIOCountersStat, error) { lines, err := common.ReadLines(filename) if err != nil { return nil, err diff --git a/net/net_windows.go b/net/net_windows.go index 33aceb6..8c6b8b4 100644 --- a/net/net_windows.go +++ b/net/net_windows.go @@ -74,6 +74,11 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { return ret, nil } +// NetIOCountersByFile is an method which is added just a compatibility for linux. +func NetIOCountersByFile(pernic bool, filename string) ([]NetIOCountersStat, error) { + return NetIOCounters(pernic) +} + // Return a list of network connections opened by a process func NetConnections(kind string) ([]NetConnectionStat, error) { var ret []NetConnectionStat @@ -98,6 +103,10 @@ func getAdapterList() (*syscall.IpAdapterInfo, error) { return a, nil } +func NetFilterCounters() ([]NetFilterStat, error) { + return nil, errors.New("NetFilterCounters not implemented for windows") +} + // 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. diff --git a/process/process_darwin.go b/process/process_darwin.go index 01cb66c..75d5aff 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -301,6 +301,10 @@ func (p *Process) Connections() ([]net.NetConnectionStat, error) { return net.NetConnectionsPid("all", p.Pid) } +func (p *Process) NetIOCounters(pernic bool) ([]net.NetIOCountersStat, error) { + return nil, common.NotImplementedError +} + func (p *Process) IsRunning() (bool, error) { return true, common.NotImplementedError } diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 857e103..ab7f5da 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -223,6 +223,10 @@ func (p *Process) Connections() ([]net.NetConnectionStat, error) { return nil, common.NotImplementedError } +func (p *Process) NetIOCounters(pernic bool) ([]net.NetIOCountersStat, error) { + return nil, common.NotImplementedError +} + func (p *Process) IsRunning() (bool, error) { return true, common.NotImplementedError } diff --git a/process/process_linux.go b/process/process_linux.go index f54af9f..6ff2bc1 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -235,6 +235,11 @@ func (p *Process) Connections() ([]net.NetConnectionStat, error) { return net.NetConnectionsPid("all", p.Pid) } +func (p *Process) NetIOCounters(pernic bool) ([]net.NetIOCountersStat, error) { + filename := common.HostProc(strconv.Itoa(int(p.Pid)), "net/dev") + return net.NetIOCountersByFile(pernic, filename) +} + func (p *Process) IsRunning() (bool, error) { return true, common.NotImplementedError } diff --git a/process/process_windows.go b/process/process_windows.go index 2f1f18f..987a20a 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -243,6 +243,10 @@ func (p *Process) Connections() ([]net.NetConnectionStat, error) { return nil, common.NotImplementedError } +func (p *Process) NetIOCounters(pernic bool) ([]net.NetIOCountersStat, error) { + return nil, common.NotImplementedError +} + func (p *Process) IsRunning() (bool, error) { return true, common.NotImplementedError }