From: Sergey Matveev Date: Sun, 7 Nov 2021 07:39:36 +0000 (+0300) Subject: Show all IO devices, show network bps X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=492f52c944210b9a449e5de5518ee68676bc8c58;hp=0b1c15ae6e4bf933240068f0696bc4ed2370f9fe;p=godwmstat.git Show all IO devices, show network bps --- diff --git a/go.mod b/go.mod index de0386d..3d70c78 100644 --- 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 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 04cca53..da12e03 100644 --- 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"), )