Add --collector.netdev.device-whitelist flag (#1279)

* Add --collector.netdev.device-whitelist flag

Sometimes it is desired to monitor only one netdev. The golang regexp
does not support a negated regex, so the ignored-devices flag is too
cumbersome for this task.
This change introduces a new flag: accept-devices, which is mutually
exclusive to ignored-devices. This flag allows specifying ONLY the
netdev you'd like.

Signed-off-by: Noam Meltzer <noam@cynerio.co>
This commit is contained in:
Noam Meltzer 2019-05-31 18:55:50 +03:00 committed by Ben Kochie
commit 501ccf9fb4
7 changed files with 72 additions and 16 deletions

View file

@ -17,6 +17,7 @@
package collector
import (
"errors"
"fmt"
"regexp"
"strconv"
@ -26,12 +27,14 @@ import (
)
var (
netdevIgnoredDevices = kingpin.Flag("collector.netdev.ignored-devices", "Regexp of net devices to ignore for netdev collector.").Default("^$").String()
netdevIgnoredDevices = kingpin.Flag("collector.netdev.device-blacklist", "Regexp of net devices to blacklist (mutually exclusive to device-whitelist).").String()
netdevAcceptDevices = kingpin.Flag("collector.netdev.device-whitelist", "Regexp of net devices to whitelist (mutually exclusive to device-blacklist).").String()
)
type netDevCollector struct {
subsystem string
ignoredDevicesPattern *regexp.Regexp
acceptDevicesPattern *regexp.Regexp
metricDescs map[string]*prometheus.Desc
}
@ -41,16 +44,30 @@ func init() {
// NewNetDevCollector returns a new Collector exposing network device stats.
func NewNetDevCollector() (Collector, error) {
pattern := regexp.MustCompile(*netdevIgnoredDevices)
if *netdevIgnoredDevices != "" && *netdevAcceptDevices != "" {
return nil, errors.New("device-blacklist & accept-devices are mutually exclusive")
}
var ignorePattern *regexp.Regexp = nil
if *netdevIgnoredDevices != "" {
ignorePattern = regexp.MustCompile(*netdevIgnoredDevices)
}
var acceptPattern *regexp.Regexp = nil
if *netdevAcceptDevices != "" {
acceptPattern = regexp.MustCompile(*netdevAcceptDevices)
}
return &netDevCollector{
subsystem: "network",
ignoredDevicesPattern: pattern,
ignoredDevicesPattern: ignorePattern,
acceptDevicesPattern: acceptPattern,
metricDescs: map[string]*prometheus.Desc{},
}, nil
}
func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error {
netDev, err := getNetDevStats(c.ignoredDevicesPattern)
netDev, err := getNetDevStats(c.ignoredDevicesPattern, c.acceptDevicesPattern)
if err != nil {
return fmt.Errorf("couldn't get netstats: %s", err)
}