2016-02-12 18:02:16 +00:00
|
|
|
// +build freebsd
|
|
|
|
|
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-07-28 16:46:44 +00:00
|
|
|
"sync/atomic"
|
2016-02-12 18:02:16 +00:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2016-08-30 17:48:07 +00:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
|
|
|
|
UTLineSize = 8
|
|
|
|
UTHostSize = 16
|
|
|
|
)
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func Info() (*InfoStat, error) {
|
|
|
|
ret := &InfoStat{
|
2016-02-12 18:02:16 +00:00
|
|
|
OS: runtime.GOOS,
|
|
|
|
PlatformFamily: "freebsd",
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
ret.KernelVersion = version
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-08-30 17:48:07 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
boot, err := BootTime()
|
|
|
|
if err == nil {
|
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime = uptime(boot)
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:48:07 +00:00
|
|
|
procs, err := process.Pids()
|
|
|
|
if err == nil {
|
|
|
|
ret.Procs = uint64(len(procs))
|
|
|
|
}
|
|
|
|
|
|
|
|
values, err := common.DoSysctrl("kern.hostuuid")
|
|
|
|
if err == nil && len(values) == 1 && values[0] != "" {
|
2017-02-07 00:16:20 +00:00
|
|
|
ret.HostID = strings.ToLower(values[0])
|
2016-08-30 17:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
// cachedBootTime must be accessed via atomic.Load/StoreUint64
|
|
|
|
var cachedBootTime uint64
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
func BootTime() (uint64, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
t := atomic.LoadUint64(&cachedBootTime)
|
|
|
|
if t != 0 {
|
|
|
|
return t, nil
|
2017-02-02 06:41:29 +00:00
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
values, err := common.DoSysctrl("kern.boottime")
|
|
|
|
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.ParseUint(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
t = uint64(boottime)
|
|
|
|
atomic.StoreUint64(&cachedBootTime, t)
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
return t, nil
|
2016-02-12 18:02:16 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func Users() ([]UserStat, error) {
|
|
|
|
utmpfile := "/var/run/utx.active"
|
|
|
|
if !common.PathExists(utmpfile) {
|
|
|
|
utmpfile = "/var/run/utmp" // before 9.0
|
|
|
|
return getUsersFromUtmp(utmpfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []UserStat
|
|
|
|
file, err := os.Open(utmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return ret, 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 ret, err
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
entrySize := sizeOfUtmpx
|
2016-02-12 18:02:16 +00:00
|
|
|
count := len(buf) / entrySize
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2016-05-09 15:19:04 +00:00
|
|
|
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
|
2016-02-12 18:02:16 +00:00
|
|
|
var u Utmpx
|
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil || u.Type != 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sec := (binary.LittleEndian.Uint32(u.Tv.Sec[:])) / 2 // TODO:
|
|
|
|
user := UserStat{
|
|
|
|
User: common.IntToString(u.User[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
|
|
|
Started: int(sec),
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func PlatformInformation() (string, string, string, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
platform := ""
|
|
|
|
family := ""
|
|
|
|
version := ""
|
2016-05-09 15:19:04 +00:00
|
|
|
uname, err := exec.LookPath("uname")
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2016-06-17 14:31:00 +00:00
|
|
|
out, err := invoke.Command(uname, "-s")
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
platform = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:31:00 +00:00
|
|
|
out, err = invoke.Command(uname, "-r")
|
2016-02-12 18:02:16 +00:00
|
|
|
if err == nil {
|
|
|
|
version = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return platform, family, version, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func Virtualization() (string, string, error) {
|
2017-08-16 22:05:24 +00:00
|
|
|
return "", "", common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// before 9.0
|
|
|
|
func getUsersFromUtmp(utmpfile string) ([]UserStat, error) {
|
|
|
|
var ret []UserStat
|
|
|
|
file, err := os.Open(utmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return ret, 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 ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := Utmp{}
|
|
|
|
entrySize := int(unsafe.Sizeof(u))
|
|
|
|
count := len(buf) / entrySize
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := buf[i*entrySize : i*entrySize+entrySize]
|
|
|
|
var u Utmp
|
|
|
|
br := bytes.NewReader(b)
|
|
|
|
err := binary.Read(br, binary.LittleEndian, &u)
|
|
|
|
if err != nil || u.Time == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user := UserStat{
|
|
|
|
User: common.IntToString(u.Name[:]),
|
|
|
|
Terminal: common.IntToString(u.Line[:]),
|
|
|
|
Host: common.IntToString(u.Host[:]),
|
|
|
|
Started: int(u.Time),
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
func SensorsTemperatures() ([]TemperatureStat, error) {
|
|
|
|
return []TemperatureStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
2017-08-16 22:05:24 +00:00
|
|
|
|
|
|
|
func KernelVersion() (string, error) {
|
|
|
|
_, _, version, err := PlatformInformation()
|
|
|
|
return version, err
|
|
|
|
}
|