"strconv"
"strings"
"time"
+
+ "github.com/dustin/go-humanize"
)
var (
}
}
-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)),
+ )
}
}
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))
}
}()
go top()
- go iostat(strings.Split(*iodevs, " ")...)
+ go iostat()
go netstat()
var now time.Time
var status string
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"),
)