Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
93e226a5
G
Gopsutil
项目概览
Canread
/
Gopsutil
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gopsutil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
93e226a5
编写于
10月 11, 2015
作者:
S
Shirou WAKAYAMA
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
net[freebsd,linux,darwin]: merge unix like OSes.
上级
34195b4b
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
109 addition
and
248 deletion
+109
-248
common/common_darwin.go
common/common_darwin.go
+0
-33
common/common_linux.go
common/common_linux.go
+0
-40
common/common_unix.go
common/common_unix.go
+41
-0
net/net_darwin.go
net/net_darwin.go
+0
-60
net/net_freebsd.go
net/net_freebsd.go
+0
-55
net/net_linux.go
net/net_linux.go
+0
-60
net/net_unix.go
net/net_unix.go
+68
-0
未找到文件。
common/common_darwin.go
浏览文件 @
93e226a5
...
...
@@ -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
}
common/common_linux.go
浏览文件 @
93e226a5
// +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
}
common/common_unix.go
0 → 100644
浏览文件 @
93e226a5
// +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
}
net/net_darwin.go
浏览文件 @
93e226a5
...
...
@@ -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
}
net/net_freebsd.go
浏览文件 @
93e226a5
...
...
@@ -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
}
net/net_linux.go
浏览文件 @
93e226a5
...
...
@@ -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
}
net/net_unix.go
0 → 100644
浏览文件 @
93e226a5
// +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
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录