shirou_gopsutil/host/host_freebsd.go

152 lines
3.1 KiB
Go
Raw Normal View History

2021-12-23 05:54:41 +08:00
//go:build freebsd
2014-04-22 11:04:16 +08:00
// +build freebsd
2014-12-30 21:09:05 +08:00
package host
2014-04-22 11:04:16 +08:00
import (
2014-04-22 16:45:39 +08:00
"bytes"
2017-12-31 14:25:49 +08:00
"context"
2014-04-22 16:45:39 +08:00
"encoding/binary"
"io/ioutil"
"math"
2014-04-22 11:04:16 +08:00
"os"
2014-04-22 11:43:31 +08:00
"strings"
2014-04-22 16:45:39 +08:00
"unsafe"
"github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/process"
"golang.org/x/sys/unix"
2014-04-22 11:04:16 +08:00
)
2014-12-30 23:30:55 +08:00
const (
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
UTLineSize = 8
UTHostSize = 16
)
2020-09-11 19:51:20 +08:00
func HostIDWithContext(ctx context.Context) (string, error) {
uuid, err := unix.Sysctl("kern.hostuuid")
if err != nil {
return "", err
}
2020-09-11 19:51:20 +08:00
return strings.ToLower(uuid), err
2014-04-22 11:04:16 +08:00
}
2020-09-11 19:51:20 +08:00
func numProcs(ctx context.Context) (uint64, error) {
procs, err := process.PidsWithContext(ctx)
if err != nil {
return 0, err
}
return uint64(len(procs)), nil
2017-12-31 14:25:49 +08:00
}
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
utmpfile := "/var/run/utx.active"
if !common.PathExists(utmpfile) {
utmpfile = "/var/run/utmp" // before 9.0
return getUsersFromUtmp(utmpfile)
}
2014-04-22 16:45:39 +08:00
var ret []UserStat
2014-04-22 16:45:39 +08:00
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
}
2017-02-22 21:46:23 +08:00
defer file.Close()
2014-04-22 16:45:39 +08:00
buf, err := ioutil.ReadAll(file)
if err != nil {
return ret, err
}
2016-04-23 22:10:23 +08:00
entrySize := sizeOfUtmpx
2014-04-22 16:45:39 +08:00
count := len(buf) / entrySize
for i := 0; i < count; i++ {
2016-04-23 22:10:23 +08:00
b := buf[i*sizeOfUtmpx : (i+1)*sizeOfUtmpx]
var u Utmpx
2014-04-22 16:45:39 +08:00
br := bytes.NewReader(b)
err := binary.Read(br, binary.BigEndian, &u)
if err != nil || u.Type != 4 {
2014-04-22 16:45:39 +08:00
continue
}
sec := math.Floor(float64(u.Tv) / 1000000)
2014-04-22 16:45:39 +08:00
user := UserStat{
User: common.IntToString(u.User[:]),
2014-12-30 23:30:55 +08:00
Terminal: common.IntToString(u.Line[:]),
Host: common.IntToString(u.Host[:]),
Started: int(sec),
2014-04-22 16:45:39 +08:00
}
2014-12-30 23:30:55 +08:00
2014-04-22 16:45:39 +08:00
ret = append(ret, user)
}
return ret, nil
}
2017-12-31 14:25:49 +08:00
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform, err := unix.Sysctl("kern.ostype")
if err != nil {
return "", "", "", err
}
version, err := unix.Sysctl("kern.osrelease")
if err != nil {
return "", "", "", err
}
return strings.ToLower(platform), "", strings.ToLower(version), nil
}
2017-12-31 14:25:49 +08:00
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
2017-08-03 10:08:35 +08:00
return "", "", common.ErrNotImplementedError
}
// before 9.0
func getUsersFromUtmp(utmpfile string) ([]UserStat, error) {
var ret []UserStat
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
}
2017-02-22 21:46:23 +08:00
defer file.Close()
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-03-19 09:05:46 +08:00
2017-12-31 14:25:49 +08:00
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
2017-04-10 21:24:36 +08:00
return []TemperatureStat{}, common.ErrNotImplementedError
2017-03-19 09:05:46 +08:00
}
2017-08-03 10:08:35 +08:00
2017-12-31 14:25:49 +08:00
func KernelVersionWithContext(ctx context.Context) (string, error) {
2020-09-11 19:51:20 +08:00
_, _, version, err := PlatformInformationWithContext(ctx)
2017-08-03 10:08:35 +08:00
return version, err
}