forked from nshttpd/mikrotik-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (152 loc) · 4.45 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
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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
"mikrotik-exporter/internal/collector"
"mikrotik-exporter/internal/config"
"mikrotik-exporter/internal/metrics"
)
// single device can be defined via CLI flags, multiple via config file.
var (
listen = flag.String("port", ":9436", "port number to listen on")
address = flag.String("address", "", "address of the device to monitor")
configFile = flag.String("config-file", "", "config file to load")
device = flag.String("device", "", "single device to monitor")
devPort = flag.String("deviceport", "8728", "port for single device")
user = flag.String("user", "", "user for authentication with single device")
password = flag.String("password", "", "password for authentication for single device")
timeout = flag.Duration("timeout", 5*time.Second, "timeout when connecting to devices")
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)")
features = flag.String("features", "interface,resource", "enabled features")
metricsPath = flag.String("path", "/metrics", "path to answer requests on")
logFormat = flag.String("log-format", "json", "logformat text or json (default json)")
logLevel = flag.String("log-level", "info", "log level")
showVersion = flag.Bool("version", false, "show the version of binary")
cfg *config.Config
appVersion = "DEVELOPMENT"
shortSha = "0xDEADBEEF"
)
func init() {
prometheus.MustRegister(version.NewCollector("mikrotik_exporter"))
}
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("\nVersion: %s\nShort SHA: %s\n\n", appVersion, shortSha)
os.Exit(0)
}
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)
if *logFormat == "text" {
log.SetFormatter(&log.TextFormatter{})
} else {
log.SetFormatter(&log.JSONFormatter{})
}
}
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) {
// Attempt to read credentials from env if not already defined
if *user == "" {
*user = os.Getenv("MIKROTIK_USER")
}
if *password == "" {
*password = os.Getenv("MIKROTIK_PASSWORD")
}
if *device == "" || *address == "" || *user == "" || *password == "" {
return nil, fmt.Errorf("missing required param for single device configuration")
}
return &config.Config{
Devices: []*config.Device{
{
Name: *device,
Address: *address,
User: *user,
Password: *password,
Port: *devPort,
},
},
}, nil
}
func startServer() {
h, err := createMetricsHandler()
if err != nil {
log.Fatal(err)
}
http.Handle(*metricsPath, h)
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 ", *listen)
log.Fatal(http.ListenAndServe(*listen, nil))
}
func createMetricsHandler() (http.Handler, error) {
feats, err := metrics.Registry.Load(strings.Split(*features, ",")...)
if err != nil {
return nil, err
}
opts := []collector.Option{
collector.WithTimeout(*timeout),
collector.WithMetrics(feats...),
}
if *tls {
opts = append(opts, collector.WithTLS(*insecure))
}
nc, err := collector.NewCollector(cfg, opts...)
if err != nil {
return nil, err
}
registry := prometheus.NewRegistry()
if err := registry.Register(nc); err != nil {
return nil, err
}
return promhttp.HandlerFor(registry,
promhttp.HandlerOpts{
ErrorLog: log.New(),
ErrorHandling: promhttp.ContinueOnError,
}), nil
}