diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 00:46:58 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 00:46:58 +0100 |
| commit | f459cec5e073de22019de4ae160d91cbaa926b63 (patch) | |
| tree | 381f7f5820ce8fff6fd7c7bfe086b5f8856c8d3f | |
| parent | 18fa47922cdb0fa7da8e264ddd2807fbccb1db5b (diff) | |
initial flags
| -rw-r--r-- | gstat/main.go | 52 | ||||
| -rw-r--r-- | utils/utils.go | 2 |
2 files changed, 45 insertions, 9 deletions
diff --git a/gstat/main.go b/gstat/main.go index 86e3b0a..c5ee164 100644 --- a/gstat/main.go +++ b/gstat/main.go @@ -1,7 +1,10 @@ +// gstat (C) 2015 Paul Buetow (gstat@dev.buetow.org) + package main import ( "container/list" + "flag" "fmt" "github.com/buetow/gstat/diskstats" "github.com/buetow/gstat/process" @@ -14,7 +17,12 @@ import ( "time" ) -var interval_ time.Duration +var config struct { + banner string + interval time.Duration + binary *bool + mode *int +} type twoP struct { first, second process.Process @@ -54,7 +62,7 @@ func sortP(lastP *mapP) *list.List { for _, val := range *lastP { nowTimestamp := int32(time.Now().Unix()) - if val.first.Timestamp+int32(interval_)*2 < nowTimestamp { + if val.first.Timestamp+int32(config.interval)*2 < nowTimestamp { // Schedule remove obsolete pids from lastP remove.PushBack(val.first.Id) @@ -93,7 +101,8 @@ func printP(sortedP *list.List) { } // Clear the screen + print header - fmt.Println("\033[H\033[2J") + fmt.Printf("\033[H\033[2J") + fmt.Printf("(Hit Ctr-C to quit, re-run with -h for flags)\n") fmt.Printf("%6s %6s %6s %s\n", "WRITES", "READS", "PID", "COMMAND") // Print the results @@ -105,7 +114,14 @@ func printP(sortedP *list.List) { } val := e.Value.(twoP) first := val.first - humanW, humanR := utils.Human(val.diffW), utils.Human(val.diffR) + + var humanW, humanR string + if *config.binary { + humanW, humanR = utils.HumanBinary(val.diffW), utils.HumanBinary(val.diffR) + } else { + humanW, humanR = utils.Human(val.diffW), utils.Human(val.diffR) + } + outstr := fmt.Sprintf("%6s %6s %6d %s", humanW, humanR, first.Pid, first.Cmdline) l := len(outstr) @@ -149,13 +165,32 @@ func receiveP(pRxChan <-chan process.Process) { } } +func parseFlags() { + helpF := flag.Bool("v", false, "Print the version") + interF := flag.Int("i", 2, "Update interval in seconds") + + config.binary = flag.Bool("b", false, "Use binary instead of deciman (e.g. kiB an not kB") + config.mode = flag.Int("m", 0, "The stats mode: 0:bytes 1:syscalls 2:chars") + + flag.Parse() + + config.banner = "gstat v0.1 (C) 2015 Paul buetow <http://gstat.buetow.org>" + + if *helpF { + fmt.Println(config.banner) + os.Exit(0) + } + + config.interval = time.Duration(*interF) +} + func main() { + parseFlags() + tChan := make(chan bool) dRxChan := make(chan diskstats.Diskstats) pRxChan := make(chan process.Process) - interval_ = 2 - go timedGather(tChan, dRxChan, pRxChan) go receiveD(dRxChan) go receiveP(pRxChan) @@ -167,12 +202,13 @@ func main() { go func() { <-termChan tChan <- false - fmt.Println("Good bye") + fmt.Println("Good bye! This was:") + fmt.Println(config.banner) os.Exit(1) }() for { tChan <- true - time.Sleep(time.Second * interval_) + time.Sleep(time.Second * config.interval) } } diff --git a/utils/utils.go b/utils/utils.go index 65461f4..28532ab 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -36,7 +36,7 @@ func Human(x int) string { return fmt.Sprintf("%d%s", int(f), units[i]) } -func HumanMebi(x int) string { +func HumanBinary(x int) string { units := []string{"ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"} f := float32(x) |
