2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package host
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-11-24 04:14:56 +08:00
|
|
|
"time"
|
2014-08-08 22:09:28 +08:00
|
|
|
"unsafe"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func HostInfo() (*HostInfoStat, error) {
|
|
|
|
ret := &HostInfoStat{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
PlatformFamily: "darwin",
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
2015-10-14 04:02:02 +08:00
|
|
|
if err == nil {
|
|
|
|
ret.Hostname = hostname
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
platform, family, version, err := GetPlatformInformation()
|
|
|
|
if err == nil {
|
|
|
|
ret.Platform = platform
|
|
|
|
ret.PlatformFamily = family
|
|
|
|
ret.PlatformVersion = version
|
|
|
|
}
|
|
|
|
system, role, err := GetVirtualization()
|
|
|
|
if err == nil {
|
|
|
|
ret.VirtualizationSystem = system
|
|
|
|
ret.VirtualizationRole = role
|
|
|
|
}
|
|
|
|
|
2015-11-24 04:14:56 +08:00
|
|
|
boot, err := BootTime()
|
2014-08-08 22:09:28 +08:00
|
|
|
if err == nil {
|
2015-11-24 04:14:56 +08:00
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime = uptime(boot)
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2015-03-12 08:24:27 +08:00
|
|
|
func BootTime() (uint64, error) {
|
2014-11-27 09:18:15 +08:00
|
|
|
values, err := common.DoSysctrl("kern.boottime")
|
2014-08-08 22:09:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
|
|
|
|
v := strings.Replace(values[2], ",", "", 1)
|
|
|
|
boottime, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2015-03-12 08:25:32 +08:00
|
|
|
return uint64(boottime), nil
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-24 04:14:56 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:09:28 +08:00
|
|
|
func Users() ([]UserStat, error) {
|
|
|
|
utmpfile := "/var/run/utmpx"
|
|
|
|
var ret []UserStat
|
|
|
|
|
|
|
|
file, err := os.Open(utmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2015-02-14 22:04:17 +08:00
|
|
|
u := Utmpx{}
|
2014-08-08 22:09:28 +08: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 22:04:17 +08:00
|
|
|
var u Utmpx
|
2014-08-08 22:09:28 +08:00
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-02-14 22:08:05 +08:00
|
|
|
if u.Type != 7 { // skip if not USERPROCESS
|
2015-02-14 22:04:17 +08:00
|
|
|
continue
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
user := UserStat{
|
2015-02-14 22:04:17 +08:00
|
|
|
User: common.IntToString(u.User[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
|
|
|
Started: int(u.Tv.Sec),
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPlatformInformation() (string, string, string, error) {
|
|
|
|
platform := ""
|
|
|
|
family := ""
|
|
|
|
version := ""
|
|
|
|
|
|
|
|
out, err := exec.Command("uname", "-s").Output()
|
|
|
|
if err == nil {
|
|
|
|
platform = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err = exec.Command("uname", "-r").Output()
|
|
|
|
if err == nil {
|
|
|
|
version = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return platform, family, version, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetVirtualization() (string, string, error) {
|
|
|
|
system := ""
|
|
|
|
role := ""
|
|
|
|
|
|
|
|
return system, role, nil
|
|
|
|
}
|