mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
[net]linux: add process_unix for NetConnections.
This commit is contained in:
parent
32c62b5d48
commit
e4ddff09d5
@ -273,6 +273,7 @@ type connTmp struct {
|
|||||||
status string
|
status string
|
||||||
pid int32
|
pid int32
|
||||||
boundPid int32
|
boundPid int32
|
||||||
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a list of network connections opened.
|
// Return a list of network connections opened.
|
||||||
@ -320,7 +321,7 @@ func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, c := range ls {
|
for _, c := range ls {
|
||||||
ret = append(ret, NetConnectionStat{
|
conn := NetConnectionStat{
|
||||||
Fd: c.fd,
|
Fd: c.fd,
|
||||||
Family: t.family,
|
Family: t.family,
|
||||||
Type: t.sockType,
|
Type: t.sockType,
|
||||||
@ -328,7 +329,13 @@ func NetConnectionsPid(kind string, pid int32) ([]NetConnectionStat, error) {
|
|||||||
Raddr: c.raddr,
|
Raddr: c.raddr,
|
||||||
Status: c.status,
|
Status: c.status,
|
||||||
Pid: c.pid,
|
Pid: c.pid,
|
||||||
})
|
}
|
||||||
|
if c.pid == 0 {
|
||||||
|
conn.Pid = c.boundPid
|
||||||
|
} else {
|
||||||
|
conn.Pid = c.pid
|
||||||
|
}
|
||||||
|
ret = append(ret, conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -493,8 +500,8 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// skip first line
|
|
||||||
var ret []connTmp
|
var ret []connTmp
|
||||||
|
// skip first line
|
||||||
for _, line := range lines[1:] {
|
for _, line := range lines[1:] {
|
||||||
l := strings.Fields(line)
|
l := strings.Fields(line)
|
||||||
if len(l) < 10 {
|
if len(l) < 10 {
|
||||||
@ -543,6 +550,49 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processUnix(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
|
func processUnix(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
|
||||||
|
lines, err := common.ReadLines(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []connTmp
|
||||||
|
// skip first line
|
||||||
|
for _, line := range lines[1:] {
|
||||||
|
tokens := strings.Fields(line)
|
||||||
|
if len(tokens) < 7 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
st, err := strconv.Atoi(tokens[4])
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
inode := tokens[6]
|
||||||
|
|
||||||
|
var pairs []inodeMap
|
||||||
|
pairs, exists := inodes[inode]
|
||||||
|
if !exists {
|
||||||
|
pairs = []inodeMap{}
|
||||||
|
}
|
||||||
|
for _, pair := range pairs {
|
||||||
|
if filterPid > 0 && filterPid != pair.pid {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var path string
|
||||||
|
if len(tokens) == 8 {
|
||||||
|
path = tokens[len(tokens)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = append(ret, connTmp{
|
||||||
|
family: kind.family,
|
||||||
|
sockType: uint32(st),
|
||||||
|
raddr: Addr{},
|
||||||
|
pid: pair.pid,
|
||||||
|
status: "NONE",
|
||||||
|
path: path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return []connTmp{}, nil
|
return []connTmp{}, nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -10,8 +11,7 @@ import (
|
|||||||
|
|
||||||
func TestGetProcInodes(t *testing.T) {
|
func TestGetProcInodes(t *testing.T) {
|
||||||
root := common.HostProc("")
|
root := common.HostProc("")
|
||||||
// checkPid := os.Getpid() // process.test
|
checkPid := os.Getpid() // process.test
|
||||||
checkPid := 13378
|
|
||||||
|
|
||||||
v, err := getProcInodes(root, int32(checkPid))
|
v, err := getProcInodes(root, int32(checkPid))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user