15 "github.com/dustin/go-humanize"
19 MyPID string = strconv.Itoa(os.Getpid())
27 Net = map[string]string{}
31 func piper(c chan []string, name string, args ...string) error {
32 cmd := exec.Command(name, args...)
33 r, err := cmd.StdoutPipe()
41 scanner := bufio.NewScanner(r)
43 cols := strings.Fields(scanner.Text())
53 func bg(cmd string, args ...string) chan []string {
54 c := make(chan []string)
57 err := piper(c, cmd, args...)
59 log.Println("error:", cmd, ":", err)
61 time.Sleep(time.Second)
68 for cols := range bg("top", "-b", "-d", "infinity", "-p", MyPID, "-s", "5", "infinity") {
71 CPU = strings.ReplaceAll(fmt.Sprintf(
72 "%s sys:%s int:%s n:%s",
73 cols[1], cols[5], cols[7], cols[3],
76 Mem = strings.ReplaceAll(strings.Join(cols[1:], " "), ",", "")
80 if len(cols) >= 5 && cols[4] == "Used," {
81 Swap = cols[3] + " Swap "
89 for cols := range bg("iostat", "-d", "-w", "1", "-x") {
90 if cols[0] == "device" {
91 IOs = strings.Join(stats, " ")
95 if len(cols) < 4 || (cols[1] == "0" && cols[2] == "0") {
98 stats = append(stats, fmt.Sprintf("%s:%s/%s", cols[0], cols[1], cols[2]))
102 func netstat(iface, short string) {
103 for cols := range bg("netstat", "-I", iface, "-n", "1") {
104 if _, err := strconv.Atoi(cols[0]); err != nil {
107 ibps, err := strconv.Atoi(cols[3])
111 obps, err := strconv.Atoi(cols[6])
115 _ = cols[0] // pkts rx
116 _ = cols[4] // pkts tx
118 Net[short] = fmt.Sprintf(
121 humanize.IBytes(uint64(ibps)),
122 humanize.IBytes(uint64(obps)),
129 for cols := range bg("sysctl", "-n", "dev.cpu.0.freq") {
130 if strings.HasSuffix(cols[0], "01") {
133 raw, err := strconv.Atoi(cols[0])
137 Freq = fmt.Sprintf("%.1fG", float64(raw)/1000)
143 ents, err := os.ReadDir("/tmp/stargrave-flags")
148 fs := make([]string, 0, len(ents))
149 for _, ent := range ents {
150 fs = append(fs, ent.Name())
152 Flags = strings.Join(fs, " ")
156 xsetroot := flag.Bool("xsetroot", false, "Call xsetroot")
161 time.Sleep(20 * time.Second)
167 go netstat("bridge0", "br")
168 go netstat("ix0", "ix")
175 status = fmt.Sprintf(
176 "[%s] [%s %s] [%s %s] [%s%s %s ARC] [%s] %s",
177 IOs, Net["br"], Net["ix"], CPU, Freq, Swap, Mem, ARC, Flags,
178 now.Format("2006-01-02 15:04:05"),
182 cmd = exec.Command("xsetroot", "-name", status)
187 time.Sleep(time.Second)