Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
57f6aebc
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,发现更多精彩内容 >>
提交
57f6aebc
编写于
5月 20, 2016
作者:
S
Shirou WAKAYAMA
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add Timeout to invoke command and use common.Invoke refs: #201
上级
bae75faa
变更
20
隐藏空白更改
内联
并排
Showing
20 changed file
with
128 addition
and
38 deletion
+128
-38
cpu/cpu.go
cpu/cpu.go
+8
-0
cpu/cpu_darwin.go
cpu/cpu_darwin.go
+2
-2
cpu/cpu_freebsd.go
cpu/cpu_freebsd.go
+1
-1
cpu/cpu_linux.go
cpu/cpu_linux.go
+1
-1
disk/disk.go
disk/disk.go
+8
-0
disk/disk_linux.go
disk/disk_linux.go
+1
-1
docker/docker.go
docker/docker.go
+11
-1
docker/docker_linux.go
docker/docker_linux.go
+2
-2
host/host.go
host/host.go
+11
-3
host/host_darwin.go
host/host_darwin.go
+2
-2
host/host_freebsd.go
host/host_freebsd.go
+2
-2
host/host_linux.go
host/host_linux.go
+1
-1
internal/common/common.go
internal/common/common.go
+62
-15
mem/mem.go
mem/mem.go
+8
-0
mem/mem_darwin_nocgo.go
mem/mem_darwin_nocgo.go
+1
-1
mem/mem_darwin_test.go
mem/mem_darwin_test.go
+1
-2
mem/mem_freebsd.go
mem/mem_freebsd.go
+1
-1
net/net_darwin.go
net/net_darwin.go
+1
-1
net/net_freebsd.go
net/net_freebsd.go
+1
-1
process/process_posix.go
process/process_posix.go
+3
-1
未找到文件。
cpu/cpu.go
浏览文件 @
57f6aebc
...
...
@@ -5,6 +5,8 @@ import (
"runtime"
"strconv"
"strings"
"github.com/shirou/gopsutil/internal/common"
)
type
TimesStat
struct
{
...
...
@@ -37,6 +39,12 @@ type InfoStat struct {
Flags
[]
string
`json:"flags"`
}
var
invoke
common
.
Invoker
func
init
()
{
invoke
=
common
.
Invoke
{}
}
var
lastCPUTimes
[]
TimesStat
var
lastPerCPUTimes
[]
TimesStat
...
...
cpu/cpu_darwin.go
浏览文件 @
57f6aebc
...
...
@@ -36,7 +36,7 @@ func Info() ([]InfoStat, error) {
if
err
!=
nil
{
return
ret
,
err
}
out
,
err
:=
exec
.
Command
(
sysctl
,
"machdep.cpu"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
sysctl
,
"machdep.cpu"
)
if
err
!=
nil
{
return
ret
,
err
}
...
...
@@ -90,7 +90,7 @@ func Info() ([]InfoStat, error) {
// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
out
,
err
=
exec
.
Command
(
sysctl
,
"hw.cpufrequency"
)
.
Output
(
)
out
,
err
=
invoke
.
Command
(
sysctl
,
"hw.cpufrequency"
)
if
err
!=
nil
{
return
ret
,
err
}
...
...
cpu/cpu_freebsd.go
浏览文件 @
57f6aebc
...
...
@@ -29,7 +29,7 @@ func init() {
if
err
!=
nil
{
return
}
out
,
err
:=
exec
.
Command
(
getconf
,
"CLK_TCK"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
getconf
,
"CLK_TCK"
)
// ignore errors
if
err
==
nil
{
i
,
err
:=
strconv
.
ParseFloat
(
strings
.
TrimSpace
(
string
(
out
)),
64
)
...
...
cpu/cpu_linux.go
浏览文件 @
57f6aebc
...
...
@@ -19,7 +19,7 @@ func init() {
if
err
!=
nil
{
return
}
out
,
err
:=
exec
.
Command
(
getconf
,
"CLK_TCK"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
getconf
,
"CLK_TCK"
)
// ignore errors
if
err
==
nil
{
i
,
err
:=
strconv
.
ParseFloat
(
strings
.
TrimSpace
(
string
(
out
)),
64
)
...
...
disk/disk.go
浏览文件 @
57f6aebc
...
...
@@ -2,8 +2,16 @@ package disk
import
(
"encoding/json"
"github.com/shirou/gopsutil/internal/common"
)
var
invoke
common
.
Invoker
func
init
()
{
invoke
=
common
.
Invoke
{}
}
type
UsageStat
struct
{
Path
string
`json:"path"`
Fstype
string
`json:"fstype"`
...
...
disk/disk_linux.go
浏览文件 @
57f6aebc
...
...
@@ -339,7 +339,7 @@ func GetDiskSerialNumber(name string) string {
return
""
}
out
,
err
:=
exec
.
Command
(
udevadm
,
"info"
,
"--query=property"
,
n
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
udevadm
,
"info"
,
"--query=property"
,
n
)
// does not return error, just an empty string
if
err
!=
nil
{
...
...
docker/docker.go
浏览文件 @
57f6aebc
package
docker
import
"errors"
import
(
"errors"
"github.com/shirou/gopsutil/internal/common"
)
var
ErrDockerNotAvailable
=
errors
.
New
(
"docker not available"
)
var
ErrCgroupNotAvailable
=
errors
.
New
(
"cgroup not available"
)
var
invoke
common
.
Invoker
func
init
()
{
invoke
=
common
.
Invoke
{}
}
type
CgroupMemStat
struct
{
ContainerID
string
`json:"containerID"`
Cache
uint64
`json:"cache"`
...
...
docker/docker_linux.go
浏览文件 @
57f6aebc
...
...
@@ -23,7 +23,7 @@ func GetDockerStat() ([]CgroupDockerStat, error) {
return
nil
,
ErrDockerNotAvailable
}
out
,
err
:=
exec
.
Command
(
path
,
"ps"
,
"-a"
,
"--no-trunc"
,
"--format"
,
"{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
path
,
"ps"
,
"-a"
,
"--no-trunc"
,
"--format"
,
"{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}"
)
if
err
!=
nil
{
return
[]
CgroupDockerStat
{},
err
}
...
...
@@ -65,7 +65,7 @@ func GetDockerIDList() ([]string, error) {
return
nil
,
ErrDockerNotAvailable
}
out
,
err
:=
exec
.
Command
(
path
,
"ps"
,
"-q"
,
"--no-trunc"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
path
,
"ps"
,
"-q"
,
"--no-trunc"
)
if
err
!=
nil
{
return
[]
string
{},
err
}
...
...
host/host.go
浏览文件 @
57f6aebc
...
...
@@ -2,17 +2,25 @@ package host
import
(
"encoding/json"
"github.com/shirou/gopsutil/internal/common"
)
var
invoke
common
.
Invoker
func
init
()
{
invoke
=
common
.
Invoke
{}
}
// A HostInfoStat describes the host status.
// This is not in the psutil but it useful.
type
InfoStat
struct
{
Hostname
string
`json:"hostname"`
Uptime
uint64
`json:"uptime"`
BootTime
uint64
`json:"bootTime"`
Procs
uint64
`json:"procs"`
// number of processes
OS
string
`json:"os"`
// ex: freebsd, linux
Platform
string
`json:"platform"`
// ex: ubuntu, linuxmint
Procs
uint64
`json:"procs"`
// number of processes
OS
string
`json:"os"`
// ex: freebsd, linux
Platform
string
`json:"platform"`
// ex: ubuntu, linuxmint
PlatformFamily
string
`json:"platformFamily"`
// ex: debian, rhel
PlatformVersion
string
`json:"platformVersion"`
VirtualizationSystem
string
`json:"virtualizationSystem"`
...
...
host/host_darwin.go
浏览文件 @
57f6aebc
...
...
@@ -131,12 +131,12 @@ func PlatformInformation() (string, string, string, error) {
if
err
!=
nil
{
return
""
,
""
,
""
,
err
}
out
,
err
:=
exec
.
Command
(
uname
,
"-s"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
uname
,
"-s"
)
if
err
==
nil
{
platform
=
strings
.
ToLower
(
strings
.
TrimSpace
(
string
(
out
)))
}
out
,
err
=
exec
.
Command
(
uname
,
"-r"
)
.
Output
(
)
out
,
err
=
invoke
.
Command
(
uname
,
"-r"
)
if
err
==
nil
{
version
=
strings
.
ToLower
(
strings
.
TrimSpace
(
string
(
out
)))
}
...
...
host/host_freebsd.go
浏览文件 @
57f6aebc
...
...
@@ -136,12 +136,12 @@ func PlatformInformation() (string, string, string, error) {
return
""
,
""
,
""
,
err
}
out
,
err
:=
exec
.
Command
(
uname
,
"-s"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
uname
,
"-s"
)
if
err
==
nil
{
platform
=
strings
.
ToLower
(
strings
.
TrimSpace
(
string
(
out
)))
}
out
,
err
=
exec
.
Command
(
uname
,
"-r"
)
.
Output
(
)
out
,
err
=
invoke
.
Command
(
uname
,
"-r"
)
if
err
==
nil
{
version
=
strings
.
ToLower
(
strings
.
TrimSpace
(
string
(
out
)))
}
...
...
host/host_linux.go
浏览文件 @
57f6aebc
...
...
@@ -164,7 +164,7 @@ func getLSB() (*LSB, error) {
if
err
!=
nil
{
return
ret
,
err
}
out
,
err
:=
exec
.
Command
(
lsb_release
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
lsb_release
)
if
err
!=
nil
{
return
ret
,
err
}
...
...
internal/common/common.go
浏览文件 @
57f6aebc
...
...
@@ -8,8 +8,10 @@ package common
// - windows (amd64)
import
(
"bufio"
"bytes"
"errors"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
...
...
@@ -19,6 +21,12 @@ import (
"runtime"
"strconv"
"strings"
"time"
)
var
(
Timeout
=
3
*
time
.
Second
TimeoutErr
=
errors
.
New
(
"Command timed out."
)
)
type
Invoker
interface
{
...
...
@@ -28,7 +36,8 @@ type Invoker interface {
type
Invoke
struct
{}
func
(
i
Invoke
)
Command
(
name
string
,
arg
...
string
)
([]
byte
,
error
)
{
return
exec
.
Command
(
name
,
arg
...
)
.
Output
()
cmd
:=
exec
.
Command
(
name
,
arg
...
)
return
CombinedOutputTimeout
(
cmd
,
Timeout
)
}
type
FakeInvoke
struct
{
...
...
@@ -118,20 +127,20 @@ func IntToString(orig []int8) string {
}
func
UintToString
(
orig
[]
uint8
)
string
{
ret
:=
make
([]
byte
,
len
(
orig
))
size
:=
-
1
for
i
,
o
:=
range
orig
{
if
o
==
0
{
size
=
i
break
}
ret
[
i
]
=
byte
(
o
)
}
if
size
==
-
1
{
size
=
len
(
orig
)
}
return
string
(
ret
[
0
:
size
])
ret
:=
make
([]
byte
,
len
(
orig
))
size
:=
-
1
for
i
,
o
:=
range
orig
{
if
o
==
0
{
size
=
i
break
}
ret
[
i
]
=
byte
(
o
)
}
if
size
==
-
1
{
size
=
len
(
orig
)
}
return
string
(
ret
[
0
:
size
])
}
func
ByteToString
(
orig
[]
byte
)
string
{
...
...
@@ -294,3 +303,41 @@ func HostSys(combineWith ...string) string {
func
HostEtc
(
combineWith
...
string
)
string
{
return
GetEnv
(
"HOST_ETC"
,
"/etc"
,
combineWith
...
)
}
// CombinedOutputTimeout runs the given command with the given timeout and
// returns the combined output of stdout and stderr.
// If the command times out, it attempts to kill the process.
// copied from https://github.com/influxdata/telegraf
func
CombinedOutputTimeout
(
c
*
exec
.
Cmd
,
timeout
time
.
Duration
)
([]
byte
,
error
)
{
var
b
bytes
.
Buffer
c
.
Stdout
=
&
b
c
.
Stderr
=
&
b
if
err
:=
c
.
Start
();
err
!=
nil
{
return
nil
,
err
}
err
:=
WaitTimeout
(
c
,
timeout
)
return
b
.
Bytes
(),
err
}
// WaitTimeout waits for the given command to finish with a timeout.
// It assumes the command has already been started.
// If the command times out, it attempts to kill the process.
// copied from https://github.com/influxdata/telegraf
func
WaitTimeout
(
c
*
exec
.
Cmd
,
timeout
time
.
Duration
)
error
{
timer
:=
time
.
NewTimer
(
timeout
)
done
:=
make
(
chan
error
)
go
func
()
{
done
<-
c
.
Wait
()
}()
select
{
case
err
:=
<-
done
:
timer
.
Stop
()
return
err
case
<-
timer
.
C
:
if
err
:=
c
.
Process
.
Kill
();
err
!=
nil
{
log
.
Printf
(
"FATAL error killing process: %s"
,
err
)
return
err
}
// wait for the command to return after killing it
<-
done
return
TimeoutErr
}
}
mem/mem.go
浏览文件 @
57f6aebc
...
...
@@ -2,8 +2,16 @@ package mem
import
(
"encoding/json"
"github.com/shirou/gopsutil/internal/common"
)
var
invoke
common
.
Invoker
func
init
()
{
invoke
=
common
.
Invoke
{}
}
// Memory usage statistics. Total, Available and Used contain numbers of bytes
// for human consumption.
//
...
...
mem/mem_darwin_nocgo.go
浏览文件 @
57f6aebc
...
...
@@ -16,7 +16,7 @@ func getVMStat(vms *VirtualMemoryStat) error {
if
err
!=
nil
{
return
err
}
out
,
err
:=
exec
.
Command
(
vm_stat
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
vm_stat
)
if
err
!=
nil
{
return
err
}
...
...
mem/mem_darwin_test.go
浏览文件 @
57f6aebc
...
...
@@ -3,7 +3,6 @@
package
mem
import
(
"os/exec"
"strconv"
"strings"
"testing"
...
...
@@ -15,7 +14,7 @@ func TestVirtualMemoryDarwin(t *testing.T) {
v
,
err
:=
VirtualMemory
()
assert
.
Nil
(
t
,
err
)
outBytes
,
err
:=
exec
.
Command
(
"/usr/sbin/sysctl"
,
"hw.memsize"
)
.
Output
(
)
outBytes
,
err
:=
invoke
.
Command
(
"/usr/sbin/sysctl"
,
"hw.memsize"
)
assert
.
Nil
(
t
,
err
)
outString
:=
string
(
outBytes
)
outString
=
strings
.
TrimSpace
(
outString
)
...
...
mem/mem_freebsd.go
浏览文件 @
57f6aebc
...
...
@@ -93,7 +93,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
return
nil
,
err
}
out
,
err
:=
exec
.
Command
(
swapinfo
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
swapinfo
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
net/net_darwin.go
浏览文件 @
57f6aebc
...
...
@@ -21,7 +21,7 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
if
err
!=
nil
{
return
nil
,
err
}
out
,
err
:=
exec
.
Command
(
netstat
,
"-ibdnW"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
netstat
,
"-ibdnW"
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
net/net_freebsd.go
浏览文件 @
57f6aebc
...
...
@@ -16,7 +16,7 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
if
err
!=
nil
{
return
nil
,
err
}
out
,
err
:=
exec
.
Command
(
netstat
,
"-ibdnW"
)
.
Output
(
)
out
,
err
:=
invoke
.
Command
(
netstat
,
"-ibdnW"
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
process/process_posix.go
浏览文件 @
57f6aebc
...
...
@@ -9,6 +9,8 @@ import (
"strconv"
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
)
// POSIX
...
...
@@ -72,7 +74,7 @@ func (p *Process) SendSignal(sig syscall.Signal) error {
}
cmd
:=
exec
.
Command
(
kill
,
"-s"
,
sigAsStr
,
strconv
.
Itoa
(
int
(
p
.
Pid
)))
cmd
.
Stderr
=
os
.
Stderr
err
=
c
md
.
Run
(
)
err
=
c
ommon
.
WaitTimeout
(
cmd
,
common
.
Timeout
)
if
err
!=
nil
{
return
err
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录