提交 b3cf7354 编写于 作者: S Shirou WAKAYAMA

process[linux, darwin]: implements Connections using lsof.

上级 a369a885
......@@ -186,8 +186,9 @@ threads
cpu_percent
cpu_affinity
memory_percent
parent x x
children
connections
connections x x
is_running
================ ===== ======= ====== =======
......@@ -231,7 +232,6 @@ hostname x x x x
- wait_procs
- Process class
- parent (use ppid instead)
- as_dict
- wait
......
......@@ -74,6 +74,10 @@ func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) {
}
out, err := invoke.Command(lsof, cmd...)
if err != nil {
// if not pid found, lsof returnes code 1
if err.Error() == "exit status 1" && len(out) == 0 {
return []string{}, nil
}
return []string{}, err
}
lines := strings.Split(string(out), "\n")
......
......@@ -8,6 +8,8 @@ import (
"strings"
)
// CallLsof invokes lsof to get connection informations.
// This is same as darwin currently.
func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) {
var cmd []string
if pid == 0 { // will get from all processes.
......@@ -22,6 +24,10 @@ func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) {
}
out, err := invoke.Command(lsof, cmd...)
if err != nil {
// if not pid found, lsof returnes code 1
if err.Error() == "exit status 1" && len(out) == 0 {
return []string{}, nil
}
return []string{}, err
}
lines := strings.Split(string(out), "\n")
......
......@@ -91,8 +91,13 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
return ret, nil
}
// Return a list of network connections opened by a process
// Return a list of network connections opened.
func NetConnections(kind string) ([]NetConnectionStat, error) {
return NetConnectionsPid(kind, 0)
}
// Return a list of network connections opened by a process.
func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) {
var ret []NetConnectionStat
args := []string{"-i"}
......@@ -126,7 +131,7 @@ func NetConnections(kind string) ([]NetConnectionStat, error) {
}
// we can not use -F filter to get all of required information at once.
r, err := common.CallLsof(invoke, 0, args...)
r, err := common.CallLsof(invoke, pid, args...)
if err != nil {
return nil, err
}
......
......@@ -90,7 +90,13 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
return ret, nil
}
// Return a list of network connections opened.
func NetConnections(kind string) ([]NetConnectionStat, error) {
return NetConnectionsPid(kind, 0)
}
// Return a list of network connections opened by a process.
func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) {
var ret []NetConnectionStat
args := []string{"-i"}
......@@ -124,7 +130,7 @@ func NetConnections(kind string) ([]NetConnectionStat, error) {
}
// we can not use -F filter to get all of required information at once.
r, err := common.CallLsof(invoke, 0, args...)
r, err := common.CallLsof(invoke, pid, args...)
if err != nil {
return nil, err
}
......
......@@ -286,7 +286,7 @@ func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
}
func (p *Process) Connections() ([]net.NetConnectionStat, error) {
return nil, common.NotImplementedError
return net.NetConnectionsPid("all", p.Pid)
}
func (p *Process) IsRunning() (bool, error) {
......
......@@ -211,7 +211,7 @@ func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
}
func (p *Process) Connections() ([]net.NetConnectionStat, error) {
return nil, common.NotImplementedError
return net.NetConnectionsPid("all", p.Pid)
}
func (p *Process) IsRunning() (bool, error) {
......
......@@ -97,7 +97,7 @@ func Test_Process_MemoryInfo(t *testing.T) {
v, err := p.MemoryInfo()
if err != nil {
t.Errorf("geting ppid error %v", err)
t.Errorf("geting memory info error %v", err)
}
empty := MemoryInfoStat{}
if v == nil || *v == empty {
......@@ -110,7 +110,7 @@ func Test_Process_CmdLine(t *testing.T) {
v, err := p.Cmdline()
if err != nil {
t.Errorf("geting ppid error %v", err)
t.Errorf("geting cmdline error %v", err)
}
if !strings.Contains(v, "process.test") {
t.Errorf("invalid cmd line %v", v)
......@@ -134,7 +134,7 @@ func Test_Process_Status(t *testing.T) {
v, err := p.Status()
if err != nil {
t.Errorf("geting ppid error %v", err)
t.Errorf("geting status error %v", err)
}
if !strings.HasPrefix(v, "S") && v != "running" && v != "sleeping" {
t.Errorf("could not get state %v", v)
......@@ -289,3 +289,18 @@ func Test_Parent(t *testing.T) {
t.Fatalf("wrong parent pid")
}
}
func Test_Connections(t *testing.T) {
p := testGetProcess()
c, err := p.Connections()
if err != nil {
t.Fatalf("error %v", err)
}
// TODO:
// Since go test open no conneciton, ret is empty.
// should invoke child process or other solutions.
if len(c) != 0 {
t.Fatalf("wrong connections")
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册