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
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 == ' '
})

@ -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

@ -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 {

@ -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"

@ -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
}

@ -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...)
}

@ -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
}

@ -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

@ -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)

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

@ -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

@ -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.

@ -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

@ -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
}

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

Loading…
Cancel
Save