From 97d34424cacf950d12515a6a501769a303aa4276 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Wed, 7 Aug 2024 21:16:11 +0200 Subject: [PATCH] prometheus monitoring now more complete. Including a guid to uniquely identify agents and clients. --- cmd/converge/prometheus.go | 22 ++++++++++++++-------- cmd/templaterender/render.go | 4 ++++ pkg/models/agent.go | 1 + pkg/models/client.go | 1 + pkg/server/converge/admin.go | 3 +++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd/converge/prometheus.go b/cmd/converge/prometheus.go index 48fd9b3..e67bc26 100644 --- a/cmd/converge/prometheus.go +++ b/cmd/converge/prometheus.go @@ -7,7 +7,6 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "log" "net/http" - "strconv" ) const NAMESPACE = "converge" @@ -30,29 +29,36 @@ var ( Name: "agent_info", Help: "A flexible gauge with dynamic labels, always set to 1", }, - []string{"id", "os"}, // Label names - ) + []string{"guid", "id", "username", "hostname", "pwd", "os", "shell"}) // Label names + clientInfo = promauto.NewGaugeVec( prometheus.GaugeOpts{ Namespace: NAMESPACE, Name: "client_info", Help: "A flexible gauge with dynamic labels, always set to 1", }, - []string{"id", "agentid"}, // Label names + []string{"guid", "id", "agentid", "sessiontype"}, // Label names ) ) func agentLabels(agent models.Agent) prometheus.Labels { return prometheus.Labels{ - "id": agent.PublicId, - "os": agent.AgentInfo.OS, + "guid": agent.Guid, + "id": agent.PublicId, + "username": agent.AgentInfo.Username, + "hostname": agent.AgentInfo.Hostname, + "pwd": agent.AgentInfo.Pwd, + "os": agent.AgentInfo.OS, + "shell": agent.AgentInfo.Shell, } } func clientLabels(client models.Client) prometheus.Labels { return prometheus.Labels{ - "id": strconv.Itoa(client.ClientId), - "agentid": client.PublicId, + "guid": client.Guid, + "id": client.ClientId, + "agentid": client.PublicId, + "sessiontype": client.SessionType, } } diff --git a/cmd/templaterender/render.go b/cmd/templaterender/render.go index a92ba10..c2018a8 100644 --- a/cmd/templaterender/render.go +++ b/cmd/templaterender/render.go @@ -7,8 +7,10 @@ import ( templates2 "converge/pkg/server/templates" "github.com/a-h/templ" "log" + "math/rand" "os" "path/filepath" + "strconv" "time" ) @@ -79,6 +81,7 @@ func main() { state := models.State{} agent := models.Agent{ + Guid: strconv.Itoa(rand.Int()), PublicId: "id", StartTime: time.Now().In(japan), AgentInfo: comms.AgentInfo{ @@ -92,6 +95,7 @@ func main() { } state.Agents = append(state.Agents, agent) client := models.Client{ + Guid: strconv.Itoa(rand.Int()), PublicId: "c1", ClientId: "3", StartTime: time.Now().In(japan), diff --git a/pkg/models/agent.go b/pkg/models/agent.go index 5660039..e27954c 100644 --- a/pkg/models/agent.go +++ b/pkg/models/agent.go @@ -6,6 +6,7 @@ import ( ) type Agent struct { + Guid string PublicId string StartTime time.Time diff --git a/pkg/models/client.go b/pkg/models/client.go index 9861537..14756a6 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -5,6 +5,7 @@ import ( ) type Client struct { + Guid string PublicId string ClientId string StartTime time.Time diff --git a/pkg/server/converge/admin.go b/pkg/server/converge/admin.go index 711841e..2d23580 100644 --- a/pkg/server/converge/admin.go +++ b/pkg/server/converge/admin.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "log" + "math/rand" "net" "strconv" "strings" @@ -33,6 +34,7 @@ type ClientConnection struct { func NewAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.AgentInfo) *AgentConnection { return &AgentConnection{ Agent: models.Agent{ + Guid: strconv.Itoa(rand.Int()), PublicId: publicId, StartTime: time.Now(), AgentInfo: agentInfo, @@ -45,6 +47,7 @@ func NewClient(publicId string, clientConn iowrappers2.ReadWriteAddrCloser, agentConn net.Conn) *ClientConnection { return &ClientConnection{ Client: models.Client{ + Guid: strconv.Itoa(rand.Int()), PublicId: publicId, ClientId: strconv.Itoa(clientIdGenerator.IncrementAndGet()), StartTime: time.Now(),