1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-06 19:29:13 +08:00

Merge branch 'master' of github.com:shirou/gopsutil

This commit is contained in:
Shirou WAKAYAMA 2014-04-22 17:46:14 +09:00
commit ff18ba92f8
5 changed files with 69 additions and 20 deletions

View File

@ -10,7 +10,6 @@ import (
"bufio" "bufio"
"os" "os"
"strings" "strings"
) )
// Read contents from file and split by new line. // Read contents from file and split by new line.
@ -33,7 +32,6 @@ func ReadLines(filename string) ([]string, error) {
return ret, err return ret, err
} }
func byteToString(orig []byte) string { func byteToString(orig []byte) string {
n := -1 n := -1
for i, b := range orig { for i, b := range orig {

View File

@ -2,13 +2,8 @@
package gopsutil package gopsutil
import (
"fmt"
)
func Cpu_times() ([]CPU_TimesStat, error) { func Cpu_times() ([]CPU_TimesStat, error) {
ret := make([]CPU_TimesStat, 0) ret := make([]CPU_TimesStat, 0)
fmt.Println("FreeBSD")
return ret, nil return ret, nil
} }

View File

@ -3,11 +3,28 @@
package gopsutil package gopsutil
import ( import (
"bytes"
"encoding/binary"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"unsafe"
) )
const (
UT_NAMESIZE = 16 /* see MAXLOGNAME in <sys/param.h> */
UT_LINESIZE = 8
UT_HOSTSIZE = 16
)
type utmp struct {
Ut_line [UT_LINESIZE]byte
Ut_name [UT_NAMESIZE]byte
Ut_host [UT_HOSTSIZE]byte
Ut_time int32
}
func HostInfo() (HostInfoStat, error) { func HostInfo() (HostInfoStat, error) {
ret := HostInfoStat{} ret := HostInfoStat{}
@ -35,3 +52,43 @@ func Boot_time() (int64, error) {
return boottime, nil return boottime, nil
} }
func Users() ([]UserStat, error) {
utmpfile := "/var/run/utmp"
ret := make([]UserStat, 0)
file, err := os.Open(utmpfile)
if err != nil {
return ret, err
}
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 {
continue
}
user := UserStat{
User: byteToString(u.Ut_name[:]),
Terminal: byteToString(u.Ut_line[:]),
Host: byteToString(u.Ut_host[:]),
Started: int(u.Ut_time),
}
ret = append(ret, user)
}
return ret, nil
}

View File

@ -3,8 +3,8 @@
package gopsutil package gopsutil
import ( import (
"encoding/binary"
"bytes" "bytes"
"encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
"syscall" "syscall"

View File

@ -24,7 +24,6 @@ func TestBoot_time(t *testing.T) {
} }
} }
func TestUsers(t *testing.T) { func TestUsers(t *testing.T) {
v, err := Users() v, err := Users()
if err != nil { if err != nil {