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:
parent
fc02b5dfbc
commit
501ccf9fb4
7 changed files with 72 additions and 16 deletions
|
|
@ -31,17 +31,17 @@ var (
|
|||
procNetDevFieldSep = regexp.MustCompile(` +`)
|
||||
)
|
||||
|
||||
func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error) {
|
||||
func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) {
|
||||
file, err := os.Open(procFilePath("net/dev"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return parseNetDevStats(file, ignore)
|
||||
return parseNetDevStats(file, ignore, accept)
|
||||
}
|
||||
|
||||
func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string]string, error) {
|
||||
func parseNetDevStats(r io.Reader, ignore *regexp.Regexp, accept *regexp.Regexp) (map[string]map[string]string, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Scan() // skip first header
|
||||
scanner.Scan()
|
||||
|
|
@ -64,7 +64,11 @@ func parseNetDevStats(r io.Reader, ignore *regexp.Regexp) (map[string]map[string
|
|||
}
|
||||
|
||||
dev := parts[1]
|
||||
if ignore.MatchString(dev) {
|
||||
if ignore != nil && ignore.MatchString(dev) {
|
||||
log.Debugf("Ignoring device: %s", dev)
|
||||
continue
|
||||
}
|
||||
if accept != nil && !accept.MatchString(dev) {
|
||||
log.Debugf("Ignoring device: %s", dev)
|
||||
continue
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue