97074e7a0c
- Add heartbeat ping (every 5 min, controller alive signal) - Add backup integrity check (weekly restic check, Sunday 04:00) - Add Heartbeat + BackupIntegrity fields to PingUUIDsConfig - Add HubConfig for central hub reporting - Add report package (types, builder, pusher) for hub push - Wire hub reporting into scheduler (configurable interval) - Update controller.yaml.example with new monitoring + hub sections - Add monitoring/DEPRECATED.md for legacy bash scripts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package report
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
|
|
)
|
|
|
|
// Pusher sends reports to the central hub.
|
|
type Pusher struct {
|
|
hubURL string
|
|
apiKey string
|
|
httpClient *http.Client
|
|
logger *log.Logger
|
|
enabled bool
|
|
}
|
|
|
|
// NewPusher creates a new report pusher from hub configuration.
|
|
func NewPusher(cfg *config.HubConfig, logger *log.Logger) *Pusher {
|
|
return &Pusher{
|
|
hubURL: strings.TrimRight(cfg.URL, "/"),
|
|
apiKey: cfg.APIKey,
|
|
httpClient: &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
},
|
|
logger: logger,
|
|
enabled: cfg.Enabled,
|
|
}
|
|
}
|
|
|
|
// Push sends a report to the hub. Retries 3 times with 5s backoff.
|
|
// Never returns error to caller — push failures should not affect controller operation.
|
|
func (p *Pusher) Push(report *Report) error {
|
|
if !p.enabled {
|
|
return nil
|
|
}
|
|
|
|
data, err := json.Marshal(report)
|
|
if err != nil {
|
|
p.logger.Printf("[WARN] Hub report marshal failed: %v", err)
|
|
return nil
|
|
}
|
|
|
|
url := p.hubURL + "/api/v1/report"
|
|
|
|
var lastErr error
|
|
for attempt := 0; attempt < 3; attempt++ {
|
|
if attempt > 0 {
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if p.apiKey != "" {
|
|
req.Header.Set("Authorization", "Bearer "+p.apiKey)
|
|
}
|
|
|
|
resp, err := p.httpClient.Do(req)
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
io.Copy(io.Discard, resp.Body)
|
|
resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
|
p.logger.Printf("[INFO] Hub report pushed successfully (%d bytes)", len(data))
|
|
return nil
|
|
}
|
|
lastErr = fmt.Errorf("HTTP %d", resp.StatusCode)
|
|
}
|
|
|
|
p.logger.Printf("[WARN] Hub report push failed after 3 attempts: %v", lastErr)
|
|
return nil
|
|
}
|