14 "github.com/dustin/go-humanize"
18 MyPID string = strconv.Itoa(os.Getpid())
29 func piper(c chan []string, name string, args ...string) error {
30 cmd := exec.Command(name, args...)
31 r, err := cmd.StdoutPipe()
39 scanner := bufio.NewScanner(r)
41 cols := strings.Fields(scanner.Text())
51 func bg(cmd string, args ...string) chan []string {
52 c := make(chan []string)
55 err := piper(c, cmd, args...)
57 log.Println("error:", cmd, ":", err)
59 time.Sleep(time.Second)
66 for cols := range bg("top", "-b", "-d", "infinity", "-p", MyPID, "-s", "5", "infinity") {
69 CPU = strings.ReplaceAll(fmt.Sprintf(
70 "%s sys:%s int:%s n:%s",
71 cols[1], cols[5], cols[7], cols[3],
74 Mem = strings.ReplaceAll(strings.Join(cols[1:], " "), ",", "")
78 if len(cols) >= 5 && cols[4] == "Used," {
79 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]))
103 for cols := range bg("netstat", "-I", "bridge0", "-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])
118 humanize.IBytes(uint64(ibps)),
119 humanize.IBytes(uint64(obps)),
125 for cols := range bg("sysctl", "-n", "dev.cpu.0.freq") {
126 if strings.HasSuffix(cols[0], "01") {
129 raw, err := strconv.Atoi(cols[0])
133 Freq = fmt.Sprintf("%.1fG", float64(raw)/1000)
139 ents, err := os.ReadDir("/tmp/stargrave-flags")
144 fs := make([]string, 0, len(ents))
145 for _, ent := range ents {
146 fs = append(fs, ent.Name())
148 Flags = strings.Join(fs, " ")
152 xsetroot := flag.Bool("xsetroot", false, "Call xsetroot")
157 time.Sleep(20 * time.Second)
169 status = fmt.Sprintf(
170 "[%s] [%s] [%s %s] [%s%s %s ARC] [%s] %s",
171 IOs, Net, CPU, Freq, Swap, Mem, ARC, Flags,
172 now.Format("2006-01-02 15:04:05"),
175 cmd = exec.Command("xsetroot", "-name", status)
180 time.Sleep(time.Second)