2016-01-27 21:21:36 +00:00
|
|
|
package internal_models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-12 23:06:27 +00:00
|
|
|
"fmt"
|
2016-01-22 18:54:12 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
"github.com/gobwas/glob"
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-22 18:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TagFilter is the name of a tag, and the values on which to filter
|
|
|
|
type TagFilter struct {
|
|
|
|
Name string
|
|
|
|
Filter []string
|
2016-04-12 23:06:27 +00:00
|
|
|
filter glob.Glob
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter containing drop/pass and tagdrop/tagpass rules
|
|
|
|
type Filter struct {
|
2016-02-20 05:35:12 +00:00
|
|
|
NameDrop []string
|
2016-04-12 23:06:27 +00:00
|
|
|
nameDrop glob.Glob
|
2016-02-20 05:35:12 +00:00
|
|
|
NamePass []string
|
2016-04-12 23:06:27 +00:00
|
|
|
namePass glob.Glob
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
FieldDrop []string
|
2016-04-12 23:06:27 +00:00
|
|
|
fieldDrop glob.Glob
|
2016-02-20 05:35:12 +00:00
|
|
|
FieldPass []string
|
2016-04-12 23:06:27 +00:00
|
|
|
fieldPass glob.Glob
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
TagDrop []TagFilter
|
|
|
|
TagPass []TagFilter
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
TagExclude []string
|
|
|
|
tagExclude glob.Glob
|
|
|
|
TagInclude []string
|
|
|
|
tagInclude glob.Glob
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
IsActive bool
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
// Compile all Filter lists into glob.Glob objects.
|
|
|
|
func (f *Filter) CompileFilter() error {
|
|
|
|
var err error
|
|
|
|
f.nameDrop, err = compileFilter(f.NameDrop)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'namedrop', %s", err)
|
|
|
|
}
|
|
|
|
f.namePass, err = compileFilter(f.NamePass)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'namepass', %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.fieldDrop, err = compileFilter(f.FieldDrop)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'fielddrop', %s", err)
|
|
|
|
}
|
|
|
|
f.fieldPass, err = compileFilter(f.FieldPass)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'fieldpass', %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.tagExclude, err = compileFilter(f.TagExclude)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagexclude', %s", err)
|
|
|
|
}
|
|
|
|
f.tagInclude, err = compileFilter(f.TagInclude)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'taginclude', %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, _ := range f.TagDrop {
|
|
|
|
f.TagDrop[i].filter, err = compileFilter(f.TagDrop[i].Filter)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagdrop', %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, _ := range f.TagPass {
|
|
|
|
f.TagPass[i].filter, err = compileFilter(f.TagPass[i].Filter)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagpass', %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileFilter(filter []string) (glob.Glob, error) {
|
|
|
|
if len(filter) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var g glob.Glob
|
|
|
|
var err error
|
|
|
|
if len(filter) == 1 {
|
|
|
|
g, err = glob.Compile(filter[0])
|
|
|
|
} else {
|
|
|
|
g, err = glob.Compile("{" + strings.Join(filter, ",") + "}")
|
|
|
|
}
|
|
|
|
return g, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) ShouldMetricPass(metric telegraf.Metric) bool {
|
2016-02-22 20:35:06 +00:00
|
|
|
if f.ShouldNamePass(metric.Name()) && f.ShouldTagsPass(metric.Tags()) {
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
// ShouldFieldsPass returns true if the metric should pass, false if should drop
|
|
|
|
// based on the drop/pass filter parameters
|
2016-04-12 23:06:27 +00:00
|
|
|
func (f *Filter) ShouldNamePass(key string) bool {
|
|
|
|
if f.namePass != nil {
|
|
|
|
if f.namePass.Match(key) {
|
|
|
|
return true
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.nameDrop != nil {
|
|
|
|
if f.nameDrop.Match(key) {
|
|
|
|
return false
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldFieldsPass returns true if the metric should pass, false if should drop
|
2016-01-22 18:54:12 +00:00
|
|
|
// based on the drop/pass filter parameters
|
2016-04-12 23:06:27 +00:00
|
|
|
func (f *Filter) ShouldFieldsPass(key string) bool {
|
|
|
|
if f.fieldPass != nil {
|
|
|
|
if f.fieldPass.Match(key) {
|
|
|
|
return true
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.fieldDrop != nil {
|
|
|
|
if f.fieldDrop.Match(key) {
|
|
|
|
return false
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldTagsPass returns true if the metric should pass, false if should drop
|
|
|
|
// based on the tagdrop/tagpass filter parameters
|
2016-04-12 23:06:27 +00:00
|
|
|
func (f *Filter) ShouldTagsPass(tags map[string]string) bool {
|
2016-01-22 18:54:12 +00:00
|
|
|
if f.TagPass != nil {
|
|
|
|
for _, pat := range f.TagPass {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter.Match(tagval) {
|
|
|
|
return true
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.TagDrop != nil {
|
|
|
|
for _, pat := range f.TagDrop {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter.Match(tagval) {
|
|
|
|
return false
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
// Apply TagInclude and TagExclude filters.
|
|
|
|
// modifies the tags map in-place.
|
|
|
|
func (f *Filter) FilterTags(tags map[string]string) {
|
|
|
|
if f.tagInclude != nil {
|
|
|
|
for k, _ := range tags {
|
|
|
|
if !f.tagInclude.Match(k) {
|
|
|
|
delete(tags, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.tagExclude != nil {
|
|
|
|
for k, _ := range tags {
|
|
|
|
if f.tagExclude.Match(k) {
|
|
|
|
delete(tags, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|