Merge pull request #94 from supershabam/master

Allows netdev devices to be ignored
This commit is contained in:
Julius Volz 2015-07-14 01:50:19 +02:00
commit 0f0daef4e0
2 changed files with 27 additions and 7 deletions

View File

@ -4,6 +4,7 @@ package collector
import ( import (
"bufio" "bufio"
"flag"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -12,6 +13,7 @@ import (
"strings" "strings"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
) )
const ( const (
@ -21,9 +23,12 @@ const (
var ( 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 { type netDevCollector struct {
ignoredDevicesPattern *regexp.Regexp
metrics map[string]*prometheus.GaugeVec metrics map[string]*prometheus.GaugeVec
} }
@ -35,12 +40,13 @@ func init() {
// network device stats. // network device stats.
func NewNetDevCollector() (Collector, error) { func NewNetDevCollector() (Collector, error) {
return &netDevCollector{ return &netDevCollector{
ignoredDevicesPattern: regexp.MustCompile(*netdevIgnoredDevices),
metrics: map[string]*prometheus.GaugeVec{}, metrics: map[string]*prometheus.GaugeVec{},
}, nil }, nil
} }
func (c *netDevCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *netDevCollector) Update(ch chan<- prometheus.Metric) (err error) {
netDev, err := getNetDevStats() netDev, err := getNetDevStats(c.ignoredDevicesPattern)
if err != nil { if err != nil {
return fmt.Errorf("Couldn't get netstats: %s", err) 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 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) file, err := os.Open(procNetDev)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close() 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 := map[string]map[string]map[string]string{}
netDev["transmit"] = map[string]map[string]string{} netDev["transmit"] = map[string]map[string]string{}
netDev["receive"] = 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 { if err != nil {
return nil, err return nil, err
} }
if ignore.MatchString(dev) {
log.Debugf("Ignoring device: %s", dev)
continue
}
netDev["transmit"][dev] = transmit netDev["transmit"][dev] = transmit
netDev["receive"][dev] = receive netDev["receive"][dev] = receive
} }

View File

@ -2,6 +2,7 @@ package collector
import ( import (
"os" "os"
"regexp"
"testing" "testing"
) )
@ -12,7 +13,7 @@ func TestNetDevStats(t *testing.T) {
} }
defer file.Close() defer file.Close()
netStats, err := parseNetDevStats(file) netStats, err := parseNetDevStats(file, regexp.MustCompile("^veth"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -28,4 +29,12 @@ func TestNetDevStats(t *testing.T) {
if want, got := "934", netStats["transmit"]["tun0"]["packets"]; want != got { if want, got := "934", netStats["transmit"]["tun0"]["packets"]; want != got {
t.Errorf("want netstat tun0 packets %s, got %s", 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")
}
} }