74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
// +build linux,amd64
|
|
|
|
package gopsutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"io/ioutil"
|
|
"os"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
func HostInfo() (*HostInfoStat, error) {
|
|
hostname, err := os.Hostname()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := &HostInfoStat{
|
|
Hostname: hostname,
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func BootTime() (int64, error) {
|
|
sysinfo := &syscall.Sysinfo_t{}
|
|
if err := syscall.Sysinfo(sysinfo); err != nil {
|
|
return 0, err
|
|
}
|
|
return sysinfo.Uptime, nil
|
|
}
|
|
|
|
func Users() ([]UserStat, error) {
|
|
utmpfile := "/var/run/utmp"
|
|
|
|
file, err := os.Open(utmpfile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
u := utmp{}
|
|
entrySize := int(unsafe.Sizeof(u))
|
|
count := len(buf) / entrySize
|
|
|
|
ret := make([]UserStat, 0, count)
|
|
|
|
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 {
|
|
continue
|
|
}
|
|
user := UserStat{
|
|
User: byteToString(u.UtUser[:]),
|
|
Terminal: byteToString(u.UtLine[:]),
|
|
Host: byteToString(u.UtHost[:]),
|
|
Started: int(u.UtTv.TvSec),
|
|
}
|
|
ret = append(ret, user)
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|