fix on Linux.

tags/1.0.0^2
Shirou WAKAYAMA 11 years ago
parent d11680c773
commit a1c1d7b25f

@ -94,7 +94,7 @@ func mustParseFloat64(val string) float64 {
} }
// Check the target string slice containes src or not // Check the target string slice containes src or not
func stringContains(target []string, src string) bool { func StringContains(target []string, src string) bool {
for _, t := range target { for _, t := range target {
if t == src { if t == src {
return true return true
@ -126,7 +126,7 @@ func attributes(m interface{}) map[string]reflect.Type {
return attrs return attrs
} }
func pathExists(filename string) bool { func PathExists(filename string) bool {
if _, err := os.Stat(filename); err == nil { if _, err := os.Stat(filename); err == nil {
return true return true
} }

@ -10,6 +10,7 @@ import (
"strings" "strings"
common "github.com/shirou/gopsutil/common" common "github.com/shirou/gopsutil/common"
cpu "github.com/shirou/gopsutil/cpu"
) )
type CgroupMemStat struct { type CgroupMemStat struct {
@ -64,7 +65,7 @@ func GetDockerIDList() ([]string, error) {
// containerid is same as docker id if you use docker. // containerid is same as docker id if you use docker.
// If you use container via systemd.slice, you could use // If you use container via systemd.slice, you could use
// containerid = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/ // containerid = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func CgroupCPU(containerid string, base string) (*CPUTimesStat, error) { func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) {
if len(base) == 0 { if len(base) == 0 {
base = "/sys/fs/cgroup/cpuacct/docker" base = "/sys/fs/cgroup/cpuacct/docker"
} }
@ -75,7 +76,7 @@ func CgroupCPU(containerid string, base string) (*CPUTimesStat, error) {
if len(containerid) == 0 { if len(containerid) == 0 {
containerid = "all" containerid = "all"
} }
ret := &CPUTimesStat{CPU: containerid} ret := &cpu.CPUTimesStat{CPU: containerid}
for _, line := range lines { for _, line := range lines {
fields := strings.Split(line, " ") fields := strings.Split(line, " ")
if fields[0] == "user" { if fields[0] == "user" {
@ -95,7 +96,7 @@ func CgroupCPU(containerid string, base string) (*CPUTimesStat, error) {
return ret, nil return ret, nil
} }
func CgroupCPUDocker(containerid string) (*CPUTimesStat, error) { func CgroupCPUDocker(containerid string) (*cpu.CPUTimesStat, error) {
return CgroupCPU(containerid, "/sys/fs/cgroup/cpuacct/docker") return CgroupCPU(containerid, "/sys/fs/cgroup/cpuacct/docker")
} }

@ -105,7 +105,7 @@ func Users() ([]UserStat, error) {
func getLSB() (*LSB, error) { func getLSB() (*LSB, error) {
ret := &LSB{} ret := &LSB{}
if pathExists("/etc/lsb-release") { if common.PathExists("/etc/lsb-release") {
contents, err := common.ReadLines("/etc/lsb-release") contents, err := common.ReadLines("/etc/lsb-release")
if err != nil { if err != nil {
return ret, err // return empty return ret, err // return empty
@ -126,7 +126,7 @@ func getLSB() (*LSB, error) {
ret.Description = field[1] ret.Description = field[1]
} }
} }
} else if pathExists("/usr/bin/lsb_release") { } else if common.PathExists("/usr/bin/lsb_release") {
out, err := exec.Command("/usr/bin/lsb_release").Output() out, err := exec.Command("/usr/bin/lsb_release").Output()
if err != nil { if err != nil {
return ret, err return ret, err
@ -160,19 +160,19 @@ func GetPlatformInformation() (platform string, family string, version string, e
lsb = &LSB{} lsb = &LSB{}
} }
if pathExists("/etc/oracle-release") { if common.PathExists("/etc/oracle-release") {
platform = "oracle" platform = "oracle"
contents, err := common.ReadLines("/etc/oracle-release") contents, err := common.ReadLines("/etc/oracle-release")
if err == nil { if err == nil {
version = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
} else if pathExists("/etc/enterprise-release") { } else if common.PathExists("/etc/enterprise-release") {
platform = "oracle" platform = "oracle"
contents, err := common.ReadLines("/etc/enterprise-release") contents, err := common.ReadLines("/etc/enterprise-release")
if err == nil { if err == nil {
version = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
} else if pathExists("/etc/debian_version") { } else if common.PathExists("/etc/debian_version") {
if lsb.ID == "Ubuntu" { if lsb.ID == "Ubuntu" {
platform = "ubuntu" platform = "ubuntu"
version = lsb.Release version = lsb.Release
@ -180,7 +180,7 @@ func GetPlatformInformation() (platform string, family string, version string, e
platform = "linuxmint" platform = "linuxmint"
version = lsb.Release version = lsb.Release
} else { } else {
if pathExists("/usr/bin/raspi-config") { if common.PathExists("/usr/bin/raspi-config") {
platform = "raspbian" platform = "raspbian"
} else { } else {
platform = "debian" platform = "debian"
@ -190,19 +190,19 @@ func GetPlatformInformation() (platform string, family string, version string, e
version = contents[0] version = contents[0]
} }
} }
} else if pathExists("/etc/redhat-release") { } else if common.PathExists("/etc/redhat-release") {
contents, err := common.ReadLines("/etc/redhat-release") contents, err := common.ReadLines("/etc/redhat-release")
if err == nil { if err == nil {
version = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents) platform = getRedhatishPlatform(contents)
} }
} else if pathExists("/etc/system-release") { } else if common.PathExists("/etc/system-release") {
contents, err := common.ReadLines("/etc/system-release") contents, err := common.ReadLines("/etc/system-release")
if err == nil { if err == nil {
version = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents) platform = getRedhatishPlatform(contents)
} }
} else if pathExists("/etc/gentoo-release") { } else if common.PathExists("/etc/gentoo-release") {
platform = "gentoo" platform = "gentoo"
contents, err := common.ReadLines("/etc/gentoo-release") contents, err := common.ReadLines("/etc/gentoo-release")
if err == nil { if err == nil {
@ -210,7 +210,7 @@ func GetPlatformInformation() (platform string, family string, version string, e
} }
// TODO: suse detection // TODO: suse detection
// TODO: slackware detecion // TODO: slackware detecion
} else if pathExists("/etc/arch-release") { } else if common.PathExists("/etc/arch-release") {
platform = "arch" platform = "arch"
// TODO: exherbo detection // TODO: exherbo detection
} else if lsb.ID == "RedHat" { } else if lsb.ID == "RedHat" {
@ -280,78 +280,78 @@ func GetVirtualization() (string, string, error) {
var system string var system string
var role string var role string
if pathExists("/proc/xen") { if common.PathExists("/proc/xen") {
system = "xen" system = "xen"
role = "guest" // assume guest role = "guest" // assume guest
if pathExists("/proc/xen/capabilities") { if common.PathExists("/proc/xen/capabilities") {
contents, err := common.ReadLines("/proc/xen/capabilities") contents, err := common.ReadLines("/proc/xen/capabilities")
if err == nil { if err == nil {
if stringContains(contents, "control_d") { if common.StringContains(contents, "control_d") {
role = "host" role = "host"
} }
} }
} }
} }
if pathExists("/proc/modules") { if common.PathExists("/proc/modules") {
contents, err := common.ReadLines("/proc/modules") contents, err := common.ReadLines("/proc/modules")
if err == nil { if err == nil {
if stringContains(contents, "kvm") { if common.StringContains(contents, "kvm") {
system = "kvm" system = "kvm"
role = "host" role = "host"
} else if stringContains(contents, "vboxdrv") { } else if common.StringContains(contents, "vboxdrv") {
system = "vbox" system = "vbox"
role = "host" role = "host"
} else if stringContains(contents, "vboxguest") { } else if common.StringContains(contents, "vboxguest") {
system = "vbox" system = "vbox"
role = "guest" role = "guest"
} }
} }
} }
if pathExists("/proc/cpuinfo") { if common.PathExists("/proc/cpuinfo") {
contents, err := common.ReadLines("/proc/cpuinfo") contents, err := common.ReadLines("/proc/cpuinfo")
if err == nil { if err == nil {
if stringContains(contents, "QEMU Virtual CPU") || if common.StringContains(contents, "QEMU Virtual CPU") ||
stringContains(contents, "Common KVM processor") || common.StringContains(contents, "Common KVM processor") ||
stringContains(contents, "Common 32-bit KVM processor") { common.StringContains(contents, "Common 32-bit KVM processor") {
system = "kvm" system = "kvm"
role = "guest" role = "guest"
} }
} }
} }
if pathExists("/proc/bc/0") { if common.PathExists("/proc/bc/0") {
system = "openvz" system = "openvz"
role = "host" role = "host"
} else if pathExists("/proc/vz") { } else if common.PathExists("/proc/vz") {
system = "openvz" system = "openvz"
role = "guest" role = "guest"
} }
// not use dmidecode because it requires root // not use dmidecode because it requires root
if pathExists("/proc/self/status") { if common.PathExists("/proc/self/status") {
contents, err := common.ReadLines("/proc/self/status") contents, err := common.ReadLines("/proc/self/status")
if err == nil { if err == nil {
if stringContains(contents, "s_context:") || if common.StringContains(contents, "s_context:") ||
stringContains(contents, "VxID:") { common.StringContains(contents, "VxID:") {
system = "linux-vserver" system = "linux-vserver"
} }
// TODO: guest or host // TODO: guest or host
} }
} }
if pathExists("/proc/self/cgroup") { if common.PathExists("/proc/self/cgroup") {
contents, err := common.ReadLines("/proc/self/cgroup") contents, err := common.ReadLines("/proc/self/cgroup")
if err == nil { if err == nil {
if stringContains(contents, "lxc") || if common.StringContains(contents, "lxc") ||
stringContains(contents, "docker") { common.StringContains(contents, "docker") {
system = "lxc" system = "lxc"
role = "guest" role = "guest"
} else if pathExists("/usr/bin/lxc-version") { // TODO: which } else if common.PathExists("/usr/bin/lxc-version") { // TODO: which
system = "lxc" system = "lxc"
role = "host" role = "host"
} }

@ -13,6 +13,7 @@ import (
common "github.com/shirou/gopsutil/common" common "github.com/shirou/gopsutil/common"
cpu "github.com/shirou/gopsutil/cpu" cpu "github.com/shirou/gopsutil/cpu"
host "github.com/shirou/gopsutil/host"
net "github.com/shirou/gopsutil/net" net "github.com/shirou/gopsutil/net"
) )
@ -555,7 +556,7 @@ func (p *Process) fillFromStat() (string, int32, *cpu.CPUTimesStat, int64, int32
System: float32(stime * (1000 / ClockTicks)), System: float32(stime * (1000 / ClockTicks)),
} }
bootTime, _ := BootTime() bootTime, _ := host.BootTime()
t, err := strconv.ParseUint(fields[21], 10, 64) t, err := strconv.ParseUint(fields[21], 10, 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, err return "", 0, nil, 0, 0, err

Loading…
Cancel
Save