2017-02-01 18:26:00 +00:00
|
|
|
// +build openbsd
|
|
|
|
|
|
|
|
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"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
2020-07-01 12:47:56 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UTNameSize = 32 /* 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) {
|
|
|
|
return "", common.ErrNotImplementedError
|
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 PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
platform := ""
|
|
|
|
family := ""
|
|
|
|
version := ""
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
p, err := unix.Sysctl("kern.ostype")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
2020-07-01 12:47:56 +00:00
|
|
|
platform = strings.ToLower(p)
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
v, err := unix.Sysctl("kern.osrelease")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
2020-07-01 12:47:56 +00:00
|
|
|
version = strings.ToLower(v)
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return platform, family, version, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
var ret []UserStat
|
|
|
|
utmpfile := "/var/run/utmp"
|
|
|
|
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
|
|
|
|
}
|