Commit 7396461d authored by WAKAYAMA Shirou's avatar WAKAYAMA Shirou Committed by Shirou WAKAYAMA

remove each metrics top struct such as CPU, all functions become flat.

parent d0da149b
......@@ -4,9 +4,7 @@ import (
"runtime"
)
type CPU struct{}
type CPU_Times struct {
type CPU_TimesStat struct {
Cpu string `json:"cpu"`
User uint64 `json:"user"`
System uint64 `json:"system"`
......@@ -21,11 +19,6 @@ type CPU_Times struct {
Stolen uint64 `json:"stolen"`
}
func NewCPU() CPU {
p := CPU{}
return p
}
func (c CPU) Cpu_counts() (int, error) {
func Cpu_counts() (int, error) {
return runtime.NumCPU(), nil
}
......@@ -7,8 +7,8 @@ import (
"strings"
)
func (c CPU) Cpu_times() ([]CPU_Times, error) {
ret := make([]CPU_Times, 0)
func Cpu_times() ([]CPU_TimesStat, error) {
ret := make([]CPU_TimesStat, 0)
filename := "/proc/stat"
lines, _ := ReadLines(filename)
......@@ -31,7 +31,7 @@ func (c CPU) Cpu_times() ([]CPU_Times, error) {
irq, _ := strconv.ParseUint(fields[6], 10, 64)
softirq, _ := strconv.ParseUint(fields[7], 10, 64)
stolen, _ := strconv.ParseUint(fields[8], 10, 64)
ct := CPU_Times{
ct := CPU_TimesStat{
Cpu: cpu,
User: user,
Nice: nice,
......
......@@ -5,9 +5,7 @@ import (
)
func TestCpu_times(t *testing.T) {
cpu := NewCPU()
v, err := cpu.Cpu_times()
v, err := Cpu_times()
if err != nil {
t.Errorf("error %v", err)
}
......@@ -23,9 +21,7 @@ func TestCpu_times(t *testing.T) {
}
func TestCpu_counts(t *testing.T) {
cpu := NewCPU()
v, err := cpu.Cpu_counts()
v, err := Cpu_counts()
if err != nil {
t.Errorf("error %v", err)
}
......
......@@ -17,8 +17,8 @@ type FILETIME struct {
DwHighDateTime uint32
}
func (c CPU) Cpu_times() ([]CPU_Times, error) {
ret := make([]CPU_Times, 0)
func Cpu_times() ([]CPU_TimesStat, error) {
ret := make([]CPU_TimesStat, 0)
var lpIdleTime FILETIME
var lpKernelTime FILETIME
......@@ -38,7 +38,7 @@ func (c CPU) Cpu_times() ([]CPU_Times, error) {
kernel := ((HI_T * float64(lpKernelTime.DwHighDateTime)) + (LO_T * float64(lpKernelTime.DwLowDateTime)))
system := (kernel - idle)
ret = append(ret, CPU_Times{
ret = append(ret, CPU_TimesStat{
Idle: uint64(idle),
User: uint64(user),
System: uint64(system),
......
package gopsutil
type Disk struct{}
type Disk_usage struct {
type Disk_usageStat struct {
Path string `json:"path"`
Total uint64 `json:"total"`
Free uint64 `json:"free"`
......@@ -10,14 +8,14 @@ type Disk_usage struct {
UsedPercent float64 `json:"usedPercent"`
}
type Disk_partition struct {
type Disk_partitionStat struct {
Device string `json:"device"`
Mountpoint string `json:"mountpoint"`
Fstype string `json:"fstype"`
Opts string `json:"opts"`
}
type Disk_IO_Counters struct {
type Disk_IO_CountersStat struct {
ReadCount uint64 `json:"readCount"`
WriteCount uint64 `json:"writeCount"`
ReadBytes uint64 `json:"readBytes"`
......@@ -25,8 +23,3 @@ type Disk_IO_Counters struct {
ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"`
}
func NewDisk() Disk {
d := Disk{}
return d
}
......@@ -8,13 +8,11 @@ import (
)
func TestDisk_usage(t *testing.T) {
disk := NewDisk()
path := "/"
if runtime.GOOS == "windows" {
path = "C:"
}
v, err := disk.Disk_usage(path)
v, err := Disk_usage(path)
if err != nil {
t.Errorf("error %v", err)
}
......@@ -23,9 +21,7 @@ func TestDisk_usage(t *testing.T) {
}
func TestDisk_partitions(t *testing.T) {
disk := NewDisk()
v, err := disk.Disk_partitions()
v, err := Disk_partitions()
if err != nil {
t.Errorf("error %v", err)
}
......
......@@ -4,16 +4,16 @@ package gopsutil
import "syscall"
func (d Disk) Disk_usage(path string) (Disk_usage, error) {
func Disk_usage(path string) (Disk_usageStat, error) {
stat := syscall.Statfs_t{}
err := syscall.Statfs(path, &stat)
if err != nil {
return Disk_usage{Path: path}, err
return Disk_usageStat{Path: path}, err
}
bsize := stat.Bsize / 512
ret := Disk_usage{
ret := Disk_usageStat{
Path: path,
Total: (uint64(stat.Blocks) * uint64(bsize)) >> 1,
Free: (uint64(stat.Bfree) * uint64(bsize)) >> 1,
......@@ -25,7 +25,7 @@ func (d Disk) Disk_usage(path string) (Disk_usage, error) {
return ret, nil
}
func (d Disk) Disk_partitions() ([]Disk_partition, error) {
ret := make([]Disk_partition, 0)
func Disk_partitions() ([]Disk_partitionStat, error) {
ret := make([]Disk_partitionStat, 0)
return ret, nil
}
package gopsutil
type Host struct{}
type HostInfo struct {
type HostInfoStat struct {
Hostname string `json:"hostname"`
Uptime int64 `json:"uptime"`
Procs uint64 `json:"procs"`
}
func NewHost() Host {
h := Host{}
return h
}
......@@ -5,9 +5,7 @@ import (
)
func TestHostInfo(t *testing.T) {
host := NewHost()
v, err := host.HostInfo()
v, err := HostInfo()
if err != nil {
t.Errorf("error %v", err)
}
......
......@@ -7,8 +7,8 @@ import (
"syscall"
)
func (h Host) HostInfo() (HostInfo, error) {
ret := HostInfo{}
func HostInfo() (HostInfoStat, error) {
ret := HostInfoStat{}
hostname, err := os.Hostname()
ret.Hostname = hostname
......
......@@ -8,8 +8,8 @@ import (
"syscall"
)
func (h Host) HostInfo() (HostInfo, error) {
ret := HostInfo{}
func HostInfo() (HostInfoStat, error) {
ret := HostInfoStat{}
hostname, err := os.Hostname()
if err != nil {
return ret, err
......
package gopsutil
type Load struct{}
type LoadAvg struct {
type LoadAvgStat struct {
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
}
func NewLoad() Load {
l := Load{}
return l
}
......@@ -8,10 +8,10 @@ import (
"strings"
)
func (l Load) LoadAvg() (LoadAvg, error) {
func LoadAvg() (LoadAvgStat, error) {
out, err := exec.Command("/sbin/sysctl", "-n", "vm.loadavg").Output()
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
v := strings.Replace(string(out), "{ ", "", 1)
v = strings.Replace(string(v), " }", "", 1)
......@@ -19,18 +19,18 @@ func (l Load) LoadAvg() (LoadAvg, error) {
load1, err := strconv.ParseFloat(values[0], 32)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
load5, err := strconv.ParseFloat(values[1], 32)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
load15, err := strconv.ParseFloat(values[2], 32)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
ret := LoadAvg{
ret := LoadAvgStat{
Load1: float32(load1),
Load5: float32(load5),
Load15: float32(load15),
......
......@@ -8,29 +8,29 @@ import (
"strings"
)
func (l Load) LoadAvg() (LoadAvg, error) {
func LoadAvg() (LoadAvgStat, error) {
filename := "/proc/loadavg"
line, err := ioutil.ReadFile(filename)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
values := strings.Fields(string(line))
load1, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
load5, err := strconv.ParseFloat(values[1], 64)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
load15, err := strconv.ParseFloat(values[2], 64)
if err != nil {
return LoadAvg{}, err
return LoadAvgStat{}, err
}
ret := LoadAvg{
ret := LoadAvgStat{
Load1: load1,
Load5: load5,
Load15: load15,
......
......@@ -7,9 +7,7 @@ import (
)
func TestLoad(t *testing.T) {
load := NewLoad()
v, err := load.LoadAvg()
v, err := LoadAvg()
if err != nil {
t.Errorf("error %v", err)
}
......
package gopsutil
type Mem struct{}
type Virtual_memory struct {
type Virtual_memoryStat struct {
Total uint64 `json:"total"`
Available uint64 `json:"available"`
Used uint64 `json:"used"`
......@@ -16,7 +14,7 @@ type Virtual_memory struct {
Shared uint64 `json:"shared"`
}
type Swap_memory struct {
type Swap_memoryStat struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
......@@ -24,8 +22,3 @@ type Swap_memory struct {
Sin uint64 `json:"sin"`
Sout uint64 `json:"sout"`
}
func NewMem() Mem {
m := Mem{}
return m
}
......@@ -6,8 +6,8 @@ import (
"syscall"
)
func (m Mem) Virtual_memory() (Virtual_memory, error) {
ret := Virtual_memory{}
func Virtual_memory() (Virtual_memoryStat, error) {
ret := Virtual_memoryStat{}
sysinfo := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(sysinfo); err != nil {
......@@ -35,8 +35,8 @@ func (m Mem) Virtual_memory() (Virtual_memory, error) {
return ret, nil
}
func (m Mem) Swap_memory() (Swap_memory, error) {
ret := Swap_memory{}
func Swap_memory() (Swap_memoryStat, error) {
ret := Swap_memoryStat{}
sysinfo := &syscall.Sysinfo_t{}
if err := syscall.Sysinfo(sysinfo); err != nil {
......
......@@ -7,9 +7,7 @@ import (
)
func TestVirtual_memory(t *testing.T) {
mem := NewMem()
v, err := mem.Virtual_memory()
v, err := Virtual_memory()
if err != nil {
t.Errorf("error %v", err)
}
......@@ -18,9 +16,7 @@ func TestVirtual_memory(t *testing.T) {
}
func TestSwap_memory(t *testing.T) {
mem := NewMem()
v, err := mem.Swap_memory()
v, err := Swap_memory()
if err != nil {
t.Errorf("error %v", err)
}
......
......@@ -23,7 +23,7 @@ type MEMORYSTATUSEX struct {
ullAvailExtendedVirtual uint64
}
func (m Mem) Virtual_memory() (Virtual_memory, error) {
func Virtual_memory() (Virtual_memory, error) {
ret := Virtual_memory{}
var memInfo MEMORYSTATUSEX
......@@ -40,7 +40,7 @@ func (m Mem) Virtual_memory() (Virtual_memory, error) {
return ret, nil
}
func (m Mem) Swap_memory() (Swap_memory, error) {
func Swap_memory() (Swap_memory, error) {
ret := Swap_memory{}
return ret, nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment