diff --git a/agent/src/main.rs b/agent/src/main.rs index 8f4bbb3..727579a 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -68,6 +68,7 @@ fn main() { hostname: hostname.clone(), ip: ip.clone(), status: "online".to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), ..Default::default() }; diff --git a/agent/src/payload.rs b/agent/src/payload.rs index 79d2f52..fca810a 100644 --- a/agent/src/payload.rs +++ b/agent/src/payload.rs @@ -5,6 +5,7 @@ pub struct AgentMetrics { pub hostname: String, pub ip: String, pub status: String, + pub version: String, pub cpu_percent: Option, pub memory_used: Option, pub memory_free: Option, diff --git a/dashboard/js/popups.js b/dashboard/js/popups.js index d1158b1..7bba143 100644 --- a/dashboard/js/popups.js +++ b/dashboard/js/popups.js @@ -119,6 +119,9 @@ const Popups = (() => {
HOSTNAME
${esc(agent.hostname)}
ADRESSE IP
${esc(agent.ip) || '—'}
+
VERSION AGENT
+ ${agent.version ? `v${esc(agent.version)}` : ''} +
PROTOCOLES ACTIFS
${protos || '—'}
DERNIER CONTACT
${new Date(agent.last_seen * 1000).toLocaleTimeString('fr-FR')}
diff --git a/server/db/db.go b/server/db/db.go index b54ed0b..d14f36e 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -20,7 +20,7 @@ const schema = ` CREATE TABLE IF NOT EXISTS agents ( id TEXT PRIMARY KEY, hostname TEXT NOT NULL, ip TEXT NOT NULL DEFAULT '', status TEXT NOT NULL DEFAULT 'offline', - last_seen INTEGER NOT NULL DEFAULT 0 + last_seen INTEGER NOT NULL DEFAULT 0, version TEXT NOT NULL DEFAULT '' ); CREATE TABLE IF NOT EXISTS metrics ( id INTEGER PRIMARY KEY AUTOINCREMENT, agent_id TEXT NOT NULL, ts INTEGER NOT NULL, @@ -57,8 +57,12 @@ func Open(path string) (*DB, error) { } func (d *DB) migrate() error { - _, err := d.conn.Exec(schema) - return err + if _, err := d.conn.Exec(schema); err != nil { + return err + } + // Migrations additives pour les colonnes ajoutées après la création initiale + _, _ = d.conn.Exec(`ALTER TABLE agents ADD COLUMN version TEXT NOT NULL DEFAULT ''`) + return nil } func (d *DB) Close() { _ = d.conn.Close() } @@ -66,11 +70,12 @@ func (d *DB) Close() { _ = d.conn.Close() } func (d *DB) UpsertAgent(m *models.AgentMetrics) error { ts := time.Now().Unix() _, err := d.conn.Exec(` - INSERT INTO agents (id, hostname, ip, status, last_seen) - VALUES (?, ?, ?, ?, ?) + INSERT INTO agents (id, hostname, ip, status, last_seen, version) + VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET - ip=excluded.ip, status=excluded.status, last_seen=excluded.last_seen`, - m.Hostname, m.Hostname, m.IP, m.Status, ts) + ip=excluded.ip, status=excluded.status, last_seen=excluded.last_seen, + version=CASE WHEN excluded.version != '' THEN excluded.version ELSE version END`, + m.Hostname, m.Hostname, m.IP, m.Status, ts, m.Version) return err } @@ -104,7 +109,7 @@ func (d *DB) InsertMetrics(m *models.AgentMetrics) error { } func (d *DB) GetAgents() ([]models.Agent, error) { - rows, err := d.conn.Query(`SELECT id, hostname, ip, status, last_seen FROM agents`) + rows, err := d.conn.Query(`SELECT id, hostname, ip, status, last_seen, version FROM agents`) if err != nil { return nil, err } @@ -112,7 +117,7 @@ func (d *DB) GetAgents() ([]models.Agent, error) { var agents []models.Agent for rows.Next() { var a models.Agent - if err := rows.Scan(&a.ID, &a.Hostname, &a.IP, &a.Status, &a.LastSeen); err != nil { + if err := rows.Scan(&a.ID, &a.Hostname, &a.IP, &a.Status, &a.LastSeen, &a.Version); err != nil { return nil, err } agents = append(agents, a) diff --git a/server/models/models.go b/server/models/models.go index ae9433d..60463aa 100644 --- a/server/models/models.go +++ b/server/models/models.go @@ -4,6 +4,7 @@ type AgentMetrics struct { Hostname string `json:"hostname"` IP string `json:"ip"` Status string `json:"status"` + Version string `json:"version"` CPUPercent *float64 `json:"cpu_percent"` MemoryUsed *int64 `json:"memory_used"` MemoryFree *int64 `json:"memory_free"` @@ -32,6 +33,7 @@ type Agent struct { IP string `json:"ip"` Status string `json:"status"` LastSeen int64 `json:"last_seen"` + Version string `json:"version,omitempty"` LastMetrics *AgentMetrics `json:"last_metrics,omitempty"` }