From 26da8c8b88fe3049ddfe7c53f00c57e8ca8b95c1 Mon Sep 17 00:00:00 2001 From: ren yuze Date: Fri, 21 Aug 2020 11:17:48 +0800 Subject: [PATCH] fix bug that darwin cannot compile due to syscall_t error --- memory_darwin.go | 8 +++ memory_linux.go | 24 +++++++++ os_linux.go => os_unix.go | 19 -------- process_linux.go => process_unix.go | 76 ++++++++++++++++++++++++----- typed.go | 7 +++ 5 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 memory_darwin.go create mode 100644 memory_linux.go rename os_linux.go => os_unix.go (82%) rename process_linux.go => process_unix.go (64%) diff --git a/memory_darwin.go b/memory_darwin.go new file mode 100644 index 0000000..268d6d8 --- /dev/null +++ b/memory_darwin.go @@ -0,0 +1,8 @@ +//+build darwin + +package staros + +// Memory 系统内存信息 +func Memory() MemStatus { + return MemStatus{} +} diff --git a/memory_linux.go b/memory_linux.go new file mode 100644 index 0000000..4b5789b --- /dev/null +++ b/memory_linux.go @@ -0,0 +1,24 @@ +//+build linux + +package staros + +import "syscall" + +// Memory 系统内存信息 +func Memory() MemStatus { + var mem MemStatus + ram := new(syscall.Sysinfo_t) + if err := syscall.Sysinfo(ram); err != nil { + return mem + } + mem.All = uint64(ram.Totalram) + mem.BuffCache = uint64(ram.Bufferram) + mem.Free = uint64(ram.Freeram) + mem.Shared = uint64(ram.Sharedram) + mem.Available = uint64(ram.Freeram + ram.Sharedram + ram.Bufferram) + mem.SwapAll = uint64(ram.Totalswap) + mem.SwapFree = uint64(ram.Freeswap) + mem.SwapUsed = uint64(mem.SwapAll - mem.SwapFree) + mem.Used = uint64(mem.All - mem.Free) + return mem +} diff --git a/os_linux.go b/os_unix.go similarity index 82% rename from os_linux.go rename to os_unix.go index 028f1ee..7333aff 100644 --- a/os_linux.go +++ b/os_unix.go @@ -29,25 +29,6 @@ func IsRoot() bool { 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 diff --git a/process_linux.go b/process_unix.go similarity index 64% rename from process_linux.go rename to process_unix.go index d54e5c4..8be6dc2 100644 --- a/process_linux.go +++ b/process_unix.go @@ -16,8 +16,18 @@ import ( "time" ) -// FindProcessByName 通过进程名来查询应用信息 -func FindProcessByName(pname string) (datas []Process, err error) { +//FindProcessByName 通过进程名来查询应用信息 +func FindProcessByName(name string) (datas []Process, err error) { + return FindProcess(func(pname, exepath, folderpath string) bool { + if name == pname { + return true + } + return false + }) +} + +// FindProcess 通过进程信息来查询应用信息 +func FindProcess(compare func(string, string, string) bool) (datas []Process, err error) { var name, main string var mainb []byte paths, errs := ioutil.ReadDir("/proc") @@ -31,14 +41,24 @@ func FindProcessByName(pname string) (datas []Process, err error) { if err != nil { return } - if strings.TrimSpace(name) == pname { - var tmp Process + var tmp Process + tmp.LocalPath, err = os.Readlink("/proc/" + v.Name() + "/exe") + if err != nil { + return + } + tmp.Path = tmp.LocalPath + tmp.LocalPath = filepath.Dir(tmp.LocalPath) + tmp.ExecPath, err = os.Readlink("/proc/" + v.Name() + "/cwd") + if err != nil { + return + } + tmp.Name = strings.TrimSpace(name) + if compare(tmp.Name, tmp.LocalPath, tmp.ExecPath) { main, err = readAsString("/proc/" + v.Name() + "/status") if err != nil { return } data := splitBy(main, ":") - tmp.Name = pname tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64) tmp.PPid, _ = strconv.ParseInt(data["PPid"], 10, 64) tmp.TPid, _ = strconv.ParseInt(data["TracerPid"], 10, 64) @@ -48,6 +68,18 @@ func FindProcessByName(pname string) (datas []Process, err error) { tmp.EUID, _ = strconv.Atoi(uids[1]) tmp.RGID, _ = strconv.Atoi(gids[0]) tmp.EGID, _ = strconv.Atoi(gids[1]) + tmp.VmPeak, _ = strconv.ParseInt(splitBySpace(data["VmPeak"])[0], 10, 64) + tmp.VmSize, _ = strconv.ParseInt(splitBySpace(data["VmSize"])[0], 10, 64) + tmp.VmHWM, _ = strconv.ParseInt(splitBySpace(data["VmHWM"])[0], 10, 64) + tmp.VmRSS, _ = strconv.ParseInt(splitBySpace(data["VmRSS"])[0], 10, 64) + tmp.VmLck, _ = strconv.ParseInt(splitBySpace(data["VmLck"])[0], 10, 64) + tmp.VmData, _ = strconv.ParseInt(splitBySpace(data["VmData"])[0], 10, 64) + tmp.VmLck *= 1024 + tmp.VmData *= 1024 + tmp.VmPeak *= 1024 + tmp.VmSize *= 1024 + tmp.VmHWM *= 1024 + tmp.VmRSS *= 1024 mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline") if err != nil { return @@ -56,16 +88,16 @@ func FindProcessByName(pname string) (datas []Process, err error) { for _, v := range args { tmp.Args = append(tmp.Args, string(v)) } - tmp.LocalPath, err = os.Readlink("/proc/" + v.Name() + "/exe") + + mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/environ") if err != nil { return } - tmp.Path = tmp.LocalPath - tmp.LocalPath = filepath.Dir(tmp.LocalPath) - tmp.ExecPath, err = os.Readlink("/proc/" + v.Name() + "/cwd") - if err != nil { - return + args = bytes.Split(mainb, []byte{0}) + for _, v := range args { + tmp.Env = append(tmp.Env, string(v)) } + main, err = readAsString("/proc/" + v.Name() + "/stat") if err != nil { return @@ -107,6 +139,18 @@ func FindProcessByPid(pid int64) (datas Process, err error) { datas.EUID, _ = strconv.Atoi(uids[1]) datas.RGID, _ = strconv.Atoi(gids[0]) datas.EGID, _ = strconv.Atoi(gids[1]) + datas.VmPeak, _ = strconv.ParseInt(splitBySpace(data["VmPeak"])[0], 10, 64) + datas.VmSize, _ = strconv.ParseInt(splitBySpace(data["VmSize"])[0], 10, 64) + datas.VmHWM, _ = strconv.ParseInt(splitBySpace(data["VmHWM"])[0], 10, 64) + datas.VmRSS, _ = strconv.ParseInt(splitBySpace(data["VmRSS"])[0], 10, 64) + datas.VmLck, _ = strconv.ParseInt(splitBySpace(data["VmLck"])[0], 10, 64) + datas.VmData, _ = strconv.ParseInt(splitBySpace(data["VmData"])[0], 10, 64) + datas.VmLck *= 1024 + datas.VmData *= 1024 + datas.VmPeak *= 1024 + datas.VmSize *= 1024 + datas.VmHWM *= 1024 + datas.VmRSS *= 1024 mainb, err = ioutil.ReadFile("/proc/" + fmt.Sprint(pid) + "/cmdline") if err != nil { return @@ -115,6 +159,16 @@ func FindProcessByPid(pid int64) (datas Process, err error) { for _, v := range args { datas.Args = append(datas.Args, string(v)) } + + mainb, err = ioutil.ReadFile("/proc/" + fmt.Sprint(pid) + "/environ") + if err != nil { + return + } + args = bytes.Split(mainb, []byte{0}) + for _, v := range args { + datas.Env = append(datas.Env, string(v)) + } + datas.LocalPath, err = os.Readlink("/proc/" + fmt.Sprint(pid) + "/exe") if err != nil { return diff --git a/typed.go b/typed.go index 950f497..ef5e8f8 100644 --- a/typed.go +++ b/typed.go @@ -33,12 +33,19 @@ type Process struct { LocalPath string Path string Args []string + Env []string RUID int EUID int RGID int EGID int TPid int64 Uptime time.Time + VmPeak int64 + VmSize int64 + VmLck int64 + VmHWM int64 + VmRSS int64 + VmData int64 } type MemStatus struct {