[GO][ADD] Added a new collector for detect-internet
This commit is contained in:
@@ -175,6 +175,13 @@ func WithIpsec() Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithInternet enables internet access metrics
|
||||||
|
func WithInternet() Option {
|
||||||
|
return func(c *collector) {
|
||||||
|
c.collectors = append(c.collectors, newInternetCollector())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Option applies options to collector
|
// Option applies options to collector
|
||||||
type Option func(*collector)
|
type Option func(*collector)
|
||||||
|
|
||||||
|
|||||||
81
collector/internet_collector.go
Normal file
81
collector/internet_collector.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package collector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type internetCollector struct {
|
||||||
|
props []string
|
||||||
|
descriptions map[string]*prometheus.Desc
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInternetCollector() routerOSCollector {
|
||||||
|
c := &internetCollector{}
|
||||||
|
c.init()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *internetCollector) init() {
|
||||||
|
// Properties other than 'name' that will be used in .proplist
|
||||||
|
c.props = []string{"state"}
|
||||||
|
// Description for the returned values in the metric
|
||||||
|
labelNames := []string{"devicename", "interface"}
|
||||||
|
c.descriptions = make(map[string]*prometheus.Desc)
|
||||||
|
for _, p := range c.props {
|
||||||
|
c.descriptions[p] = descriptionForPropertyName("internet", p, labelNames)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *internetCollector) describe(ch chan<- *prometheus.Desc) {
|
||||||
|
for _, d := range c.descriptions {
|
||||||
|
ch <- d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *internetCollector) collect(ctx *collectorContext) error {
|
||||||
|
reply, err := ctx.client.Run("/interface/detect-internet/state/print", "=.proplist=name,"+strings.Join(c.props, ","))
|
||||||
|
if err != nil {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"device": ctx.device.Name,
|
||||||
|
"error": err,
|
||||||
|
}).Error("error fetching detect-internet state")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range reply.Re {
|
||||||
|
for _, prop := range c.props {
|
||||||
|
v, ok := e.Map[prop]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
value := float64(c.valueForProp(prop, v))
|
||||||
|
ctx.ch <- prometheus.MustNewConstMetric(c.descriptions[prop], prometheus.GaugeValue, value, ctx.device.Name, e.Map["name"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Here are the corresponding values for detect-internet's output:
|
||||||
|
// - internet: 2
|
||||||
|
// - wan: 1
|
||||||
|
// - lan: 0
|
||||||
|
func (c *internetCollector) valueForProp(name, value string) int {
|
||||||
|
switch {
|
||||||
|
case name == "state":
|
||||||
|
return func(v string) int {
|
||||||
|
if v == "internet" {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
if v == "wan" {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}(value)
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ type Config struct {
|
|||||||
WlanIF bool `yaml:"wlanif,omitempty"`
|
WlanIF bool `yaml:"wlanif,omitempty"`
|
||||||
Monitor bool `yaml:"monitor,omitempty"`
|
Monitor bool `yaml:"monitor,omitempty"`
|
||||||
Ipsec bool `yaml:"ipsec,omitempty"`
|
Ipsec bool `yaml:"ipsec,omitempty"`
|
||||||
|
Internet bool `yaml:"internet,omitempty"`
|
||||||
} `yaml:"features,omitempty"`
|
} `yaml:"features,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
main.go
10
main.go
@@ -51,8 +51,8 @@ var (
|
|||||||
withWlanIF = flag.Bool("with-wlanif", false, "retrieves wlan interface metrics")
|
withWlanIF = flag.Bool("with-wlanif", false, "retrieves wlan interface metrics")
|
||||||
withMonitor = flag.Bool("with-monitor", false, "retrieves ethernet interface monitor info")
|
withMonitor = flag.Bool("with-monitor", false, "retrieves ethernet interface monitor info")
|
||||||
withIpsec = flag.Bool("with-ipsec", false, "retrieves ipsec metrics")
|
withIpsec = flag.Bool("with-ipsec", false, "retrieves ipsec metrics")
|
||||||
|
withInternet = flag.Bool("with-internet", false, "retrieves detect-internet interfaces")
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
|
|
||||||
appVersion = "DEVELOPMENT"
|
appVersion = "DEVELOPMENT"
|
||||||
shortSha = "0xDEADBEEF"
|
shortSha = "0xDEADBEEF"
|
||||||
@@ -241,12 +241,16 @@ func collectorOptions() []collector.Option {
|
|||||||
opts = append(opts, collector.WithIpsec())
|
opts = append(opts, collector.WithIpsec())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *withInternet || cfg.Features.Internet {
|
||||||
|
opts = append(opts, collector.WithInternet())
|
||||||
|
}
|
||||||
|
|
||||||
if *timeout != collector.DefaultTimeout {
|
if *timeout != collector.DefaultTimeout {
|
||||||
opts = append(opts, collector.WithTimeout(*timeout))
|
opts = append(opts, collector.WithTimeout(*timeout))
|
||||||
}
|
}
|
||||||
|
|
||||||
if *tls || cfg.Features.TLS {
|
if *tls || cfg.Features.TLS {
|
||||||
fmt.Printf("TLS activated\n");
|
fmt.Printf("TLS activated\n")
|
||||||
opts = append(opts, collector.WithTLS(*insecure))
|
opts = append(opts, collector.WithTLS(*insecure))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user