2017-02-01 18:26:00 +00:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2017-02-01 18:26:00 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
2020-07-01 12:47:56 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// from utmpx.h
|
2020-11-07 01:48:38 +00:00
|
|
|
const user_PROCESS = 7
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
func HostIDWithContext(ctx context.Context) (string, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
uuid, err := unix.Sysctl("kern.uuid")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:57:18 +00:00
|
|
|
return "", err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
return strings.ToLower(uuid), err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
func numProcs(ctx context.Context) (uint64, error) {
|
|
|
|
procs, err := process.PidsWithContext(ctx)
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
return uint64(len(procs)), nil
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
utmpfile := "/var/run/utmpx"
|
|
|
|
var ret []UserStat
|
|
|
|
|
|
|
|
file, err := os.Open(utmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
defer file.Close()
|
2017-02-01 18:26:00 +00:00
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := Utmpx{}
|
|
|
|
entrySize := int(unsafe.Sizeof(u))
|
|
|
|
count := len(buf) / entrySize
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := buf[i*entrySize : i*entrySize+entrySize]
|
|
|
|
|
|
|
|
var u Utmpx
|
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-07 01:48:38 +00:00
|
|
|
if u.Type != user_PROCESS {
|
2017-02-01 18:26:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
user := UserStat{
|
|
|
|
User: common.IntToString(u.User[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
|
|
|
Started: int(u.Tv.Sec),
|
|
|
|
}
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
platform := ""
|
|
|
|
family := ""
|
|
|
|
pver := ""
|
|
|
|
|
|
|
|
sw_vers, err := exec.LookPath("sw_vers")
|
|
|
|
if err != nil {
|
2018-10-19 18:33:23 +00:00
|
|
|
return "", "", "", err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
p, err := unix.Sysctl("kern.ostype")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
2020-07-01 12:47:56 +00:00
|
|
|
platform = strings.ToLower(p)
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
out, err := invoke.CommandWithContext(ctx, sw_vers, "-productVersion")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
|
|
|
pver = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
// check if the macos server version file exists
|
|
|
|
_, err = os.Stat("/System/Library/CoreServices/ServerVersion.plist")
|
|
|
|
|
|
|
|
// server file doesn't exist
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
family = "Standalone Workstation"
|
|
|
|
} else {
|
|
|
|
family = "Server"
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
return platform, family, pver, nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
|
|
|
|
return "", "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
version, err := unix.Sysctl("kern.osrelease")
|
|
|
|
return strings.ToLower(version), err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|