shirou_gopsutil/host/host_openbsd.go

106 lines
2.2 KiB
Go
Raw Normal View History

2021-12-23 05:54:41 +08:00
//go:build openbsd
// +build openbsd
package host
import (
"bytes"
2017-12-31 14:25:49 +08:00
"context"
"encoding/binary"
"io/ioutil"
"os"
"strings"
"unsafe"
"github.com/shirou/gopsutil/v3/internal/common"
"github.com/shirou/gopsutil/v3/process"
"golang.org/x/sys/unix"
)
const (
UTNameSize = 32 /* 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) {
return "", common.ErrNotImplementedError
2017-12-31 14:25:49 +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
}
2020-09-11 19:51:20 +08:00
return uint64(len(procs)), nil
2017-12-31 14:25:49 +08:00
}
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform := ""
family := ""
version := ""
p, err := unix.Sysctl("kern.ostype")
if err == nil {
platform = strings.ToLower(p)
}
v, err := unix.Sysctl("kern.osrelease")
if err == nil {
version = strings.ToLower(v)
}
return platform, family, 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
}
2017-12-31 14:25:49 +08:00
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
var ret []UserStat
utmpfile := "/var/run/utmp"
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 || u.Name[0] == 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
}