2017-02-01 18:26:00 +00:00
|
|
|
// +build freebsd
|
|
|
|
|
|
|
|
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"
|
2020-07-01 12:47:56 +00:00
|
|
|
"math"
|
2017-02-01 18:26:00 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2018-10-19 18:33:23 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2017-02-01 18:26:00 +00:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
|
|
|
|
UTLineSize = 8
|
|
|
|
UTHostSize = 16
|
|
|
|
)
|
|
|
|
|
|
|
|
func Info() (*InfoStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) (*InfoStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
ret := &InfoStat{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
PlatformFamily: "freebsd",
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
ret.Hostname = hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
platform, family, version, err := PlatformInformation()
|
|
|
|
if err == nil {
|
|
|
|
ret.Platform = platform
|
|
|
|
ret.PlatformFamily = family
|
|
|
|
ret.PlatformVersion = version
|
|
|
|
ret.KernelVersion = version
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
kernelArch, err := kernelArch()
|
|
|
|
if err == nil {
|
|
|
|
ret.KernelArch = kernelArch
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
system, role, err := Virtualization()
|
|
|
|
if err == nil {
|
|
|
|
ret.VirtualizationSystem = system
|
|
|
|
ret.VirtualizationRole = role
|
|
|
|
}
|
|
|
|
|
|
|
|
boot, err := BootTime()
|
|
|
|
if err == nil {
|
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime = uptime(boot)
|
|
|
|
}
|
|
|
|
|
|
|
|
procs, err := process.Pids()
|
|
|
|
if err == nil {
|
|
|
|
ret.Procs = uint64(len(procs))
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
hostid, err := unix.Sysctl("kern.hostuuid")
|
|
|
|
if err == nil && hostid != "" {
|
|
|
|
ret.HostID = strings.ToLower(hostid)
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
// cachedBootTime must be accessed via atomic.Load/StoreUint64
|
|
|
|
var cachedBootTime uint64
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
func BootTime() (uint64, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return BootTimeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BootTimeWithContext(ctx context.Context) (uint64, error) {
|
|
|
|
t := atomic.LoadUint64(&cachedBootTime)
|
|
|
|
if t != 0 {
|
|
|
|
return t, nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
buf, err := unix.SysctlRaw("kern.boottime")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
tv := *(*syscall.Timeval)(unsafe.Pointer((&buf[0])))
|
|
|
|
atomic.StoreUint64(&cachedBootTime, uint64(tv.Sec))
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
return t, nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func uptime(boot uint64) uint64 {
|
|
|
|
return uint64(time.Now().Unix()) - boot
|
|
|
|
}
|
|
|
|
|
|
|
|
func Uptime() (uint64, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return UptimeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func UptimeWithContext(ctx context.Context) (uint64, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
boot, err := BootTime()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uptime(boot), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Users() ([]UserStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return UsersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
entrySize := sizeOfUtmpx
|
|
|
|
count := len(buf) / entrySize
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
|
|
|
|
var u Utmpx
|
|
|
|
br := bytes.NewReader(b)
|
2020-07-01 12:47:56 +00:00
|
|
|
err := binary.Read(br, binary.BigEndian, &u)
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil || u.Type != 4 {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
sec := math.Floor(float64(u.Tv) / 1000000)
|
2017-02-01 18:26:00 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlatformInformation() (string, string, string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return PlatformInformationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
|
|
|
platform, err := unix.Sysctl("kern.ostype")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
version, err := unix.Sysctl("kern.osrelease")
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
return strings.ToLower(platform), "", strings.ToLower(version), nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Virtualization() (string, string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return VirtualizationWithContext(context.Background())
|
|
|
|
}
|
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
|
2017-02-01 18:26:00 +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
|
|
|
|
}
|
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 := 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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
func SensorsTemperatures() ([]TemperatureStat, error) {
|
|
|
|
return SensorsTemperaturesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
|
|
|
|
return []TemperatureStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersion() (string, error) {
|
|
|
|
return KernelVersionWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
|
|
|
_, _, version, err := PlatformInformation()
|
|
|
|
return version, err
|
|
|
|
}
|