From 93e226a5e3139e20202fda2fdc1389ef71afb1cd Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Sun, 11 Oct 2015 21:57:53 +0900 Subject: [PATCH] net[freebsd,linux,darwin]: merge unix like OSes. --- common/common_darwin.go | 33 ------------------------ common/common_linux.go | 40 ----------------------------- common/common_unix.go | 41 +++++++++++++++++++++++++++++ net/net_darwin.go | 60 ------------------------------------------- net/net_freebsd.go | 55 --------------------------------------- net/net_linux.go | 60 ------------------------------------------- net/net_unix.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 109 insertions(+), 248 deletions(-) create mode 100644 common/common_unix.go create mode 100644 net/net_unix.go diff --git a/common/common_darwin.go b/common/common_darwin.go index 4d41382..7d6f3c6 100644 --- a/common/common_darwin.go +++ b/common/common_darwin.go @@ -4,7 +4,6 @@ package common import ( "os/exec" - "strconv" "strings" "syscall" "unsafe" @@ -59,35 +58,3 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) { return buf, length, nil } - -func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { - var cmd []string - if pid == 0 { // will get from all processes. - cmd = []string{"-a", "-n", "-P"} - } else { - cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))} - } - cmd = append(cmd, args...) - lsof, err := exec.LookPath("lsof") - if err != nil { - return []string{}, err - } - out, err := invoke.Command(lsof, cmd...) - if err != nil { - // if no 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") - - var ret []string - for _, l := range lines[1:] { - if len(l) == 0 { - continue - } - ret = append(ret, l) - } - return ret, nil -} diff --git a/common/common_linux.go b/common/common_linux.go index bfd96a9..0a122e9 100644 --- a/common/common_linux.go +++ b/common/common_linux.go @@ -1,43 +1,3 @@ // +build linux package common - -import ( - "os/exec" - "strconv" - "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. - cmd = []string{"-a", "-n", "-P"} - } else { - cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))} - } - cmd = append(cmd, args...) - lsof, err := exec.LookPath("lsof") - if err != nil { - return []string{}, err - } - 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") - - var ret []string - for _, l := range lines[1:] { - if len(l) == 0 { - continue - } - ret = append(ret, l) - } - return ret, nil -} diff --git a/common/common_unix.go b/common/common_unix.go new file mode 100644 index 0000000..66284cc --- /dev/null +++ b/common/common_unix.go @@ -0,0 +1,41 @@ +// +build linux freebsd darwin + +package common + +import ( + "os/exec" + "strconv" + "strings" +) + +func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { + var cmd []string + if pid == 0 { // will get from all processes. + cmd = []string{"-a", "-n", "-P"} + } else { + cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))} + } + cmd = append(cmd, args...) + lsof, err := exec.LookPath("lsof") + if err != nil { + return []string{}, err + } + out, err := invoke.Command(lsof, cmd...) + if err != nil { + // if no 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") + + var ret []string + for _, l := range lines[1:] { + if len(l) == 0 { + continue + } + ret = append(ret, l) + } + return ret, nil +} diff --git a/net/net_darwin.go b/net/net_darwin.go index b9275e8..65c21f8 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -90,63 +90,3 @@ 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"} - switch strings.ToLower(kind) { - default: - fallthrough - case "": - fallthrough - case "all": - fallthrough - case "inet": - args = append(args, "tcp", "-i", "udp") - case "inet4": - args = append(args, "4") - case "inet6": - args = append(args, "6") - case "tcp": - args = append(args, "tcp") - case "tcp4": - args = append(args, "4tcp") - case "tcp6": - args = append(args, "6tcp") - case "udp": - args = append(args, "udp") - case "udp4": - args = append(args, "6udp") - case "udp6": - args = append(args, "6udp") - case "unix": - return ret, common.NotImplementedError - } - - // we can not use -F filter to get all of required information at once. - r, err := common.CallLsof(invoke, pid, args...) - if err != nil { - return nil, err - } - for _, rr := range r { - if strings.HasPrefix(rr, "COMMAND") { - continue - } - n, err := parseNetLine(rr) - if err != nil { - // fmt.Println(err) // TODO: should debug print? - continue - } - - ret = append(ret, n) - } - - return ret, nil -} diff --git a/net/net_freebsd.go b/net/net_freebsd.go index 54dc1b5..703d321 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -81,58 +81,3 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { return ret, nil } - -// Return a list of network connections opened by a process. -func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) { - var ret []NetConnectionStat - - args := []string{"-i"} - switch strings.ToLower(kind) { - default: - fallthrough - case "": - fallthrough - case "all": - fallthrough - case "inet": - args = append(args, "tcp", "-i", "udp") - case "inet4": - args = append(args, "4") - case "inet6": - args = append(args, "6") - case "tcp": - args = append(args, "tcp") - case "tcp4": - args = append(args, "4tcp") - case "tcp6": - args = append(args, "6tcp") - case "udp": - args = append(args, "udp") - case "udp4": - args = append(args, "6udp") - case "udp6": - args = append(args, "6udp") - case "unix": - return ret, common.NotImplementedError - } - - // we can not use -F filter to get all of required information at once. - r, err := common.CallLsof(invoke, pid, args...) - if err != nil { - return nil, err - } - for _, rr := range r { - if strings.HasPrefix(rr, "COMMAND") { - continue - } - n, err := parseNetLine(rr) - if err != nil { - // fmt.Println(err) // TODO: should debug print? - continue - } - - ret = append(ret, n) - } - - return ret, nil -} diff --git a/net/net_linux.go b/net/net_linux.go index 7c1d015..9184439 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -89,63 +89,3 @@ 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"} - switch strings.ToLower(kind) { - default: - fallthrough - case "": - fallthrough - case "all": - fallthrough - case "inet": - args = append(args, "tcp", "-i", "udp") - case "inet4": - args = append(args, "4") - case "inet6": - args = append(args, "6") - case "tcp": - args = append(args, "tcp") - case "tcp4": - args = append(args, "4tcp") - case "tcp6": - args = append(args, "6tcp") - case "udp": - args = append(args, "udp") - case "udp4": - args = append(args, "6udp") - case "udp6": - args = append(args, "6udp") - case "unix": - return ret, common.NotImplementedError - } - - // we can not use -F filter to get all of required information at once. - r, err := common.CallLsof(invoke, pid, args...) - if err != nil { - return nil, err - } - for _, rr := range r { - if strings.HasPrefix(rr, "COMMAND") { - continue - } - n, err := parseNetLine(rr) - if err != nil { - // fmt.Println(err) // TODO: should debug print? - continue - } - - ret = append(ret, n) - } - - return ret, nil -} diff --git a/net/net_unix.go b/net/net_unix.go new file mode 100644 index 0000000..0b8a844 --- /dev/null +++ b/net/net_unix.go @@ -0,0 +1,68 @@ +// +build linux freebsd darwin + +package net + +import ( + "strings" + + "github.com/shirou/gopsutil/common" +) + +// 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"} + switch strings.ToLower(kind) { + default: + fallthrough + case "": + fallthrough + case "all": + fallthrough + case "inet": + args = append(args, "tcp", "-i", "udp") + case "inet4": + args = append(args, "4") + case "inet6": + args = append(args, "6") + case "tcp": + args = append(args, "tcp") + case "tcp4": + args = append(args, "4tcp") + case "tcp6": + args = append(args, "6tcp") + case "udp": + args = append(args, "udp") + case "udp4": + args = append(args, "6udp") + case "udp6": + args = append(args, "6udp") + case "unix": + return ret, common.NotImplementedError + } + + r, err := common.CallLsof(invoke, pid, args...) + if err != nil { + return nil, err + } + for _, rr := range r { + if strings.HasPrefix(rr, "COMMAND") { + continue + } + n, err := parseNetLine(rr) + if err != nil { + + continue + } + + ret = append(ret, n) + } + + return ret, nil +}