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:
GnomeZworc 2026-10-31 15:19:42 +01:00
commit 9a1e941b15
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
13 changed files with 537 additions and 8 deletions

View file

@ -0,0 +1,56 @@
package agentapi
import (
"encoding/json"
"net/http"
"git.g3e.fr/syonad/two/internal/host"
)
type HostInfo interface {
Register() (host.Facts, error)
Ping() (host.Ping, bool)
}
func (s *Server) RegisterHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodGet {
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
return
}
f, err := s.host.Register()
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(ErrorResponse{Error: "host facts unavailable: " + err.Error()})
return
}
json.NewEncoder(w).Encode(HostRegister{
Hostname: f.Hostname,
CPU: HostCPU{Vendor: f.CPU.Vendor, Model: f.CPU.Model, Sockets: f.CPU.Sockets, Cores: f.CPU.Cores, Threads: f.CPU.Threads},
MemoryBytes: f.MemoryBytes,
OS: HostOS{ID: f.OS.ID, VersionID: f.OS.VersionID, PrettyName: f.OS.PrettyName},
Kernel: f.Kernel,
})
}
func (s *Server) PingHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodGet {
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
return
}
p, ok := s.host.Ping()
if !ok {
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(ErrorResponse{Error: "no host state collected yet"})
return
}
json.NewEncoder(w).Encode(HostPing{
AgentVersion: p.AgentVersion,
CollectedAt: p.CollectedAt,
BootTime: p.BootTime,
AgentStartTime: p.AgentStartTime,
UptimeSeconds: p.UptimeSeconds,
Load: p.Load,
})
}