Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

feat(backend): implement real-time log broadcasting and handlers

rcourtmanbb8fbfb4113a
diff --git a/internal/api/log_handlers.go b/internal/api/log_handlers.go
new file mode 100644
index 0000000000..a50d8a40a9
--- /dev/null
+++ b/internal/api/log_handlers.go
@@ -0,0 +1,218 @@
+package api
+
+import (
+ "archive/zip"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "os"
+ "strings"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ "github.com/rcourtman/pulse-go-rewrite/internal/logging"
+ "github.com/rs/zerolog/log"
+)
+
+type LogHandlers struct {
+ config *config.Config
+ persistence *config.ConfigPersistence
+}
+
+func NewLogHandlers(cfg *config.Config, persistence *config.ConfigPersistence) *LogHandlers {
+ return &LogHandlers{
+ config: cfg,
+ persistence: persistence,
+ }
+}
+
+// HandleStreamLogs streams logs using SSE (Server-Sent Events)
+func (h *LogHandlers) HandleStreamLogs(w http.ResponseWriter, r *http.Request) {
+ flusher, ok := w.(http.Flusher)
+ if !ok {
+ http.Error(w, "Streaming not supported", http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Header().Set("Connection", "keep-alive")
+
+ broadcaster := logging.GetBroadcaster()
+ id, ch, history := broadcaster.Subscribe()
+ defer broadcaster.Unsubscribe(id)
+
+ // Send history first
+ for _, line := range history {
+ if _, err := fmt.Fprintf(w, "data: %s\n\n", line); err != nil {
+ return
+ }
+ }
+ flusher.Flush()
+
+ notify := r.Context().Done()
+
+ for {
+ select {
+ case msg, ok := <-ch:
+ if !ok {
+ return
+ }
+ if _, err := fmt.Fprintf(w, "data: %s\n\n", msg); err != nil {
+ return
+ }
+ flusher.Flush()
+ case <-notify:
+ return
+ }
+ }
+}
+
+// HandleDownloadBundle creates a zip file with system logs and sanitized config
+func (h *LogHandlers) HandleDownloadBundle(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/zip")
+ w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"pulse-support-bundle-%s.zip\"", time.Now().Format("20060102-150405")))
+
+ zipWriter := zip.NewWriter(w)
+ defer zipWriter.Close()
+
+ // 1. Add Logs
+ // If a log file is configured and exists, add it.
+ // Otherwise (or in addition), dump the in-memory buffer.
+ addedLogFile := false
+ if h.config.LogFile != "" {
+ if f, err := os.Open(h.config.LogFile); err == nil {
+ defer f.Close()
+ if wr, err := zipWriter.Create("pulse.log"); err == nil {
+ io.Copy(wr, f)
+ addedLogFile = true
+ }
+ }
+ }
+
+ if !addedLogFile {
+ // Dump memory buffer
+ if wr, err := zipWriter.Create("pulse-tail.log"); err == nil {
+ for _, line := range logging.GetBroadcaster().GetHistory() {
+ fmt.Fprintln(wr, line)
+ }
+ }
+ }
+
+ // 2. Add Sanitized System Info
+ if wr, err := zipWriter.Create("system-info.json"); err == nil {
+ // Create a sanitized copy of config
+ sanitizedConfig := h.config.DeepCopy()
+
+ // Scrub top-level secrets
+ if sanitizedConfig.AuthPass != "" {
+ sanitizedConfig.AuthPass = "[REDACTED]"
+ }
+ if sanitizedConfig.APIToken != "" {
+ sanitizedConfig.APIToken = "[REDACTED]"
+ }
+ if sanitizedConfig.ProxyAuthSecret != "" {
+ sanitizedConfig.ProxyAuthSecret = "[REDACTED]"
+ }
+
+ // Scrub array secrets
+ for i := range sanitizedConfig.PVEInstances {
+ sanitizedConfig.PVEInstances[i].Password = "[REDACTED]"
+ sanitizedConfig.PVEInstances[i].TokenValue = "[REDACTED]"
+ }
+ for i := range sanitizedConfig.PBSInstances {
+ sanitizedConfig.PBSInstances[i].Password = "[REDACTED]"
+ sanitizedConfig.PBSInstances[i].TokenValue = "[REDACTED]"
+ }
+ for i := range sanitizedConfig.PMGInstances {
+ sanitizedConfig.PMGInstances[i].Password = "[REDACTED]"
+ sanitizedConfig.PMGInstances[i].TokenValue = "[REDACTED]"
+ }
+
+ // Sanitize environment variables
+ rawEnv := os.Environ()
+ sanitizedEnv := make([]string, 0, len(rawEnv))
+ for _, e := range rawEnv {
+ parts := strings.SplitN(e, "=", 2)
+ if len(parts) == 2 {
+ key := parts[0]
+ upperKey := strings.ToUpper(key)
+ // Redact known sensitive keys
+ if strings.Contains(upperKey, "TOKEN") ||
+ strings.Contains(upperKey, "PASS") ||
+ strings.Contains(upperKey, "SECRET") ||
+ strings.Contains(upperKey, "KEY") {
+ sanitizedEnv = append(sanitizedEnv, fmt.Sprintf("%s=[REDACTED]", key))
+ } else {
+ sanitizedEnv = append(sanitizedEnv, e)
+ }
+ }
+ }
+
+ safeConfig := struct {
+ Version string
+ GoVersion string
+ OS string
+ Config *config.Config
+ Env []string
+ }{
+ Version: "5.x",
+ GoVersion: "go1.21+",
+ OS: "linux",
+ Config: sanitizedConfig,
+ Env: sanitizedEnv,
+ }
+
+ enc := json.NewEncoder(wr)
+ enc.SetIndent("", " ")
+ enc.Encode(safeConfig)
+ }
+}
+
+type SetLogLevelRequest struct {
+ Level string `json:"level"`
+}
+
+// HandleSetLevel changes the log level at runtime and persists it
+func (h *LogHandlers) HandleSetLevel(w http.ResponseWriter, r *http.Request) {
+ var req SetLogLevelRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ level := strings.ToLower(req.Level)
+ validLevels := map[string]bool{"debug": true, "info": true, "warn": true, "error": true}
+ if !validLevels[level] {
+ http.Error(w, "Invalid log level", http.StatusBadRequest)
+ return
+ }
+
+ // 1. Update Runtime
+ logging.SetGlobalLevel(level)
+ h.config.LogLevel = level
+ log.Info().Str("level", level).Msg("Log level updated via API")
+
+ // 2. Persist to system.json
+ if h.persistence != nil {
+ settings, err := h.persistence.LoadSystemSettings()
+ if err == nil {
+ settings.LogLevel = level
+ if err := h.persistence.SaveSystemSettings(*settings); err != nil {
+ log.Error().Err(err).Msg("Failed to persist log level change")
+ // Don't fail the request, runtime update succeeded
+ }
+ }
+ }
+
+ w.WriteHeader(http.StatusOK)
+}
+
+// HandleGetLevel returns the current log level
+func (h *LogHandlers) HandleGetLevel(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(map[string]string{
+ "level": logging.GetGlobalLevel(),
+ })
+}
diff --git a/internal/logging/broadcast.go b/internal/logging/broadcast.go
new file mode 100644
index 0000000000..30692097f6
--- /dev/null
+++ b/internal/logging/broadcast.go
@@ -0,0 +1,125 @@
+package logging
+
+import (
+ "container/ring"
+ "fmt"
+ "sync"
+
+ "github.com/google/uuid"
+ "github.com/rs/zerolog"
+)
+
+// DefaultBufferSize is the number of log lines to keep in memory.
+const (
+ DefaultBufferSize = 1000
+)
+
+var (
+ broadcaster *LogBroadcaster
+ broadcastMu sync.Once
+)
+
+// LogBroadcaster captures log writes, buffers them, and broadcasts them to subscribers.
+type LogBroadcaster struct {
+ mu sync.RWMutex
+ buffer *ring.Ring
+ subscribers map[string]chan string
+}
+
+// GetBroadcaster returns the singleton broadcaster instance.
+func GetBroadcaster() *LogBroadcaster {
+ broadcastMu.Do(func() {
+ broadcaster = &LogBroadcaster{
+ buffer: ring.New(DefaultBufferSize),
+ subscribers: make(map[string]chan string),
+ }
+ })
+ return broadcaster
+}
+
+// Write implements io.Writer. It writes to the internal buffer and notifies subscribers.
+func (b *LogBroadcaster) Write(p []byte) (n int, err error) {
+ // Copy slice to avoid race conditions if p is reused
+ msg := string(p)
+
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ // 1. Write to ring buffer
+ b.buffer.Value = msg
+ b.buffer = b.buffer.Next()
+
+ // 2. Broadcast to subscribers
+ for id, ch := range b.subscribers {
+ select {
+ case ch <- msg:
+ // Sent successfully
+ default:
+ // Channel blocked, too slow consumer.
+ // In a real production system we might drop the client,
+ // here we just skip this message for them to avoid blocking writer.
+ // Ideally we should warn or close their channel.
+ fmt.Printf("logging: subscriber %s blocked, dropping message\n", id)
+ }
+ }
+
+ return len(p), nil
+}
+
+// Subscribe adds a new subscriber and returns a channel of log lines
+// plus a snapshot of the current history.
+func (b *LogBroadcaster) Subscribe() (string, chan string, []string) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ id := uuid.NewString()
+ ch := make(chan string, 1000) // Large buffer to prevent blocking
+ b.subscribers[id] = ch
+
+ // Generate history snapshot
+ history := make([]string, 0, DefaultBufferSize)
+ b.buffer.Do(func(p interface{}) {
+ if p != nil {
+ history = append(history, p.(string))
+ }
+ })
+
+ return id, ch, history
+}
+
+// Unsubscribe removes a subscriber.
+func (b *LogBroadcaster) Unsubscribe(id string) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if ch, ok := b.subscribers[id]; ok {
+ close(ch)
+ delete(b.subscribers, id)
+ }
+}
+
+// GetHistory returns the current in-memory log log history.
+func (b *LogBroadcaster) GetHistory() []string {
+ b.mu.RLock()
+ defer b.mu.RUnlock()
+
+ history := make([]string, 0, DefaultBufferSize)
+ b.buffer.Do(func(p interface{}) {
+ if p != nil {
+ history = append(history, p.(string))
+ }
+ })
+ return history
+}
+
+// SetGlobalLevel updates the global zerolog level at runtime.
+func SetGlobalLevel(level string) {
+ mu.Lock()
+ defer mu.Unlock()
+ zerolog.SetGlobalLevel(parseLevel(level))
+}
+
+// GetGlobalLevel returns the current global level string.
+func GetGlobalLevel() string {
+ return zerolog.GlobalLevel().String()
+}
diff --git a/internal/logging/logging.go b/internal/logging/logging.go
index 60428b1d08..7e7ec045ad 100644
--- a/internal/logging/logging.go
+++ b/internal/logging/logging.go
@@ -81,6 +81,11 @@ func Init(cfg Config) zerolog.Logger {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
writer := selectWriter(cfg.Format)
+
+ // Hook in the in-memory broadcaster for live UI streaming
+ broadcaster := GetBroadcaster()
+ writer = io.MultiWriter(writer, broadcaster)
+
if fileWriter, err := newRollingFileWriter(cfg); err != nil {
fmt.Fprintf(os.Stderr, "logging: unable to configure file output: %v\n", err)
} else if fileWriter != nil {