-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
158 lines (132 loc) · 3.97 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
const (
applicationName = "opentsdb_exporter"
metricsRoute = "/metrics"
probeRoute = "/opentsdb"
)
var (
nameSanitiser = strings.NewReplacer("tsd.", "opentsdb_", ".", "_", "-", "_")
revision = "unknown"
versionString = fmt.Sprintf("%s %s (%s)", applicationName, revision, runtime.Version())
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9250", "Address to listen on for web interface and telemetry.")
timeout = flag.Duration("opentsdb.timeout", 5*time.Second, "Timeout for HTTP requests to OpenTSDB.")
)
func main() {
flag.Parse()
if *showVersion {
fmt.Println(versionString)
os.Exit(0)
}
log.Infoln("Starting", versionString)
http.Handle(metricsRoute, promhttp.Handler())
http.HandleFunc("/opentsdb", handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>OpenTSDB Exporter</title></head>
<body>
<h1>` + versionString + `</h1>
<p><a href='` + metricsRoute + `'>Metrics</a></p>
<p><a href='` + probeRoute + `?target=http://opentsdb.localhost'>Example OpenTSDB probe</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
s := &http.Server{
Addr: *listenAddress,
ReadTimeout: *timeout * 2,
WriteTimeout: *timeout * 2,
}
log.Fatal(s.ListenAndServe())
}
type collector struct {
target string
timeout time.Duration
}
func handler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", 400)
return
}
log.Debugf("Scraping target %q", target)
registry := prometheus.NewRegistry()
registry.MustRegister(&collector{target: target, timeout: *timeout})
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
metrics, err := c.queryOpenTSDB()
if err != nil {
log.Errorf("Error scraping target %s: %s", c.target, err)
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("api_error", "", nil, nil), errors.New("Error scraping target, check exporter logs"))
return
}
for _, m := range *metrics {
value, err := m.Value.Float64()
if err != nil {
log.Errorf("Error scraping target %s: %s", c.target, err)
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("api_error", "", nil, nil), errors.New("Error scraping target, check exporter logs"))
return
}
var labelNames, labelValues []string
for k, v := range m.Tags {
if k == "host" {
continue
}
labelNames = append(labelNames, k)
labelValues = append(labelValues, v)
}
name := nameSanitiser.Replace(m.Metric)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(name, "", labelNames, nil),
prometheus.GaugeValue,
value,
labelValues...,
)
}
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
// Copied from https://github.com/prometheus/snmp_exporter/blob/bc4b0a4db1e22c5379f7fdb3f314dbb58a48c637/collector.go#L118
ch <- prometheus.NewDesc("dummy", "dummy", nil, nil)
}
func (c *collector) queryOpenTSDB() (m *metrics, err error) {
client := &http.Client{
Timeout: c.timeout,
}
resp, err := client.Get(c.target + "/api/stats")
if err != nil {
return m, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return m, err
}
if err = json.Unmarshal(body, &m); err != nil {
return m, err
}
return
}
type metrics []struct {
Metric string `json:"metric"`
// We don't care about the timestamp, let Prometheus use the scrape time
// https://prometheus.io/docs/instrumenting/writing_exporters/#scheduling
Value json.Number `json:"value"`
Tags map[string]string `json:"tags"`
}