fix bugs on FreeBSD.

tags/1.0.0^2
WAKAYAMA shirou 10 years ago
parent a4671fcc2a
commit d11680c773

@ -17,18 +17,18 @@ import (
var NotImplementedError = errors.New("not implemented yet") var NotImplementedError = errors.New("not implemented yet")
// readLines reads contents from file and splits them by new line. // ReadLines reads contents from file and splits them by new line.
// A convenience wrapper to readLinesOffsetN(filename, 0, -1). // A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).
func readLines(filename string) ([]string, error) { func ReadLines(filename string) ([]string, error) {
return readLinesOffsetN(filename, 0, -1) 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 offset tells at which line number to start.
// The count determines the number of lines to read (starting from offset): // The count determines the number of lines to read (starting from offset):
// n >= 0: at most n lines // n >= 0: at most n lines
// n < 0: whole file // 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) f, err := os.Open(filename)
if err != nil { if err != nil {
return []string{""}, err return []string{""}, err

@ -89,7 +89,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
// Returns only one CPUInfoStat on FreeBSD // Returns only one CPUInfoStat on FreeBSD
func CPUInfo() ([]CPUInfoStat, error) { func CPUInfo() ([]CPUInfoStat, error) {
filename := "/var/run/dmesg.boot" filename := "/var/run/dmesg.boot"
lines, _ := readLines(filename) lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat var ret []CPUInfoStat

@ -6,6 +6,8 @@ import (
"errors" "errors"
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common"
) )
func CPUTimes(percpu bool) ([]CPUTimesStat, error) { func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
@ -13,9 +15,9 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var lines []string var lines []string
if percpu { if percpu {
ncpu, _ := CPUCounts(true) ncpu, _ := CPUCounts(true)
lines, _ = readLinesOffsetN(filename, 1, ncpu) lines, _ = common.ReadLinesOffsetN(filename, 1, ncpu)
} else { } else {
lines, _ = readLinesOffsetN(filename, 0, 1) lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
} }
ret := make([]CPUTimesStat, 0, len(lines)) ret := make([]CPUTimesStat, 0, len(lines))
@ -33,7 +35,7 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
func CPUInfo() ([]CPUInfoStat, error) { func CPUInfo() ([]CPUInfoStat, error) {
filename := "/proc/cpuinfo" filename := "/proc/cpuinfo"
lines, _ := readLines(filename) lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat var ret []CPUInfoStat

@ -101,7 +101,7 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
ret := make(map[string]DiskIOCountersStat, 0) ret := make(map[string]DiskIOCountersStat, 0)
for _, stat := range fs { for _, stat := range fs {
name := ByteToString(stat.FMntonname[:]) name := common.ByteToString(stat.FMntonname[:])
d := DiskIOCountersStat{ d := DiskIOCountersStat{
Name: name, Name: name,
ReadCount: stat.FSyncwrites + stat.FAsyncwrites, ReadCount: stat.FSyncwrites + stat.FAsyncwrites,

@ -7,6 +7,8 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common"
) )
const ( const (
@ -18,7 +20,7 @@ const (
func DiskPartitions(all bool) ([]DiskPartitionStat, error) { func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
filename := "/etc/mtab" filename := "/etc/mtab"
lines, err := readLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -41,7 +43,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
func DiskIOCounters() (map[string]DiskIOCountersStat, error) { func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
filename := "/proc/diskstats" filename := "/proc/diskstats"
lines, err := readLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -8,6 +8,8 @@ import (
"path" "path"
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common"
) )
type CgroupMemStat struct { type CgroupMemStat struct {
@ -68,7 +70,7 @@ func CgroupCPU(containerid string, base string) (*CPUTimesStat, error) {
} }
path := path.Join(base, containerid, "cpuacct.stat") path := path.Join(base, containerid, "cpuacct.stat")
lines, _ := readLines(path) lines, _ := common.ReadLines(path)
// empty containerid means all cgroup // empty containerid means all cgroup
if len(containerid) == 0 { if len(containerid) == 0 {
containerid = "all" containerid = "all"
@ -106,7 +108,7 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
if len(containerid) == 0 { if len(containerid) == 0 {
containerid = "all" containerid = "all"
} }
lines, _ := readLines(path) lines, _ := common.ReadLines(path)
ret := &CgroupMemStat{ContainerID: containerid} ret := &CgroupMemStat{ContainerID: containerid}
for _, line := range lines { for _, line := range lines {
fields := strings.Split(line, " ") fields := strings.Split(line, " ")

@ -106,7 +106,7 @@ func Users() ([]UserStat, error) {
func getLSB() (*LSB, error) { func getLSB() (*LSB, error) {
ret := &LSB{} ret := &LSB{}
if pathExists("/etc/lsb-release") { if pathExists("/etc/lsb-release") {
contents, err := 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
} }
@ -162,13 +162,13 @@ func GetPlatformInformation() (platform string, family string, version string, e
if pathExists("/etc/oracle-release") { if pathExists("/etc/oracle-release") {
platform = "oracle" platform = "oracle"
contents, err := 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 pathExists("/etc/enterprise-release") {
platform = "oracle" platform = "oracle"
contents, err := readLines("/etc/enterprise-release") contents, err := common.ReadLines("/etc/enterprise-release")
if err == nil { if err == nil {
version = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
@ -185,26 +185,26 @@ func GetPlatformInformation() (platform string, family string, version string, e
} else { } else {
platform = "debian" platform = "debian"
} }
contents, err := readLines("/etc/debian_version") contents, err := common.ReadLines("/etc/debian_version")
if err == nil { if err == nil {
version = contents[0] version = contents[0]
} }
} }
} else if pathExists("/etc/redhat-release") { } else if pathExists("/etc/redhat-release") {
contents, err := 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 pathExists("/etc/system-release") {
contents, err := 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 pathExists("/etc/gentoo-release") {
platform = "gentoo" platform = "gentoo"
contents, err := readLines("/etc/gentoo-release") contents, err := common.ReadLines("/etc/gentoo-release")
if err == nil { if err == nil {
version = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
@ -285,7 +285,7 @@ func GetVirtualization() (string, string, error) {
role = "guest" // assume guest role = "guest" // assume guest
if pathExists("/proc/xen/capabilities") { if pathExists("/proc/xen/capabilities") {
contents, err := readLines("/proc/xen/capabilities") contents, err := common.ReadLines("/proc/xen/capabilities")
if err == nil { if err == nil {
if stringContains(contents, "control_d") { if stringContains(contents, "control_d") {
role = "host" role = "host"
@ -294,7 +294,7 @@ func GetVirtualization() (string, string, error) {
} }
} }
if pathExists("/proc/modules") { if pathExists("/proc/modules") {
contents, err := readLines("/proc/modules") contents, err := common.ReadLines("/proc/modules")
if err == nil { if err == nil {
if stringContains(contents, "kvm") { if stringContains(contents, "kvm") {
system = "kvm" system = "kvm"
@ -310,7 +310,7 @@ func GetVirtualization() (string, string, error) {
} }
if pathExists("/proc/cpuinfo") { if pathExists("/proc/cpuinfo") {
contents, err := readLines("/proc/cpuinfo") contents, err := common.ReadLines("/proc/cpuinfo")
if err == nil { if err == nil {
if stringContains(contents, "QEMU Virtual CPU") || if stringContains(contents, "QEMU Virtual CPU") ||
stringContains(contents, "Common KVM processor") || stringContains(contents, "Common KVM processor") ||
@ -332,7 +332,7 @@ func GetVirtualization() (string, string, error) {
// not use dmidecode because it requires root // not use dmidecode because it requires root
if pathExists("/proc/self/status") { if pathExists("/proc/self/status") {
contents, err := readLines("/proc/self/status") contents, err := common.ReadLines("/proc/self/status")
if err == nil { if err == nil {
if stringContains(contents, "s_context:") || if stringContains(contents, "s_context:") ||
@ -344,7 +344,7 @@ func GetVirtualization() (string, string, error) {
} }
if pathExists("/proc/self/cgroup") { if pathExists("/proc/self/cgroup") {
contents, err := readLines("/proc/self/cgroup") contents, err := common.ReadLines("/proc/self/cgroup")
if err == nil { if err == nil {
if stringContains(contents, "lxc") || if stringContains(contents, "lxc") ||

@ -6,11 +6,13 @@ import (
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
common "github.com/shirou/gopsutil/common"
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
filename := "/proc/meminfo" filename := "/proc/meminfo"
lines, _ := readLines(filename) lines, _ := common.ReadLines(filename)
ret := &VirtualMemoryStat{} ret := &VirtualMemoryStat{}
for _, line := range lines { for _, line := range lines {
@ -65,7 +67,7 @@ func SwapMemory() (*SwapMemoryStat, error) {
} else { } else {
ret.UsedPercent = 0 ret.UsedPercent = 0
} }
lines, _ := readLines("/proc/vmstat") lines, _ := common.ReadLines("/proc/vmstat")
for _, l := range lines { for _, l := range lines {
fields := strings.Fields(l) fields := strings.Fields(l)
if len(fields) < 2 { if len(fields) < 2 {

@ -5,11 +5,13 @@ package gopsutil
import ( import (
"strconv" "strconv"
"strings" "strings"
common "github.com/shirou/gopsutil/common"
) )
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
filename := "/proc/net/dev" filename := "/proc/net/dev"
lines, err := readLines(filename) lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save