X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=main.go;h=20b4d350014c95b1e8fe77d55fa008da4158fc6b;hb=HEAD;hp=da12e03d66564e6620bec9fcc7f9de8254743740;hpb=492f52c944210b9a449e5de5518ee68676bc8c58;p=godwmstat.git diff --git a/main.go b/main.go index da12e03..20b4d35 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "os/exec" "strconv" "strings" + "sync" "time" "github.com/dustin/go-humanize" @@ -17,13 +18,14 @@ import ( var ( MyPID string = strconv.Itoa(os.Getpid()) CPU string = "?" + Freq string = "?" Mem string = "?" ARC string = "?" - Swap string = "?" + Swap string = "" Flags string = "?" IOs string = "?" - Net string = "?" - N string = "?" + Net = map[string]string{} + NetM sync.RWMutex ) func piper(c chan []string, name string, args ...string) error { @@ -43,12 +45,9 @@ func piper(c chan []string, name string, args ...string) error { c <- cols } } - if err = scanner.Err(); err != nil { - cmd.Process.Kill() - cmd.Wait() - return err - } - return nil + cmd.Process.Kill() + cmd.Wait() + return scanner.Err() } func bg(cmd string, args ...string) chan []string { @@ -69,10 +68,10 @@ func top() { for cols := range bg("top", "-b", "-d", "infinity", "-p", MyPID, "-s", "5", "infinity") { switch cols[0] { case "CPU:": - CPU = strings.ReplaceAll( - strings.Join([]string{cols[1], cols[5], cols[7]}, " "), - "%", "", - ) + CPU = strings.ReplaceAll(fmt.Sprintf( + "%s sys:%s int:%s n:%s", + cols[1], cols[5], cols[7], cols[3], + ), "%", "") case "Mem:": Mem = strings.ReplaceAll(strings.Join(cols[1:], " "), ",", "") case "ARC:": @@ -80,8 +79,6 @@ func top() { case "Swap:": if len(cols) >= 5 && cols[4] == "Used," { Swap = cols[3] + " Swap " - } else { - Swap = "" } } } @@ -89,7 +86,7 @@ func top() { func iostat() { var stats []string - for cols := range bg("iostat", "-d", "-w", N, "-x") { + for cols := range bg("iostat", "-d", "-w", "1", "-x") { if cols[0] == "device" { IOs = strings.Join(stats, " ") stats = nil @@ -102,8 +99,8 @@ func iostat() { } } -func netstat() { - for cols := range bg("netstat", "-n", N) { +func netstat(iface, short string) { + for cols := range bg("netstat", "-I", iface, "-n", "1") { if _, err := strconv.Atoi(cols[0]); err != nil { continue } @@ -115,12 +112,30 @@ 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() + } +} + +func freq() { + for cols := range bg("sysctl", "-n", "dev.cpu.0.freq") { + if strings.HasSuffix(cols[0], "01") { + Freq = "TB" + } else { + raw, err := strconv.Atoi(cols[0]) + if err != nil { + continue + } + Freq = fmt.Sprintf("%.1fG", float64(raw)/1000) + } } } @@ -139,9 +154,7 @@ func flagfiles() { func main() { xsetroot := flag.Bool("xsetroot", false, "Call xsetroot") - refresh := flag.Uint("refresh", 2, "Refresh interval") flag.Parse() - N = strconv.Itoa(int(*refresh)) go func() { for { flagfiles() @@ -149,24 +162,28 @@ 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] [CPU %s] [%s%s %s ARC] [%s] %s", - IOs, Net, CPU, 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() } else { fmt.Println(status) } - time.Sleep(time.Duration(*refresh) * time.Second) + time.Sleep(time.Second) } }