-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopenvpn_exporter.go
149 lines (138 loc) · 4.73 KB
/
openvpn_exporter.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"flag"
"log"
"net/http"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shrikantpatnaik/go-openvpn-status"
)
type openVPNExporter struct {
statusPath string
}
var (
openvpnUpDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "", "up"),
"Whether scraping OpenVPN's metrics was successful.",
nil, nil)
openvpnLastUpdatedDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "", "last_updated"),
"Whether scraping OpenVPN's metrics was successful.",
nil, nil)
openvpnConnectedClientsDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "", "connected_clients"),
"Number Of Connected Clients", nil, nil)
openvpnGlobalStatsDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "global_stats", "max_bcast_mcast_queue_len"),
"Global Stats", nil, nil)
openvpnClientConnectedSinceDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "client", "connected_since"),
"Client Connected Since",
[]string{"name", "real_address"}, nil)
openvpnClientBytesReceivedDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "client", "bytes_received"),
"Client Bytes Received",
[]string{"name", "real_address"}, nil)
openvpnClientBytesSentDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "client", "bytes_sent"),
"Client Bytes Sent",
[]string{"name", "real_address"}, nil)
openvpnRoutingLastRegDesc = prometheus.NewDesc(
prometheus.BuildFQName("openvpn", "routing", "last_ref"),
"Routing last reference time",
[]string{"name", "virtual_address", "real_address"}, nil)
)
func (e *openVPNExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- openvpnUpDesc
}
func (e *openVPNExporter) Collect(ch chan<- prometheus.Metric) {
status, err := openvpnStatus.ParseFile(e.statusPath)
up := 0.0
if status.IsUp {
up = 1
}
ch <- prometheus.MustNewConstMetric(
openvpnUpDesc,
prometheus.GaugeValue,
up)
if err == nil {
ch <- prometheus.MustNewConstMetric(
openvpnConnectedClientsDesc,
prometheus.GaugeValue,
float64(len(status.ClientList)))
ch <- prometheus.MustNewConstMetric(
openvpnGlobalStatsDesc,
prometheus.GaugeValue,
float64(status.GlobalStats.MaxBcastMcastQueueLen))
ch <- prometheus.MustNewConstMetric(
openvpnLastUpdatedDesc,
prometheus.GaugeValue,
float64(status.UpdatedAt.Unix()))
for _, client := range status.ClientList {
nameSlice := []string{client.CommonName}
nameAndAddressSlice := append(nameSlice, client.RealAddress)
ch <- prometheus.MustNewConstMetric(
openvpnClientConnectedSinceDesc,
prometheus.GaugeValue,
float64(client.ConnectedSince.Unix()),
nameAndAddressSlice...)
bytesReceived, _ := strconv.ParseFloat(client.BytesReceived, 64)
ch <- prometheus.MustNewConstMetric(
openvpnClientBytesReceivedDesc,
prometheus.GaugeValue,
bytesReceived,
nameAndAddressSlice...)
bytesSent, _ := strconv.ParseFloat(client.BytesSent, 64)
ch <- prometheus.MustNewConstMetric(
openvpnClientBytesSentDesc,
prometheus.GaugeValue,
bytesSent,
nameAndAddressSlice...)
}
for _, route := range status.RoutingTable {
labelSlice := []string{route.CommonName, route.VirtualAddress, route.RealAddress}
ch <- prometheus.MustNewConstMetric(
openvpnRoutingLastRegDesc,
prometheus.GaugeValue,
float64(route.LastRef.Unix()),
labelSlice...)
}
}
}
func newOpenVPNExporter(statusPath string) *openVPNExporter {
return &openVPNExporter{
statusPath: statusPath,
}
}
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9176", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
openvpnStatusPath = flag.String("openvpn.status_path", "examples/server.status", "Paths at which OpenVPN places its status files.")
)
flag.Parse()
exporter := newOpenVPNExporter(*openvpnStatusPath)
log.Printf("Starting OpenVPN Exporter\n")
log.Printf("openvpn.status_path: %v\n", *openvpnStatusPath)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>OpenVPN Exporter</title></head>
<body>
<h1>OpenVPN Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Printf("Listening on %s\n", *listenAddress)
http.ListenAndServe(*listenAddress, logRequest(http.DefaultServeMux))
}