converge/cmd/converge/prometheus.go

133 lines
3.7 KiB
Go

package main
import (
"converge/pkg/models"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
)
const NAMESPACE = "converge"
var (
// remember previous values of agent guids and clients so that we can increment
// the cumulative counters.
lastAgentGuids map[string]bool = make(map[string]bool)
lastClientGuids map[string]bool = make(map[string]bool)
cumulativeAgentCount = promauto.NewCounter(prometheus.CounterOpts{
Namespace: NAMESPACE,
Name: "agent_count_total",
Help: "Total number of agents connected over time",
})
cumulativeClientCount = promauto.NewCounter(prometheus.CounterOpts{
Namespace: NAMESPACE,
Name: "client_count_total",
Help: "Total number of clients connected over time",
})
agentCount = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "agent_count",
Help: "Current number of agents",
})
clientCount = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "client_count",
Help: "Current number of clients",
})
agentInfo = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: NAMESPACE,
Name: "agent_info",
Help: "A flexible gauge with dynamic labels, always set to 1",
},
[]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{"guid", "id", "agentid", "sessiontype"}, // 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,
}
}
func clientLabels(client models.Client) prometheus.Labels {
return prometheus.Labels{
"guid": client.Guid,
"id": client.ClientId,
"agentid": client.PublicId,
"sessiontype": client.SessionType,
}
}
func agentActive(agent models.Agent) {
agentInfo.With(agentLabels(agent)).Set(1)
}
func clientActive(client models.Client) {
clientInfo.With(clientLabels(client)).Set(1)
}
func setupPrometheus(mux *http.ServeMux, notifications chan *models.State) {
go func() {
for {
state := <-notifications
updateMetrics(state)
}
}()
mux.Handle("/metrics", promhttp.Handler())
}
func updateMetrics(state *models.State) {
// This implemnetation has a small probability that the metric will be in a partially
// initialized state. This is however unlikely. It would lead to in incorrect determination
// that an agent or client is not available. However, each agent and client will have a UID
// so that is still possible to identify the client or agent even though some values might
// become 0.
log.Printf("Got notification %v", *state)
agentGuids := make(map[string]bool)
clientGuids := make(map[string]bool)
agentCount.Set(float64(len(state.Agents)))
agentInfo.Reset()
for _, agent := range state.Agents {
if !lastAgentGuids[agent.Guid] {
cumulativeAgentCount.Inc()
}
agentGuids[agent.Guid] = true
agentActive(agent)
}
lastAgentGuids = agentGuids
clientCount.Set(float64(len(state.Clients)))
clientInfo.Reset()
for _, client := range state.Clients {
if !lastClientGuids[client.Guid] {
cumulativeClientCount.Inc()
}
clientGuids[client.Guid] = true
clientActive(client)
}
lastClientGuids = clientGuids
}