]> Sergey Matveev's repositories - godwmstat.git/blobdiff - main.go
CPU notes
[godwmstat.git] / main.go
diff --git a/main.go b/main.go
index 8c85756047dda1238f1779fc4e4b6f1fb54f5963..50ede58eb7c76ac1d14c65f982f5ebc2d59f6e9c 100644 (file)
--- a/main.go
+++ b/main.go
@@ -10,6 +10,8 @@ import (
        "strconv"
        "strings"
        "time"
+
+       "github.com/dustin/go-humanize"
 )
 
 var (
@@ -21,7 +23,6 @@ var (
        Flags string = "?"
        IOs   string = "?"
        Net   string = "?"
-       N     string = "?"
 )
 
 func piper(c chan []string, name string, args ...string) error {
@@ -68,7 +69,7 @@ func top() {
                switch cols[0] {
                case "CPU:":
                        CPU = strings.ReplaceAll(
-                               strings.Join([]string{cols[1], cols[5], cols[7]}, " "),
+                               fmt.Sprintf("%s sys:%s int:%s", cols[1], cols[5], cols[7]),
                                "%", "",
                        )
                case "Mem:":
@@ -76,8 +77,8 @@ func top() {
                case "ARC:":
                        ARC = cols[1]
                case "Swap:":
-                       if cols[4] == "Used," {
-                               Swap = cols[3] + " "
+                       if len(cols) >= 5 && cols[4] == "Used," {
+                               Swap = cols[3] + " Swap "
                        } else {
                                Swap = ""
                        }
@@ -85,32 +86,40 @@ func top() {
        }
 }
 
-func iostat(devs ...string) {
-       stats := make([]string, len(devs)*4)
-       for i := 0; i < len(devs); i++ {
-               stats[i*4+1] = "/"
-               stats[i*4+3] = " "
-       }
-       stats = stats[:len(stats)-1]
-       for cols := range bg("iostat", append([]string{"-d", "-w", N, "-x"}, devs...)...) {
-               for i, dev := range devs {
-                       if cols[0] == dev {
-                               stats[i*4+0] = cols[1]
-                               stats[i*4+2] = cols[2]
-                               IOs = strings.Join(stats, "")
-                       }
+func iostat() {
+       var stats []string
+       for cols := range bg("iostat", "-d", "-w", "1", "-x") {
+               if cols[0] == "device" {
+                       IOs = strings.Join(stats, " ")
+                       stats = nil
+                       continue
+               }
+               if len(cols) < 4 || (cols[1] == "0" && cols[2] == "0") {
+                       continue
                }
+               stats = append(stats, fmt.Sprintf("%s:%s/%s", cols[0], cols[1], cols[2]))
        }
 }
 
 func netstat() {
-       stats := []string{"0", "0"}
-       for cols := range bg("netstat", "-n", N) {
-               if _, err := strconv.Atoi(cols[0]); err == nil {
-                       stats[0] = cols[0]
-                       stats[1] = cols[4]
-                       Net = strings.Join(stats, "/")
+       for cols := range bg("netstat", "-I", "bridge0", "-n", "1") {
+               if _, err := strconv.Atoi(cols[0]); err != nil {
+                       continue
                }
+               ibps, err := strconv.Atoi(cols[3])
+               if err != nil {
+                       continue
+               }
+               obps, err := strconv.Atoi(cols[6])
+               if err != nil {
+                       continue
+               }
+               Net = fmt.Sprintf(
+                       "%s/%s %s / %s",
+                       cols[0], cols[4],
+                       humanize.IBytes(uint64(ibps)),
+                       humanize.IBytes(uint64(obps)),
+               )
        }
 }
 
@@ -129,10 +138,7 @@ func flagfiles() {
 
 func main() {
        xsetroot := flag.Bool("xsetroot", false, "Call xsetroot")
-       iodevs := flag.String("iodevs", "ada0 ada1", "iostat devices")
-       refresh := flag.Uint("refresh", 2, "Refresh interval")
        flag.Parse()
-       N = strconv.Itoa(int(*refresh))
        go func() {
                for {
                        flagfiles()
@@ -140,7 +146,7 @@ func main() {
                }
        }()
        go top()
-       go iostat(strings.Split(*iodevs, " ")...)
+       go iostat()
        go netstat()
        var now time.Time
        var status string
@@ -148,7 +154,7 @@ func main() {
        for {
                now = time.Now()
                status = fmt.Sprintf(
-                       "[IO %s]  [Net %s]  [CPU %s]  [%sMem %s ARC %s]  [%s] %s",
+                       "[%s]  [%s]  [%s]  [%s%s %s ARC]  [%s] %s",
                        IOs, Net, CPU, Swap, Mem, ARC, Flags,
                        now.Format("2006-01-02 15:04:05"),
                )
@@ -158,6 +164,6 @@ func main() {
                } else {
                        fmt.Println(status)
                }
-               time.Sleep(time.Duration(*refresh) * time.Second)
+               time.Sleep(time.Second)
        }
 }