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