2014-08-08 23:09:28 +09:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 22:09:05 +09:00
|
|
|
package host
|
2014-08-08 23:09:28 +09:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-12-31 15:25:49 +09:00
|
|
|
"context"
|
2014-08-08 23:09:28 +09:00
|
|
|
"encoding/binary"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2014-11-27 10:18:15 +09:00
|
|
|
|
2015-10-20 00:04:57 +09:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2016-07-10 23:47:29 -05:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2018-12-29 13:38:21 +01:00
|
|
|
"golang.org/x/sys/unix"
|
2014-08-08 23:09:28 +09:00
|
|
|
)
|
|
|
|
|
2016-02-02 15:23:34 +01:00
|
|
|
// from utmpx.h
|
|
|
|
const USER_PROCESS = 7
|
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
func Info() (*InfoStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
|
2016-03-22 23:09:12 +09:00
|
|
|
ret := &InfoStat{
|
2014-08-08 23:09:28 +09:00
|
|
|
OS: runtime.GOOS,
|
|
|
|
PlatformFamily: "darwin",
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
2015-10-13 13:02:02 -07:00
|
|
|
if err == nil {
|
|
|
|
ret.Hostname = hostname
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
2018-09-01 17:23:39 +02:00
|
|
|
kernelVersion, err := KernelVersionWithContext(ctx)
|
2017-04-06 22:17:56 +09:00
|
|
|
if err == nil {
|
2018-09-01 17:23:39 +02:00
|
|
|
ret.KernelVersion = kernelVersion
|
2017-04-06 22:17:56 +09:00
|
|
|
}
|
|
|
|
|
2019-08-07 23:59:43 +02:00
|
|
|
kernelArch, err := kernelArch()
|
|
|
|
if err == nil {
|
|
|
|
ret.KernelArch = kernelArch
|
|
|
|
}
|
|
|
|
|
2017-04-06 22:17:56 +09:00
|
|
|
platform, family, pver, err := PlatformInformation()
|
2014-08-08 23:09:28 +09:00
|
|
|
if err == nil {
|
|
|
|
ret.Platform = platform
|
|
|
|
ret.PlatformFamily = family
|
2016-09-12 08:07:12 +09:00
|
|
|
ret.PlatformVersion = pver
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
2016-07-10 23:47:29 -05:00
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
system, role, err := Virtualization()
|
2014-08-08 23:09:28 +09:00
|
|
|
if err == nil {
|
|
|
|
ret.VirtualizationSystem = system
|
|
|
|
ret.VirtualizationRole = role
|
|
|
|
}
|
|
|
|
|
2015-11-23 13:14:56 -07:00
|
|
|
boot, err := BootTime()
|
2014-08-08 23:09:28 +09:00
|
|
|
if err == nil {
|
2015-11-23 13:14:56 -07:00
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime = uptime(boot)
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
2016-07-10 23:47:29 -05:00
|
|
|
procs, err := process.Pids()
|
|
|
|
if err == nil {
|
|
|
|
ret.Procs = uint64(len(procs))
|
|
|
|
}
|
|
|
|
|
2019-03-02 19:44:18 +01:00
|
|
|
uuid, err := unix.Sysctl("kern.uuid")
|
|
|
|
if err == nil && uuid != "" {
|
|
|
|
ret.HostID = strings.ToLower(uuid)
|
2016-08-11 00:51:07 -07:00
|
|
|
}
|
|
|
|
|
2014-08-08 23:09:28 +09:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Users() ([]UserStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return UsersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
2014-08-08 23:09:28 +09:00
|
|
|
utmpfile := "/var/run/utmpx"
|
|
|
|
var ret []UserStat
|
|
|
|
|
|
|
|
file, err := os.Open(utmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2017-02-22 08:46:23 -05:00
|
|
|
defer file.Close()
|
2014-08-08 23:09:28 +09:00
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2015-02-14 23:04:17 +09:00
|
|
|
u := Utmpx{}
|
2014-08-08 23:09:28 +09:00
|
|
|
entrySize := int(unsafe.Sizeof(u))
|
|
|
|
count := len(buf) / entrySize
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := buf[i*entrySize : i*entrySize+entrySize]
|
|
|
|
|
2015-02-14 23:04:17 +09:00
|
|
|
var u Utmpx
|
2014-08-08 23:09:28 +09:00
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-02 15:23:34 +01:00
|
|
|
if u.Type != USER_PROCESS {
|
2015-02-14 23:04:17 +09:00
|
|
|
continue
|
|
|
|
}
|
2014-08-08 23:09:28 +09:00
|
|
|
user := UserStat{
|
2015-02-14 23:04:17 +09:00
|
|
|
User: common.IntToString(u.User[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
|
|
|
Started: int(u.Tv.Sec),
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-06 22:17:56 +09:00
|
|
|
func PlatformInformation() (string, string, string, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return PlatformInformationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
2014-08-08 23:09:28 +09:00
|
|
|
platform := ""
|
|
|
|
family := ""
|
2016-09-12 08:07:12 +09:00
|
|
|
pver := ""
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2016-09-12 08:07:12 +09:00
|
|
|
sw_vers, err := exec.LookPath("sw_vers")
|
|
|
|
if err != nil {
|
2017-04-06 22:17:56 +09:00
|
|
|
return "", "", "", err
|
2016-09-12 08:07:12 +09:00
|
|
|
}
|
|
|
|
|
2018-12-29 13:38:21 +01:00
|
|
|
p, err := unix.Sysctl("kern.ostype")
|
2014-08-08 23:09:28 +09:00
|
|
|
if err == nil {
|
2018-12-29 13:38:21 +01:00
|
|
|
platform = strings.ToLower(p)
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
2018-12-29 13:38:21 +01:00
|
|
|
out, err := invoke.CommandWithContext(ctx, sw_vers, "-productVersion")
|
2016-09-12 08:07:12 +09:00
|
|
|
if err == nil {
|
|
|
|
pver = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
2019-05-31 13:13:06 -04:00
|
|
|
// check if the macos server version file exists
|
2019-05-31 13:19:04 -04:00
|
|
|
_, err = os.Stat("/System/Library/CoreServices/ServerVersion.plist")
|
2019-05-31 13:13:06 -04:00
|
|
|
|
|
|
|
// server file doesn't exist
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
family = "Standalone Workstation"
|
|
|
|
} else {
|
|
|
|
family = "Server"
|
|
|
|
}
|
|
|
|
|
2017-04-06 22:17:56 +09:00
|
|
|
return platform, family, pver, nil
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
func Virtualization() (string, string, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return VirtualizationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
|
2017-08-03 11:08:35 +09:00
|
|
|
return "", "", common.ErrNotImplementedError
|
|
|
|
}
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2017-08-03 11:08:35 +09:00
|
|
|
func KernelVersion() (string, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return KernelVersionWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
2018-12-29 13:38:21 +01:00
|
|
|
version, err := unix.Sysctl("kern.osrelease")
|
|
|
|
return strings.ToLower(version), err
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|