cleanup project structur
This commit is contained in:
@@ -1,59 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
|
||||||
"github.com/shirou/gopsutil/v3/mem"
|
|
||||||
)
|
|
||||||
|
|
||||||
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 isSupported() bool {
|
|
||||||
switch runtime.GOOS {
|
|
||||||
case "darwin":
|
|
||||||
return true
|
|
||||||
case "linux":
|
|
||||||
id := linuxID()
|
|
||||||
return id == "ubuntu" || id == "arch"
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if !isSupported() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
values, err := cpu.Percent(400*time.Millisecond, false)
|
|
||||||
if err != nil || len(values) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vm, err := mem.VirtualMemory()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Printf("CPU %.0f%% | RAM %.0f%%", values[0], vm.UsedPercent)
|
|
||||||
}
|
|
||||||
|
|
||||||
128
main.go
128
main.go
@@ -1,111 +1,59 @@
|
|||||||
// cmd/sysusage/main.go
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"bufio"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
"github.com/shirou/gopsutil/v3/mem"
|
"github.com/shirou/gopsutil/v3/mem"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Result struct {
|
func linuxID() string {
|
||||||
CPUPercent float64 `json:"cpu_percent"`
|
f, err := os.Open("/etc/os-release")
|
||||||
RAMPercent float64 `json:"ram_percent"`
|
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 sampleCPU(interval time.Duration, samples int) (float64, error) {
|
func isSupported() bool {
|
||||||
if samples <= 0 {
|
switch runtime.GOOS {
|
||||||
samples = 1
|
case "darwin":
|
||||||
|
return true
|
||||||
|
case "linux":
|
||||||
|
id := linuxID()
|
||||||
|
return id == "ubuntu" || id == "arch"
|
||||||
|
default:
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
var sum float64
|
|
||||||
for i := 0; i < samples; i++ {
|
|
||||||
// cpu.Percent blockiert für "interval" und liefert die Auslastung in diesem Zeitfenster
|
|
||||||
values, err := cpu.Percent(interval, false)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if len(values) == 0 {
|
|
||||||
return 0, fmt.Errorf("no cpu values returned")
|
|
||||||
}
|
|
||||||
sum += values[0]
|
|
||||||
}
|
|
||||||
return sum / float64(samples), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func readRAM() (float64, error) {
|
|
||||||
vm, err := mem.VirtualMemory()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return vm.UsedPercent, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
if !isSupported() {
|
||||||
intervalStr string
|
return
|
||||||
samples int
|
|
||||||
jsonOut bool
|
|
||||||
watch bool
|
|
||||||
)
|
|
||||||
|
|
||||||
flag.StringVar(&intervalStr, "interval", "1s", "Messintervall pro Stichprobe (z.B. 200ms, 1s, 2s)")
|
|
||||||
flag.IntVar(&samples, "samples", 1, "Anzahl der Stichproben (über das Intervall gemittelt)")
|
|
||||||
flag.BoolVar(&jsonOut, "json", false, "Ausgabe als JSON")
|
|
||||||
flag.BoolVar(&watch, "watch", false, "Kontinuierlich messen, bis Strg+C")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
interval, err := time.ParseDuration(intervalStr)
|
|
||||||
if err != nil || interval <= 0 {
|
|
||||||
log.Fatalf("ungültiges -interval: %v", intervalStr)
|
|
||||||
}
|
}
|
||||||
|
values, err := cpu.Percent(400*time.Millisecond, false)
|
||||||
printOnce := func() error {
|
if err != nil || len(values) == 0 {
|
||||||
cpuPct, err := sampleCPU(interval, samples)
|
return
|
||||||
|
}
|
||||||
|
vm, err := mem.VirtualMemory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("CPU-Messung fehlgeschlagen: %w", err)
|
return
|
||||||
}
|
|
||||||
ramPct, err := readRAM()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("RAM-Messung fehlgeschlagen: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if jsonOut {
|
|
||||||
out := Result{CPUPercent: cpuPct, RAMPercent: ramPct}
|
|
||||||
enc := json.NewEncoder(stdoutNoHTMLEscape{})
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
return enc.Encode(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("CPU: %.1f%% | RAM: %.1f%%\n", cpuPct, ramPct)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if watch {
|
|
||||||
for {
|
|
||||||
if err := printOnce(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// Bei watch: zwischen den Zeilen kurz pausieren, damit die Ausgabe lesbar bleibt.
|
|
||||||
time.Sleep(250 * time.Millisecond)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := printOnce(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
fmt.Printf("CPU %.0f%% | RAM %.0f%%", values[0], vm.UsedPercent)
|
||||||
|
|
||||||
// stdoutNoHTMLEscape verhindert das HTML-Escaping des Standard-Encoders (nur Kosmetik für JSON)
|
|
||||||
type stdoutNoHTMLEscape struct{}
|
|
||||||
|
|
||||||
func (stdoutNoHTMLEscape) Write(p []byte) (int, error) { return fmt.Print(string(p)) }
|
|
||||||
func (stdoutNoHTMLEscape) Encode(v any) error {
|
|
||||||
enc := json.NewEncoder(stdoutNoHTMLEscape{})
|
|
||||||
enc.SetEscapeHTML(false)
|
|
||||||
return enc.Encode(v)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|||||||
|
|
||||||
# Binary bauen (still). Wenn go fehlt: überspringen
|
# Binary bauen (still). Wenn go fehlt: überspringen
|
||||||
tmux run-shell "cd $CURRENT_DIR && GO111MODULE=on go mod tidy >/dev/null 2>&1 || true"
|
tmux run-shell "cd $CURRENT_DIR && GO111MODULE=on go mod tidy >/dev/null 2>&1 || true"
|
||||||
tmux run-shell "cd $CURRENT_DIR && GO111MODULE=on go build -ldflags='-s -w' -o bin.sysusage ./cmd/monitor >/dev/null 2>&1 || true"
|
tmux run-shell "cd $CURRENT_DIR && GO111MODULE=on go build -ldflags='-s -w' -o bin.sysusage . >/dev/null 2>&1 || true"
|
||||||
|
|
||||||
# tmux-Variable setzen: #{sysusage}
|
# tmux-Variable setzen: #{sysusage}
|
||||||
tmux set -g @sysusage_cmd "$CURRENT_DIR/bin.sysusage"
|
tmux set -g @sysusage_cmd "$CURRENT_DIR/bin.sysusage"
|
||||||
|
|||||||
Reference in New Issue
Block a user