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
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,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue