common: rename package common to internal/common. Add ENV helper funcs.

Package common wasn't used for public functions. Place it in an
internal directory to prevent other packages from using.

Remove the distributed references to "HOST_PROC" and "HOST_SYS"
consts and combine into a common function. This also helps so that
if a env var is defined with a trailing slash all will continue to
work as expected.

Fixes #100
pull/102/head
Daniel Theophanes 10 years ago
parent 69221741e8
commit 0af895258e

@ -3,14 +3,13 @@
package cpu package cpu
import ( import (
"path/filepath"
"fmt"
"errors" "errors"
"fmt"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
var cpu_tick = float64(100) var cpu_tick = float64(100)
@ -27,7 +26,7 @@ func init() {
} }
func CPUTimes(percpu bool) ([]CPUTimesStat, error) { func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/stat" filename := common.HostProc("stat")
var lines = []string{} var lines = []string{}
if percpu { if percpu {
var startIdx uint = 1 var startIdx uint = 1
@ -58,8 +57,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
} }
func sysCpuPath(cpu int32, relPath string) string { func sysCpuPath(cpu int32, relPath string) string {
root := common.GetEnv("HOST_SYS", "/sys") return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
return filepath.Join(root, fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
} }
func finishCPUInfo(c *CPUInfoStat) error { func finishCPUInfo(c *CPUInfoStat) error {
@ -83,7 +81,7 @@ func finishCPUInfo(c *CPUInfoStat) error {
} }
func CPUInfo() ([]CPUInfoStat, error) { func CPUInfo() ([]CPUInfoStat, error) {
filename := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), "cpuinfo") filename := common.HostProc("cpuinfo")
lines, _ := common.ReadLines(filename) lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat var ret []CPUInfoStat
@ -148,7 +146,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
return ret, err return ret, err
} }
c.Cores = int32(t) c.Cores = int32(t)
case "flags","Features": case "flags", "Features":
c.Flags = strings.FieldsFunc(value, func(r rune) bool { c.Flags = strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ' ' return r == ',' || r == ' '
}) })

@ -9,7 +9,7 @@ import (
"strings" "strings"
"syscall" "syscall"
common "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
const ( const (
@ -238,7 +238,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
} }
func DiskIOCounters() (map[string]DiskIOCountersStat, error) { func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/diskstats" filename := common.HostProc("diskstats")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err

@ -10,8 +10,8 @@ import (
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common"
cpu "github.com/shirou/gopsutil/cpu" cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
) )
// GetDockerIDList returnes a list of DockerID. // GetDockerIDList returnes a list of DockerID.
@ -50,7 +50,7 @@ func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) {
statfile := path.Join(base, containerid, "cpuacct.stat") statfile := path.Join(base, containerid, "cpuacct.stat")
if _, err := os.Stat(statfile); os.IsNotExist(err) { if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join("/sys/fs/cgroup/cpuacct/system.slice", "docker-" + containerid + ".scope", "cpuacct.stat") statfile = path.Join("/sys/fs/cgroup/cpuacct/system.slice", "docker-"+containerid+".scope", "cpuacct.stat")
} }
lines, err := common.ReadLines(statfile) lines, err := common.ReadLines(statfile)
@ -92,7 +92,7 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
statfile := path.Join(base, containerid, "memory.stat") statfile := path.Join(base, containerid, "memory.stat")
if _, err := os.Stat(statfile); os.IsNotExist(err) { if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join("/sys/fs/cgroup/memory/system.slice", "docker-" + containerid + ".scope", "memory.stat") statfile = path.Join("/sys/fs/cgroup/memory/system.slice", "docker-"+containerid+".scope", "memory.stat")
} }
// empty containerid means all cgroup // empty containerid means all cgroup

@ -15,7 +15,7 @@ import (
"strings" "strings"
"unsafe" "unsafe"
common "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
type LSB struct { type LSB struct {
@ -56,7 +56,7 @@ func HostInfo() (*HostInfoStat, error) {
// BootTime returns the system boot time expressed in seconds since the epoch. // BootTime returns the system boot time expressed in seconds since the epoch.
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/stat" filename := common.HostProc("stat")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return 0, err return 0, err
@ -321,7 +321,7 @@ func GetVirtualization() (string, string, error) {
var system string var system string
var role string var role string
filename := common.GetEnv("HOST_PROC", "/proc") + "/xen" filename := common.HostProc("xen")
if common.PathExists(filename) { if common.PathExists(filename) {
system = "xen" system = "xen"
role = "guest" // assume guest 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) { if common.PathExists(filename) {
contents, err := common.ReadLines(filename) contents, err := common.ReadLines(filename)
if err == nil { 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) { if common.PathExists(filename) {
contents, err := common.ReadLines(filename) contents, err := common.ReadLines(filename)
if err == nil { 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") { if common.PathExists(filename + "/bc/0") {
system = "openvz" system = "openvz"
role = "host" role = "host"

@ -14,6 +14,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"strconv" "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. //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) value := os.Getenv(key)
if value == "" { if value == "" {
value = dfault value = dfault
} }
switch len(combineWith) {
case 0:
return value 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...)
} }

@ -3,9 +3,9 @@
package common package common
import ( import (
"syscall"
"os/exec" "os/exec"
"strings" "strings"
"syscall"
"unsafe" "unsafe"
) )
@ -58,4 +58,3 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) {
return buf, length, nil return buf, length, nil
} }

@ -7,11 +7,11 @@ import (
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
func LoadAvg() (*LoadAvgStat, error) { func LoadAvg() (*LoadAvgStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/loadavg" filename := common.HostProc("loadavg")
line, err := ioutil.ReadFile(filename) line, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err

@ -7,11 +7,11 @@ import (
"strings" "strings"
"syscall" "syscall"
common "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/meminfo" filename := common.HostProc("meminfo")
lines, _ := common.ReadLines(filename) lines, _ := common.ReadLines(filename)
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+) // flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
memavail := false memavail := false
@ -74,7 +74,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
} else { } else {
ret.UsedPercent = 0 ret.UsedPercent = 0
} }
filename := common.GetEnv("HOST_PROC", "/proc") + "/vmstat" filename := common.HostProc("vmstat")
lines, _ := common.ReadLines(filename) lines, _ := common.ReadLines(filename)
for _, l := range lines { for _, l := range lines {
fields := strings.Fields(l) fields := strings.Fields(l)

@ -8,7 +8,7 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
var invoke common.Invoker var invoke common.Invoker

@ -6,7 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
// NetIOCounters returnes network I/O statistics for every network // NetIOCounters returnes network I/O statistics for every network
@ -15,7 +15,7 @@ import (
// every network interface installed on the system is returned // every network interface installed on the system is returned
// separately. // separately.
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "/net/dev" filename := common.HostProc("net/dev")
lines, err := common.ReadLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err

@ -5,7 +5,7 @@ package net
import ( import (
"strings" "strings"
"github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
// Return a list of network connections opened. // Return a list of network connections opened.

@ -5,8 +5,8 @@ import (
"runtime" "runtime"
"time" "time"
"github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
) )
var invoke common.Invoker var invoke common.Invoker

@ -12,9 +12,9 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/shirou/gopsutil/common"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/net" "github.com/shirou/gopsutil/net"
) )
@ -222,7 +222,7 @@ func (p *Process) IsRunning() (bool, error) {
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
pid := p.Pid pid := p.Pid
var ret []MemoryMapsStat 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) contents, err := ioutil.ReadFile(smapsPath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -303,7 +303,7 @@ func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
// Get num_fds from /proc/(pid)/fd // Get num_fds from /proc/(pid)/fd
func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) { func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
pid := p.Pid 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) d, err := os.Open(statPath)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
@ -336,7 +336,7 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
// Get cwd from /proc/(pid)/cwd // Get cwd from /proc/(pid)/cwd
func (p *Process) fillFromCwd() (string, error) { func (p *Process) fillFromCwd() (string, error) {
pid := p.Pid 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) cwd, err := os.Readlink(cwdPath)
if err != nil { if err != nil {
return "", err return "", err
@ -347,7 +347,7 @@ func (p *Process) fillFromCwd() (string, error) {
// Get exe from /proc/(pid)/exe // Get exe from /proc/(pid)/exe
func (p *Process) fillFromExe() (string, error) { func (p *Process) fillFromExe() (string, error) {
pid := p.Pid 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) exe, err := os.Readlink(exePath)
if err != nil { if err != nil {
return "", err return "", err
@ -358,7 +358,7 @@ func (p *Process) fillFromExe() (string, error) {
// Get cmdline from /proc/(pid)/cmdline // Get cmdline from /proc/(pid)/cmdline
func (p *Process) fillFromCmdline() (string, error) { func (p *Process) fillFromCmdline() (string, error) {
pid := p.Pid 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) cmdline, err := ioutil.ReadFile(cmdPath)
if err != nil { if err != nil {
return "", err return "", err
@ -376,7 +376,7 @@ func (p *Process) fillFromCmdline() (string, error) {
// Get IO status from /proc/(pid)/io // Get IO status from /proc/(pid)/io
func (p *Process) fillFromIO() (*IOCountersStat, error) { func (p *Process) fillFromIO() (*IOCountersStat, error) {
pid := p.Pid 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) ioline, err := ioutil.ReadFile(ioPath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -415,7 +415,7 @@ func (p *Process) fillFromIO() (*IOCountersStat, error) {
// Get memory info from /proc/(pid)/statm // Get memory info from /proc/(pid)/statm
func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) { func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
pid := p.Pid 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) contents, err := ioutil.ReadFile(memPath)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -467,7 +467,7 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
// Get various status from /proc/(pid)/status // Get various status from /proc/(pid)/status
func (p *Process) fillFromStatus() error { func (p *Process) fillFromStatus() error {
pid := p.Pid 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) contents, err := ioutil.ReadFile(statPath)
if err != nil { if err != nil {
return err return err
@ -554,7 +554,7 @@ func (p *Process) fillFromStatus() error {
func (p *Process) fillFromStat() (string, int32, *cpu.CPUTimesStat, int64, int32, error) { func (p *Process) fillFromStat() (string, int32, *cpu.CPUTimesStat, int64, int32, error) {
pid := p.Pid 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) contents, err := ioutil.ReadFile(statPath)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, err return "", 0, nil, 0, 0, err
@ -610,7 +610,7 @@ func (p *Process) fillFromStat() (string, int32, *cpu.CPUTimesStat, int64, int32
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
var ret []int32 var ret []int32
d, err := os.Open(common.GetEnv("HOST_PROC", "/proc")) d, err := os.Open(common.HostProc())
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -8,7 +8,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/shirou/gopsutil/common" "github.com/shirou/gopsutil/internal/common"
) )
var mu sync.Mutex var mu sync.Mutex

Loading…
Cancel
Save