f-39: api: /register et /ping, mesure périodique de l'état de l'hôte #39
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
080ce82d0d
commit
9a1e941b15
13 changed files with 537 additions and 8 deletions
78
internal/host/collector.go
Normal file
78
internal/host/collector.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ping struct {
|
||||
State
|
||||
AgentVersion string
|
||||
AgentStartTime int64
|
||||
CollectedAt int64
|
||||
}
|
||||
|
||||
type Collector struct {
|
||||
fsys fs.FS
|
||||
version string
|
||||
start time.Time
|
||||
interval time.Duration
|
||||
logger *slog.Logger
|
||||
now func() time.Time
|
||||
facts Facts
|
||||
factsErr error
|
||||
|
||||
mu sync.RWMutex
|
||||
last *Ping
|
||||
}
|
||||
|
||||
func NewCollector(fsys fs.FS, version string, start time.Time, interval time.Duration, logger *slog.Logger) *Collector {
|
||||
c := &Collector{fsys: fsys, version: version, start: start, interval: interval, logger: logger, now: time.Now}
|
||||
c.facts, c.factsErr = ReadFacts(fsys)
|
||||
if c.factsErr != nil {
|
||||
logger.Error("host facts unavailable", "error", c.factsErr)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Collector) Register() (Facts, error) {
|
||||
return c.facts, c.factsErr
|
||||
}
|
||||
|
||||
func (c *Collector) Ping() (Ping, bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
if c.last == nil {
|
||||
return Ping{}, false
|
||||
}
|
||||
return *c.last, true
|
||||
}
|
||||
|
||||
func (c *Collector) Run(ctx context.Context) {
|
||||
ticker := time.NewTicker(c.interval)
|
||||
defer ticker.Stop()
|
||||
c.collect()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
c.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collect() {
|
||||
s, err := ReadState(c.fsys)
|
||||
if err != nil {
|
||||
c.logger.Error("host state collection failed", "error", err)
|
||||
return
|
||||
}
|
||||
p := Ping{State: s, AgentVersion: c.version, AgentStartTime: c.start.Unix(), CollectedAt: c.now().Unix()}
|
||||
c.mu.Lock()
|
||||
c.last = &p
|
||||
c.mu.Unlock()
|
||||
}
|
||||
102
internal/host/collector_test.go
Normal file
102
internal/host/collector_test.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func discard() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
||||
|
||||
func TestCollector_RegisterServesTheFactsReadAtCreation(t *testing.T) {
|
||||
fsys := debian12()
|
||||
c := NewCollector(fsys, "0.3.0", time.Unix(1792210950, 0), time.Hour, discard())
|
||||
fsys["proc/sys/kernel/hostname"].Data = []byte("autre\n")
|
||||
f, err := c.Register()
|
||||
if err != nil || f.Hostname != "hv1" {
|
||||
t.Errorf("Register = %q, %v ; hv1 lu à la création attendu", f.Hostname, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_RegisterKeepsTheReadError(t *testing.T) {
|
||||
fsys := debian12()
|
||||
delete(fsys, "proc/cpuinfo")
|
||||
var logs strings.Builder
|
||||
c := NewCollector(fsys, "0.3.0", time.Unix(0, 0), time.Hour, slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
if _, err := c.Register(); err == nil {
|
||||
t.Error("Register sans cpuinfo : aucune erreur")
|
||||
}
|
||||
if !strings.Contains(logs.String(), "host facts unavailable") {
|
||||
t.Errorf("erreur non journalisée : %q", logs.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_PingBeforeAnyCollection(t *testing.T) {
|
||||
c := NewCollector(debian12(), "0.3.0", time.Unix(0, 0), time.Hour, discard())
|
||||
if _, ok := c.Ping(); ok {
|
||||
t.Error("Ping avant toute mesure : instantané rendu")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_PingServesTheLastCollection(t *testing.T) {
|
||||
c := NewCollector(debian12(), "0.3.0", time.Unix(1792210950, 0), time.Hour, discard())
|
||||
c.now = func() time.Time { return time.Unix(1793454325, 0) }
|
||||
c.collect()
|
||||
got, ok := c.Ping()
|
||||
want := Ping{
|
||||
State: State{BootTime: 1792210880, UptimeSeconds: 1243445.41, Load: [3]float64{0.08, 0.03, 0.04}},
|
||||
AgentVersion: "0.3.0",
|
||||
AgentStartTime: 1792210950,
|
||||
CollectedAt: 1793454325,
|
||||
}
|
||||
if !ok || got != want {
|
||||
t.Errorf("Ping = %+v, %v ; %+v attendu", got, ok, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_FailedCollectionKeepsThePreviousOne(t *testing.T) {
|
||||
fsys := debian12()
|
||||
var logs strings.Builder
|
||||
c := NewCollector(fsys, "0.3.0", time.Unix(0, 0), time.Hour, slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
c.now = func() time.Time { return time.Unix(100, 0) }
|
||||
c.collect()
|
||||
delete(fsys, "proc/loadavg")
|
||||
c.now = func() time.Time { return time.Unix(200, 0) }
|
||||
c.collect()
|
||||
got, ok := c.Ping()
|
||||
if !ok || got.CollectedAt != 100 {
|
||||
t.Errorf("Ping = %+v, %v ; mesure de 100 gardée attendue", got, ok)
|
||||
}
|
||||
if !strings.Contains(logs.String(), "host state collection failed") {
|
||||
t.Errorf("échec non journalisé : %q", logs.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_RunCollectsAtOnceAndStopsWithTheContext(t *testing.T) {
|
||||
c := NewCollector(debian12(), "0.3.0", time.Unix(0, 0), time.Hour, discard())
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
c.Run(ctx)
|
||||
close(done)
|
||||
}()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
if _, ok := c.Ping(); ok {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("aucune mesure au démarrage de Run")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("Run ne s'arrête pas à l'annulation du contexte")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue