"strconv"
"strings"
"time"
+
+ "github.com/dustin/go-humanize"
)
var (
MyPID string = strconv.Itoa(os.Getpid())
CPU string = "?"
+ Freq string = "?"
Mem string = "?"
ARC string = "?"
Swap string = "?"
Flags string = "?"
IOs string = "?"
Net string = "?"
- N string = "?"
)
func piper(c chan []string, name string, args ...string) error {
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:":
}
}
-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", "1", "-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, "/")
+ for cols := range bg("netstat", "-I", "bridge0", "-n", "1") {
+ 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 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)
}
}
}
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 func() {
for {
flagfiles()
}
}()
go top()
- go iostat(strings.Split(*iodevs, " ")...)
+ go freq()
+ 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",
- IOs, Net, CPU, Swap, Mem, ARC, Flags,
+ "[%s] [%s] [%s %s] [%s%s %s ARC] [%s] %s",
+ IOs, Net, CPU, Freq, Swap, Mem, ARC, Flags,
now.Format("2006-01-02 15:04:05"),
)
if *xsetroot {
} else {
fmt.Println(status)
}
- time.Sleep(time.Duration(*refresh) * time.Second)
+ time.Sleep(time.Second)
}
}