-
Notifications
You must be signed in to change notification settings - Fork 23
/
solace_prometheus_exporter.go
317 lines (287 loc) · 12.4 KB
/
solace_prometheus_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"net/http"
"os"
"solace_exporter/exporter"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
promVersion "github.com/prometheus/common/version"
"golang.org/x/sync/semaphore"
)
const version = float64(1004005)
func logDataSource(dataSources []exporter.DataSource) string {
dS := make([]string, len(dataSources))
for index, dataSource := range dataSources {
dS[index] = dataSource.String()
}
return strings.Join(dS, "&")
}
func main() {
kingpin.HelpFlag.Short('h')
promlogConfig := promlog.Config{
Level: &promlog.AllowedLevel{},
Format: &promlog.AllowedFormat{},
}
promlogConfig.Level.Set("info")
promlogConfig.Format.Set("logfmt")
flag.AddFlags(kingpin.CommandLine, &promlogConfig)
configFile := kingpin.Flag(
"config-file",
"Path and name of ini file with configuration settings. See sample file solace_prometheus_exporter.ini.",
).String()
enableTLS := kingpin.Flag(
"enable-tls",
"Set to true, to start listenAddr as TLS port. Make sure to provide valid server certificate and private key files.",
).Bool()
certfile := kingpin.Flag(
"certificate",
"If using TLS, you must provide a valid server certificate in PEM format. Can be set via config file, cli parameter or env variable.",
).ExistingFile()
privateKey := kingpin.Flag(
"private-key",
"If using TLS, you must provide a valid private key in PEM format. Can be set via config file, cli parameter or env variable.",
).ExistingFile()
certType := kingpin.Flag(
"cert-type",
" Set the certificate type PEM | PKCS12.",
).String()
pkcs12File := kingpin.Flag(
"pkcs12File",
"If using TLS, you must provide a valid pkcs12 file.",
).ExistingFile()
pkcs12Pass := kingpin.Flag(
"pkcs12Pass",
"If using TLS, you must provide a valid pkcs12 password.",
).String()
kingpin.Parse()
logger := promlog.New(&promlogConfig)
endpoints, conf, err := exporter.ParseConfig(*configFile)
if err != nil {
level.Error(logger).Log("msg", err)
os.Exit(1)
}
level.Info(logger).Log("msg", "Starting solace_prometheus_exporter", "version", version)
level.Info(logger).Log("msg", "Build context", "context", promVersion.BuildContext())
if *enableTLS {
conf.EnableTLS = *enableTLS
}
if len(*certfile) > 0 {
conf.Certificate = *certfile
}
if len(*privateKey) > 0 {
conf.PrivateKey = *privateKey
}
if len(*certType) > 0 {
conf.CertType = *certType
}
if len(*pkcs12File) > 0 {
conf.Pkcs12File = *pkcs12File
}
if len(*pkcs12Pass) > 0 {
conf.Pkcs12Pass = *pkcs12Pass
}
level.Info(logger).Log("msg", "Scraping",
"listenAddr", conf.GetListenURI(),
"scrapeURI", conf.ScrapeURI,
"username", conf.Username,
"sslVerify", conf.SslVerify,
"timeout", conf.Timeout)
// Configure endpoints
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
doHandle(w, r, nil, *conf, logger)
})
// A broker has only max 10 semp connections that can be served in parallel.
var sempConnections = semaphore.NewWeighted(conf.ParallelSempConnections)
declareHandlerFromConfig := func(urlPath string, dataSource []exporter.DataSource) {
_ = level.Info(logger).Log("msg", "Register handler from config", "handler", "/"+urlPath, "dataSource", logDataSource(dataSource))
if conf.PrefetchInterval.Seconds() > 0 {
var asyncFetcher = exporter.NewAsyncFetcher(urlPath, dataSource, *conf, logger, sempConnections, version)
http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) {
doHandleAsync(w, r, asyncFetcher, *conf, logger)
})
} else {
http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) {
doHandle(w, r, dataSource, *conf, logger)
})
}
}
for urlPath, dataSource := range endpoints {
declareHandlerFromConfig(urlPath, dataSource)
}
http.HandleFunc("/solace", func(w http.ResponseWriter, r *http.Request) {
var err = r.ParseForm()
if err != nil {
level.Error(logger).Log("msg", "Can not parse the request parameter", "err", err)
return
}
var dataSource []exporter.DataSource
for key, values := range r.Form {
if strings.HasPrefix(key, "m.") {
for _, value := range values {
parts := strings.Split(value, "|")
if len(parts) < 2 {
level.Error(logger).Log("msg", "One or two | expected. Use VPN wildcard | Item wildcard | Optional metric filter for v2 apis", "key", key, "value", value)
} else {
var metricFilter []string
if len(parts) == 3 && len(strings.TrimSpace(parts[2])) > 0 {
metricFilter = strings.Split(parts[2], ",")
}
dataSource = append(dataSource, exporter.DataSource{
Name: strings.TrimPrefix(key, "m."),
VpnFilter: parts[0],
ItemFilter: parts[1],
MetricFilter: metricFilter,
})
}
}
}
}
doHandle(w, r, dataSource, *conf, logger)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var endpointsDoc bytes.Buffer
for urlPath, dataSources := range endpoints {
endpointsDoc.WriteString("<li><a href='/" + urlPath + "'>Custom Exporter " + urlPath + " -> " + logDataSource(dataSources) + "</a></li>")
}
w.Write([]byte(`<html>
<head><title>Solace Exporter</title></head>
<body>
<h1>Solace Exporter</h1>
<ul style="list-style: none;">
<li><a href='` + "/metrics" + `'>Exporter Metrics</a></li>
` + endpointsDoc.String() + `
<li><a href='` + "/solace?m.ClientStats=*|*&m.VpnStats=*|*&m.BridgeStats=*|*&m.QueueRates=*|*" + `'>Solace Broker</a>
<br>
<p>Configure the data you want ot receive, via HTTP GET parameters.
<br>Please use in format "m.ClientStats=*|*&m.VpnStats=*|*"
<br>Here is "m." the prefix.
<br>Here is "ClientStats" the scrape target.
<br>The first asterisk the VPN filter and the second asterisk the item filter.
Not all scrape targets support filter.
<br>Scrape targets:<br>
<table><tr><th>scape target</th><th>vpn filter supports</th><th>item filter supported</th><th>performance</th><tr>
<tr><td>Version</td><td>no</td><td>no</td><td>dont harm broker</td></tr>`))
if !conf.IsHWBroker {
w.Write([]byte(`
<tr><td>Health</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>StorageElement</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Disk</td><td>no</td><td>yes</td><td>dont harm broker</td></tr>`))
}
w.Write([]byte(`
<tr><td>Memory</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Interface</td><td>no</td><td>yes</td><td>dont harm broker</td></tr>
<tr><td>GlobalStats</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Spool</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Redundancy (only for HA brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>ConfigSync (only for HA brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>ConfigSyncRouter (only for HA brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>ReplicationStats (only for DR replication brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Vpn</td><td>yes</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>VpnReplication</td><td>yes</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>ConfigSyncVpn (only for HA broker)</td><td>yes</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Bridge</td><td>yes</td><td>yes</td><td>dont harm broker</td></tr>
<tr><td>VpnSpool</td><td>yes</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Client</td><td>yes</td><td>yes</td><td>may harm broker if many clients</td></tr>
<tr><td>ClientSlowSubscriber</td><td>yes</td><td>yes</td><td>may harm broker if many clients</td></tr>
<tr><td>ClientStats</td><td>yes</td><td>no</td><td>may harm broker if many clients</td></tr>
<tr><td>ClientConnections</td><td>yes</td><td>no</td><td>may harm broker if many clients</td></tr>
<tr><td>ClientMessageSpoolStats</td><td>yes</td><td>yes</td><td>no</td></tr>
<tr><td>ClusterLinks</td><td>yes</td><td>no</td><td>may harm broker if many clients</td></tr>
<tr><td>VpnStats</td><td>yes</td><td>no</td><td>has a very small performance down site</td></tr>
<tr><td>BridgeStats</td><td>yes</td><td>yes</td><td>has a very small performance down site</td></tr>
<tr><td>QueueRates</td><td>yes</td><td>yes</td><td>DEPRECATED: may harm broker if many queues</td></tr>
<tr><td>QueueStats</td><td>yes</td><td>yes</td><td>may harm broker if many queues</td></tr>
<tr><td>QueueStatsV2</td><td>yes</td><td>yes</td><td>may harm broker if many queues</td></tr>
<tr><td>QueueDetails</td><td>yes</td><td>yes</td><td>may harm broker if many queues</td></tr>
<tr><td>TopicEndpointRates</td><td>yes</td><td>yes</td><td>DEPRECATED: may harm broker if many topic-endpoints</td></tr>
<tr><td>TopicEndpointStats</td><td>yes</td><td>yes</td><td>may harm broker if many topic-endpoints</td></tr>
<tr><td>TopicEndpointDetails</td><td>yes</td><td>yes</td><td>may harm broker if many topic-endpoints</td></tr>
<tr><td>BridgeRemote</td><td>yes</td><td>yes</td><td>dont harm broker</td></tr>`))
if conf.IsHWBroker == true {
w.Write([]byte(`
<tr><td>Alarm (only for Hardware brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Environment (only for Hardware brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>Hardware (only for Hardware brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>
<tr><td>InterfaceHW (only for Hardware brokers)</td><td>no</td><td>yes</td><td>dont harm broker</td></tr>
<tr><td>Raid (only for Hardware brokers)</td><td>no</td><td>no</td><td>dont harm broker</td></tr>`))
}
w.Write([]byte(`
</table>
<br>
</p>
</li>
<ul>
</body>
</html>`))
})
// start server
if conf.EnableTLS {
exporter.ListenAndServeTLS(*conf)
} else {
if err := http.ListenAndServe(conf.ListenAddr, nil); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(2)
}
}
}
func doHandleAsync(w http.ResponseWriter, r *http.Request, asyncFetcher *exporter.AsyncFetcher, conf exporter.Config, logger log.Logger) (resultCode string) {
registry := prometheus.NewRegistry()
registry.MustRegister(asyncFetcher)
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
handler.ServeHTTP(w, r)
return w.Header().Get("status")
}
func doHandle(w http.ResponseWriter, r *http.Request, dataSource []exporter.DataSource, conf exporter.Config, logger log.Logger) (resultCode string) {
if dataSource == nil {
handler := promhttp.Handler()
handler.ServeHTTP(w, r)
} else {
// Exporter for endpoint
username := r.FormValue("username")
password := r.FormValue("password")
scrapeURI := r.FormValue("scrapeURI")
timeout := r.FormValue("timeout")
if len(username) > 0 {
conf.Username = username
}
if len(password) > 0 {
conf.Password = password
}
if len(scrapeURI) > 0 {
conf.ScrapeURI = scrapeURI
}
if len(timeout) > 0 {
var err error
conf.Timeout, err = time.ParseDuration(timeout)
if err != nil {
level.Error(logger).Log("msg", "Per HTTP given timeout parameter is not valid", "err", err, "timeout", timeout)
}
}
level.Info(logger).Log("msg", "handle http request", "dataSource", logDataSource(dataSource), "scrapeURI", conf.ScrapeURI)
exp := exporter.NewExporter(logger, &conf, &dataSource, version)
registry := prometheus.NewRegistry()
registry.MustRegister(exp)
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
handler.ServeHTTP(w, r)
}
return w.Header().Get("status")
}