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())
46 if err = scanner.Err(); err != nil {
54 func bg(cmd string, args ...string) chan []string {
55 c := make(chan []string)
58 err := piper(c, cmd, args...)
60 log.Println("error:", cmd, ":", err)
62 time.Sleep(time.Second)
69 for cols := range bg("top", "-b", "-d", "infinity", "-p", MyPID, "-s", "5", "infinity") {
72 CPU = strings.ReplaceAll(
73 strings.Join([]string{cols[1], cols[5], cols[7]}, " "),
77 Mem = strings.ReplaceAll(strings.Join(cols[1:], " "), ",", "")
81 if len(cols) >= 5 && cols[4] == "Used," {
82 Swap = cols[3] + " Swap "
92 for cols := range bg("iostat", "-d", "-w", N, "-x") {
93 if cols[0] == "device" {
94 IOs = strings.Join(stats, " ")
98 if len(cols) < 4 || (cols[1] == "0" && cols[2] == "0") {
101 stats = append(stats, fmt.Sprintf("%s:%s/%s", cols[0], cols[1], cols[2]))
106 for cols := range bg("netstat", "-n", N) {
107 if _, err := strconv.Atoi(cols[0]); err != nil {
110 ibps, err := strconv.Atoi(cols[3])
114 obps, err := strconv.Atoi(cols[6])
121 humanize.IBytes(uint64(ibps)),
122 humanize.IBytes(uint64(obps)),
128 ents, err := os.ReadDir("/tmp/stargrave-flags")
133 fs := make([]string, 0, len(ents))
134 for _, ent := range ents {
135 fs = append(fs, ent.Name())
137 Flags = strings.Join(fs, " ")
141 xsetroot := flag.Bool("xsetroot", false, "Call xsetroot")
142 refresh := flag.Uint("refresh", 2, "Refresh interval")
144 N = strconv.Itoa(int(*refresh))
148 time.Sleep(20 * time.Second)
159 status = fmt.Sprintf(
160 "[%s] [%s] [CPU %s] [%s%s %s ARC] [%s] %s",
161 IOs, Net, CPU, Swap, Mem, ARC, Flags,
162 now.Format("2006-01-02 15:04:05"),
165 cmd = exec.Command("xsetroot", "-name", status)
170 time.Sleep(time.Duration(*refresh) * time.Second)