2018-10-19 18:33:23 +00:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
)
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
func HostIDWithContext(ctx context.Context) (string, error) {
|
|
|
|
platform, err := parseReleaseFile()
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
return "", err
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
if platform == "SmartOS" {
|
2018-10-19 18:33:23 +00:00
|
|
|
// If everything works, use the current zone ID as the HostID if present.
|
2020-07-01 12:47:56 +00:00
|
|
|
zonename, err := exec.LookPath("zonename")
|
2018-10-19 18:33:23 +00:00
|
|
|
if err == nil {
|
|
|
|
out, err := invoke.CommandWithContext(ctx, zonename)
|
|
|
|
if err == nil {
|
|
|
|
sc := bufio.NewScanner(bytes.NewReader(out))
|
|
|
|
for sc.Scan() {
|
|
|
|
line := sc.Text()
|
|
|
|
|
|
|
|
// If we're in the global zone, rely on the hostname.
|
|
|
|
if line == "global" {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err == nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
return hostname, nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-07 16:57:18 +00:00
|
|
|
return strings.TrimSpace(line), nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
// If HostID is still unknown, use hostid(1), which can lie to callers but at
|
2018-10-19 18:33:23 +00:00
|
|
|
// this point there are no hardware facilities available. This behavior
|
|
|
|
// matches that of other supported OSes.
|
2020-10-07 16:57:18 +00:00
|
|
|
hostID, err := exec.LookPath("hostid")
|
|
|
|
if err == nil {
|
|
|
|
out, err := invoke.CommandWithContext(ctx, hostID)
|
2018-10-19 18:33:23 +00:00
|
|
|
if err == nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
sc := bufio.NewScanner(bytes.NewReader(out))
|
|
|
|
for sc.Scan() {
|
|
|
|
line := sc.Text()
|
|
|
|
return strings.TrimSpace(line), nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
// Count number of processes based on the number of entries in /proc
|
|
|
|
func numProcs(ctx context.Context) (uint64, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
dirs, err := ioutil.ReadDir("/proc")
|
|
|
|
if err != nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
return 0, err
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
return uint64(len(dirs)), nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`)
|
|
|
|
|
|
|
|
func BootTimeWithContext(ctx context.Context) (uint64, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
kstat, err := exec.LookPath("kstat")
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "unix:0:system_misc:boot_time")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kstats := kstatMatch.FindAllStringSubmatch(string(out), -1)
|
|
|
|
if len(kstats) != 1 {
|
|
|
|
return 0, fmt.Errorf("expected 1 kstat, found %d", len(kstats))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.ParseUint(kstats[0][2], 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UptimeWithContext(ctx context.Context) (uint64, error) {
|
|
|
|
bootTime, err := BootTime()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
return timeSince(bootTime), nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
|
|
|
return []UserStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
|
|
|
|
return []TemperatureStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
|
|
|
|
return "", "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
// Find distribution name from /etc/release
|
|
|
|
func parseReleaseFile() (string, error) {
|
|
|
|
b, err := ioutil.ReadFile("/etc/release")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
var platform string
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(s, "SmartOS"):
|
|
|
|
platform = "SmartOS"
|
|
|
|
case strings.HasPrefix(s, "OpenIndiana"):
|
|
|
|
platform = "OpenIndiana"
|
|
|
|
case strings.HasPrefix(s, "OmniOS"):
|
|
|
|
platform = "OmniOS"
|
|
|
|
case strings.HasPrefix(s, "Open Storage"):
|
|
|
|
platform = "NexentaStor"
|
|
|
|
case strings.HasPrefix(s, "Solaris"):
|
|
|
|
platform = "Solaris"
|
|
|
|
case strings.HasPrefix(s, "Oracle Solaris"):
|
|
|
|
platform = "Solaris"
|
|
|
|
default:
|
|
|
|
platform = strings.Fields(s)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return platform, nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
// parseUnameOutput returns platformFamily, kernelVersion and platformVersion
|
|
|
|
func parseUnameOutput(ctx context.Context) (string, string, string, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
uname, err := exec.LookPath("uname")
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
return "", "", "", err
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err := invoke.CommandWithContext(ctx, uname, "-srv")
|
|
|
|
if err != nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
return "", "", "", err
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(string(out))
|
2020-10-07 16:57:18 +00:00
|
|
|
if len(fields) < 3 {
|
|
|
|
return "", "", "", fmt.Errorf("malformed `uname` output")
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
|
|
|
|
return fields[0], fields[1], fields[2], nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
|
|
|
_, kernelVersion, _, err := parseUnameOutput(ctx)
|
|
|
|
return kernelVersion, err
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
|
|
|
platform, err := parseReleaseFile()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
platformFamily, _, platformVersion, err := parseUnameOutput(ctx)
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
return platform, platformFamily, platformVersion, nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|