2017-09-05 04:52:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-21 02:28:10 +01:00
|
|
|
"bytes"
|
2017-09-05 04:52:14 +02:00
|
|
|
"flag"
|
2018-03-21 02:28:10 +01:00
|
|
|
"io/ioutil"
|
2017-09-05 04:52:14 +02:00
|
|
|
"os"
|
2018-03-21 02:28:10 +01:00
|
|
|
"time"
|
2017-09-05 04:52:14 +02:00
|
|
|
|
2017-11-29 05:58:39 +01:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/nshttpd/mikrotik-exporter/collector"
|
2018-03-21 02:28:10 +01:00
|
|
|
"github.com/nshttpd/mikrotik-exporter/config"
|
2017-11-29 05:58:39 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"github.com/prometheus/common/version"
|
2017-11-30 04:42:59 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-09-05 04:52:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// single device can be defined via CLI flags, mutliple via config file.
|
|
|
|
var (
|
2017-11-30 04:42:59 +01:00
|
|
|
device = flag.String("device", "", "single device to monitor")
|
|
|
|
address = flag.String("address", "", "address of the device to monitor")
|
|
|
|
user = flag.String("user", "", "user for authentication with single device")
|
|
|
|
password = flag.String("password", "", "password for authentication for single device")
|
|
|
|
logLevel = flag.String("log-level", "info", "log level")
|
|
|
|
logFormat = flag.String("log-format", "json", "logformat text or json (default json)")
|
|
|
|
port = flag.String("port", ":9090", "port number to listen on")
|
|
|
|
metricsPath = flag.String("path", "/metrics", "path to answer requests on")
|
2018-03-21 02:28:10 +01:00
|
|
|
configFile = flag.String("config-file", "", "config file to load")
|
|
|
|
withBgp = flag.Bool("with-bgp", false, "retrieves BGP routing infrormation")
|
|
|
|
withRoutes = flag.Bool("with-routes", false, "retrieves routing table information")
|
|
|
|
timeout = flag.Duration("timeout", collector.DefaultTimeout*time.Second, "timeout when connecting to routers")
|
|
|
|
tls = flag.Bool("tls", false, "use tls to connect to routers")
|
|
|
|
insecure = flag.Bool("insecure", false, "skips verification of server certificate when using TLS (not recommended)")
|
|
|
|
cfg *config.Config
|
2017-09-05 04:52:14 +02:00
|
|
|
)
|
|
|
|
|
2017-11-29 05:58:39 +01:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(version.NewCollector("mikrotik_exporter"))
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
configureLog()
|
|
|
|
|
|
|
|
c, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not load config: %v", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
cfg = c
|
|
|
|
|
|
|
|
startServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureLog() {
|
|
|
|
ll, err := log.ParseLevel(*logLevel)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetLevel(ll)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig() (*config.Config, error) {
|
|
|
|
if *configFile != "" {
|
|
|
|
return loadConfigFromFile()
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadConfigFromFlags()
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfigFromFile() (*config.Config, error) {
|
|
|
|
b, err := ioutil.ReadFile(*configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.Load(bytes.NewReader(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfigFromFlags() (*config.Config, error) {
|
|
|
|
if *device == "" || *address == "" || *user == "" || *password == "" {
|
|
|
|
return nil, fmt.Errorf("missing required param for single device configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config.Config{
|
|
|
|
Devices: []config.Device{
|
|
|
|
config.Device{
|
|
|
|
Name: *device,
|
|
|
|
Address: *address,
|
|
|
|
User: *user,
|
|
|
|
Password: *password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServer() {
|
|
|
|
http.HandleFunc(*metricsPath, prometheus.InstrumentHandlerFunc("prometheus", handler))
|
|
|
|
|
|
|
|
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("ok"))
|
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`<html>
|
|
|
|
<head><title>Mikrotik Exporter</title></head>
|
|
|
|
<body>
|
|
|
|
<h1>Mikrotik Exporter</h1>
|
|
|
|
<p><a href="` + *metricsPath + `">Metrics</a></p>
|
|
|
|
</body>
|
|
|
|
</html>`))
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Info("Listening on", *port)
|
|
|
|
log.Fatal(http.ListenAndServe(*port, nil))
|
|
|
|
}
|
|
|
|
|
2017-11-29 05:58:39 +01:00
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
2018-03-21 02:28:10 +01:00
|
|
|
opts := collectorOptions()
|
|
|
|
nc, err := collector.NewCollector(cfg, opts...)
|
2017-11-29 05:58:39 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln("Couldn't create", err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(fmt.Sprintf("Couldn't create %s", err)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
registry := prometheus.NewRegistry()
|
|
|
|
err = registry.Register(nc)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Couldn't register collector:", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(fmt.Sprintf("Couldn't register collector: %s", err)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delegate http serving to Prometheus client library, which will call collector.Collect.
|
2018-03-21 02:28:10 +01:00
|
|
|
h := promhttp.HandlerFor(registry,
|
2017-11-29 05:58:39 +01:00
|
|
|
promhttp.HandlerOpts{
|
2017-11-30 04:42:59 +01:00
|
|
|
ErrorLog: log.New(),
|
2017-11-29 05:58:39 +01:00
|
|
|
ErrorHandling: promhttp.ContinueOnError,
|
|
|
|
})
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
2017-09-05 04:52:14 +02:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
func collectorOptions() []collector.Option {
|
|
|
|
opts := []collector.Option{}
|
2017-09-05 04:52:14 +02:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
if *withBgp {
|
|
|
|
opts = append(opts, collector.WithBGP())
|
2017-09-05 04:52:14 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
if *withRoutes {
|
|
|
|
opts = append(opts, collector.WithRoutes())
|
2017-09-05 04:52:14 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
if *timeout != collector.DefaultTimeout {
|
|
|
|
opts = append(opts, collector.WithTimeout(*timeout))
|
|
|
|
}
|
2017-11-29 05:58:39 +01:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
if *tls {
|
|
|
|
opts = append(opts, collector.WithTLS(*insecure))
|
2017-09-05 04:52:14 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
return opts
|
2017-09-05 04:52:14 +02:00
|
|
|
}
|