提交 d11680c7 编写于 作者: W WAKAYAMA shirou

fix bugs on FreeBSD.

上级 a4671fcc
......@@ -17,18 +17,18 @@ import (
var NotImplementedError = errors.New("not implemented yet")
// readLines reads contents from file and splits them by new line.
// A convenience wrapper to readLinesOffsetN(filename, 0, -1).
func readLines(filename string) ([]string, error) {
return readLinesOffsetN(filename, 0, -1)
// ReadLines reads contents from file and splits them by new line.
// A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).
func ReadLines(filename string) ([]string, error) {
return ReadLinesOffsetN(filename, 0, -1)
}
// readLines reads contents from file and splits them by new line.
// ReadLines reads contents from file and splits them by new line.
// The offset tells at which line number to start.
// The count determines the number of lines to read (starting from offset):
// n >= 0: at most n lines
// n < 0: whole file
func readLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
f, err := os.Open(filename)
if err != nil {
return []string{""}, err
......
......@@ -89,7 +89,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
// Returns only one CPUInfoStat on FreeBSD
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/var/run/dmesg.boot"
lines, _ := readLines(filename)
lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat
......
......@@ -6,6 +6,8 @@ import (
"errors"
"strconv"
"strings"
common "github.com/shirou/gopsutil/common"
)
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
......@@ -13,9 +15,9 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var lines []string
if percpu {
ncpu, _ := CPUCounts(true)
lines, _ = readLinesOffsetN(filename, 1, ncpu)
lines, _ = common.ReadLinesOffsetN(filename, 1, ncpu)
} else {
lines, _ = readLinesOffsetN(filename, 0, 1)
lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
}
ret := make([]CPUTimesStat, 0, len(lines))
......@@ -33,7 +35,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/proc/cpuinfo"
lines, _ := readLines(filename)
lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat
......
......@@ -101,7 +101,7 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
ret := make(map[string]DiskIOCountersStat, 0)
for _, stat := range fs {
name := ByteToString(stat.FMntonname[:])
name := common.ByteToString(stat.FMntonname[:])
d := DiskIOCountersStat{
Name: name,
ReadCount: stat.FSyncwrites + stat.FAsyncwrites,
......
......@@ -7,6 +7,8 @@ import (
"os/exec"
"strconv"
"strings"
common "github.com/shirou/gopsutil/common"
)
const (
......@@ -18,7 +20,7 @@ const (
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
filename := "/etc/mtab"
lines, err := readLines(filename)
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err
}
......@@ -41,7 +43,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
filename := "/proc/diskstats"
lines, err := readLines(filename)
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err
}
......
......@@ -8,6 +8,8 @@ import (
"path"
"strconv"
"strings"
common "github.com/shirou/gopsutil/common"
)
type CgroupMemStat struct {
......@@ -68,7 +70,7 @@ func CgroupCPU(containerid string, base string) (*CPUTimesStat, error) {
}
path := path.Join(base, containerid, "cpuacct.stat")
lines, _ := readLines(path)
lines, _ := common.ReadLines(path)
// empty containerid means all cgroup
if len(containerid) == 0 {
containerid = "all"
......@@ -106,7 +108,7 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
if len(containerid) == 0 {
containerid = "all"
}
lines, _ := readLines(path)
lines, _ := common.ReadLines(path)
ret := &CgroupMemStat{ContainerID: containerid}
for _, line := range lines {
fields := strings.Split(line, " ")
......
......@@ -106,7 +106,7 @@ func Users() ([]UserStat, error) {
func getLSB() (*LSB, error) {
ret := &LSB{}
if pathExists("/etc/lsb-release") {
contents, err := readLines("/etc/lsb-release")
contents, err := common.ReadLines("/etc/lsb-release")
if err != nil {
return ret, err // return empty
}
......@@ -162,13 +162,13 @@ func GetPlatformInformation() (platform string, family string, version string, e
if pathExists("/etc/oracle-release") {
platform = "oracle"
contents, err := readLines("/etc/oracle-release")
contents, err := common.ReadLines("/etc/oracle-release")
if err == nil {
version = getRedhatishVersion(contents)
}
} else if pathExists("/etc/enterprise-release") {
platform = "oracle"
contents, err := readLines("/etc/enterprise-release")
contents, err := common.ReadLines("/etc/enterprise-release")
if err == nil {
version = getRedhatishVersion(contents)
}
......@@ -185,26 +185,26 @@ func GetPlatformInformation() (platform string, family string, version string, e
} else {
platform = "debian"
}
contents, err := readLines("/etc/debian_version")
contents, err := common.ReadLines("/etc/debian_version")
if err == nil {
version = contents[0]
}
}
} else if pathExists("/etc/redhat-release") {
contents, err := readLines("/etc/redhat-release")
contents, err := common.ReadLines("/etc/redhat-release")
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if pathExists("/etc/system-release") {
contents, err := readLines("/etc/system-release")
contents, err := common.ReadLines("/etc/system-release")
if err == nil {
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if pathExists("/etc/gentoo-release") {
platform = "gentoo"
contents, err := readLines("/etc/gentoo-release")
contents, err := common.ReadLines("/etc/gentoo-release")
if err == nil {
version = getRedhatishVersion(contents)
}
......@@ -285,7 +285,7 @@ func GetVirtualization() (string, string, error) {
role = "guest" // assume guest
if pathExists("/proc/xen/capabilities") {
contents, err := readLines("/proc/xen/capabilities")
contents, err := common.ReadLines("/proc/xen/capabilities")
if err == nil {
if stringContains(contents, "control_d") {
role = "host"
......@@ -294,7 +294,7 @@ func GetVirtualization() (string, string, error) {
}
}
if pathExists("/proc/modules") {
contents, err := readLines("/proc/modules")
contents, err := common.ReadLines("/proc/modules")
if err == nil {
if stringContains(contents, "kvm") {
system = "kvm"
......@@ -310,7 +310,7 @@ func GetVirtualization() (string, string, error) {
}
if pathExists("/proc/cpuinfo") {
contents, err := readLines("/proc/cpuinfo")
contents, err := common.ReadLines("/proc/cpuinfo")
if err == nil {
if stringContains(contents, "QEMU Virtual CPU") ||
stringContains(contents, "Common KVM processor") ||
......@@ -332,7 +332,7 @@ func GetVirtualization() (string, string, error) {
// not use dmidecode because it requires root
if pathExists("/proc/self/status") {
contents, err := readLines("/proc/self/status")
contents, err := common.ReadLines("/proc/self/status")
if err == nil {
if stringContains(contents, "s_context:") ||
......@@ -344,7 +344,7 @@ func GetVirtualization() (string, string, error) {
}
if pathExists("/proc/self/cgroup") {
contents, err := readLines("/proc/self/cgroup")
contents, err := common.ReadLines("/proc/self/cgroup")
if err == nil {
if stringContains(contents, "lxc") ||
......
......@@ -6,11 +6,13 @@ import (
"strconv"
"strings"
"syscall"
common "github.com/shirou/gopsutil/common"
)
func VirtualMemory() (*VirtualMemoryStat, error) {
filename := "/proc/meminfo"
lines, _ := readLines(filename)
lines, _ := common.ReadLines(filename)
ret := &VirtualMemoryStat{}
for _, line := range lines {
......@@ -65,7 +67,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
} else {
ret.UsedPercent = 0
}
lines, _ := readLines("/proc/vmstat")
lines, _ := common.ReadLines("/proc/vmstat")
for _, l := range lines {
fields := strings.Fields(l)
if len(fields) < 2 {
......
......@@ -5,11 +5,13 @@ package gopsutil
import (
"strconv"
"strings"
common "github.com/shirou/gopsutil/common"
)
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := "/proc/net/dev"
lines, err := readLines(filename)
lines, err := common.ReadLines(filename)
if err != nil {
return nil, err
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册