Skip to content

Commit

Permalink
Create ipsec collector (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
maerlyn authored and nshttpd committed Oct 25, 2019
1 parent 0250e6c commit c757fc7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
7 changes: 7 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func WithTLS(insecure bool) Option {
}
}

// WithIpsec enables ipsec metrics
func WithIpsec() Option {
return func(c *collector) {
c.collectors = append(c.collectors, newIpsecCollector())
}
}

// Option applies options to collector
type Option func(*collector)

Expand Down
110 changes: 110 additions & 0 deletions collector/ipsec_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package collector

import (
"strconv"
"strings"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2/proto"
)

type ipsecCollector struct {
props []string
descriptions map[string]*prometheus.Desc
}

func newIpsecCollector() routerOSCollector {
c := &ipsecCollector{}
c.init()
return c
}

func (c *ipsecCollector) init() {
c.props = []string{"src-address", "dst-address", "ph2-state", "invalid", "active", "comment"}

labelNames := []string{"devicename", "srcdst", "comment"}
c.descriptions = make(map[string]*prometheus.Desc)
for _, p := range c.props[1:] {
c.descriptions[p] = descriptionForPropertyName("ipsec", p, labelNames)
}
}

func (c *ipsecCollector) describe(ch chan<- *prometheus.Desc) {
for _, d := range c.descriptions {
ch <- d
}
}

func (c *ipsecCollector) collect(ctx *collectorContext) error {
stats, err := c.fetch(ctx)
if err != nil {
return err
}

for _, re := range stats {
c.collectForStat(re, ctx)
}

return nil
}

func (c *ipsecCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
reply, err := ctx.client.Run("/ip/ipsec/policy/print", "?disabled=false", "?dynamic=false", "=.proplist="+strings.Join(c.props, ","))
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error fetching interface metrics")
return nil, err
}

return reply.Re, nil
}

func (c *ipsecCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
srcdst := re.Map["src-address"] + "-" + re.Map["dst-address"]
comment := re.Map["comment"]

for _, p := range c.props[2:] {
c.collectMetricForProperty(p, srcdst, comment, re, ctx)
}
}

func (c *ipsecCollector) collectMetricForProperty(property, srcdst, comment string, re *proto.Sentence, ctx *collectorContext) {
desc := c.descriptions[property]
if value := re.Map[property]; value != "" {
var v float64
var err error
v, err = strconv.ParseFloat(value, 64)

switch property {
case "ph2-state":
if value == "established" {
v, err = 1, nil
} else {
v, err = 0, nil
}
case "active", "invalid":
if value == "true" {
v, err = 1, nil
} else {
v, err = 0, nil
}
case "comment":
return
}

if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"srcdst": srcdst,
"property": property,
"value": value,
"error": err,
}).Error("error parsing ipsec metric value")
return
}
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, srcdst, comment)
}
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
WlanSTA bool `yaml:"wlansta,omitempty"`
WlanIF bool `yaml:"wlanif,omitempty"`
Monitor bool `yaml:"monitor,omitempty"`
Ipsec bool `yaml:"ipsec,omitempty"`
} `yaml:"features,omitempty"`
}

Expand Down
3 changes: 2 additions & 1 deletion config/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ features:
pools: true
optics: true
wlansta: true
wlanif: true
wlanif: true
ipsec: true
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestShouldParse(t *testing.T) {
assertFeature("Optics", c.Features.Optics, t)
assertFeature("WlanSTA", c.Features.WlanSTA, t)
assertFeature("WlanIF", c.Features.WlanIF, t)
assertFeature("Ipsec", c.Features.Ipsec, t)
}

func loadTestFile(t *testing.T) []byte {
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
withWlanSTA = flag.Bool("with-wlansta", false, "retrieves connected wlan station metrics")
withWlanIF = flag.Bool("with-wlanif", false, "retrieves wlan interface metrics")
withMonitor = flag.Bool("with-monitor", false, "retrieves ethernet interface monitor info")
withIpsec = flag.Bool("with-ipsec", false, "retrieves ipsec metrics")

cfg *config.Config

Expand Down Expand Up @@ -208,6 +209,10 @@ func collectorOptions() []collector.Option {

}

if *withIpsec || cfg.Features.Ipsec {
opts = append(opts, collector.WithIpsec())
}

if *timeout != collector.DefaultTimeout {
opts = append(opts, collector.WithTimeout(*timeout))
}
Expand Down

0 comments on commit c757fc7

Please sign in to comment.