diff --git a/common/common.go b/common/common.go index 92ec34fb8a9d4ee1cb575fcacc013de6e27c9b70..99119ff61df534b6d1d0785b6a57b495919a378c 100644 --- a/common/common.go +++ b/common/common.go @@ -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 diff --git a/cpu/cpu_freebsd.go b/cpu/cpu_freebsd.go index f1055bfba5a4c6db1ec8920b28e1ee047d0a16bc..29c8b7cc0207a52cefbe1cbbd75c92ec1af4ed54 100644 --- a/cpu/cpu_freebsd.go +++ b/cpu/cpu_freebsd.go @@ -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 diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index 3b2ee715eba146371e420e1ac7d330f4162610e9..8fb03c240d6ff8d3664a72e0cdee78bc6d7b24f5 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -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 diff --git a/disk/disk_freebsd.go b/disk/disk_freebsd.go index c415e159c7759454e4ee6fa47fc587204f59a675..1c8d746f57a941dc8bea58106f3671e82f574faf 100644 --- a/disk/disk_freebsd.go +++ b/disk/disk_freebsd.go @@ -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, diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 51e504b6d75e49033dbcac2bf45a49e96a31228f..3f075ff12c29274358340bbe85eb30e686094c21 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -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 } diff --git a/docker/docker_linux.go b/docker/docker_linux.go index 5320c31780a6e7bc90757b3b82eaf27b69783f1c..0845837ef6404e4190f46952126245f6e956bb57 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -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, " ") diff --git a/host/host_linux.go b/host/host_linux.go index d5a5936fdb7610e81527f6c9094972c14f96982c..46480055fe01f9e17b610c3003740619882937bb 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -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") || diff --git a/mem/mem_linux.go b/mem/mem_linux.go index fa113ba705c911b4e667593e1349b4a9372f0599..aecdee1f79f8e089334a0601975cdae04e7ba1c3 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -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 { diff --git a/net/net_linux.go b/net/net_linux.go index 9cc38aeb6e065d36b2c90a98af90f821e0bcaf38..b3a8841955afb7973c11fe2e04f0d134e44c7ad5 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -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 }