-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics.go
69 lines (57 loc) · 1.9 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
type connectionStats struct {
established uint
failed uint
}
type prometheusExporter struct {
certTTL *prometheus.Desc
connUp *prometheus.Desc
conns *prometheus.Desc
fwds *prometheus.Desc
connections connectionStats
forwardings connectionStats
}
var (
connLabels = []string{"state"}
hostLabel = []string{"host"}
)
var metrics = prometheusExporter{
certTTL: prometheus.NewDesc("sshproxy_certificate_ttl", "TTL until SSH certificate expires", hostLabel, nil),
connUp: prometheus.NewDesc("sshproxy_connection_up", "SSH connection up", hostLabel, nil),
conns: prometheus.NewDesc("sshproxy_connections_total", "SSH connections", connLabels, nil),
fwds: prometheus.NewDesc("sshproxy_forwardings_total", "TCP forwardings", connLabels, nil),
}
// Describe implements (part of the) prometheus.Collector interface.
func (e *prometheusExporter) Describe(c chan<- *prometheus.Desc) {
c <- metrics.certTTL
c <- metrics.connUp
c <- metrics.conns
c <- metrics.fwds
}
// Collect implements (part of the) prometheus.Collector interface.
func (e prometheusExporter) Collect(c chan<- prometheus.Metric) {
const C = prometheus.CounterValue
const G = prometheus.GaugeValue
met := prometheus.MustNewConstMetric
c <- met(metrics.conns, C, float64(e.connections.established), "established")
c <- met(metrics.conns, C, float64(e.connections.failed), "failed")
c <- met(metrics.fwds, C, float64(e.forwardings.established), "established")
c <- met(metrics.fwds, C, float64(e.forwardings.failed), "failed")
proxy.mtx.Lock()
for key, client := range proxy.clients {
host := key.String()
var up float64
if client.sshClient != nil {
up = 1
}
c <- met(metrics.connUp, G, up, host)
if cert := client.sshCert; cert != nil {
ttl := float64(cert.ValidBefore)
c <- met(metrics.certTTL, G, ttl, host)
}
}
proxy.mtx.Unlock()
}