diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index 6a4e672b1..b36797fb4 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -3,14 +3,13 @@ package cpu import ( - "path/filepath" - "fmt" "errors" + "fmt" "os/exec" "strconv" "strings" - common "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) var cpu_tick = float64(100) @@ -27,7 +26,7 @@ func init() { } func CPUTimes(percpu bool) ([]CPUTimesStat, error) { - filename := common.GetEnv("HOST_PROC", "/proc") + "/stat" + filename := common.HostProc("stat") var lines = []string{} if percpu { var startIdx uint = 1 @@ -58,8 +57,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { } func sysCpuPath(cpu int32, relPath string) string { - root := common.GetEnv("HOST_SYS", "/sys") - return filepath.Join(root, fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath) + return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath) } func finishCPUInfo(c *CPUInfoStat) error { @@ -75,7 +73,7 @@ func finishCPUInfo(c *CPUInfoStat) error { } if len(c.CoreID) == 0 { lines, err := common.ReadLines(sysCpuPath(c.CPU, "topology/core_id")) - if err == nil { + if err == nil { c.CoreID = lines[0] } } @@ -83,7 +81,7 @@ func finishCPUInfo(c *CPUInfoStat) error { } func CPUInfo() ([]CPUInfoStat, error) { - filename := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), "cpuinfo") + filename := common.HostProc("cpuinfo") lines, _ := common.ReadLines(filename) var ret []CPUInfoStat @@ -148,7 +146,7 @@ func CPUInfo() ([]CPUInfoStat, error) { return ret, err } c.Cores = int32(t) - case "flags","Features": + case "flags", "Features": c.Flags = strings.FieldsFunc(value, func(r rune) bool { return r == ',' || r == ' ' }) diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 93f59b098..caa1e17f7 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -9,7 +9,7 @@ import ( "strings" "syscall" - common "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) const ( @@ -238,7 +238,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) { } func DiskIOCounters() (map[string]DiskIOCountersStat, error) { - filename := common.GetEnv("HOST_PROC", "/proc") + "/diskstats" + filename := common.HostProc("diskstats") lines, err := common.ReadLines(filename) if err != nil { return nil, err diff --git a/docker/docker_linux.go b/docker/docker_linux.go index fd6df6d8b..bc2fd6ef3 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -4,14 +4,14 @@ package docker import ( "encoding/json" - "os" + "os" "os/exec" "path" "strconv" "strings" - common "github.com/shirou/gopsutil/common" cpu "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/internal/common" ) // GetDockerIDList returnes a list of DockerID. @@ -49,9 +49,9 @@ func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) { } statfile := path.Join(base, containerid, "cpuacct.stat") - if _, err := os.Stat(statfile); os.IsNotExist(err) { - statfile = path.Join("/sys/fs/cgroup/cpuacct/system.slice", "docker-" + containerid + ".scope", "cpuacct.stat") - } + if _, err := os.Stat(statfile); os.IsNotExist(err) { + statfile = path.Join("/sys/fs/cgroup/cpuacct/system.slice", "docker-"+containerid+".scope", "cpuacct.stat") + } lines, err := common.ReadLines(statfile) if err != nil { @@ -91,9 +91,9 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) { } statfile := path.Join(base, containerid, "memory.stat") - if _, err := os.Stat(statfile); os.IsNotExist(err) { - statfile = path.Join("/sys/fs/cgroup/memory/system.slice", "docker-" + containerid + ".scope", "memory.stat") - } + if _, err := os.Stat(statfile); os.IsNotExist(err) { + statfile = path.Join("/sys/fs/cgroup/memory/system.slice", "docker-"+containerid+".scope", "memory.stat") + } // empty containerid means all cgroup if len(containerid) == 0 { diff --git a/host/host_linux.go b/host/host_linux.go index 9131fa84d..0ac9ea853 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -15,7 +15,7 @@ import ( "strings" "unsafe" - common "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) type LSB struct { @@ -56,7 +56,7 @@ func HostInfo() (*HostInfoStat, error) { // BootTime returns the system boot time expressed in seconds since the epoch. func BootTime() (uint64, error) { - filename := common.GetEnv("HOST_PROC", "/proc") + "/stat" + filename := common.HostProc("stat") lines, err := common.ReadLines(filename) if err != nil { return 0, err @@ -321,7 +321,7 @@ func GetVirtualization() (string, string, error) { var system string var role string - filename := common.GetEnv("HOST_PROC", "/proc") + "/xen" + filename := common.HostProc("xen") if common.PathExists(filename) { system = "xen" role = "guest" // assume guest @@ -336,7 +336,7 @@ func GetVirtualization() (string, string, error) { } } - filename = common.GetEnv("HOST_PROC", "/proc") + "/modules" + filename = common.HostProc("modules") if common.PathExists(filename) { contents, err := common.ReadLines(filename) if err == nil { @@ -353,7 +353,7 @@ func GetVirtualization() (string, string, error) { } } - filename = common.GetEnv("HOST_PROC", "/proc") + "/cpuinfo" + filename = common.HostProc("cpuinfo") if common.PathExists(filename) { contents, err := common.ReadLines(filename) if err == nil { @@ -366,7 +366,7 @@ func GetVirtualization() (string, string, error) { } } - filename = common.GetEnv("HOST_PROC", "/proc") + filename = common.HostProc() if common.PathExists(filename + "/bc/0") { system = "openvz" role = "host" diff --git a/host/host_linux_386.go b/host/host_linux_386.go index d8f31c2f6..fb6d7a0f6 100644 --- a/host/host_linux_386.go +++ b/host/host_linux_386.go @@ -6,39 +6,39 @@ package host const ( - sizeofPtr = 0x4 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x4 - sizeofLongLong = 0x8 + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 ) type ( - _C_short int16 - _C_int int32 - _C_long int32 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 ) type utmp struct { - Type int16 - Pad_cgo_0 [2]byte - Pid int32 - Line [32]int8 - Id [4]int8 - User [32]int8 - Host [256]int8 - Exit exit_status - Session int32 - Tv UtTv - Addr_v6 [4]int32 - X__unused [20]int8 + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv UtTv + Addr_v6 [4]int32 + X__unused [20]int8 } type exit_status struct { - Termination int16 - Exit int16 + Termination int16 + Exit int16 } type UtTv struct { - TvSec int32 - TvUsec int32 + TvSec int32 + TvUsec int32 } diff --git a/common/common.go b/internal/common/common.go similarity index 88% rename from common/common.go rename to internal/common/common.go index d04cdfd7f..aeadda6b2 100644 --- a/common/common.go +++ b/internal/common/common.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "reflect" "runtime" "strconv" @@ -209,10 +210,30 @@ func PathExists(filename string) bool { } //GetEnv retreives the environment variable key. If it does not exist it returns the default. -func GetEnv(key string, dfault string) string { +func GetEnv(key string, dfault string, combineWith ...string) string { value := os.Getenv(key) if value == "" { value = dfault } - return value + + switch len(combineWith) { + case 0: + return value + case 1: + return filepath.Join(value, combineWith[0]) + default: + all := make([]string, len(combineWith)+1) + all[0] = value + copy(all[1:], combineWith) + return filepath.Join(all...) + } + panic("invalid switch case") +} + +func HostProc(combineWith ...string) string { + return GetEnv("HOST_PROC", "/proc", combineWith...) +} + +func HostSys(combineWith ...string) string { + return GetEnv("HOST_SYS", "/sys", combineWith...) } diff --git a/common/common_darwin.go b/internal/common/common_darwin.go similarity index 100% rename from common/common_darwin.go rename to internal/common/common_darwin.go diff --git a/common/common_freebsd.go b/internal/common/common_freebsd.go similarity index 99% rename from common/common_freebsd.go rename to internal/common/common_freebsd.go index 3c1124655..8ccd40e90 100644 --- a/common/common_freebsd.go +++ b/internal/common/common_freebsd.go @@ -3,9 +3,9 @@ package common import ( - "syscall" "os/exec" "strings" + "syscall" "unsafe" ) @@ -58,4 +58,3 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) { return buf, length, nil } - diff --git a/common/common_linux.go b/internal/common/common_linux.go similarity index 100% rename from common/common_linux.go rename to internal/common/common_linux.go diff --git a/common/common_test.go b/internal/common/common_test.go similarity index 100% rename from common/common_test.go rename to internal/common/common_test.go diff --git a/common/common_unix.go b/internal/common/common_unix.go similarity index 100% rename from common/common_unix.go rename to internal/common/common_unix.go diff --git a/common/common_windows.go b/internal/common/common_windows.go similarity index 100% rename from common/common_windows.go rename to internal/common/common_windows.go diff --git a/load/load_linux.go b/load/load_linux.go index 4ab5a9686..80621c966 100644 --- a/load/load_linux.go +++ b/load/load_linux.go @@ -7,11 +7,11 @@ import ( "strconv" "strings" - common "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) func LoadAvg() (*LoadAvgStat, error) { - filename := common.GetEnv("HOST_PROC", "/proc") + "/loadavg" + filename := common.HostProc("loadavg") line, err := ioutil.ReadFile(filename) if err != nil { return nil, err diff --git a/mem/mem_linux.go b/mem/mem_linux.go index e45a0842d..fc9226219 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -7,11 +7,11 @@ import ( "strings" "syscall" - common "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) func VirtualMemory() (*VirtualMemoryStat, error) { - filename := common.GetEnv("HOST_PROC", "/proc") + "/meminfo" + filename := common.HostProc("meminfo") lines, _ := common.ReadLines(filename) // flag if MemAvailable is in /proc/meminfo (kernel 3.14+) memavail := false @@ -74,7 +74,7 @@ func SwapMemory() (*SwapMemoryStat, error) { } else { ret.UsedPercent = 0 } - filename := common.GetEnv("HOST_PROC", "/proc") + "/vmstat" + filename := common.HostProc("vmstat") lines, _ := common.ReadLines(filename) for _, l := range lines { fields := strings.Fields(l) diff --git a/net/net.go b/net/net.go index 506fe57e0..e8a1035a9 100644 --- a/net/net.go +++ b/net/net.go @@ -8,7 +8,7 @@ import ( "strings" "syscall" - "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) var invoke common.Invoker diff --git a/net/net_linux.go b/net/net_linux.go index 73b2bee6c..421837302 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - common "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) // NetIOCounters returnes network I/O statistics for every network @@ -15,7 +15,7 @@ import ( // every network interface installed on the system is returned // separately. func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { - filename := common.GetEnv("HOST_PROC", "/proc") + "/net/dev" + filename := common.HostProc("net/dev") lines, err := common.ReadLines(filename) if err != nil { return nil, err diff --git a/net/net_unix.go b/net/net_unix.go index 0b8a844e7..fe2f1eb4d 100644 --- a/net/net_unix.go +++ b/net/net_unix.go @@ -5,7 +5,7 @@ package net import ( "strings" - "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) // Return a list of network connections opened. diff --git a/process/process.go b/process/process.go index abe522c80..0526f78a7 100644 --- a/process/process.go +++ b/process/process.go @@ -5,8 +5,8 @@ import ( "runtime" "time" - "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/internal/common" ) var invoke common.Invoker diff --git a/process/process_linux.go b/process/process_linux.go index 0d3a3973e..9855b74a6 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -12,9 +12,9 @@ import ( "strings" "syscall" - "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/host" + "github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/net" ) @@ -222,7 +222,7 @@ func (p *Process) IsRunning() (bool, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { pid := p.Pid var ret []MemoryMapsStat - smapsPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "smaps") + smapsPath := common.HostProc(strconv.Itoa(int(pid)), "smaps") contents, err := ioutil.ReadFile(smapsPath) if err != nil { return nil, err @@ -303,7 +303,7 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { // Get num_fds from /proc/(pid)/fd func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) { pid := p.Pid - statPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "fd") + statPath := common.HostProc(strconv.Itoa(int(pid)), "fd") d, err := os.Open(statPath) if err != nil { return 0, nil, err @@ -336,7 +336,7 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) { // Get cwd from /proc/(pid)/cwd func (p *Process) fillFromCwd() (string, error) { pid := p.Pid - cwdPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "cwd") + cwdPath := common.HostProc(strconv.Itoa(int(pid)), "cwd") cwd, err := os.Readlink(cwdPath) if err != nil { return "", err @@ -347,7 +347,7 @@ func (p *Process) fillFromCwd() (string, error) { // Get exe from /proc/(pid)/exe func (p *Process) fillFromExe() (string, error) { pid := p.Pid - exePath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "exe") + exePath := common.HostProc(strconv.Itoa(int(pid)), "exe") exe, err := os.Readlink(exePath) if err != nil { return "", err @@ -358,7 +358,7 @@ func (p *Process) fillFromExe() (string, error) { // Get cmdline from /proc/(pid)/cmdline func (p *Process) fillFromCmdline() (string, error) { pid := p.Pid - cmdPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "cmdline") + cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") cmdline, err := ioutil.ReadFile(cmdPath) if err != nil { return "", err @@ -376,7 +376,7 @@ func (p *Process) fillFromCmdline() (string, error) { // Get IO status from /proc/(pid)/io func (p *Process) fillFromIO() (*IOCountersStat, error) { pid := p.Pid - ioPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "io") + ioPath := common.HostProc(strconv.Itoa(int(pid)), "io") ioline, err := ioutil.ReadFile(ioPath) if err != nil { return nil, err @@ -415,7 +415,7 @@ func (p *Process) fillFromIO() (*IOCountersStat, error) { // Get memory info from /proc/(pid)/statm func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) { pid := p.Pid - memPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "statm") + memPath := common.HostProc(strconv.Itoa(int(pid)), "statm") contents, err := ioutil.ReadFile(memPath) if err != nil { return nil, nil, err @@ -467,7 +467,7 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) { // Get various status from /proc/(pid)/status func (p *Process) fillFromStatus() error { pid := p.Pid - statPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "status") + statPath := common.HostProc(strconv.Itoa(int(pid)), "status") contents, err := ioutil.ReadFile(statPath) if err != nil { return err @@ -554,7 +554,7 @@ func (p *Process) fillFromStatus() error { func (p *Process) fillFromStat() (string, int32, *cpu.CPUTimesStat, int64, int32, error) { pid := p.Pid - statPath := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), strconv.Itoa(int(pid)), "stat") + statPath := common.HostProc(strconv.Itoa(int(pid)), "stat") contents, err := ioutil.ReadFile(statPath) if err != nil { return "", 0, nil, 0, 0, err @@ -610,7 +610,7 @@ func (p *Process) fillFromStat() (string, int32, *cpu.CPUTimesStat, int64, int32 func Pids() ([]int32, error) { var ret []int32 - d, err := os.Open(common.GetEnv("HOST_PROC", "/proc")) + d, err := os.Open(common.HostProc()) if err != nil { return nil, err } diff --git a/process/process_test.go b/process/process_test.go index 854d63c12..4cb475a19 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/shirou/gopsutil/common" + "github.com/shirou/gopsutil/internal/common" ) var mu sync.Mutex