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
138
api/agent.yaml
138
api/agent.yaml
|
|
@ -489,6 +489,63 @@ paths:
|
|||
"500":
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
# ── Host ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/register:
|
||||
get:
|
||||
summary: Facts of the host that do not change while it runs
|
||||
operationId: getRegister
|
||||
description: >-
|
||||
Read once when the agent starts and kept in memory. Will also be sent to the broker of the
|
||||
orchestrator at startup.
|
||||
responses:
|
||||
"200":
|
||||
description: Host facts
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/HostRegister"
|
||||
"405":
|
||||
description: Method other than GET
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"503":
|
||||
description: Host facts could not be read when the agent started (the error says why)
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
|
||||
/ping:
|
||||
get:
|
||||
summary: Current state of the host and of the agent
|
||||
operationId: getPing
|
||||
description: >-
|
||||
Last reading, taken when the agent starts then every times.host_state_interval seconds and
|
||||
kept in memory; collected_at gives its age. boot_time and agent_start_time tell a host
|
||||
reboot from an agent restart; boot_time plus uptime_seconds give the clock of the host.
|
||||
responses:
|
||||
"200":
|
||||
description: Last host state
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/HostPing"
|
||||
"405":
|
||||
description: Method other than GET
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"503":
|
||||
description: No reading has succeeded yet
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
|
||||
components:
|
||||
|
||||
parameters:
|
||||
|
|
@ -932,6 +989,87 @@ components:
|
|||
type: string
|
||||
example: "203.0.113.1"
|
||||
|
||||
HostRegister:
|
||||
type: object
|
||||
properties:
|
||||
hostname:
|
||||
type: string
|
||||
example: hv1
|
||||
cpu:
|
||||
type: object
|
||||
properties:
|
||||
vendor:
|
||||
type: string
|
||||
example: AuthenticAMD
|
||||
model:
|
||||
type: string
|
||||
example: AMD EPYC 7272 12-Core Processor
|
||||
sockets:
|
||||
type: integer
|
||||
example: 1
|
||||
cores:
|
||||
type: integer
|
||||
description: Physical cores, all sockets together
|
||||
example: 12
|
||||
threads:
|
||||
type: integer
|
||||
description: Logical processors, all sockets together
|
||||
example: 24
|
||||
memory_bytes:
|
||||
type: integer
|
||||
format: int64
|
||||
example: 134217728000
|
||||
os:
|
||||
type: object
|
||||
description: From /etc/os-release
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
example: debian
|
||||
version_id:
|
||||
type: string
|
||||
example: "12"
|
||||
pretty_name:
|
||||
type: string
|
||||
example: Debian GNU/Linux 12 (bookworm)
|
||||
kernel:
|
||||
type: string
|
||||
example: 6.1.0-26-amd64
|
||||
|
||||
HostPing:
|
||||
type: object
|
||||
properties:
|
||||
agent_version:
|
||||
type: string
|
||||
description: Release tag, commit for a lab build, undefined when not set at build time
|
||||
example: 0.3.0
|
||||
collected_at:
|
||||
type: integer
|
||||
format: int64
|
||||
description: Unix time (seconds) of the reading
|
||||
example: 1793520853
|
||||
boot_time:
|
||||
type: integer
|
||||
format: int64
|
||||
description: Unix time (seconds) the host booted
|
||||
example: 1793499123
|
||||
agent_start_time:
|
||||
type: integer
|
||||
format: int64
|
||||
description: Unix time (seconds) the agent started
|
||||
example: 1793499190
|
||||
uptime_seconds:
|
||||
type: number
|
||||
example: 21730.4
|
||||
load:
|
||||
type: array
|
||||
description: Load average over 1, 5 and 15 minutes
|
||||
minItems: 3
|
||||
maxItems: 3
|
||||
items:
|
||||
type: number
|
||||
example: [0.12, 0.3, 0.25]
|
||||
|
||||
Error:
|
||||
type: object
|
||||
properties:
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||
"git.g3e.fr/syonad/two/internal/frr"
|
||||
"git.g3e.fr/syonad/two/internal/host"
|
||||
"git.g3e.fr/syonad/two/internal/migration"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent"
|
||||
|
|
@ -28,6 +29,8 @@ import (
|
|||
|
||||
const shutdownTimeout = 20 * time.Second
|
||||
|
||||
var version = "undefined"
|
||||
|
||||
func main() {
|
||||
confFile := flag.String("config", "/etc/two/agent.yml", "config file path")
|
||||
flag.Parse()
|
||||
|
|
@ -35,6 +38,7 @@ func main() {
|
|||
}
|
||||
|
||||
func run(confFile string) int {
|
||||
start := time.Now()
|
||||
cfg, err := configuration.LoadConfig(confFile)
|
||||
if err != nil {
|
||||
slog.Error("failed to load config", "error", err)
|
||||
|
|
@ -96,6 +100,7 @@ func run(confFile string) int {
|
|||
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
||||
|
||||
log.Info("starting agent",
|
||||
"version", version,
|
||||
"api", apiAddr,
|
||||
"prometheus", promAddr,
|
||||
"workers", cfg.Worker.Count,
|
||||
|
|
@ -108,7 +113,10 @@ func run(confFile string) int {
|
|||
|
||||
d := dispatcher.New(q, db, cfg, log.With(slog.String("component", "dispatcher")))
|
||||
|
||||
apiSrv := agentapi.New(d, db, log.With(slog.String("component", "api")), apiAddr)
|
||||
hostInfo := host.NewCollector(os.DirFS("/"), version, start, cfg.Times.HostState(), log.With(slog.String("component", "host")))
|
||||
go hostInfo.Run(ctx)
|
||||
|
||||
apiSrv := agentapi.New(d, db, log.With(slog.String("component", "api")), apiAddr, hostInfo)
|
||||
go apiSrv.Start()
|
||||
|
||||
promSrv := promserver.New(promAddr, registry)
|
||||
|
|
|
|||
|
|
@ -35,6 +35,8 @@ times:
|
|||
timeout_vms_start: 300
|
||||
# Interval between each state check
|
||||
poll_interval: 2
|
||||
# Interval between two readings of the host state served by GET /ping
|
||||
host_state_interval: 10
|
||||
|
||||
# Bridge interface used when the requested iface_type is not found in interfaces
|
||||
default_interface: br-000000
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ func newTestServerWith(t *testing.T, cfg *configuration.Config) (*Server, *badge
|
|||
q := worker.New(100)
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
d := dispatcher.New(q, db, cfg, logger)
|
||||
return New(d, db, logger, "127.0.0.1:0"), db
|
||||
return New(d, db, logger, "127.0.0.1:0", &fakeHost{}), db
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
|||
56
internal/api/agent/host.go
Normal file
56
internal/api/agent/host.go
Normal 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,
|
||||
})
|
||||
}
|
||||
103
internal/api/agent/host_test.go
Normal file
103
internal/api/agent/host_test.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package agentapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/host"
|
||||
)
|
||||
|
||||
type fakeHost struct {
|
||||
facts host.Facts
|
||||
factsErr error
|
||||
ping *host.Ping
|
||||
}
|
||||
|
||||
func (f *fakeHost) Register() (host.Facts, error) { return f.facts, f.factsErr }
|
||||
|
||||
func (f *fakeHost) Ping() (host.Ping, bool) {
|
||||
if f.ping == nil {
|
||||
return host.Ping{}, false
|
||||
}
|
||||
return *f.ping, true
|
||||
}
|
||||
|
||||
func serveHost(t *testing.T, h *fakeHost, method, path string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
s, _ := newTestServer(t)
|
||||
s.host = h
|
||||
w := httptest.NewRecorder()
|
||||
s.srv.Handler.ServeHTTP(w, httptest.NewRequest(method, path, nil))
|
||||
return w
|
||||
}
|
||||
|
||||
func TestRegister_ServesTheFacts(t *testing.T) {
|
||||
h := &fakeHost{facts: host.Facts{
|
||||
Hostname: "hv1",
|
||||
CPU: host.CPU{Vendor: "AuthenticAMD", Model: "AMD EPYC 7272 12-Core Processor", Sockets: 1, Cores: 12, Threads: 24},
|
||||
MemoryBytes: 134217728000,
|
||||
OS: host.OS{ID: "debian", VersionID: "12", PrettyName: "Debian GNU/Linux 12 (bookworm)"},
|
||||
Kernel: "6.1.0-26-amd64",
|
||||
}}
|
||||
w := serveHost(t, h, http.MethodGet, "/register")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("attendu 200, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
want := `{"hostname":"hv1","cpu":{"vendor":"AuthenticAMD","model":"AMD EPYC 7272 12-Core Processor","sockets":1,"cores":12,"threads":24},"memory_bytes":134217728000,"os":{"id":"debian","version_id":"12","pretty_name":"Debian GNU/Linux 12 (bookworm)"},"kernel":"6.1.0-26-amd64"}`
|
||||
if got := strings.TrimSpace(w.Body.String()); got != want {
|
||||
t.Errorf("corps :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegister_UnavailableFacts(t *testing.T) {
|
||||
w := serveHost(t, &fakeHost{factsErr: errors.New("open proc/cpuinfo: file does not exist")}, http.MethodGet, "/register")
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("attendu 503, obtenu %d", w.Code)
|
||||
}
|
||||
var e ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&e); err != nil || e.Error != "host facts unavailable: open proc/cpuinfo: file does not exist" {
|
||||
t.Errorf("erreur %q, %v", e.Error, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPing_ServesTheLastState(t *testing.T) {
|
||||
h := &fakeHost{ping: &host.Ping{
|
||||
State: host.State{BootTime: 1793499123, UptimeSeconds: 21730.4, Load: [3]float64{0.12, 0.3, 0.25}},
|
||||
AgentVersion: "0.3.0",
|
||||
AgentStartTime: 1793499190,
|
||||
CollectedAt: 1793520853,
|
||||
}}
|
||||
w := serveHost(t, h, http.MethodGet, "/ping")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("attendu 200, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
want := `{"agent_version":"0.3.0","collected_at":1793520853,"boot_time":1793499123,"agent_start_time":1793499190,"uptime_seconds":21730.4,"load":[0.12,0.3,0.25]}`
|
||||
if got := strings.TrimSpace(w.Body.String()); got != want {
|
||||
t.Errorf("corps :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPing_BeforeTheFirstCollection(t *testing.T) {
|
||||
w := serveHost(t, &fakeHost{}, http.MethodGet, "/ping")
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("attendu 503, obtenu %d", w.Code)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "no host state collected yet") {
|
||||
t.Errorf("corps %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostRoutes_ReadOnly(t *testing.T) {
|
||||
h := &fakeHost{ping: &host.Ping{}}
|
||||
for _, path := range []string{"/register", "/ping"} {
|
||||
for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodDelete} {
|
||||
if w := serveHost(t, h, method, path); w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("%s %s : attendu 405, obtenu %d", method, path, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,3 +123,34 @@ type FloatingIP struct {
|
|||
IP string `json:"ip,omitempty"`
|
||||
Gateway string `json:"gateway,omitempty"`
|
||||
}
|
||||
|
||||
type HostCPU struct {
|
||||
Vendor string `json:"vendor"`
|
||||
Model string `json:"model"`
|
||||
Sockets int `json:"sockets"`
|
||||
Cores int `json:"cores"`
|
||||
Threads int `json:"threads"`
|
||||
}
|
||||
|
||||
type HostOS struct {
|
||||
ID string `json:"id"`
|
||||
VersionID string `json:"version_id"`
|
||||
PrettyName string `json:"pretty_name"`
|
||||
}
|
||||
|
||||
type HostRegister struct {
|
||||
Hostname string `json:"hostname"`
|
||||
CPU HostCPU `json:"cpu"`
|
||||
MemoryBytes uint64 `json:"memory_bytes"`
|
||||
OS HostOS `json:"os"`
|
||||
Kernel string `json:"kernel"`
|
||||
}
|
||||
|
||||
type HostPing struct {
|
||||
AgentVersion string `json:"agent_version"`
|
||||
CollectedAt int64 `json:"collected_at"`
|
||||
BootTime int64 `json:"boot_time"`
|
||||
AgentStartTime int64 `json:"agent_start_time"`
|
||||
UptimeSeconds float64 `json:"uptime_seconds"`
|
||||
Load [3]float64 `json:"load"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,11 +17,12 @@ type Server struct {
|
|||
dispatcher *dispatcher.Dispatcher
|
||||
db *badger.DB
|
||||
logger *slog.Logger
|
||||
host HostInfo
|
||||
srv *http.Server
|
||||
}
|
||||
|
||||
func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address string) *Server {
|
||||
s := &Server{dispatcher: d, db: db, logger: logger}
|
||||
func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address string, h HostInfo) *Server {
|
||||
s := &Server{dispatcher: d, db: db, logger: logger, host: h}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/vpcs", s.VpcsHandler)
|
||||
mux.HandleFunc("/vpcs/", s.VpcByNameHandler)
|
||||
|
|
@ -33,6 +34,8 @@ func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address s
|
|||
mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler)
|
||||
mux.HandleFunc("/floating-ips", s.FloatingIPsHandler)
|
||||
mux.HandleFunc("/floating-ips/", s.FloatingIPByNameHandler)
|
||||
mux.HandleFunc("/register", s.RegisterHandler)
|
||||
mux.HandleFunc("/ping", s.PingHandler)
|
||||
s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)}
|
||||
return s
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,6 +78,7 @@ func LoadConfig(path string) (*Config, error) {
|
|||
v.SetDefault("times.timeout_subnet_start", TimeoutSubnetStart)
|
||||
v.SetDefault("times.timeout_vms_start", TimeoutVMsStart)
|
||||
v.SetDefault("times.poll_interval", PollInterval)
|
||||
v.SetDefault("times.host_state_interval", HostStateInterval)
|
||||
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
||||
v.SetDefault("dhcp.backend", BackendTwo)
|
||||
v.SetDefault("hostsvc.dns_upstream", []string{})
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ const (
|
|||
TimeoutSubnetStart = 300
|
||||
TimeoutVMsStart = 300
|
||||
PollInterval = 2
|
||||
HostStateInterval = 10
|
||||
)
|
||||
|
||||
type Times struct {
|
||||
|
|
@ -23,6 +24,7 @@ type Times struct {
|
|||
TimeoutSubnetStart int `mapstructure:"timeout_subnet_start"`
|
||||
TimeoutVMsStart int `mapstructure:"timeout_vms_start"`
|
||||
PollInterval int `mapstructure:"poll_interval"`
|
||||
HostStateInterval int `mapstructure:"host_state_interval"`
|
||||
}
|
||||
|
||||
func seconds(value, fallback int) time.Duration {
|
||||
|
|
@ -39,6 +41,7 @@ func (t Times) VPCStop() time.Duration { return seconds(t.TimeoutVPCStop, Ti
|
|||
func (t Times) SubnetStart() time.Duration { return seconds(t.TimeoutSubnetStart, TimeoutSubnetStart) }
|
||||
func (t Times) VMsStart() time.Duration { return seconds(t.TimeoutVMsStart, TimeoutVMsStart) }
|
||||
func (t Times) Poll() time.Duration { return seconds(t.PollInterval, PollInterval) }
|
||||
func (t Times) HostState() time.Duration { return seconds(t.HostStateInterval, HostStateInterval) }
|
||||
|
||||
func (t Times) Validate() error {
|
||||
for _, f := range []struct {
|
||||
|
|
@ -52,6 +55,7 @@ func (t Times) Validate() error {
|
|||
{"timeout_subnet_start", t.TimeoutSubnetStart},
|
||||
{"timeout_vms_start", t.TimeoutVMsStart},
|
||||
{"poll_interval", t.PollInterval},
|
||||
{"host_state_interval", t.HostStateInterval},
|
||||
} {
|
||||
if f.value <= 0 {
|
||||
return fmt.Errorf("times.%s %d must be a positive number of seconds", f.key, f.value)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ func TestLoadConfig_TimesDefauts(t *testing.T) {
|
|||
TimeoutSubnetStart: 300,
|
||||
TimeoutVMsStart: 300,
|
||||
PollInterval: 2,
|
||||
HostStateInterval: 10,
|
||||
}
|
||||
if cfg.Times != want {
|
||||
t.Errorf("times par défaut : %+v, attendu %+v", cfg.Times, want)
|
||||
|
|
@ -26,11 +27,11 @@ func TestLoadConfig_TimesDefauts(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLoadConfig_TimesValeursExplicites(t *testing.T) {
|
||||
cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n"))
|
||||
cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n host_state_interval: 5\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig a échoué : %v", err)
|
||||
}
|
||||
want := Times{600, 10, 700, 800, 50, 60, 1}
|
||||
want := Times{600, 10, 700, 800, 50, 60, 1, 5}
|
||||
if cfg.Times != want {
|
||||
t.Errorf("times : %+v, attendu %+v", cfg.Times, want)
|
||||
}
|
||||
|
|
@ -55,6 +56,7 @@ func TestLoadConfig_TimesInvalidesRefuses(t *testing.T) {
|
|||
{"times:\n timeout_subnet_start: 0\n", "times.timeout_subnet_start 0"},
|
||||
{"times:\n timeout_vms_start: 0\n", "times.timeout_vms_start 0"},
|
||||
{"times:\n poll_interval: 0\n", "times.poll_interval 0"},
|
||||
{"times:\n host_state_interval: 0\n", "times.host_state_interval 0"},
|
||||
{"times:\n next_vms_shutdown: 300\n", "times.next_vms_shutdown 300 must be below times.timeout_vms_stop 300"},
|
||||
} {
|
||||
if _, err := LoadConfig(writeYAML(t, c.yaml)); err == nil || !strings.Contains(err.Error(), c.msg) {
|
||||
|
|
@ -79,14 +81,15 @@ func TestTimesDurees(t *testing.T) {
|
|||
{"SubnetStart", zero.SubnetStart(), 300 * time.Second},
|
||||
{"VMsStart", zero.VMsStart(), 300 * time.Second},
|
||||
{"Poll", zero.Poll(), 2 * time.Second},
|
||||
{"HostState", zero.HostState(), 10 * time.Second},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if c.got != c.want {
|
||||
t.Errorf("Times{}.%s() = %v, attendu %v", c.name, c.got, c.want)
|
||||
}
|
||||
}
|
||||
set := Times{11, 12, 13, 14, 15, 16, 17}
|
||||
got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll()}
|
||||
set := Times{11, 12, 13, 14, 15, 16, 17, 18}
|
||||
got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll(), set.HostState()}
|
||||
for i, d := range got {
|
||||
if want := time.Duration(11+i) * time.Second; d != want {
|
||||
t.Errorf("durée %d = %v, attendu %v", i, d, want)
|
||||
|
|
|
|||
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