2016-02-12 18:02:16 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-03-06 00:52:43 +00:00
|
|
|
"context"
|
2016-02-12 18:02:16 +00:00
|
|
|
"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"
|
|
|
|
"time"
|
|
|
|
|
2016-08-30 17:48:07 +00:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2020-03-15 08:36:59 +00:00
|
|
|
"golang.org/x/sys/unix"
|
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) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
kernelArch, err := kernelArch()
|
|
|
|
if err == nil {
|
|
|
|
ret.KernelArch = kernelArch
|
|
|
|
}
|
|
|
|
|
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")
|
2020-03-15 08:36:59 +00:00
|
|
|
machineID := common.HostEtc("machine-id")
|
|
|
|
procSysKernelRandomBootID := common.HostProc("sys/kernel/random/boot_id")
|
2016-08-30 17:48:07 +00:00
|
|
|
switch {
|
2020-03-15 08:36:59 +00:00
|
|
|
// In order to read this file, needs to be supported by kernel/arch and run as root
|
|
|
|
// so having fallback is important
|
2016-08-30 17:48:07 +00:00
|
|
|
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
|
2020-03-15 08:36:59 +00:00
|
|
|
// Fallback on GNU Linux systems with systemd, readable by everyone
|
|
|
|
case common.PathExists(machineID):
|
|
|
|
lines, err := common.ReadLines(machineID)
|
|
|
|
if err == nil && len(lines) > 0 && len(lines[0]) == 32 {
|
|
|
|
st := lines[0]
|
|
|
|
ret.HostID = fmt.Sprintf("%s-%s-%s-%s-%s", st[0:8], st[8:12], st[12:16], st[16:20], st[20:32])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
// Not stable between reboot, but better than nothing
|
2016-08-30 17:48:07 +00:00
|
|
|
default:
|
2020-03-15 08:36:59 +00:00
|
|
|
lines, err := common.ReadLines(procSysKernelRandomBootID)
|
|
|
|
if err == nil && len(lines) > 0 && lines[0] != "" {
|
|
|
|
ret.HostID = strings.ToLower(lines[0])
|
2016-08-30 17:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BootTime returns the system boot time expressed in seconds since the epoch.
|
|
|
|
func BootTime() (uint64, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return BootTimeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BootTimeWithContext(ctx context.Context) (uint64, error) {
|
2020-03-15 08:36:59 +00:00
|
|
|
return common.BootTimeWithContext(ctx)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func uptime(boot uint64) uint64 {
|
|
|
|
return uint64(time.Now().Unix()) - boot
|
|
|
|
}
|
|
|
|
|
|
|
|
func Uptime() (uint64, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return UptimeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func UptimeWithContext(ctx context.Context) (uint64, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
boot, err := BootTime()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uptime(boot), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Users() ([]UserStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return UsersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
|
|
|
utmpfile := common.HostVar("run/utmp")
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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") {
|
2020-03-15 08:36:59 +00:00
|
|
|
lsb_release, err := exec.LookPath("lsb_release")
|
2016-05-09 15:19:04 +00:00
|
|
|
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) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return PlatformInformationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlatformInformationWithContext(ctx context.Context) (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)
|
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
} else if common.PathExists(common.HostEtc("slackware-version")) {
|
|
|
|
platform = "slackware"
|
|
|
|
contents, err := common.ReadLines(common.HostEtc("slackware-version"))
|
|
|
|
if err == nil {
|
|
|
|
version = getSlackwareVersion(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"))
|
2020-03-15 08:36:59 +00:00
|
|
|
if err == nil && len(contents) > 0 && contents[0] != "" {
|
2016-02-12 18:02:16 +00:00
|
|
|
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"))
|
2020-03-15 08:36:59 +00:00
|
|
|
if err == nil && len(contents) > 0 && contents[0] != "" {
|
2016-06-17 14:31:00 +00:00
|
|
|
version = contents[0]
|
|
|
|
}
|
|
|
|
} else if common.PathExists(common.HostEtc("os-release")) {
|
2020-03-15 08:36:59 +00:00
|
|
|
p, v, err := common.GetOSRelease()
|
2016-06-17 14:31:00 +00:00
|
|
|
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"
|
2020-03-15 08:36:59 +00:00
|
|
|
case "suse", "opensuse", "sles":
|
2016-02-12 18:02:16 +00:00
|
|
|
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"
|
2020-03-15 08:36:59 +00:00
|
|
|
case "solus":
|
|
|
|
family = "solus"
|
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) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return KernelVersionWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersionWithContext(ctx context.Context) (version string, err error) {
|
2020-03-15 08:36:59 +00:00
|
|
|
var utsname unix.Utsname
|
|
|
|
err = unix.Uname(&utsname)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-08-30 17:48:07 +00:00
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
return string(utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)]), nil
|
|
|
|
}
|
2016-08-30 17:48:07 +00:00
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
func getSlackwareVersion(contents []string) string {
|
|
|
|
c := strings.ToLower(strings.Join(contents, ""))
|
|
|
|
c = strings.Replace(c, "slackware ", "", 1)
|
|
|
|
return c
|
2016-08-30 17:48:07 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return VirtualizationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
|
2020-03-15 08:36:59 +00:00
|
|
|
return common.VirtualizationWithContext(ctx)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
func SensorsTemperatures() ([]TemperatureStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return SensorsTemperaturesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
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
|
2018-03-06 00:52:43 +00:00
|
|
|
files, err = filepath.Glob(common.HostSys("/class/hwmon/hwmon*/device/temp*_*"))
|
2017-07-28 16:46:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return temperatures, err
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
var warns Warnings
|
2017-07-28 16:46:44 +00:00
|
|
|
|
2020-06-03 20:09:27 +00:00
|
|
|
if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files
|
|
|
|
files, err = filepath.Glob(common.HostSys("/class/thermal/thermal_zone*/"))
|
|
|
|
if err != nil {
|
|
|
|
return temperatures, err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
// Get the name of the temperature you are reading
|
|
|
|
name, err := ioutil.ReadFile(filepath.Join(file, "type"))
|
|
|
|
if err != nil {
|
|
|
|
warns.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Get the temperature reading
|
|
|
|
current, err := ioutil.ReadFile(filepath.Join(file, "temp"))
|
|
|
|
if err != nil {
|
|
|
|
warns.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
temperature, err := strconv.ParseInt(strings.TrimSpace(string(current)), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
warns.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
temperatures = append(temperatures, TemperatureStat{
|
|
|
|
SensorKey: strings.TrimSpace(string(name)),
|
|
|
|
Temperature: float64(temperature) / 1000.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return temperatures, warns.Reference()
|
|
|
|
}
|
|
|
|
|
2018-03-06 00:52:43 +00:00
|
|
|
// example directory
|
|
|
|
// device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm
|
|
|
|
// name temp1_input temp2_input temp3_input temp4_input temp5_input temp6_input temp7_input
|
|
|
|
// power/ temp1_label temp2_label temp3_label temp4_label temp5_label temp6_label temp7_label
|
|
|
|
// subsystem/ temp1_max temp2_max temp3_max temp4_max temp5_max temp6_max temp7_max
|
|
|
|
// temp1_crit temp2_crit temp3_crit temp4_crit temp5_crit temp6_crit temp7_crit uevent
|
|
|
|
for _, file := range files {
|
|
|
|
filename := strings.Split(filepath.Base(file), "_")
|
|
|
|
if filename[1] == "label" {
|
|
|
|
// Do not try to read the temperature of the label file
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the label of the temperature you are reading
|
|
|
|
var label string
|
|
|
|
c, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(file), filename[0]+"_label"))
|
|
|
|
if c != nil {
|
|
|
|
//format the label from "Core 0" to "core0_"
|
|
|
|
label = fmt.Sprintf("%s_", strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(c))), " "), ""))
|
|
|
|
}
|
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
// Get the name of the temperature you are reading
|
2018-03-06 00:52:43 +00:00
|
|
|
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(file), "name"))
|
2017-07-28 16:46:44 +00:00
|
|
|
if err != nil {
|
2020-03-15 08:36:59 +00:00
|
|
|
warns.Add(err)
|
|
|
|
continue
|
2017-07-28 16:46:44 +00:00
|
|
|
}
|
2018-03-06 00:52:43 +00:00
|
|
|
|
|
|
|
// Get the temperature reading
|
|
|
|
current, err := ioutil.ReadFile(file)
|
2017-07-28 16:46:44 +00:00
|
|
|
if err != nil {
|
2020-03-15 08:36:59 +00:00
|
|
|
warns.Add(err)
|
|
|
|
continue
|
2017-07-28 16:46:44 +00:00
|
|
|
}
|
2018-03-06 00:52:43 +00:00
|
|
|
temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64)
|
2017-07-28 16:46:44 +00:00
|
|
|
if err != nil {
|
2020-03-15 08:36:59 +00:00
|
|
|
warns.Add(err)
|
2017-07-28 16:46:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-03-06 00:52:43 +00:00
|
|
|
|
|
|
|
tempName := strings.TrimSpace(strings.ToLower(string(strings.Join(filename[1:], ""))))
|
2017-07-28 16:46:44 +00:00
|
|
|
temperatures = append(temperatures, TemperatureStat{
|
2018-03-06 00:52:43 +00:00
|
|
|
SensorKey: fmt.Sprintf("%s_%s%s", strings.TrimSpace(string(name)), label, tempName),
|
2017-07-28 16:46:44 +00:00
|
|
|
Temperature: temperature / 1000.0,
|
|
|
|
})
|
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
return temperatures, warns.Reference()
|
2017-07-28 16:46:44 +00:00
|
|
|
}
|