Custom API port (#62)

Adds ability to specify API Port, either via CLI or in config. If port is not provided, default is used, taking into account if TLS is enabled or not
This commit is contained in:
Welby McRoberts 2019-12-26 18:17:27 +00:00 committed by Steve Brunton
parent 9ceb56fdbf
commit 55ae663ce3
4 changed files with 14 additions and 4 deletions

View File

@ -50,6 +50,7 @@ devices:
password: changeme password: changeme
- name: my_second_router - name: my_second_router
address: 10.10.0.2 address: 10.10.0.2
port: 8999
user: prometheus2 user: prometheus2
password: password_to_second_router password: password_to_second_router

View File

@ -20,8 +20,8 @@ import (
const ( const (
namespace = "mikrotik" namespace = "mikrotik"
apiPort = ":8728" apiPort = "8728"
apiPortTLS = ":8729" apiPortTLS = "8729"
// DefaultTimeout defines the default timeout when connecting to a router // DefaultTimeout defines the default timeout when connecting to a router
DefaultTimeout = 5 * time.Second DefaultTimeout = 5 * time.Second
@ -246,7 +246,10 @@ func (c *collector) connect(d *config.Device) (*routeros.Client, error) {
log.WithField("device", d.Name).Debug("trying to Dial") log.WithField("device", d.Name).Debug("trying to Dial")
if !c.enableTLS { if !c.enableTLS {
conn, err = net.DialTimeout("tcp", d.Address+apiPort, c.timeout) if(d.Port) == "" {
d.Port = apiPort
}
conn, err = net.DialTimeout("tcp", d.Address+":"+d.Port, c.timeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -255,10 +258,13 @@ func (c *collector) connect(d *config.Device) (*routeros.Client, error) {
tlsCfg := &tls.Config{ tlsCfg := &tls.Config{
InsecureSkipVerify: c.insecureTLS, InsecureSkipVerify: c.insecureTLS,
} }
if(d.Port) == "" {
d.Port = apiPortTLS
}
conn, err = tls.DialWithDialer(&net.Dialer{ conn, err = tls.DialWithDialer(&net.Dialer{
Timeout: c.timeout, Timeout: c.timeout,
}, },
"tcp", d.Address+apiPortTLS, tlsCfg) "tcp", d.Address+":"+d.Port, tlsCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -31,6 +31,7 @@ type Device struct {
Address string `yaml:"address"` Address string `yaml:"address"`
User string `yaml:"user"` User string `yaml:"user"`
Password string `yaml:"password"` Password string `yaml:"password"`
Port string `yaml:"port"`
} }
// Load reads YAML from reader and unmashals in Config // Load reads YAML from reader and unmashals in Config

View File

@ -29,6 +29,7 @@ var (
logLevel = flag.String("log-level", "info", "log level") logLevel = flag.String("log-level", "info", "log level")
metricsPath = flag.String("path", "/metrics", "path to answer requests on") metricsPath = flag.String("path", "/metrics", "path to answer requests on")
password = flag.String("password", "", "password for authentication for single device") password = flag.String("password", "", "password for authentication for single device")
deviceport = flag.String("deviceport", "8728", "port for single device")
port = flag.String("port", ":9436", "port number to listen on") port = flag.String("port", ":9436", "port number to listen on")
timeout = flag.Duration("timeout", collector.DefaultTimeout, "timeout when connecting to devices") timeout = flag.Duration("timeout", collector.DefaultTimeout, "timeout when connecting to devices")
tls = flag.Bool("tls", false, "use tls to connect to routers") tls = flag.Bool("tls", false, "use tls to connect to routers")
@ -121,6 +122,7 @@ func loadConfigFromFlags() (*config.Config, error) {
Address: *address, Address: *address,
User: *user, User: *user,
Password: *password, Password: *password,
Port: *deviceport,
}, },
}, },
}, nil }, nil