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"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UTNameSize = 32 /* 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: "openbsd",
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
system, role, err := Virtualization()
|
|
|
|
if err == nil {
|
|
|
|
ret.VirtualizationSystem = system
|
|
|
|
ret.VirtualizationRole = role
|
|
|
|
}
|
|
|
|
|
|
|
|
procs, err := process.Pids()
|
|
|
|
if err == nil {
|
|
|
|
ret.Procs = uint64(len(procs))
|
|
|
|
}
|
|
|
|
|
|
|
|
boot, err := BootTime()
|
|
|
|
if err == nil {
|
|
|
|
ret.BootTime = boot
|
|
|
|
ret.Uptime = uptime(boot)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BootTime() (uint64, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return BootTimeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BootTimeWithContext(ctx context.Context) (uint64, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
val, err := common.DoSysctrl("kern.boottime")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
boottime, err := strconv.ParseUint(val[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return boottime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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) {
|
2017-02-01 18:26:00 +00:00
|
|
|
platform := ""
|
|
|
|
family := ""
|
|
|
|
version := ""
|
|
|
|
uname, err := exec.LookPath("uname")
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
out, err := invoke.CommandWithContext(ctx, uname, "-s")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
|
|
|
platform = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
out, err = invoke.CommandWithContext(ctx, uname, "-r")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
|
|
|
version = strings.ToLower(strings.TrimSpace(string(out)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return platform, family, version, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
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 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
|
|
|
|
}
|