diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-25 00:15:30 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-25 00:15:30 +0100 |
| commit | 9b865d25ac5dde4045e3d3b6c4a333dfb51af7fc (patch) | |
| tree | b5404ee373c11f7568787f175ecbf2c3bc36d9bf | |
| parent | f6473fc7afd385b652e1ccdb745c43292f14a852 (diff) | |
do some clever stuff
| -rw-r--r-- | main/main.go | 2 | ||||
| -rw-r--r-- | process/process.go | 39 |
2 files changed, 29 insertions, 12 deletions
diff --git a/main/main.go b/main/main.go index e408e4b..6cef683 100644 --- a/main/main.go +++ b/main/main.go @@ -24,7 +24,7 @@ func gather(timer <-chan bool, processes chan<- process.Process) { func receive(processes <-chan process.Process) { for process := range processes { - fmt.Println(process) + process.Print() } } diff --git a/process/process.go b/process/process.go index 7436d16..91f1f97 100644 --- a/process/process.go +++ b/process/process.go @@ -10,32 +10,49 @@ import ( ) type Process struct { - Pid int - Cmdline string + Pid int + Cmdline string + rawIo string + firstErr error +} + +func newError() (Process, error) { + return Process{}, errors.New("Can not read process information") } func new(pidstr string) (Process, error) { pid, _ := strconv.Atoi(pidstr) - bytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid)) - if err != nil { - log.Fatal(err) - } - if len(bytes) > 0 { - return Process{Pid: pid, Cmdline: string(bytes)}, nil + process := Process{Pid: pid} + + process.gatherRaw(&process.Cmdline, "/proc/%d/cmdline") + process.gatherRaw(&process.rawIo, "/proc/%d/io") + + return process, process.firstErr +} + +func (self *Process) gatherRaw(what *string, pathf string) { + bytes, err := ioutil.ReadFile(fmt.Sprintf(pathf, self.Pid)) + if err != nil && self.firstErr == nil { + self.firstErr = err } else { - return Process{}, errors.New("Can not read process information") + *what = string(bytes) } } func (self *Process) String() string { - str := "=========================" + str := "=========================\n" str = str + fmt.Sprintf("PID: %d\n", self.Pid) - str = str + fmt.Sprintf("cmdline: %s\n", self.Cmdline) + str = str + fmt.Sprintf("Cmdline: %s\n", self.Cmdline) + str = str + fmt.Sprintf("rawIo: %s\n", self.rawIo) return str } +func (self *Process) Print() { + fmt.Println(self) +} + func Gather(processes chan<- Process) { re, _ := regexp.Compile("^[0-9]+$") |
