git.sophuwu.com > statlog
added an example with a way to implement another value as a graph
uses the amdgpu temperature from /sys to make a graph
parent

fd1c80b5872d74d5a84331f4c7ffb7c1aa366ec2

3 files changed, 232 insertions(+), 0 deletions(-)

jump to
M device/mem.godevice/mem.go

@@ -47,6 +47,7 @@ }

func (m *MEM) Bar() (string, error) { w, _ := types.TermSize() + w-=2 if w < 40 { return "", types.ErrTooNarrow }
A test/gpu-temp.go

@@ -0,0 +1,78 @@

+package main + +import ( + "errors" + "fmt" + "os" + "os/signal" + "time" + + "git.sophuwu.com/statlog/types" +) + +func fatal(e error) { + if e != nil { + fmt.Println("\033[?1049l\033[?25h") + fmt.Println("Error:", e) + os.Exit(1) + } +} + +const gpuTempFile = "/sys/class/hwmon/hwmon1/temp1_input" + +func getGPU() (int, error) { + b, err := os.ReadFile(gpuTempFile) + if err != nil { return 0, err } + i := 0 + _, err = fmt.Sscanf(string(b), "%d", &i) + if err != nil { return 0, err } + return i/1000, err +} + +func main() { + fmt.Println("\033[?25l\033[?1049h\033[2J") + defer fmt.Println("\033[?1049l\033[?25h") + ch := make(chan os.Signal, 1) + bl := true + go func() { + signal.Notify(ch, os.Interrupt, os.Kill) + <-ch + bl = false + }() + var s string + var ss string + var e error + var tempGPU func(w, h, val int) (string, error) + tempGPU, e = types.Graph("GPU Temp", 100) + fatal(e) + var w, h, t int + prnt := func() { + fmt.Printf("\033[2J\033[1;1H\r%s\n", s) + time.Sleep(250 * time.Millisecond) + } + ERR := func() bool { + if errors.Is(e, types.ErrTooNarrow) { + s = "terminal too narrow" + prnt() + time.Sleep(100 * time.Millisecond) + return true + } + fatal(e) + return false + } + for bl { + s = "" + w, h = types.TermSize() + t, e = getGPU() + if ERR() { + continue + } + ss, e = tempGPU(w, h-10, t) + if ERR() { + continue + } + // print gpu temp graph + s += ss + "\n" + prnt() + } +}
A test/many-graphs.go

@@ -0,0 +1,153 @@

+package main + +import ( + "errors" + "fmt" + "os" + "os/signal" + "time" + + "git.sophuwu.com/statlog" + "git.sophuwu.com/statlog/types" +) + +func fatal(e error) { + if e != nil { + fmt.Println("\033[?1049l\033[?25h") + fmt.Println("Error:", e) + os.Exit(1) + } +} + + +const gpuTempFile = "/sys/class/hwmon/hwmon1/temp1_input" + +func getGPU() (int, error) { + b, err := os.ReadFile(gpuTempFile) + if err != nil { return 0, err } + i := 0 + _, err = fmt.Sscanf(string(b), "%d", &i) + if err != nil { return 0, err } + return i/1000, err +} + + +func main() { + fmt.Println("\033[?25l\033[?1049h\033[2J") + defer fmt.Println("\033[?1049l\033[?25h") + hw := &statlog.HWInfo{} + hw.Update() + ch := make(chan os.Signal, 1) + bl := true + go func() { + signal.Notify(ch, os.Interrupt, os.Kill) + <-ch + bl = false + }() + var s string + var ss string + var e error + + var grCpu func(w, h, val int) (string, error) + grCpu, e = types.Graph("CPU Load", 100) + fatal(e) + + var grMem func(w, h, val int) (string, error) + grMem, e = types.Graph("Memory (%)", 100) + fatal(e) + + var tempGPU func(w, h, val int) (string, error) + tempGPU, e = types.Graph("GPU Temp", 100) + fatal(e) + + var tempCPU func(w, h, val int) (string, error) + tempCPU, e = types.Graph("CPU Temp", 100) + fatal(e) + + var w, h, t int + prnt := func() { + fmt.Printf("\033[2J\033[1;1H\r%s\n", s) + } + ERR := func() bool { + if errors.Is(e, types.ErrTooNarrow) { + s = "terminal too narrow" + prnt() + time.Sleep(100 * time.Millisecond) + return true + } + fatal(e) + return false + } + szfn := func(){ + w, h = types.TermSize() + h-=30 + } + szfn() + for bl { + s = "" + szfn() + hw.Update() + + // mem + ss, e = hw.MEM.Bar() + + if ERR() { + continue + } + s += "MEM: " + hw.MEM.String() + "\n" + ss + "\n" + ss, e = grMem(w, h/6, int(hw.MEM.Percent.Used)) + if ERR() { + continue + } + s += ss + "\n" + + // cpu + + // print cpu info string + s += "CPU: " + hw.CPU.String() + "\n" + // make load graph + ss, e = grCpu(w, h/6, int(hw.CPU.LoadAvg)) + if ERR() { + continue + } + // print load graph + s += ss + "\n" + + // cpu bar graphs + // make bar graph + ss, e = hw.CPU.LoadAvg.Bar("CPU Avg", w) + if ERR() { + continue + } + // print bar graph + s += ss + "\n" + // make core bar graphs + ss, e = hw.CPU.LoadBar(w) + if ERR() { + continue + } + // print core bar graphs + s += ss + "\n" + + ss, e = tempCPU(w, h/4, int(hw.CPU.Temp)) + if ERR() { + continue + } + // print cpu temp graph + s += ss + "\n" + + + t, e = getGPU() + if ERR() { + continue + } + ss, e = tempGPU(w, h/4, t) + if ERR() { + continue + } + // print gpu temp graph + s += ss + "\n" + + prnt() + } +}