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"
|
|
|
|
"strings"
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
func HostIDWithContext(ctx context.Context) (string, error) {
|
|
|
|
uuid, err := unix.Sysctl("kern.hostuuid")
|
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
|
2018-10-19 18:33:23 +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/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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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 SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
|
|
|
|
return []TemperatureStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
2020-10-07 16:57:18 +00:00
|
|
|
_, _, version, err := PlatformInformationWithContext(ctx)
|
2018-10-19 18:33:23 +00:00
|
|
|
return version, err
|
|
|
|
}
|