Added agent_start_time, agent_stop_time, client_start_time, and client_stop_time metrics.

This commit is contained in:
Erik Brakkee 2024-08-10 20:00:40 +02:00
parent c2ec1ce117
commit 1be96d8742

View File

@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"log" "log"
"net/http" "net/http"
"time"
) )
const NAMESPACE = "converge" const NAMESPACE = "converge"
@ -39,6 +40,28 @@ var (
Help: "Current number of clients", Help: "Current number of clients",
}) })
agentStartTime = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "agent_start_time_seconds",
Help: "Time the agent started",
}, []string{"agent_guid"})
clientStartTime = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "client_start_time_seconds",
Help: "Time the client started",
}, []string{"client_guid"})
agentStopTime = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "agent_stop_time_seconds",
Help: "Time the agent stopped",
}, []string{"agent_guid"})
clientStopTime = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "client_stop_time_seconds",
Help: "Time the client stopped",
}, []string{"client_guid"})
agentInfo = promauto.NewGaugeVec( agentInfo = promauto.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Namespace: NAMESPACE, Namespace: NAMESPACE,
@ -46,13 +69,13 @@ var (
Help: "A flexible gauge with dynamic labels, always set to 1", Help: "A flexible gauge with dynamic labels, always set to 1",
}, },
[]string{ []string{
"guid", "agent_guid",
"id", "agent_id",
"username", "agent_username",
"hostname", "agent_hostname",
"pwd", "agent_pwd",
"os", "agent_os",
"shell", "agent_shell",
}) })
clientInfo = promauto.NewGaugeVec( clientInfo = promauto.NewGaugeVec(
@ -61,44 +84,44 @@ var (
Name: "client_info", Name: "client_info",
Help: "A flexible gauge with dynamic labels, always set to 1", Help: "A flexible gauge with dynamic labels, always set to 1",
}, },
[]string{"guid", []string{"client_guid",
"id", "client_id",
"agentid", "agent_id",
"agentguid", "agent_guid",
"sessiontype", "client_sessiontype",
"username", "client_username",
"hostname", "client_hostname",
"pwd", "client_pwd",
"os", "client_os",
"shell", "client_shell",
}, // Label names }, // Label names
) )
) )
func agentLabels(agent models.Agent) prometheus.Labels { func agentLabels(agent models.Agent) prometheus.Labels {
return prometheus.Labels{ return prometheus.Labels{
"guid": agent.Guid, "agent_guid": agent.Guid,
"id": agent.PublicId, "agent_id": agent.PublicId,
"username": agent.EnvironmentInfo.Username, "agent_username": agent.EnvironmentInfo.Username,
"hostname": agent.EnvironmentInfo.Hostname, "agent_hostname": agent.EnvironmentInfo.Hostname,
"pwd": agent.EnvironmentInfo.Pwd, "agent_pwd": agent.EnvironmentInfo.Pwd,
"os": agent.EnvironmentInfo.OS, "agent_os": agent.EnvironmentInfo.OS,
"shell": agent.EnvironmentInfo.Shell, "agent_shell": agent.EnvironmentInfo.Shell,
} }
} }
func clientLabels(client models.Client) prometheus.Labels { func clientLabels(client models.Client) prometheus.Labels {
return prometheus.Labels{ return prometheus.Labels{
"guid": client.Guid, "client_guid": client.Guid,
"id": client.ClientId, "client_id": client.ClientId,
"agentid": client.PublicId, "agent_id": client.PublicId,
"agentguid": client.AgentGuid, "agent_guid": client.AgentGuid,
"sessiontype": client.SessionType, "client_sessiontype": client.SessionType,
"username": client.EnvironmentInfo.Username, "client_username": client.EnvironmentInfo.Username,
"hostname": client.EnvironmentInfo.Hostname, "client_hostname": client.EnvironmentInfo.Hostname,
"pwd": client.EnvironmentInfo.Pwd, "client_pwd": client.EnvironmentInfo.Pwd,
"os": client.EnvironmentInfo.OS, "client_os": client.EnvironmentInfo.OS,
"shell": client.EnvironmentInfo.Shell, "client_shell": client.EnvironmentInfo.Shell,
} }
} }
@ -136,21 +159,33 @@ func updateMetrics(state *models.State) {
agentInfo.Reset() agentInfo.Reset()
for _, agent := range state.Agents { for _, agent := range state.Agents {
if !lastAgentGuids[agent.Guid] { if !lastAgentGuids[agent.Guid] {
agentStartTime.
With(prometheus.Labels{"agent_guid": agent.Guid}).
Set(float64(agent.StartTime.Unix()))
cumulativeAgentCount.Inc() cumulativeAgentCount.Inc()
} }
delete(lastAgentGuids, agent.Guid)
agentGuids[agent.Guid] = true agentGuids[agent.Guid] = true
agentActive(agent) agentActive(agent)
} }
for agent, _ := range lastAgentGuids {
agentStopTime.With(prometheus.Labels{"agent_guid": agent}).Set(float64(time.Now().Unix()))
}
lastAgentGuids = agentGuids lastAgentGuids = agentGuids
clientCount.Set(float64(len(state.Clients))) clientCount.Set(float64(len(state.Clients)))
clientInfo.Reset() clientInfo.Reset()
for _, client := range state.Clients { for _, client := range state.Clients {
if !lastClientGuids[client.Guid] { if !lastClientGuids[client.Guid] {
clientStartTime.With(prometheus.Labels{"client_guid": client.Guid}).Set(float64(client.StartTime.Unix()))
cumulativeClientCount.Inc() cumulativeClientCount.Inc()
} }
delete(lastClientGuids, client.Guid)
clientGuids[client.Guid] = true clientGuids[client.Guid] = true
clientActive(client) clientActive(client)
} }
for client, _ := range lastClientGuids {
clientStopTime.With(prometheus.Labels{"client_guid": client}).Set(float64(time.Now().Unix()))
}
lastClientGuids = clientGuids lastClientGuids = clientGuids
} }