You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
5 years ago
|
// +build linux darwin
|
||
|
|
||
|
package staros
|
||
|
|
||
|
import (
|
||
|
"os/user"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"syscall"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// StartTime 开机时间
|
||
|
func StartTime() time.Time {
|
||
|
tmp, _ := readAsString("/proc/stat")
|
||
|
data := splitBy(ReplaceByte9(tmp), " ")
|
||
|
btime, _ := strconv.ParseInt(strings.TrimSpace(data["btime"]), 10, 64)
|
||
|
return time.Unix(btime, 0)
|
||
|
}
|
||
|
|
||
|
// IsRoot 当前是否是管理员用户
|
||
|
func IsRoot() bool {
|
||
|
uid, _ := user.Current()
|
||
|
if uid.Uid == "0" {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Memory 系统内存信息
|
||
|
func Memory() MemStatus {
|
||
|
var mem MemStatus
|
||
|
ram := new(syscall.Sysinfo_t)
|
||
|
if err := syscall.Sysinfo(ram); err != nil {
|
||
|
return mem
|
||
|
}
|
||
|
mem.All = ram.Totalram
|
||
|
mem.BuffCache = ram.Bufferram
|
||
|
mem.Free = ram.Freeram
|
||
|
mem.Shared = ram.Sharedram
|
||
|
mem.Available = ram.Freeram + ram.Sharedram + ram.Bufferram
|
||
|
mem.SwapAll = ram.Totalswap
|
||
|
mem.SwapFree = ram.Freeswap
|
||
|
mem.SwapUsed = mem.SwapAll - mem.SwapFree
|
||
|
mem.Used = mem.All - mem.Free
|
||
|
return mem
|
||
|
}
|
||
|
|
||
|
func Whoami() (uid, gid int, uname, gname, home string, err error) {
|
||
|
var me *user.User
|
||
|
var gup *user.Group
|
||
|
me, err = user.Current()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
uid, _ = strconv.Atoi(me.Uid)
|
||
|
gid, _ = strconv.Atoi(me.Uid)
|
||
|
home = me.HomeDir
|
||
|
uname = me.Username
|
||
|
gup, err = user.LookupGroupId(me.Gid)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
gname = gup.Name
|
||
|
return
|
||
|
}
|