summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow (europa) <paul@buetow.org>2015-05-25 21:40:36 +0100
committerPaul Buetow (europa) <paul@buetow.org>2015-05-25 21:40:36 +0100
commit1d3f483ebb9f024324866ba87aa265ab5b06b735 (patch)
tree37e92e80ea8e28dff074cf9d38ba2c0fa172a408
parent1f1db69182a6850452752e24a725c20c4a5b6ccb (diff)
refactor
-rw-r--r--diskstats/diskstats.go14
-rw-r--r--process/process.go19
-rw-r--r--utils/utils.go13
3 files changed, 25 insertions, 21 deletions
diff --git a/diskstats/diskstats.go b/diskstats/diskstats.go
index 4e2094c..015fa56 100644
--- a/diskstats/diskstats.go
+++ b/diskstats/diskstats.go
@@ -2,7 +2,7 @@ package diskstats
import (
"fmt"
- "io/ioutil"
+ "github.com/buetow/gstat/utils"
)
type Diskstats struct {
@@ -13,23 +13,13 @@ func new() (Diskstats, error) {
var raw string
d := Diskstats{}
- if err := d.gatherRaw(&raw, "/proc/diskstats"); err != nil {
+ if err := utils.GatherRaw(&raw, "/proc/diskstats"); err != nil {
return d, err
}
return d, nil
}
-func (self *Diskstats) gatherRaw(what *string, path string) error {
- bytes, err := ioutil.ReadFile(path)
- if err != nil {
- return err
- } else {
- *what = string(bytes)
- }
- return nil
-}
-
func (self *Diskstats) String() string {
str := "DISKSTATS=========================\n"
diff --git a/process/process.go b/process/process.go
index dc953d5..f025957 100644
--- a/process/process.go
+++ b/process/process.go
@@ -2,6 +2,7 @@ package process
import (
"fmt"
+ "github.com/buetow/gstat/utils"
"io/ioutil"
"log"
"regexp"
@@ -22,19 +23,19 @@ func new(pidstr string) (Process, error) {
return Process{}, err
}
- process := Process{Pid: pid}
+ p := Process{Pid: pid}
var rawIo string
- if err = process.gatherRaw(&rawIo, "/proc/%d/io"); err != nil {
- return process, err
+ if err = utils.GatherRaw(&rawIo, fmt.Sprintf("/proc/%d/io", pid)); err != nil {
+ return p, err
}
- if err = process.parseRawIo(rawIo); err != nil {
- return process, err
+ if err = p.parseRawIo(rawIo); err != nil {
+ return p, err
}
- err = process.gatherRaw(&process.Cmdline, "/proc/%d/cmdline")
- return process, err
+ err = utils.GatherRaw(&p.Cmdline, fmt.Sprintf("/proc/%d/cmdline", pid))
+ return p, err
}
func (self *Process) gatherRaw(what *string, pathf string) error {
@@ -93,9 +94,9 @@ func Gather(processes chan<- Process) {
for _, direntry := range dir {
name := direntry.Name()
if re.MatchString(name) {
- process, err := new(name)
+ p, err := new(name)
if err == nil {
- processes <- process
+ processes <- p
}
}
}
diff --git a/utils/utils.go b/utils/utils.go
new file mode 100644
index 0000000..3a19ac5
--- /dev/null
+++ b/utils/utils.go
@@ -0,0 +1,13 @@
+package utils
+
+import "io/ioutil"
+
+func GatherRaw(what *string, path string) error {
+ bytes, err := ioutil.ReadFile(path)
+ if err != nil {
+ return err
+ } else {
+ *what = string(bytes)
+ }
+ return nil
+}