From 8ff768026dfa04d2e1a2c9c9f4cd8b08642004fa Mon Sep 17 00:00:00 2001 From: "Paul Buetow (europa)" Date: Sun, 24 May 2015 21:10:43 +0100 Subject: add packages --- main.go | 73 ------------------------------------------------------ main/main.go | 47 +++++++++++++++++++++++++++++++++++ process/process.go | 35 ++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 73 deletions(-) delete mode 100644 main.go create mode 100644 main/main.go create mode 100644 process/process.go diff --git a/main.go b/main.go deleted file mode 100644 index 44ef0ab..0000000 --- a/main.go +++ /dev/null @@ -1,73 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "log" - "regexp" -) - -const ( - STOP = 0 - GETPIDS = 1 -) - -type Program struct { - pid int - command string -} - -func gstatGatherPids(res chan<- string) { - re, _ := regexp.Compile("^[0-9]+$") - - dir, err := ioutil.ReadDir("/proc/") - if err != nil { - log.Fatal(err) - } - - for _, fi := range dir { - name := fi.Name() - if re.MatchString(name) { - res <- name - } - } -} - -func gstatGather(action <-chan int, done chan<- bool, res chan<- string) { - for { - switch <-action { - case STOP: - { - done <- true - break - } - case GETPIDS: - { - gstatGatherPids(res) - done <- true - } - } - } -} - -func main() { - action := make(chan int) - done := make(chan bool) - res := make(chan string) - - go gstatGather(action, done, res) - - action <- GETPIDS - for { - switch <-done { - case false: - { - fmt.Println(<-res) - } - case true: - { - break - } - } - } -} diff --git a/main/main.go b/main/main.go new file mode 100644 index 0000000..b8b5d39 --- /dev/null +++ b/main/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "github.com/buetow/gstat/process" +) + +const ( + STOP = 0 + GETPIDS = 1 +) + +func gstatGather(action <-chan int, res chan<- process.Process) { + for { + switch <-action { + case STOP: + { + break + } + case GETPIDS: + { + process.Gather(res) + } + } + close(res) + } +} + +func main() { + action := make(chan int) + res := make(chan process.Process) + + go gstatGather(action, res) + + action <- GETPIDS + for { + process, more := <-res + if more { + fmt.Println(process.Cmdline) + } else { + break + } + } + action <- STOP + + fmt.Println("Good bye") +} diff --git a/process/process.go b/process/process.go new file mode 100644 index 0000000..4420b44 --- /dev/null +++ b/process/process.go @@ -0,0 +1,35 @@ +package process + +import ( + "fmt" + "io/ioutil" + "log" + "regexp" + "strconv" +) + +type Process struct { + Pid int + Cmdline string +} + +func Gather(res chan<- Process) { + re, _ := regexp.Compile("^[0-9]+$") + + dir, err := ioutil.ReadDir("/proc/") + if err != nil { + log.Fatal(err) + } + + for _, direntry := range dir { + name := direntry.Name() + if re.MatchString(name) { + pid, _ := strconv.Atoi(name) + bytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid)) + if err != nil { + log.Fatal(err) + } + res <- Process{Pid: pid, Cmdline: string(bytes)} + } + } +} -- cgit v1.2.3