shirou_gopsutil/host/host_freebsd.go

144 lines
2.7 KiB
Go
Raw Normal View History

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"
"encoding/binary"
"io/ioutil"
2014-04-22 11:04:16 +08:00
"os"
2014-05-18 22:43:24 +08:00
"os/exec"
"runtime"
2014-04-22 11:04:16 +08:00
"strconv"
2014-04-22 11:43:31 +08:00
"strings"
2014-04-22 16:45:39 +08:00
"unsafe"
common "github.com/shirou/gopsutil/common"
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
)
2014-05-12 11:03:20 +08:00
func HostInfo() (*HostInfoStat, error) {
2014-05-18 22:43:24 +08:00
ret := &HostInfoStat{
OS: runtime.GOOS,
PlatformFamily: "freebsd",
}
2014-04-22 11:04:16 +08:00
hostname, err := os.Hostname()
if err != nil {
return ret, err
}
2014-05-18 22:43:24 +08:00
ret.Hostname = hostname
platform, family, version, err := GetPlatformInformation()
2014-05-18 22:43:24 +08:00
if err == nil {
ret.Platform = platform
ret.PlatformFamily = family
ret.PlatformVersion = version
2014-05-18 22:43:24 +08:00
}
system, role, err := GetVirtualization()
2014-05-18 22:43:24 +08:00
if err == nil {
ret.VirtualizationSystem = system
ret.VirtualizationRole = role
2014-05-18 22:43:24 +08:00
}
values, err := common.DoSysctrl("kern.boottime")
2014-05-18 22:43:24 +08:00
if err == nil {
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
v := strings.Replace(values[2], ",", "", 1)
2014-09-19 23:15:35 +08:00
t, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return ret, err
}
ret.Uptime = t
2014-05-18 22:43:24 +08:00
}
2014-04-22 11:04:16 +08:00
return ret, nil
}
2014-04-30 14:32:05 +08:00
func BootTime() (int64, error) {
values, err := common.DoSysctrl("kern.boottime")
2014-04-22 11:04:16 +08:00
if err != nil {
return 0, err
}
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
v := strings.Replace(values[2], ",", "", 1)
boottime, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return 0, err
}
return boottime, nil
}
2014-04-22 16:45:39 +08:00
func Users() ([]UserStat, error) {
utmpfile := "/var/run/utmp"
2014-04-30 15:16:07 +08:00
var ret []UserStat
2014-04-22 16:45:39 +08:00
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
}
buf, err := ioutil.ReadAll(file)
if err != nil {
return ret, err
}
2014-12-30 23:30:55 +08:00
u := Utmp{}
2014-04-22 16:45:39 +08:00
entrySize := int(unsafe.Sizeof(u))
count := len(buf) / entrySize
for i := 0; i < count; i++ {
b := buf[i*entrySize : i*entrySize+entrySize]
2014-12-30 23:30:55 +08:00
var u Utmp
2014-04-22 16:45:39 +08:00
br := bytes.NewReader(b)
err := binary.Read(br, binary.LittleEndian, &u)
2014-12-30 23:30:55 +08:00
if err != nil || u.Time == 0 {
2014-04-22 16:45:39 +08:00
continue
}
user := UserStat{
2014-12-30 23:30:55 +08:00
User: common.IntToString(u.Name[:]),
Terminal: common.IntToString(u.Line[:]),
Host: common.IntToString(u.Host[:]),
Started: int(u.Time),
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
}
func GetPlatformInformation() (string, string, string, error) {
platform := ""
family := ""
version := ""
out, err := exec.Command("uname", "-s").Output()
if err == nil {
platform = strings.ToLower(strings.TrimSpace(string(out)))
}
out, err = exec.Command("uname", "-r").Output()
if err == nil {
version = strings.ToLower(strings.TrimSpace(string(out)))
}
return platform, family, version, nil
}
func GetVirtualization() (string, string, error) {
system := ""
role := ""
return system, role, nil
}