mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-10 19:29:14 +08:00
Merge branch 'master' of github.com:shirou/gopsutil
This commit is contained in:
commit
c8ee4d9b07
@ -16,6 +16,7 @@ Available archtectures
|
||||
|
||||
- FreeBSD/amd64
|
||||
- Linux/amd64
|
||||
- Linux/arm (raspberry pi)
|
||||
- Windows/amd64
|
||||
|
||||
(I do not have a darwin machine)
|
||||
|
24
common.go
24
common.go
@ -9,6 +9,7 @@ package gopsutil
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -77,3 +78,26 @@ func stringContains(target []string, src string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// get struct attributes.
|
||||
// This method is used only for debugging platform dependent code.
|
||||
func attributes(m interface{}) map[string]reflect.Type {
|
||||
typ := reflect.TypeOf(m)
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
|
||||
attrs := make(map[string]reflect.Type)
|
||||
if typ.Kind() != reflect.Struct {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
p := typ.Field(i)
|
||||
if !p.Anonymous {
|
||||
attrs[p.Name] = p.Type
|
||||
}
|
||||
}
|
||||
|
||||
return attrs
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ package gopsutil
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -18,3 +19,13 @@ type FILETIME struct {
|
||||
DwLowDateTime uint32
|
||||
DwHighDateTime uint32
|
||||
}
|
||||
|
||||
// borrowed from net/interface_windows.go
|
||||
func bytePtrToString(p *uint8) string {
|
||||
a := (*[10000]uint8)(unsafe.Pointer(p))
|
||||
i := 0
|
||||
for a[i] != 0 {
|
||||
i++
|
||||
}
|
||||
return string(a[:i])
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func HostInfo() (HostInfoStat, error) {
|
||||
ret := HostInfoStat{}
|
||||
func HostInfo() (*HostInfoStat, error) {
|
||||
ret := &HostInfoStat{}
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
ret.Hostname = hostname
|
||||
|
@ -1,4 +1,4 @@
|
||||
// +build linux,amd64
|
||||
// +build linux
|
||||
|
||||
package gopsutil
|
||||
|
||||
@ -28,7 +28,7 @@ func BootTime() (int64, error) {
|
||||
if err := syscall.Sysinfo(sysinfo); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return sysinfo.Uptime, nil
|
||||
return int64(sysinfo.Uptime), nil
|
||||
}
|
||||
|
||||
func Users() ([]UserStat, error) {
|
||||
|
27
host_linux_arm.go
Normal file
27
host_linux_arm.go
Normal file
@ -0,0 +1,27 @@
|
||||
// +build linux
|
||||
// +build arm
|
||||
|
||||
package gopsutil
|
||||
|
||||
type exitStatus struct {
|
||||
Etermination int16 // Process termination status.
|
||||
Eexit int16 // Process exit status.
|
||||
}
|
||||
type timeval struct {
|
||||
TvSec uint32 // Seconds.
|
||||
TvUsec uint32 // Microseconds.
|
||||
}
|
||||
|
||||
type utmp struct {
|
||||
UtType int16 // Type of login.
|
||||
UtPid int32 // Process ID of login process.
|
||||
UtLine [32]byte // Devicename.
|
||||
UtID [4]byte // Inittab ID.
|
||||
UtUser [32]byte // Username.
|
||||
UtHost [256]byte // Hostname for remote login.
|
||||
UtExit exitStatus // Exit status of a process marked
|
||||
UtSession int32 // Session ID, used for windowing.
|
||||
UtTv timeval // Time entry was made.
|
||||
UtAddrV6 [16]byte // Internet address of remote host.
|
||||
Unused [20]byte // Reserved for future use. // original is 20
|
||||
}
|
@ -13,8 +13,8 @@ var (
|
||||
procGetTickCount = modkernel32.NewProc("GetTickCount")
|
||||
)
|
||||
|
||||
func HostInfo() (HostInfoStat, error) {
|
||||
ret := HostInfoStat{}
|
||||
func HostInfo() (*HostInfoStat, error) {
|
||||
ret := &HostInfoStat{}
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return ret, err
|
||||
|
@ -44,8 +44,8 @@ func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return nil, err
|
||||
}
|
||||
ret := &SwapMemoryStat{
|
||||
Total: sysinfo.Totalswap,
|
||||
Free: sysinfo.Freeswap,
|
||||
Total: uint64(sysinfo.Totalswap),
|
||||
Free: uint64(sysinfo.Freeswap),
|
||||
}
|
||||
ret.Used = ret.Total - ret.Free
|
||||
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
||||
|
4
net.go
4
net.go
@ -25,8 +25,8 @@ type NetConnectionStat struct {
|
||||
Fd uint32 `json:"fd"`
|
||||
Family uint32 `json:"family"`
|
||||
Type uint32 `json:"type"`
|
||||
Laddr Addr `json:"laddr"`
|
||||
Raddr Addr `json:"raddr"`
|
||||
Laddr Addr `json:"localaddr"`
|
||||
Raddr Addr `json:"remoteaddr"`
|
||||
Status string `json:"status"`
|
||||
Pid int32 `json:"pid"`
|
||||
}
|
||||
|
11
net_freebsd.go
Normal file
11
net_freebsd.go
Normal file
@ -0,0 +1,11 @@
|
||||
// +build freebsd
|
||||
|
||||
package gopsutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
||||
return nil, errors.New("not implemented yet")
|
||||
}
|
93
net_windows.go
Normal file
93
net_windows.go
Normal file
@ -0,0 +1,93 @@
|
||||
// +build windows
|
||||
|
||||
package gopsutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
modiphlpapi = NewLazyDLL("iphlpapi.dll")
|
||||
procGetExtendedTcpTable = modiphlpapi.NewProc("GetExtendedTcpTable")
|
||||
procGetExtendedUdpTable = modiphlpapi.NewProc("GetExtendedUdpTable")
|
||||
)
|
||||
|
||||
const (
|
||||
TCP_TABLE_BASIC_LISTENER = iota
|
||||
TCP_TABLE_BASIC_CONNECTIONS
|
||||
TCP_TABLE_BASIC_ALL
|
||||
TCP_TABLE_OWNER_PID_LISTENER
|
||||
TCP_TABLE_OWNER_PID_CONNECTIONS
|
||||
TCP_TABLE_OWNER_PID_ALL
|
||||
TCP_TABLE_OWNER_MODULE_LISTENER
|
||||
TCP_TABLE_OWNER_MODULE_CONNECTIONS
|
||||
TCP_TABLE_OWNER_MODULE_ALL
|
||||
)
|
||||
|
||||
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
||||
ifs, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ai, err := getAdapterList()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ret []NetIOCountersStat
|
||||
|
||||
for _, ifi := range ifs {
|
||||
name := ifi.Name
|
||||
for ; ai != nil; ai = ai.Next {
|
||||
name = bytePtrToString(&ai.Description[0])
|
||||
c := NetIOCountersStat{
|
||||
Name: name,
|
||||
}
|
||||
|
||||
row := syscall.MibIfRow{Index: ai.Index}
|
||||
e := syscall.GetIfEntry(&row)
|
||||
if e != nil {
|
||||
return nil, os.NewSyscallError("GetIfEntry", e)
|
||||
}
|
||||
c.BytesSent = uint64(row.OutOctets)
|
||||
c.BytesRecv = uint64(row.InOctets)
|
||||
c.PacketsSent = uint64(row.OutUcastPkts)
|
||||
c.PacketsRecv = uint64(row.InUcastPkts)
|
||||
c.Errin = uint64(row.InErrors)
|
||||
c.Errout = uint64(row.OutErrors)
|
||||
c.Dropin = uint64(row.InDiscards)
|
||||
c.Dropout = uint64(row.OutDiscards)
|
||||
|
||||
ret = append(ret, c)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Return a list of network connections opened by a process
|
||||
func NetConnections(kind string) ([]NetConnectionStat, error) {
|
||||
var ret []NetConnectionStat
|
||||
|
||||
return ret, erros.New("not implemented yet")
|
||||
}
|
||||
|
||||
// borrowed from src/pkg/net/interface_windows.go
|
||||
func getAdapterList() (*syscall.IpAdapterInfo, error) {
|
||||
b := make([]byte, 1000)
|
||||
l := uint32(len(b))
|
||||
a := (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
|
||||
err := syscall.GetAdaptersInfo(a, &l)
|
||||
if err == syscall.ERROR_BUFFER_OVERFLOW {
|
||||
b = make([]byte, l)
|
||||
a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
|
||||
err = syscall.GetAdaptersInfo(a, &l)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, os.NewSyscallError("GetAdaptersInfo", err)
|
||||
}
|
||||
return a, nil
|
||||
}
|
9
process_linux_arm.go
Normal file
9
process_linux_arm.go
Normal file
@ -0,0 +1,9 @@
|
||||
// +build linux
|
||||
// +build arm
|
||||
|
||||
package gopsutil
|
||||
|
||||
const (
|
||||
CLOCK_TICKS = 100 // C.sysconf(C._SC_CLK_TCK)
|
||||
PAGESIZE = 4096 // C.sysconf(C._SC_PAGE_SIZE)
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user