initial commit with AI Code
This commit is contained in:
82
cmd/monitor/main.go
Normal file
82
cmd/monitor/main.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
)
|
||||
|
||||
func isSupported() bool {
|
||||
goos := os.Getenv("TMUX_SYSUSAGE_GOOS") // Debug/Override optional
|
||||
if goos == "" {
|
||||
goos = detectGOOS()
|
||||
}
|
||||
switch goos {
|
||||
case "darwin":
|
||||
return true
|
||||
case "linux":
|
||||
id := linuxID()
|
||||
return id == "ubuntu" || id == "arch"
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func detectGOOS() string {
|
||||
// runtime.GOOS vermeiden, um Import minimal zu halten? Es ist Standard—nutzen wir.
|
||||
// (Minimale Imports sind nice-to-have, aber runtime ist winzig.)
|
||||
// Wir nehmen ihn doch, weil zuverlässig.
|
||||
// → Ein einzelner Import mehr ist ok.
|
||||
return runtimeGOOS()
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func runtimeGOOS() string {
|
||||
// kleiner Trick: getrennt, damit der Compiler runtime nicht inlined—irrelevant, aber clean.
|
||||
return getGOOS()
|
||||
}
|
||||
|
||||
//go:linkname getGOOS runtime.GOOS
|
||||
func getGOOS() string
|
||||
|
||||
func linuxID() string {
|
||||
f, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer f.Close()
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
line := sc.Text()
|
||||
if strings.HasPrefix(line, "ID=") {
|
||||
val := strings.TrimPrefix(line, "ID=")
|
||||
val = strings.Trim(val, `"`)
|
||||
return strings.ToLower(val)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
if !isSupported() {
|
||||
// Keine Ausgabe auf nicht unterstützten Systemen
|
||||
return
|
||||
}
|
||||
// Kurze CPU-Stichprobe
|
||||
values, err := cpu.Percent(400*time.Millisecond, false)
|
||||
if err != nil || len(values) == 0 {
|
||||
return
|
||||
}
|
||||
vm, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// Kompakte tmux-Ausgabe
|
||||
fmt.Printf("CPU %.0f%% | RAM %.0f%%", values[0], vm.UsedPercent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user