-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiotdb_exporter.go
74 lines (62 loc) · 1.97 KB
/
iotdb_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
package main
import (
"flag"
"fmt"
"github.com/fagnercarvalho/prometheus-iotdb-exporter/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
"net/http"
"os"
)
var (
listenPort = flag.String("listenPort", "8092", "exporter listening port")
iotDBHost = flag.String("iotDBHost", "127.0.0.1", "IoTDB server host")
iotDBPort = flag.String("iotDBPort", "6667", "IoTDB server port")
iotDBUsername = flag.String("iotDBUsername", "root", "IoTDB username")
iotDBTimeoutInMs = flag.Int("iotDBTimeoutInMs", 0, "IoTDB timeout (in ms) for opening a new database session")
)
func init() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{ForceColors: true})
}
func main() {
flag.Parse()
password := os.Getenv("IOTDB_PASSWORD")
if len(password) == 0 {
log.Error("Error when trying to get IoTDB password in `IOTDB_PASSWORD` environment variable.")
os.Exit(1)
}
config := exporter.Config{
Host: *iotDBHost,
Port: *iotDBPort,
Username: *iotDBUsername,
Password: password,
TimeoutInMs: *iotDBTimeoutInMs,
}
exporter := exporter.New(config)
registry := prometheus.NewRegistry()
registry.MustRegister(exporter)
registry.MustRegister(version.NewCollector("iotdb"))
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
log.WithFields(log.Fields{
"port": *listenPort,
}).Info("Serving IoTDB exporter metrics page")
metricsPath := "/metrics"
homePage := []byte(`<html>
<head><title>IoTDB Exporter</title></head>
<body>
<h2>IoTDB Exporter</h2>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body></html>
`)
http.Handle(metricsPath, h)
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
w.Write(homePage)
})
err := http.ListenAndServe(fmt.Sprintf(":%v", *listenPort), nil)
if err != nil {
log.WithError(err).Error("Error when serving IoTDB exporter")
}
}