]> Sergey Matveev's repositories - godwmstat.git/commitdiff
Show all IO devices, show network bps
authorSergey Matveev <stargrave@stargrave.org>
Sun, 7 Nov 2021 07:39:36 +0000 (10:39 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 7 Nov 2021 11:13:46 +0000 (14:13 +0300)
go.mod
go.sum [new file with mode: 0644]
main.go

diff --git a/go.mod b/go.mod
index de0386d0eeccb8921df491e564f2a96e3d5c3f02..3d70c785233f694b9f5b37dab441a72c1be7a1a3 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
 module go.stargrave.org/godwmstat
 
 go 1.17
+
+require github.com/dustin/go-humanize v1.0.0 // indirect
diff --git a/go.sum b/go.sum
new file mode 100644 (file)
index 0000000..4d39dd1
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
+github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
diff --git a/main.go b/main.go
index 04cca53140ad7e44e005d908e465391a517ce1a2..da12e03d66564e6620bec9fcc7f9de8254743740 100644 (file)
--- a/main.go
+++ b/main.go
@@ -10,6 +10,8 @@ import (
        "strconv"
        "strings"
        "time"
+
+       "github.com/dustin/go-humanize"
 )
 
 var (
@@ -85,32 +87,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", N, "-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, "/")
+               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,7 +139,6 @@ func flagfiles() {
 
 func main() {
        xsetroot := flag.Bool("xsetroot", false, "Call xsetroot")
-       iodevs := flag.String("iodevs", "nvd0 nvd1", "iostat devices")
        refresh := flag.Uint("refresh", 2, "Refresh interval")
        flag.Parse()
        N = strconv.Itoa(int(*refresh))
@@ -140,7 +149,7 @@ func main() {
                }
        }()
        go top()
-       go iostat(strings.Split(*iodevs, " ")...)
+       go iostat()
        go netstat()
        var now time.Time
        var status string
@@ -148,7 +157,7 @@ func main() {
        for {
                now = time.Now()
                status = fmt.Sprintf(
-                       "[IO %s]  [Net %s]  [CPU %s]  [%s%s %s ARC]  [%s] %s",
+                       "[%s]  [%s]  [CPU %s]  [%s%s %s ARC]  [%s] %s",
                        IOs, Net, CPU, Swap, Mem, ARC, Flags,
                        now.Format("2006-01-02 15:04:05"),
                )