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