提交 4899782e 编写于 作者: S Shirou WAKAYAMA

change various func return type to pointer.

上级 172ac637
...@@ -18,18 +18,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { ...@@ -18,18 +18,18 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
if err != nil { if err != nil {
continue continue
} }
ret = append(ret, ct) ret = append(ret, *ct)
} }
return ret, nil return ret, nil
} }
func parseStatLine(line string) (CPUTimesStat, error) { func parseStatLine(line string) (*CPUTimesStat, error) {
fields := strings.Fields(line) fields := strings.Fields(line)
if strings.HasPrefix(fields[0], "cpu") == false { if strings.HasPrefix(fields[0], "cpu") == false {
// return CPUTimesStat{}, e // return CPUTimesStat{}, e
return CPUTimesStat{}, errors.New("not contain cpu") return nil, errors.New("not contain cpu")
} }
cpu := fields[0] cpu := fields[0]
...@@ -44,7 +44,7 @@ func parseStatLine(line string) (CPUTimesStat, error) { ...@@ -44,7 +44,7 @@ func parseStatLine(line string) (CPUTimesStat, error) {
irq, _ := strconv.ParseFloat(fields[6], 32) irq, _ := strconv.ParseFloat(fields[6], 32)
softirq, _ := strconv.ParseFloat(fields[7], 32) softirq, _ := strconv.ParseFloat(fields[7], 32)
stolen, _ := strconv.ParseFloat(fields[8], 32) stolen, _ := strconv.ParseFloat(fields[8], 32)
ct := CPUTimesStat{ ct := &CPUTimesStat{
CPU: cpu, CPU: cpu,
User: float32(user), User: float32(user),
Nice: float32(nice), Nice: float32(nice),
......
...@@ -11,15 +11,15 @@ import ( ...@@ -11,15 +11,15 @@ import (
"unsafe" "unsafe"
) )
func HostInfo() (HostInfoStat, error) { func HostInfo() (*HostInfoStat, error) {
ret := HostInfoStat{}
hostname, err := os.Hostname() hostname, err := os.Hostname()
ret.Hostname = hostname
if err != nil { if err != nil {
return ret, err return nil, err
} }
ret := &HostInfoStat{
Hostname: hostname,
}
return ret, nil return ret, nil
} }
......
...@@ -8,29 +8,29 @@ import ( ...@@ -8,29 +8,29 @@ import (
"strings" "strings"
) )
func LoadAvg() (LoadAvgStat, error) { func LoadAvg() (*LoadAvgStat, error) {
filename := "/proc/loadavg" filename := "/proc/loadavg"
line, err := ioutil.ReadFile(filename) line, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return LoadAvgStat{}, err return nil, err
} }
values := strings.Fields(string(line)) values := strings.Fields(string(line))
load1, err := strconv.ParseFloat(values[0], 64) load1, err := strconv.ParseFloat(values[0], 64)
if err != nil { if err != nil {
return LoadAvgStat{}, err return nil, err
} }
load5, err := strconv.ParseFloat(values[1], 64) load5, err := strconv.ParseFloat(values[1], 64)
if err != nil { if err != nil {
return LoadAvgStat{}, err return nil, err
} }
load15, err := strconv.ParseFloat(values[2], 64) load15, err := strconv.ParseFloat(values[2], 64)
if err != nil { if err != nil {
return LoadAvgStat{}, err return nil, err
} }
ret := LoadAvgStat{ ret := &LoadAvgStat{
Load1: load1, Load1: load1,
Load5: load5, Load5: load5,
Load15: load15, Load15: load15,
......
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
package gopsutil package gopsutil
func VirtualMemory() (VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
ret := VirtualMemoryStat{} ret := &VirtualMemoryStat{}
return ret, nil return ret, nil
} }
func SwapMemory() (SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
ret := SwapMemoryStat{} ret := &SwapMemoryStat{}
return ret, nil return ret, nil
} }
...@@ -6,17 +6,19 @@ import ( ...@@ -6,17 +6,19 @@ import (
"syscall" "syscall"
) )
func VirtualMemory() (VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
ret := VirtualMemoryStat{}
sysinfo := &syscall.Sysinfo_t{} sysinfo := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(sysinfo); err != nil { if err := syscall.Sysinfo(sysinfo); err != nil {
return ret, err return nil, err
}
ret := &VirtualMemoryStat{
Total: uint64(sysinfo.Totalram),
Free: uint64(sysinfo.Freeram),
Shared: uint64(sysinfo.Sharedram),
Buffers: uint64(sysinfo.Bufferram),
} }
ret.Total = uint64(sysinfo.Totalram)
ret.Free = uint64(sysinfo.Freeram)
ret.Shared = uint64(sysinfo.Sharedram)
ret.Buffers = uint64(sysinfo.Bufferram)
ret.Used = ret.Total - ret.Free ret.Used = ret.Total - ret.Free
...@@ -35,15 +37,16 @@ func VirtualMemory() (VirtualMemoryStat, error) { ...@@ -35,15 +37,16 @@ func VirtualMemory() (VirtualMemoryStat, error) {
return ret, nil return ret, nil
} }
func SwapMemory() (SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
ret := SwapMemoryStat{}
sysinfo := &syscall.Sysinfo_t{} sysinfo := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(sysinfo); err != nil { if err := syscall.Sysinfo(sysinfo); err != nil {
return ret, err return nil, err
}
ret := &SwapMemoryStat{
Total: sysinfo.Totalswap,
Free: sysinfo.Freeswap,
} }
ret.Total = sysinfo.Totalswap
ret.Free = sysinfo.Freeswap
ret.Used = ret.Total - ret.Free ret.Used = ret.Total - ret.Free
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0 ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
......
...@@ -23,25 +23,26 @@ type MEMORYSTATUSEX struct { ...@@ -23,25 +23,26 @@ type MEMORYSTATUSEX struct {
ullAvailExtendedVirtual uint64 ullAvailExtendedVirtual uint64
} }
func VirtualMemory() (VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
ret := VirtualMemoryStat{}
var memInfo MEMORYSTATUSEX var memInfo MEMORYSTATUSEX
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo)) memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))) mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
if mem == 0 { if mem == 0 {
return ret, syscall.GetLastError() return nil, syscall.GetLastError()
}
ret := &VirtualMemoryStat{
Total: memInfo.ullTotalPhys,
Available: memInfo.ullAvailPhys,
UsedPercent: float64(memInfo.dwMemoryLoad),
} }
ret.Total = memInfo.ullTotalPhys
ret.Available = memInfo.ullAvailPhys
ret.UsedPercent = float64(memInfo.dwMemoryLoad)
ret.Used = ret.Total - ret.Available ret.Used = ret.Total - ret.Available
return ret, nil return ret, nil
} }
func SwapMemory() (SwapMemoryStat, error) { func SwapMemory() (*SwapMemoryStat, error) {
ret := SwapMemoryStat{} ret := &SwapMemoryStat{}
return ret, nil return ret, nil
} }
package gopsutil package gopsutil
type NetIOCountersStat struct { type NetIOCountersStat struct {
Name string `json:"name"` // interface name Name string `json:"name"` // interface name
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
BytesRecv uint64 `json:"bytes_recv"` // number of bytes received BytesRecv uint64 `json:"bytes_recv"` // number of bytes received
PacketsSent uint64 `json:"packets_sent"` // number of packets sent PacketsSent uint64 `json:"packets_sent"` // number of packets sent
PacketsRecv uint64 `json:"packets_recv"` // number of packets received PacketsRecv uint64 `json:"packets_recv"` // number of packets received
......
...@@ -10,7 +10,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { ...@@ -10,7 +10,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := "/proc/net/dev" filename := "/proc/net/dev"
lines, err := readLines(filename) lines, err := readLines(filename)
if err != nil { if err != nil {
return make([]NetIOCountersStat, 0), err return nil, err
} }
statlen := len(lines) - 1 statlen := len(lines) - 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册