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(fmt.Sprintf(
73 "%s sys:%s int:%s n:%s",
74 cols[1], cols[5], cols[7], cols[3],
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", "1", "-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", "-I", "bridge0", "-n", "1") {
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 for cols := range bg("sysctl", "-n", "dev.cpu.0.freq") {
129 raw, err := strconv.Atoi(cols[0])
133 Freq = fmt.Sprintf("%.1fG", float64(raw)/1000)
138 ents, err := os.ReadDir("/tmp/stargrave-flags")
143 fs := make([]string, 0, len(ents))
144 for _, ent := range ents {
145 fs = append(fs, ent.Name())
147 Flags = strings.Join(fs, " ")
151 xsetroot := flag.Bool("xsetroot", false, "Call xsetroot")
156 time.Sleep(20 * time.Second)
168 status = fmt.Sprintf(
169 "[%s] [%s] [%s %s] [%s%s %s ARC] [%s] %s",
170 IOs, Net, CPU, Freq, Swap, Mem, ARC, Flags,
171 now.Format("2006-01-02 15:04:05"),
174 cmd = exec.Command("xsetroot", "-name", status)
179 time.Sleep(time.Second)