/
sysinfo.go
52 lines (43 loc) · 1.14 KB
/
sysinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"io/ioutil"
"exec"
"os"
"strconv"
"strings"
)
type Poller interface {
GetMemory(pid int) float64
}
type ProcPoller struct {}
type PsPoller struct {}
func runWithOutput(binary string, args []string) string {
cwd, _ := os.Getwd()
cmd, _ := exec.Run(binary, args, nil, cwd, exec.PassThrough, exec.Pipe, exec.PassThrough)
cmd.Wait(os.WSTOPPED)
stdout, _ := ioutil.ReadAll(cmd.Stdout)
return strings.Trim(string(stdout), " \n")
}
func ps(keyword string, pid int) string {
binary := "/bin/ps"
args := []string{binary, "-o", keyword + "=", "-p", strconv.Itoa(pid)}
return runWithOutput(binary, args)
}
func psInt(keyword string, pid int) float64 {
kb, _ := strconv.Atof64(ps(keyword, pid))
return kb / 1024
}
func (p ProcPoller) GetMemory(pid int) float64 {
// filename := "/proc/" + strconv.Itoa(pid) + "/stat"
return psInt("rss", pid)
}
func (p PsPoller) GetMemory(pid int) float64 {
return psInt("rss", pid)
}
func GetPoller() Poller {
// _, err := os.Stat("/proc")
// if err == nil {
// return new(ProcPoller)
// }
return new(PsPoller)
}