diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-25 10:39:47 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-25 10:39:47 +0100 |
| commit | b5117be9fc9dd7be25735fb8da93a2d1fb0349c9 (patch) | |
| tree | 678e734c51794b8b5a75a34fa2578adeaaf7c288 | |
| parent | b804f4905f48d3a05218a323b258963c514399f7 (diff) | |
initial diskstats
| -rw-r--r-- | diskstats/diskstats.go | 102 | ||||
| -rw-r--r-- | gstat/main.go | 26 | ||||
| -rw-r--r-- | process/process.go | 1 |
3 files changed, 121 insertions, 8 deletions
diff --git a/diskstats/diskstats.go b/diskstats/diskstats.go new file mode 100644 index 0000000..c6d9f9a --- /dev/null +++ b/diskstats/diskstats.go @@ -0,0 +1,102 @@ +package diskstats + +import ( + "fmt" + "io/ioutil" + "log" + "regexp" + "strconv" + "strings" +) + +type Diskstats struct { + Pid int + Cmdline string + Count map[string]int + debug string +} + +func new(pidstr string) (Diskstats, error) { + pid, err0 := strconv.Atoi(pidstr) + if err0 != nil { + return Diskstats{}, err0 + } + diskstats := Diskstats{Pid: pid} + var rawIo string + + err1 := diskstats.gatherRaw(&rawIo, "/proc/%d/io") + if err1 != nil { + return diskstats, err1 + } + err2 := diskstats.parseRawIo(rawIo) + if err2 != nil { + return diskstats, err2 + } + + err3 := diskstats.gatherRaw(&diskstats.Cmdline, "/proc/%d/cmdline") + return diskstats, err3 +} + +func (self *Diskstats) gatherRaw(what *string, pathf string) error { + bytes, err := ioutil.ReadFile(fmt.Sprintf(pathf, self.Pid)) + if err != nil { + return err + } else { + *what = string(bytes) + } + return nil +} + +func (self *Diskstats) parseRawIo(rawIo string) error { + countMap := make(map[string]int) + for _, line := range strings.Split(rawIo, "\n") { + keyval := strings.Split(line, ": ") + if len(keyval) == 2 { + count, err := strconv.Atoi(keyval[1]) + if err != nil { + return err + } + countMap[keyval[0]] = count + } + } + self.Count = countMap + return nil +} + +func (self *Diskstats) String() string { + str := "DISKSTATS=========================\n" + + str += fmt.Sprintf("PID: %d\n", self.Pid) + str += fmt.Sprintf("Cmdline: %s\n", self.Cmdline) + for key, val := range self.Count { + str += fmt.Sprintf("%s=%d\n", key, val) + } + if self.debug != "" { + str += fmt.Sprintf("debug: %s\n", self.debug) + } + + return str +} + +func (self *Diskstats) Print() { + fmt.Println(self) +} + +func Gather(diskstatses chan<- Diskstats) { + 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) { + diskstats, err := new(name) + if err == nil { + diskstatses <- diskstats + } + } + } +} diff --git a/gstat/main.go b/gstat/main.go index 6cef683..254edeb 100644 --- a/gstat/main.go +++ b/gstat/main.go @@ -2,11 +2,12 @@ package main import ( "fmt" + "github.com/buetow/gstat/diskstats" "github.com/buetow/gstat/process" "time" ) -func gather(timer <-chan bool, processes chan<- process.Process) { +func gather(timer <-chan bool, d chan<- diskstats.Diskstats, p chan<- process.Process) { for { switch <-timer { case false: @@ -15,14 +16,22 @@ func gather(timer <-chan bool, processes chan<- process.Process) { } case true: { - process.Gather(processes) + diskstats.Gather(d) + process.Gather(p) } } } - close(processes) + close(d) + close(p) } -func receive(processes <-chan process.Process) { +func receive1(diskstats <-chan diskstats.Diskstats) { + for diskstats := range diskstats { + diskstats.Print() + } +} + +func receive2(processes <-chan process.Process) { for process := range processes { process.Print() } @@ -30,14 +39,17 @@ func receive(processes <-chan process.Process) { func main() { timer := make(chan bool) - res := make(chan process.Process) + diskstats := make(chan diskstats.Diskstats) + processes := make(chan process.Process) - go gather(timer, res) - go receive(res) + go gather(timer, diskstats, processes) + go receive1(diskstats) + go receive2(processes) for counter := 0; counter < 3; counter++ { timer <- true time.Sleep(time.Second) + fmt.Printf("Next... %d\n", counter) } timer <- false diff --git a/process/process.go b/process/process.go index 85f80f2..dc953d5 100644 --- a/process/process.go +++ b/process/process.go @@ -1,7 +1,6 @@ package process import ( - "errors" "fmt" "io/ioutil" "log" |
