diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-14 00:12:54 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-14 00:12:54 +0200 |
| commit | 5e416d613f61cfc16277d343e30c3c8a00b21b35 (patch) | |
| tree | d73b06932e9806c21f4ff7a8bf3f985b361f82cf /internal/collector | |
| parent | 956f84321860bd8e318545564474037cbd3b6fd9 (diff) | |
remove version and some refactoring
Diffstat (limited to 'internal/collector')
| -rw-r--r-- | internal/collector/collector.go | 20 | ||||
| -rw-r--r-- | internal/collector/parse_test.go | 10 | ||||
| -rw-r--r-- | internal/collector/types.go | 2 |
3 files changed, 14 insertions, 18 deletions
diff --git a/internal/collector/collector.go b/internal/collector/collector.go index 42ab689..dea88c7 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -2,9 +2,9 @@ package collector import ( "bufio" + "bytes" "context" "fmt" - "os" "os/exec" "strings" "time" @@ -20,13 +20,16 @@ type StatsStore interface { SetNet(host, iface string, net NetLine, stamp float64) } -// Run starts a collector for one host: runs the remote (or local) script and parses the stream into store. +// Run starts a collector for one host: runs the embedded remote script (local or over SSH) and parses the stream into store. // Host may be "host" or "host:user". It runs until ctx is cancelled or the command exits. -func Run(ctx context.Context, host string, cfg *config.Config, store StatsStore, scriptPath string) error { +// The script is embedded in the binary; no external script file is required. +func Run(ctx context.Context, host string, cfg *config.Config, store StatsStore) error { hostKey, user := splitHostUser(host) + script := bytes.NewReader(RemoteScript) var scanner *bufio.Scanner if isLocal(hostKey) { - cmd := exec.CommandContext(ctx, "bash", scriptPath) + cmd := exec.CommandContext(ctx, "bash", "-s") + cmd.Stdin = script stdout, err := cmd.StdoutPipe() if err != nil { return fmt.Errorf("%s: %w", hostKey, err) @@ -46,12 +49,7 @@ func Run(ctx context.Context, host string, cfg *config.Config, store StatsStore, } args = append(args, hostKey, "bash -s") cmd := exec.CommandContext(ctx, "ssh", args...) - scriptFile, err := os.Open(scriptPath) - if err != nil { - return fmt.Errorf("%s: open script: %w", hostKey, err) - } - defer scriptFile.Close() - cmd.Stdin = scriptFile + cmd.Stdin = script stdout, err := cmd.StdoutPipe() if err != nil { return fmt.Errorf("%s: %w", hostKey, err) @@ -123,5 +121,3 @@ func splitHostUser(host string) (h, u string) { func isLocal(h string) bool { return h == "localhost" || h == "127.0.0.1" } - - diff --git a/internal/collector/parse_test.go b/internal/collector/parse_test.go index 6edac33..ec77067 100644 --- a/internal/collector/parse_test.go +++ b/internal/collector/parse_test.go @@ -6,12 +6,12 @@ import ( func TestParseCPULine(t *testing.T) { tests := []struct { - name string - line string - wantName string - wantUser int64 + name string + line string + wantName string + wantUser int64 wantTotal int64 - wantErr bool + wantErr bool }{ {"normal", "cpu 100 0 50 200 0 0 0 0 0 0", "cpu", 100, 350, false}, {"cpu0", "cpu0 10 0 5 80 0 0 0 0 0 0", "cpu0", 10, 95, false}, diff --git a/internal/collector/types.go b/internal/collector/types.go index 1b10979..b0db600 100644 --- a/internal/collector/types.go +++ b/internal/collector/types.go @@ -2,7 +2,7 @@ package collector // CPULine is one line of /proc/stat: cpu name + counters (user, nice, system, idle, ...). type CPULine struct { - Name string + Name string User, Nice, System, Idle, Iowait, IRQ, SoftIRQ, Steal, Guest, GuestNice int64 } |
