-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttn_exporter.go
269 lines (244 loc) · 9.9 KB
/
ttn_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
package main
import (
"crypto/tls"
"errors"
"net/http"
_ "net/http/pprof" // #nosec G108 profiling error
"os"
"strconv"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/juusujanar/ttn-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
const (
// Exporter namespace.
namespace = "ttn"
envThingsNetworkAPIToken = "TTN_API_KEY" // #nosec G101 this is not a hardcoded credential
)
// Metrics descriptors.
var (
gatewayConnected = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "connected"),
"Gateway connection status",
[]string{"gateway_id", "name", "protocol"}, nil,
)
uplinkCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "uplink_count"),
"Number of uplink packets received by gateway",
[]string{"gateway_id", "name"}, nil,
)
downlinkCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "downlink_count"),
"Number of downlink packets sent by gateway",
[]string{"gateway_id", "name"}, nil,
)
txAcknowledgementCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "tx_acknowledgement_count"),
"Number of TX acknowledgements received by gateway",
[]string{"gateway_id", "name"}, nil,
)
roundTripMin = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "round_trip_min"),
"Minimum measured round trip time between gateway and TTN in seconds",
[]string{"gateway_id", "name"}, nil,
)
roundTripMax = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "round_trip_max"),
"Maximum measured round trip time between gateway and TTN in seconds",
[]string{"gateway_id", "name"}, nil,
)
roundTripMedian = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "round_trip_median"),
"Median measured round trip time between gateway and TTN in seconds",
[]string{"gateway_id", "name"}, nil,
)
roundTripCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "round_trip_count"),
"Total round trip measurements between gateway and TTN",
[]string{"gateway_id", "name"}, nil,
)
ttnInfo = prometheus.NewDesc(prometheus.BuildFQName(namespace, "version", "info"), "Things Network version info.", []string{"version"}, nil)
ttnUp = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "up"), "Was the last scrape of TTN successful.", nil, nil)
)
// Exporter collects HAProxy stats from the given URI and exports them using
// the prometheus metrics package.
type Exporter struct {
URI string
apiKey string
client *http.Client
up prometheus.Gauge
totalScrapes prometheus.Counter
logger log.Logger
}
// NewExporter returns an initialized Exporter.
func NewExporter(uri string, apiKey string, sslVerify bool, timeout time.Duration, logger log.Logger) (*Exporter, error) {
if !strings.HasPrefix(uri, "https://") && !strings.HasPrefix(uri, "http://") {
return nil, errors.New("invalid URI scheme")
}
return &Exporter{
URI: uri,
apiKey: apiKey,
client: &http.Client{
Timeout: timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !sslVerify, // #nosec G402 -- allow insecure TLS when requested by user
},
},
},
up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "up",
Help: "Was the last scrape of TTN successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: "exporter_scrapes_total",
Help: "Total number of scrapes.",
}),
logger: logger,
}, nil
}
// Describe describes all the metrics ever exported by the HAProxy exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// for _, m := range e.serverMetrics {
// ch <- m.Desc
// }
ch <- ttnInfo
ch <- ttnUp
ch <- e.totalScrapes.Desc()
}
// Collect fetches the stats from configured HAProxy location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
up := e.scrape(ch)
ch <- prometheus.MustNewConstMetric(ttnUp, prometheus.GaugeValue, up)
ch <- e.totalScrapes
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) (up float64) {
e.totalScrapes.Inc()
var err error
data, err := collector.GetInfo(e.client, e.URI, e.apiKey, e.logger)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Can't scrape TTN", "err", err)
return 0
}
e.parseVersion(ch, data)
for _, gw := range data {
if gw.Connected {
ch <- prometheus.MustNewConstMetric(gatewayConnected, prometheus.GaugeValue, BoolToFloat(gw.Connected), gw.GatewayID, gw.Name, gw.Stats.Protocol)
if gw.Stats.UplinkCount != "" {
uplinkCountFloat, err := strconv.ParseFloat(gw.Stats.UplinkCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert UplinkCount to float64", "err", err, "value", gw.Stats.UplinkCount)
}
ch <- prometheus.MustNewConstMetric(uplinkCount, prometheus.CounterValue, uplinkCountFloat, gw.GatewayID, gw.Name)
}
if gw.Stats.DownlinkCount != "" {
downlinkCountFloat, err := strconv.ParseFloat(gw.Stats.DownlinkCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert DownlinkCount to float64", "err", err, "value", gw.Stats.DownlinkCount)
}
ch <- prometheus.MustNewConstMetric(downlinkCount, prometheus.CounterValue, downlinkCountFloat, gw.GatewayID, gw.Name)
}
if gw.Stats.TxAcknowledgementCount != "" {
txAcknowledgementCountFloat, err := strconv.ParseFloat(gw.Stats.TxAcknowledgementCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert TxAcknowledgementCount to float64", "err", err, "value", gw.Stats.TxAcknowledgementCount)
}
ch <- prometheus.MustNewConstMetric(txAcknowledgementCount, prometheus.CounterValue, txAcknowledgementCountFloat, gw.GatewayID, gw.Name)
}
if gw.Stats.RoundTripTimes != nil {
ch <- prometheus.MustNewConstMetric(roundTripMin, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Min.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMax, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Max.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMedian, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Median.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripCount, prometheus.GaugeValue, float64(gw.Stats.RoundTripTimes.Count), gw.GatewayID, gw.Name)
}
} else {
ch <- prometheus.MustNewConstMetric(gatewayConnected, prometheus.GaugeValue, 0, gw.GatewayID, gw.Name, "")
}
}
return 1
}
func (e *Exporter) parseVersion(ch chan<- prometheus.Metric, data []collector.GatewayData) {
for _, gw := range data {
if gw.Stats != nil {
if gw.Stats.LastStatus.Versions.GatewayServerVersion != nil {
ch <- prometheus.MustNewConstMetric(ttnInfo, prometheus.GaugeValue, 1, *gw.Stats.LastStatus.Versions.GatewayServerVersion)
return
} else if gw.Stats.LastStatus.Versions.Firmware != nil {
ch <- prometheus.MustNewConstMetric(ttnInfo, prometheus.GaugeValue, 1, *gw.Stats.LastStatus.Versions.Firmware)
return
}
}
}
}
func BoolToFloat(b bool) float64 {
if b {
return 1
}
return 0
}
func main() {
var (
webConfig = webflag.AddFlags(kingpin.CommandLine, ":9981")
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
ttnURI = kingpin.Flag("ttn.uri", "URI on which Things Stack is used.").Default("https://eu1.cloud.thethings.network/").String()
ttnSSLVerify = kingpin.Flag("ttn.ssl-verify", "Flag that enables SSL certificate verification to the TTN API URI").Default("true").Bool()
ttnTimeout = kingpin.Flag("ttn.timeout", "Timeout for trying to get stats from TTN API.").Default("5s").Duration()
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("ttn_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
_ = level.Info(logger).Log("msg", "Starting ttn_exporter", "version", version.Info())
_ = level.Info(logger).Log("msg", "Build context", "context", version.BuildContext())
ttnAPIKey := os.Getenv(envThingsNetworkAPIToken)
if ttnAPIKey == "" {
_ = level.Error(logger).Log("msg", "TTN API key not set as environment variable", "variable", envThingsNetworkAPIToken)
os.Exit(1)
}
exporter, err := NewExporter(*ttnURI, ttnAPIKey, *ttnSSLVerify, *ttnTimeout, logger)
if err != nil {
_ = level.Error(logger).Log("msg", "Error creating an exporter", "err", err)
os.Exit(1)
}
registry := prometheus.NewRegistry()
registry.MustRegister(
collectors.NewGoCollector(),
exporter,
versioncollector.NewCollector("ttn_exporter"),
)
http.Handle(*metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{Registry: registry}))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>TTN Exporter</title></head>
<body>
<h1>The Things Network Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<p><a href='` + *ttnURI + `'>Things Stack</a></p>
<p><a href='https://github.com/juusujanar/ttn-exporter'>GitHub</a></p>
</body>
</html>`))
})
srv := &http.Server{
ReadHeaderTimeout: 1 * time.Second,
}
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {
_ = level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}