2016-02-12 18:02:16 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-07-28 16:46:44 +00:00
|
|
|
"path/filepath"
|
2016-02-12 18:02:16 +00:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-07-28 16:46:44 +00:00
|
|
|
"sync/atomic"
|
2016-02-12 18:02:16 +00:00
|
|
|
"time"
|
|
|
|
|
2016-08-30 17:48:07 +00:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LSB struct {
|
|
|
|
ID string
|
|
|
|
Release string
|
|
|
|
Codename string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
// from utmp.h
|
|
|
|
const USER_PROCESS = 7
|
|
|
|
|
|
|
|
func Info() (*InfoStat, error) {
|
|
|
|
ret := &InfoStat{
|
2016-02-12 18:02:16 +00:00
|
|
|
OS: runtime.GOOS,
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
ret.Hostname = hostname
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
platform, family, version, err := PlatformInformation()
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
ret.Platform = platform
|
|
|
|
ret.PlatformFamily = family
|
|
|
|
ret.PlatformVersion = version
|
|
|
|
}
|
2016-08-30 17:48:07 +00:00
|
|
|
kernelVersion, err := KernelVersion()
|
|
|
|
if err == nil {
|
|
|
|
ret.KernelVersion = kernelVersion
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
system, role, err := Virtualization()
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
ret.VirtualizationSystem = system
|
|
|
|
ret.VirtualizationRole = role
|
|
|
|
}
|
2016-08-30 17:48:07 +00:00
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
boot, err := BootTime()
|
|
|
|
if err == nil {
|
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime = uptime(boot)
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:48:07 +00:00
|
|
|
if numProcs, err := common.NumProcs(); err == nil {
|
|
|
|
ret.Procs = numProcs
|
|
|
|
}
|
|
|
|
|
|
|
|
sysProductUUID := common.HostSys("class/dmi/id/product_uuid")
|
|
|
|
switch {
|
|
|
|
case common.PathExists(sysProductUUID):
|
|
|
|
lines, err := common.ReadLines(sysProductUUID)
|
|
|
|
if err == nil && len(lines) > 0 && lines[0] != "" {
|
2017-02-07 00:16:20 +00:00
|
|
|
ret.HostID = strings.ToLower(lines[0])
|
2016-08-30 17:48:07 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
values, err := common.DoSysctrl("kernel.random.boot_id")
|
|
|
|
if err == nil && len(values) == 1 && values[0] != "" {
|
2017-02-07 00:16:20 +00:00
|
|
|
ret.HostID = strings.ToLower(values[0])
|
2016-08-30 17:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
// cachedBootTime must be accessed via atomic.Load/StoreUint64
|
|
|
|
var cachedBootTime uint64
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
// BootTime returns the system boot time expressed in seconds since the epoch.
|
|
|
|
func BootTime() (uint64, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
t := atomic.LoadUint64(&cachedBootTime)
|
|
|
|
if t != 0 {
|
|
|
|
return t, nil
|
2017-02-02 06:41:29 +00:00
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
filename := common.HostProc("stat")
|
|
|
|
lines, err := common.ReadLines(filename)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasPrefix(line, "btime") {
|
|
|
|
f := strings.Fields(line)
|
|
|
|
if len(f) != 2 {
|
|
|
|
return 0, fmt.Errorf("wrong btime format")
|
|
|
|
}
|
|
|
|
b, err := strconv.ParseInt(f[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
t = uint64(b)
|
|
|
|
atomic.StoreUint64(&cachedBootTime, t)
|
|
|
|
return t, nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("could not find btime")
|
|
|
|
}
|
|
|
|
|
|
|
|
func uptime(boot uint64) uint64 {
|
|
|
|
return uint64(time.Now().Unix()) - boot
|
|
|
|
}
|
|
|
|
|
|
|
|
func Uptime() (uint64, error) {
|
|
|
|
boot, err := BootTime()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uptime(boot), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Users() ([]UserStat, error) {
|
|
|
|
utmpfile := "/var/run/utmp"
|
|
|
|
|
|
|
|
file, err := os.Open(utmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-13 17:50:09 +00:00
|
|
|
defer file.Close()
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
count := len(buf) / sizeOfUtmp
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
ret := make([]UserStat, 0, count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2016-05-09 15:19:04 +00:00
|
|
|
b := buf[i*sizeOfUtmp : (i+1)*sizeOfUtmp]
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
var u utmp
|
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
if u.Type != USER_PROCESS {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
user := UserStat{
|
|
|
|
User: common.IntToString(u.User[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
2016-05-09 15:19:04 +00:00
|
|
|
Started: int(u.Tv.Sec),
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:31:00 +00:00
|
|
|
func getOSRelease() (platform string, version string, err error) {
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("os-release"))
|
|
|
|
if err != nil {
|
|
|
|
return "", "", nil // return empty
|
|
|
|
}
|
|
|
|
for _, line := range contents {
|
|
|
|
field := strings.Split(line, "=")
|
|
|
|
if len(field) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch field[0] {
|
|
|
|
case "ID": // use ID for lowercase
|
|
|
|
platform = field[1]
|
|
|
|
case "VERSION":
|
|
|
|
version = field[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return platform, version, nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func getLSB() (*LSB, error) {
|
|
|
|
ret := &LSB{}
|
2016-05-09 15:19:04 +00:00
|
|
|
if common.PathExists(common.HostEtc("lsb-release")) {
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("lsb-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, err // return empty
|
|
|
|
}
|
|
|
|
for _, line := range contents {
|
|
|
|
field := strings.Split(line, "=")
|
|
|
|
if len(field) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch field[0] {
|
|
|
|
case "DISTRIB_ID":
|
|
|
|
ret.ID = field[1]
|
|
|
|
case "DISTRIB_RELEASE":
|
|
|
|
ret.Release = field[1]
|
|
|
|
case "DISTRIB_CODENAME":
|
|
|
|
ret.Codename = field[1]
|
|
|
|
case "DISTRIB_DESCRIPTION":
|
|
|
|
ret.Description = field[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if common.PathExists("/usr/bin/lsb_release") {
|
2016-05-09 15:19:04 +00:00
|
|
|
lsb_release, err := exec.LookPath("/usr/bin/lsb_release")
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2016-06-17 14:31:00 +00:00
|
|
|
out, err := invoke.Command(lsb_release)
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
|
|
field := strings.Split(line, ":")
|
|
|
|
if len(field) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch field[0] {
|
|
|
|
case "Distributor ID":
|
|
|
|
ret.ID = field[1]
|
|
|
|
case "Release":
|
|
|
|
ret.Release = field[1]
|
|
|
|
case "Codename":
|
|
|
|
ret.Codename = field[1]
|
|
|
|
case "Description":
|
|
|
|
ret.Description = field[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func PlatformInformation() (platform string, family string, version string, err error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
lsb, err := getLSB()
|
|
|
|
if err != nil {
|
|
|
|
lsb = &LSB{}
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
if common.PathExists(common.HostEtc("oracle-release")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
platform = "oracle"
|
2016-05-09 15:19:04 +00:00
|
|
|
contents, err := common.ReadLines(common.HostEtc("oracle-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = getRedhatishVersion(contents)
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
|
|
|
|
} else if common.PathExists(common.HostEtc("enterprise-release")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
platform = "oracle"
|
2016-05-09 15:19:04 +00:00
|
|
|
contents, err := common.ReadLines(common.HostEtc("enterprise-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = getRedhatishVersion(contents)
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("debian_version")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
if lsb.ID == "Ubuntu" {
|
|
|
|
platform = "ubuntu"
|
|
|
|
version = lsb.Release
|
|
|
|
} else if lsb.ID == "LinuxMint" {
|
|
|
|
platform = "linuxmint"
|
|
|
|
version = lsb.Release
|
|
|
|
} else {
|
|
|
|
if common.PathExists("/usr/bin/raspi-config") {
|
|
|
|
platform = "raspbian"
|
|
|
|
} else {
|
|
|
|
platform = "debian"
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
contents, err := common.ReadLines(common.HostEtc("debian_version"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = contents[0]
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("redhat-release")) {
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("redhat-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = getRedhatishVersion(contents)
|
|
|
|
platform = getRedhatishPlatform(contents)
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("system-release")) {
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("system-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = getRedhatishVersion(contents)
|
|
|
|
platform = getRedhatishPlatform(contents)
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("gentoo-release")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
platform = "gentoo"
|
2016-05-09 15:19:04 +00:00
|
|
|
contents, err := common.ReadLines(common.HostEtc("gentoo-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = getRedhatishVersion(contents)
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("SuSE-release")) {
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("SuSE-release"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = getSuseVersion(contents)
|
|
|
|
platform = getSusePlatform(contents)
|
|
|
|
}
|
|
|
|
// TODO: slackware detecion
|
2016-05-09 15:19:04 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("arch-release")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
platform = "arch"
|
2016-05-09 15:19:04 +00:00
|
|
|
version = lsb.Release
|
2016-06-17 14:31:00 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("alpine-release")) {
|
|
|
|
platform = "alpine"
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("alpine-release"))
|
|
|
|
if err == nil && len(contents) > 0 {
|
|
|
|
version = contents[0]
|
|
|
|
}
|
|
|
|
} else if common.PathExists(common.HostEtc("os-release")) {
|
|
|
|
p, v, err := getOSRelease()
|
|
|
|
if err == nil {
|
|
|
|
platform = p
|
|
|
|
version = v
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
} else if lsb.ID == "RedHat" {
|
|
|
|
platform = "redhat"
|
|
|
|
version = lsb.Release
|
|
|
|
} else if lsb.ID == "Amazon" {
|
|
|
|
platform = "amazon"
|
|
|
|
version = lsb.Release
|
|
|
|
} else if lsb.ID == "ScientificSL" {
|
|
|
|
platform = "scientific"
|
|
|
|
version = lsb.Release
|
|
|
|
} else if lsb.ID == "XenServer" {
|
|
|
|
platform = "xenserver"
|
|
|
|
version = lsb.Release
|
|
|
|
} else if lsb.ID != "" {
|
|
|
|
platform = strings.ToLower(lsb.ID)
|
|
|
|
version = lsb.Release
|
|
|
|
}
|
|
|
|
|
|
|
|
switch platform {
|
|
|
|
case "debian", "ubuntu", "linuxmint", "raspbian":
|
|
|
|
family = "debian"
|
|
|
|
case "fedora":
|
|
|
|
family = "fedora"
|
|
|
|
case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm":
|
|
|
|
family = "rhel"
|
|
|
|
case "suse", "opensuse":
|
|
|
|
family = "suse"
|
|
|
|
case "gentoo":
|
|
|
|
family = "gentoo"
|
|
|
|
case "slackware":
|
|
|
|
family = "slackware"
|
|
|
|
case "arch":
|
|
|
|
family = "arch"
|
|
|
|
case "exherbo":
|
|
|
|
family = "exherbo"
|
2016-06-17 14:31:00 +00:00
|
|
|
case "alpine":
|
|
|
|
family = "alpine"
|
|
|
|
case "coreos":
|
|
|
|
family = "coreos"
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return platform, family, version, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:48:07 +00:00
|
|
|
func KernelVersion() (version string, err error) {
|
|
|
|
filename := common.HostProc("sys/kernel/osrelease")
|
|
|
|
if common.PathExists(filename) {
|
|
|
|
contents, err := common.ReadLines(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(contents) > 0 {
|
|
|
|
version = contents[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func getRedhatishVersion(contents []string) string {
|
|
|
|
c := strings.ToLower(strings.Join(contents, ""))
|
|
|
|
|
|
|
|
if strings.Contains(c, "rawhide") {
|
|
|
|
return "rawhide"
|
|
|
|
}
|
|
|
|
if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil {
|
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRedhatishPlatform(contents []string) string {
|
|
|
|
c := strings.ToLower(strings.Join(contents, ""))
|
|
|
|
|
|
|
|
if strings.Contains(c, "red hat") {
|
|
|
|
return "redhat"
|
|
|
|
}
|
|
|
|
f := strings.Split(c, " ")
|
|
|
|
|
|
|
|
return f[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSuseVersion(contents []string) string {
|
|
|
|
version := ""
|
|
|
|
for _, line := range contents {
|
|
|
|
if matches := regexp.MustCompile(`VERSION = ([\d.]+)`).FindStringSubmatch(line); matches != nil {
|
|
|
|
version = matches[1]
|
|
|
|
} else if matches := regexp.MustCompile(`PATCHLEVEL = ([\d]+)`).FindStringSubmatch(line); matches != nil {
|
|
|
|
version = version + "." + matches[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSusePlatform(contents []string) string {
|
|
|
|
c := strings.ToLower(strings.Join(contents, ""))
|
|
|
|
if strings.Contains(c, "opensuse") {
|
|
|
|
return "opensuse"
|
|
|
|
}
|
|
|
|
return "suse"
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func Virtualization() (string, string, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
var system string
|
|
|
|
var role string
|
|
|
|
|
|
|
|
filename := common.HostProc("xen")
|
|
|
|
if common.PathExists(filename) {
|
|
|
|
system = "xen"
|
|
|
|
role = "guest" // assume guest
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
if common.PathExists(filepath.Join(filename, "capabilities")) {
|
|
|
|
contents, err := common.ReadLines(filepath.Join(filename, "capabilities"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
2016-06-17 14:31:00 +00:00
|
|
|
if common.StringsContains(contents, "control_d") {
|
2016-02-12 18:02:16 +00:00
|
|
|
role = "host"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = common.HostProc("modules")
|
|
|
|
if common.PathExists(filename) {
|
|
|
|
contents, err := common.ReadLines(filename)
|
|
|
|
if err == nil {
|
|
|
|
if common.StringsContains(contents, "kvm") {
|
|
|
|
system = "kvm"
|
|
|
|
role = "host"
|
|
|
|
} else if common.StringsContains(contents, "vboxdrv") {
|
|
|
|
system = "vbox"
|
|
|
|
role = "host"
|
|
|
|
} else if common.StringsContains(contents, "vboxguest") {
|
|
|
|
system = "vbox"
|
|
|
|
role = "guest"
|
2017-07-28 16:46:44 +00:00
|
|
|
} else if common.StringsContains(contents, "vmware") {
|
|
|
|
system = "vmware"
|
|
|
|
role = "guest"
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = common.HostProc("cpuinfo")
|
|
|
|
if common.PathExists(filename) {
|
|
|
|
contents, err := common.ReadLines(filename)
|
|
|
|
if err == nil {
|
2016-06-17 14:31:00 +00:00
|
|
|
if common.StringsContains(contents, "QEMU Virtual CPU") ||
|
|
|
|
common.StringsContains(contents, "Common KVM processor") ||
|
|
|
|
common.StringsContains(contents, "Common 32-bit KVM processor") {
|
2016-02-12 18:02:16 +00:00
|
|
|
system = "kvm"
|
|
|
|
role = "guest"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = common.HostProc()
|
2017-07-28 16:46:44 +00:00
|
|
|
if common.PathExists(filepath.Join(filename, "bc", "0")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
system = "openvz"
|
|
|
|
role = "host"
|
2017-07-28 16:46:44 +00:00
|
|
|
} else if common.PathExists(filepath.Join(filename, "vz")) {
|
2016-02-12 18:02:16 +00:00
|
|
|
system = "openvz"
|
|
|
|
role = "guest"
|
|
|
|
}
|
|
|
|
|
|
|
|
// not use dmidecode because it requires root
|
2017-07-28 16:46:44 +00:00
|
|
|
if common.PathExists(filepath.Join(filename, "self", "status")) {
|
|
|
|
contents, err := common.ReadLines(filepath.Join(filename, "self", "status"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
|
2016-06-17 14:31:00 +00:00
|
|
|
if common.StringsContains(contents, "s_context:") ||
|
|
|
|
common.StringsContains(contents, "VxID:") {
|
2016-02-12 18:02:16 +00:00
|
|
|
system = "linux-vserver"
|
|
|
|
}
|
|
|
|
// TODO: guest or host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
if common.PathExists(filepath.Join(filename, "self", "cgroup")) {
|
|
|
|
contents, err := common.ReadLines(filepath.Join(filename, "self", "cgroup"))
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
2016-06-17 14:31:00 +00:00
|
|
|
if common.StringsContains(contents, "lxc") {
|
2016-02-12 18:02:16 +00:00
|
|
|
system = "lxc"
|
|
|
|
role = "guest"
|
2016-06-17 14:31:00 +00:00
|
|
|
} else if common.StringsContains(contents, "docker") {
|
|
|
|
system = "docker"
|
|
|
|
role = "guest"
|
|
|
|
} else if common.StringsContains(contents, "machine-rkt") {
|
|
|
|
system = "rkt"
|
|
|
|
role = "guest"
|
|
|
|
} else if common.PathExists("/usr/bin/lxc-version") {
|
2016-02-12 18:02:16 +00:00
|
|
|
system = "lxc"
|
|
|
|
role = "host"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:31:00 +00:00
|
|
|
if common.PathExists(common.HostEtc("os-release")) {
|
|
|
|
p, _, err := getOSRelease()
|
|
|
|
if err == nil && p == "coreos" {
|
|
|
|
system = "rkt" // Is it true?
|
|
|
|
role = "host"
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
return system, role, nil
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
func SensorsTemperatures() ([]TemperatureStat, error) {
|
|
|
|
var temperatures []TemperatureStat
|
|
|
|
files, err := filepath.Glob(common.HostSys("/class/hwmon/hwmon*/temp*_*"))
|
|
|
|
if err != nil {
|
|
|
|
return temperatures, err
|
|
|
|
}
|
|
|
|
if len(files) == 0 {
|
|
|
|
// CentOS has an intermediate /device directory:
|
|
|
|
// https://github.com/giampaolo/psutil/issues/971
|
|
|
|
files, err = filepath.Glob(common.HostSys("/class/hwmon/hwmon*/temp*_*"))
|
|
|
|
if err != nil {
|
|
|
|
return temperatures, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range files {
|
|
|
|
match = strings.Split(match, "_")[0]
|
|
|
|
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(match), "name"))
|
|
|
|
if err != nil {
|
|
|
|
return temperatures, err
|
|
|
|
}
|
|
|
|
current, err := ioutil.ReadFile(match + "_input")
|
|
|
|
if err != nil {
|
|
|
|
return temperatures, err
|
|
|
|
}
|
|
|
|
temperature, err := strconv.ParseFloat(string(current), 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
temperatures = append(temperatures, TemperatureStat{
|
|
|
|
SensorKey: string(name),
|
|
|
|
Temperature: temperature / 1000.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return temperatures, nil
|
|
|
|
}
|