]> Sergey Matveev's repositories - godwmstat.git/blobdiff - main.go
Shorter NIC names
[godwmstat.git] / main.go
diff --git a/main.go b/main.go
index fb515303436ef7ad2ecaf8b4b702db29bd561edf..20b4d350014c95b1e8fe77d55fa008da4158fc6b 100644 (file)
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
        "os/exec"
        "strconv"
        "strings"
+       "sync"
        "time"
 
        "github.com/dustin/go-humanize"
@@ -20,10 +21,11 @@ var (
        Freq  string = "?"
        Mem   string = "?"
        ARC   string = "?"
-       Swap  string = "?"
+       Swap  string = ""
        Flags string = "?"
        IOs   string = "?"
-       Net   string = "?"
+       Net          = map[string]string{}
+       NetM  sync.RWMutex
 )
 
 func piper(c chan []string, name string, args ...string) error {
@@ -77,8 +79,6 @@ func top() {
                case "Swap:":
                        if len(cols) >= 5 && cols[4] == "Used," {
                                Swap = cols[3] + " Swap "
-                       } else {
-                               Swap = ""
                        }
                }
        }
@@ -99,8 +99,8 @@ func iostat() {
        }
 }
 
-func netstat() {
-       for cols := range bg("netstat", "-I", "bridge0", "-n", "1") {
+func netstat(iface, short string) {
+       for cols := range bg("netstat", "-I", iface, "-n", "1") {
                if _, err := strconv.Atoi(cols[0]); err != nil {
                        continue
                }
@@ -112,12 +112,16 @@ func netstat() {
                if err != nil {
                        continue
                }
-               Net = fmt.Sprintf(
-                       "%s/%s %s / %s",
-                       cols[0], cols[4],
+               _ = cols[0] // pkts rx
+               _ = cols[4] // pkts tx
+               NetM.Lock()
+               Net[short] = fmt.Sprintf(
+                       "%s:%s/%s",
+                       short,
                        humanize.IBytes(uint64(ibps)),
                        humanize.IBytes(uint64(obps)),
                )
+               NetM.Unlock()
        }
 }
 
@@ -160,17 +164,20 @@ func main() {
        go top()
        go freq()
        go iostat()
-       go netstat()
+       go netstat("bridge0", "br")
+       go netstat("ix0", "ix")
        var now time.Time
        var status string
        var cmd *exec.Cmd
        for {
                now = time.Now()
+               NetM.RLock()
                status = fmt.Sprintf(
-                       "[%s]  [%s]  [%s %s]  [%s%s %s ARC]  [%s] %s",
-                       IOs, Net, CPU, Freq, Swap, Mem, ARC, Flags,
+                       "[%s]  [%s %s]  [%s %s]  [%s%s %s ARC]  [%s] %s",
+                       IOs, Net["br"], Net["ix"], CPU, Freq, Swap, Mem, ARC, Flags,
                        now.Format("2006-01-02 15:04:05"),
                )
+               NetM.RUnlock()
                if *xsetroot {
                        cmd = exec.Command("xsetroot", "-name", status)
                        cmd.Run()