2017-10-04 04:24:14 +02:00
|
|
|
package collector
|
2017-11-29 05:58:39 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-21 02:28:10 +01:00
|
|
|
"crypto/tls"
|
2017-11-29 05:58:39 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2017-11-30 04:42:59 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-03-21 02:28:10 +01:00
|
|
|
routeros "gopkg.in/routeros.v2"
|
2017-11-29 05:58:39 +01:00
|
|
|
)
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
const (
|
|
|
|
namespace = "mikrotik"
|
|
|
|
apiPort = ":8728"
|
|
|
|
apiPortTLS = ":8729"
|
|
|
|
|
|
|
|
// DefaultTimeout defines the default timeout when connecting to a router
|
|
|
|
DefaultTimeout = 5 * time.Second
|
|
|
|
)
|
2017-11-29 05:58:39 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
scrapeDurationDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "scrape", "collector_duration_seconds"),
|
|
|
|
"mikrotik_exporter: duration of a collector scrape",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
scrapeSuccessDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "scrape", "collector_success"),
|
|
|
|
"mikrotik_exporter: whether a collector succeeded",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
type collector struct {
|
|
|
|
devices []config.Device
|
2018-04-11 15:21:38 +02:00
|
|
|
collectors []routerOSCollector
|
2018-03-21 02:28:10 +01:00
|
|
|
timeout time.Duration
|
|
|
|
enableTLS bool
|
|
|
|
insecureTLS bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBGP enables BGP routing metrics
|
|
|
|
func WithBGP() Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.collectors = append(c.collectors, &bgpCollector{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRoutes enables routing table metrics
|
|
|
|
func WithRoutes() Option {
|
|
|
|
return func(c *collector) {
|
2018-04-11 15:21:38 +02:00
|
|
|
c.collectors = append(c.collectors, newRoutesCollector())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDHCP enables DHCP serrver metrics
|
|
|
|
func WithDHCP() Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.collectors = append(c.collectors, newDHCPCollector())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDHCPv6 enables DHCPv6 serrver metrics
|
|
|
|
func WithDHCPv6() Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.collectors = append(c.collectors, newDHCPv6Collector())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPools enables IP(v6) pool metrics
|
|
|
|
func WithPools() Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.collectors = append(c.collectors, newPoolCollector())
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
2017-11-29 05:58:39 +01:00
|
|
|
}
|
|
|
|
|
2018-05-07 20:31:19 +02:00
|
|
|
// WithOptics enables optical diagnstocs
|
|
|
|
func WithOptics() Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.collectors = append(c.collectors, newOpticsCollector())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
// WithTimeout sets timeout for connecting to router
|
|
|
|
func WithTimeout(d time.Duration) Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.timeout = d
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 05:58:39 +01:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
// WithTLS enables TLS
|
|
|
|
func WithTLS(insecure bool) Option {
|
|
|
|
return func(c *collector) {
|
|
|
|
c.enableTLS = true
|
|
|
|
c.insecureTLS = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option applies options to collector
|
|
|
|
type Option func(*collector)
|
|
|
|
|
|
|
|
// NewCollector creates a collector instance
|
|
|
|
func NewCollector(cfg *config.Config, opts ...Option) (prometheus.Collector, error) {
|
2017-11-30 04:42:59 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"numDevices": len(cfg.Devices),
|
|
|
|
}).Info("setting up collector for devices")
|
2017-11-29 05:58:39 +01:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
c := &collector{
|
|
|
|
devices: cfg.Devices,
|
|
|
|
timeout: DefaultTimeout,
|
2018-04-11 15:21:38 +02:00
|
|
|
collectors: []routerOSCollector{
|
|
|
|
newInterfaceCollector(),
|
|
|
|
newResourceCollector(),
|
2018-03-21 02:28:10 +01:00
|
|
|
},
|
|
|
|
}
|
2017-11-29 05:58:39 +01:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2017-11-29 05:58:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Describe implements the prometheus.Collector interface.
|
2018-03-21 02:28:10 +01:00
|
|
|
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
|
2017-11-29 05:58:39 +01:00
|
|
|
ch <- scrapeDurationDesc
|
|
|
|
ch <- scrapeSuccessDesc
|
2018-03-21 02:28:10 +01:00
|
|
|
|
|
|
|
for _, co := range c.collectors {
|
|
|
|
co.describe(ch)
|
|
|
|
}
|
2017-11-29 05:58:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements the prometheus.Collector interface.
|
2018-03-21 02:28:10 +01:00
|
|
|
func (c *collector) Collect(ch chan<- prometheus.Metric) {
|
2017-11-29 05:58:39 +01:00
|
|
|
wg := sync.WaitGroup{}
|
2018-03-21 02:28:10 +01:00
|
|
|
wg.Add(len(c.devices))
|
|
|
|
|
|
|
|
for _, dev := range c.devices {
|
|
|
|
go func(d config.Device) {
|
|
|
|
c.collectForDevice(d, ch)
|
2017-11-29 05:58:39 +01:00
|
|
|
wg.Done()
|
2018-03-21 02:28:10 +01:00
|
|
|
}(dev)
|
2017-11-29 05:58:39 +01:00
|
|
|
}
|
2018-03-21 02:28:10 +01:00
|
|
|
|
2017-11-29 05:58:39 +01:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
func (c *collector) collectForDevice(d config.Device, ch chan<- prometheus.Metric) {
|
2017-11-29 05:58:39 +01:00
|
|
|
begin := time.Now()
|
2018-03-21 02:28:10 +01:00
|
|
|
|
|
|
|
err := c.connectAndCollect(&d, ch)
|
|
|
|
|
2017-11-29 05:58:39 +01:00
|
|
|
duration := time.Since(begin)
|
|
|
|
var success float64
|
|
|
|
if err != nil {
|
2018-03-21 02:28:10 +01:00
|
|
|
log.Errorf("ERROR: %s collector failed after %fs: %s", d.Name, duration.Seconds(), err)
|
2017-11-29 05:58:39 +01:00
|
|
|
success = 0
|
|
|
|
} else {
|
2018-03-21 02:28:10 +01:00
|
|
|
log.Debugf("OK: %s collector succeeded after %fs.", d.Name, duration.Seconds())
|
2017-11-29 05:58:39 +01:00
|
|
|
success = 1
|
|
|
|
}
|
2018-03-21 02:28:10 +01:00
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), d.Name)
|
|
|
|
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, d.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *collector) connectAndCollect(d *config.Device, ch chan<- prometheus.Metric) error {
|
|
|
|
cl, err := c.connect(d)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"device": d.Name,
|
|
|
|
"error": err,
|
|
|
|
}).Error("error dialing device")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cl.Close()
|
|
|
|
|
|
|
|
for _, co := range c.collectors {
|
2018-04-11 15:21:38 +02:00
|
|
|
ctx := &collectorContext{ch, d, cl}
|
|
|
|
err = co.collect(ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *collector) connect(d *config.Device) (*routeros.Client, error) {
|
|
|
|
if !c.enableTLS {
|
|
|
|
return routeros.DialTimeout(d.Address+apiPort, d.User, d.Password, c.timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
tls := &tls.Config{
|
|
|
|
InsecureSkipVerify: c.insecureTLS,
|
|
|
|
}
|
|
|
|
return routeros.DialTLSTimeout(d.Address+apiPortTLS, d.User, d.Password, tls, c.timeout)
|
2017-11-29 05:58:39 +01:00
|
|
|
}
|