fix: heartbeat system_data als RawMessage — Windows/beliebige Plattformen ohne Datenverlust
This commit is contained in:
parent
f7bbe3e4b1
commit
2fe83e261a
@ -257,7 +257,7 @@ func (h *Handler) heartbeat(w http.ResponseWriter, r *http.Request) {
|
||||
h.db.UpdateAgentAPIKey(req.AgentID, keyInfo.Key)
|
||||
}
|
||||
|
||||
if err := h.db.SaveSystemData(req.AgentID, &req.SystemData); err != nil {
|
||||
if err := h.db.SaveSystemDataRaw(req.AgentID, req.SystemData); err != nil {
|
||||
if strings.Contains(err.Error(), "nicht gefunden") {
|
||||
writeError(w, http.StatusNotFound, "Agent nicht gefunden")
|
||||
return
|
||||
|
||||
@ -354,6 +354,52 @@ func (d *Database) RegisterAgent(agent *models.Agent) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// SaveSystemDataRaw speichert rohe JSON-Systemdaten (plattformunabhaengig)
|
||||
func (d *Database) SaveSystemDataRaw(agentID string, raw json.RawMessage) error {
|
||||
now := time.Now().UTC()
|
||||
|
||||
// Leeres JSON als leeres Objekt behandeln
|
||||
if len(raw) == 0 {
|
||||
raw = json.RawMessage("{}")
|
||||
}
|
||||
|
||||
tx, err := d.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
res, err := tx.Exec("UPDATE agents SET last_heartbeat = $1 WHERE id = $2", now, agentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, _ := res.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("Agent %s nicht gefunden", agentID)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO system_data (agent_id, data_json, updated_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT(agent_id) DO UPDATE SET
|
||||
data_json=EXCLUDED.data_json,
|
||||
updated_at=EXCLUDED.updated_at
|
||||
`, agentID, string(raw), now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Metriken nur fuer OPNsense/Linux (haben bekannte Struct-Form)
|
||||
var sd models.SystemData
|
||||
if err2 := json.Unmarshal(raw, &sd); err2 == nil && sd.CPU.Threads > 0 {
|
||||
if err2 := d.writeMetrics(tx, agentID, now, &sd); err2 != nil {
|
||||
log.Printf("Metriken schreiben Warnung: %v", err2)
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (d *Database) SaveSystemData(agentID string, data *models.SystemData) error {
|
||||
now := time.Now().UTC()
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package models
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// SystemData - Alle Systemdaten die der Agent sendet
|
||||
type SystemData struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
@ -291,10 +293,10 @@ type CaddyData struct {
|
||||
|
||||
// HeartbeatRequest
|
||||
type HeartbeatRequest struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
AgentVersion string `json:"agent_version,omitempty"`
|
||||
Platform string `json:"platform,omitempty"`
|
||||
SystemData SystemData `json:"system_data"`
|
||||
AgentID string `json:"agent_id"`
|
||||
AgentVersion string `json:"agent_version,omitempty"`
|
||||
Platform string `json:"platform,omitempty"`
|
||||
SystemData json.RawMessage `json:"system_data"`
|
||||
}
|
||||
|
||||
// HeartbeatResponse
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user