Browse Source

Allows netdev devices to be ignored

pull/94/head
Ian Hansen 9 years ago
parent
commit
64affe205b
  1. 23
      collector/netdev.go
  2. 11
      collector/netdev_test.go

23
collector/netdev.go

@ -4,6 +4,7 @@ package collector
import (
"bufio"
"flag"
"fmt"
"io"
"os"
@ -12,6 +13,7 @@ import (
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
const (
@ -20,10 +22,13 @@ const (
)
var (
fieldSep = regexp.MustCompile("[ :] *")
fieldSep = regexp.MustCompile("[ :] *")
netdevIgnoredDevices = flag.String("collector.netdev.ignored-devices", "^$", "Regexp of net devices to ignore for netdev collector.")
)
type netDevCollector struct {
ignoredDevicesPattern *regexp.Regexp
metrics map[string]*prometheus.GaugeVec
}
@ -35,12 +40,13 @@ func init() {
// network device stats.
func NewNetDevCollector() (Collector, error) {
return &netDevCollector{
metrics: map[string]*prometheus.GaugeVec{},
ignoredDevicesPattern: regexp.MustCompile(*netdevIgnoredDevices),
metrics: map[string]*prometheus.GaugeVec{},
}, nil
}
func (c *netDevCollector) Update(ch chan<- prometheus.Metric) (err error) {
netDev, err := getNetDevStats()
netDev, err := getNetDevStats(c.ignoredDevicesPattern)
if err != nil {
return fmt.Errorf("Couldn't get netstats: %s", err)
}
@ -73,17 +79,17 @@ func (c *netDevCollector) Update(ch chan<- prometheus.Metric) (err error) {
return err
}
func getNetDevStats() (map[string]map[string]map[string]string, error) {
func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]map[string]string, error) {
file, err := os.Open(procNetDev)
if err != nil {
return nil, err
}
defer file.Close()
return parseNetDevStats(file)
return parseNetDevStats(file, ignore)
}
func parseNetDevStats(r io.Reader) (map[string]map[string]map[string]string, error) {
func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string]map[string]string, error) {
netDev := map[string]map[string]map[string]string{}
netDev["transmit"] = map[string]map[string]string{}
netDev["receive"] = map[string]map[string]string{}
@ -115,6 +121,11 @@ func parseNetDevStats(r io.Reader) (map[string]map[string]map[string]string, err
if err != nil {
return nil, err
}
if ignore.MatchString(dev) {
log.Debugf("Ignoring device: %s", dev)
continue
}
netDev["transmit"][dev] = transmit
netDev["receive"][dev] = receive
}

11
collector/netdev_test.go

@ -2,6 +2,7 @@ package collector
import (
"os"
"regexp"
"testing"
)
@ -12,7 +13,7 @@ func TestNetDevStats(t *testing.T) {
}
defer file.Close()
netStats, err := parseNetDevStats(file)
netStats, err := parseNetDevStats(file, regexp.MustCompile("^veth"))
if err != nil {
t.Fatal(err)
}
@ -28,4 +29,12 @@ func TestNetDevStats(t *testing.T) {
if want, got := "934", netStats["transmit"]["tun0"]["packets"]; want != got {
t.Errorf("want netstat tun0 packets %s, got %s", want, got)
}
if want, got := 6, len(netStats["transmit"]); want != got {
t.Errorf("want count of devices to be %d, got %d", want, got)
}
if _, ok := netStats["transmit"]["veth4B09XN"]; ok != false {
t.Error("want fixture interface veth4B09XN to not exist, but it does")
}
}

Loading…
Cancel
Save