2016-07-28 11:31:11 +00:00
|
|
|
package 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
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-06-02 17:47:15 +00:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
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-06-02 17:47:15 +00:00
|
|
|
filter filter.Filter
|
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-06-02 17:47:15 +00:00
|
|
|
nameDrop filter.Filter
|
2016-02-20 05:35:12 +00:00
|
|
|
NamePass []string
|
2016-06-02 17:47:15 +00:00
|
|
|
namePass filter.Filter
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
FieldDrop []string
|
2016-06-02 17:47:15 +00:00
|
|
|
fieldDrop filter.Filter
|
2016-02-20 05:35:12 +00:00
|
|
|
FieldPass []string
|
2016-06-02 17:47:15 +00:00
|
|
|
fieldPass filter.Filter
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
TagDrop []TagFilter
|
|
|
|
TagPass []TagFilter
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
TagExclude []string
|
2016-06-02 17:47:15 +00:00
|
|
|
tagExclude filter.Filter
|
2016-04-12 23:06:27 +00:00
|
|
|
TagInclude []string
|
2016-06-02 17:47:15 +00:00
|
|
|
tagInclude filter.Filter
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
isActive bool
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
// Compile all Filter lists into filter.Filter objects.
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) Compile() error {
|
|
|
|
if len(f.NameDrop) == 0 &&
|
|
|
|
len(f.NamePass) == 0 &&
|
|
|
|
len(f.FieldDrop) == 0 &&
|
|
|
|
len(f.FieldPass) == 0 &&
|
|
|
|
len(f.TagInclude) == 0 &&
|
|
|
|
len(f.TagExclude) == 0 &&
|
|
|
|
len(f.TagPass) == 0 &&
|
|
|
|
len(f.TagDrop) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.isActive = true
|
2016-04-12 23:06:27 +00:00
|
|
|
var err error
|
2016-09-05 15:16:37 +00:00
|
|
|
f.nameDrop, err = filter.Compile(f.NameDrop)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'namedrop', %s", err)
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
f.namePass, err = filter.Compile(f.NamePass)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'namepass', %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.fieldDrop, err = filter.Compile(f.FieldDrop)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'fielddrop', %s", err)
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
f.fieldPass, err = filter.Compile(f.FieldPass)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'fieldpass', %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.tagExclude, err = filter.Compile(f.TagExclude)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagexclude', %s", err)
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
f.tagInclude, err = filter.Compile(f.TagInclude)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'taginclude', %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:32:54 +00:00
|
|
|
for i := range f.TagDrop {
|
2016-09-05 15:16:37 +00:00
|
|
|
f.TagDrop[i].filter, err = filter.Compile(f.TagDrop[i].Filter)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagdrop', %s", err)
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 20:32:54 +00:00
|
|
|
for i := range f.TagPass {
|
2016-09-05 15:16:37 +00:00
|
|
|
f.TagPass[i].filter, err = filter.Compile(f.TagPass[i].Filter)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagpass', %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// Select returns true if the metric matches according to the
|
|
|
|
// namepass/namedrop and tagpass/tagdrop filters. The metric is not modified.
|
|
|
|
func (f *Filter) Select(metric telegraf.Metric) bool {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.isActive {
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
if !f.shouldNamePass(metric.Name()) {
|
2016-09-05 15:16:37 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
if !f.shouldTagsPass(metric.TagList()) {
|
2016-09-05 15:16:37 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// Modify removes any tags and fields from the metric according to the
|
|
|
|
// fieldpass/fielddrop and taginclude/tagexclude filters.
|
|
|
|
func (f *Filter) Modify(metric telegraf.Metric) {
|
|
|
|
if !f.isActive {
|
|
|
|
return
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
f.filterFields(metric)
|
|
|
|
f.filterTags(metric)
|
2016-09-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
// IsActive checking if filter is active
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) IsActive() bool {
|
|
|
|
return f.isActive
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// shouldNamePass returns true if the metric should pass, false if should drop
|
2016-02-20 05:35:12 +00:00
|
|
|
// based on the drop/pass filter parameters
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) shouldNamePass(key string) bool {
|
2017-07-21 17:53:57 +00:00
|
|
|
pass := func(f *Filter) bool {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.namePass.Match(key) {
|
|
|
|
return true
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
drop := func(f *Filter) bool {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.nameDrop.Match(key) {
|
|
|
|
return false
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
2017-07-21 17:53:57 +00:00
|
|
|
return true
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
2017-07-21 17:53:57 +00:00
|
|
|
|
|
|
|
if f.namePass != nil && f.nameDrop != nil {
|
|
|
|
return pass(f) && drop(f)
|
|
|
|
} else if f.namePass != nil {
|
|
|
|
return pass(f)
|
|
|
|
} else if f.nameDrop != nil {
|
|
|
|
return drop(f)
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// shouldFieldPass 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-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) shouldFieldPass(key string) bool {
|
2017-07-21 17:53:57 +00:00
|
|
|
if f.fieldPass != nil && f.fieldDrop != nil {
|
2018-09-28 21:48:20 +00:00
|
|
|
return f.fieldPass.Match(key) && !f.fieldDrop.Match(key)
|
2017-07-21 17:53:57 +00:00
|
|
|
} else if f.fieldPass != nil {
|
2018-09-28 21:48:20 +00:00
|
|
|
return f.fieldPass.Match(key)
|
2017-07-21 17:53:57 +00:00
|
|
|
} else if f.fieldDrop != nil {
|
2018-09-28 21:48:20 +00:00
|
|
|
return !f.fieldDrop.Match(key)
|
2017-07-21 17:53:57 +00:00
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// shouldTagsPass returns true if the metric should pass, false if should drop
|
2016-01-22 18:54:12 +00:00
|
|
|
// based on the tagdrop/tagpass filter parameters
|
2018-09-28 21:48:20 +00:00
|
|
|
func (f *Filter) shouldTagsPass(tags []*telegraf.Tag) bool {
|
2017-07-21 17:53:57 +00:00
|
|
|
pass := func(f *Filter) bool {
|
2016-01-22 18:54:12 +00:00
|
|
|
for _, pat := range f.TagPass {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, tag := range tags {
|
|
|
|
if tag.Key == pat.Name {
|
|
|
|
if pat.filter.Match(tag.Value) {
|
|
|
|
return true
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
drop := func(f *Filter) bool {
|
2016-01-22 18:54:12 +00:00
|
|
|
for _, pat := range f.TagDrop {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, tag := range tags {
|
|
|
|
if tag.Key == pat.Name {
|
|
|
|
if pat.filter.Match(tag.Value) {
|
|
|
|
return false
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:08:40 +00:00
|
|
|
// Add additional logic in case where both parameters are set.
|
|
|
|
// see: https://github.com/influxdata/telegraf/issues/2860
|
|
|
|
if f.TagPass != nil && f.TagDrop != nil {
|
|
|
|
// return true only in case when tag pass and won't be dropped (true, true).
|
|
|
|
// in case when the same tag should be passed and dropped it will be dropped (true, false).
|
2017-07-21 17:53:57 +00:00
|
|
|
return pass(f) && drop(f)
|
2017-07-19 20:08:40 +00:00
|
|
|
} else if f.TagPass != nil {
|
2017-07-21 17:53:57 +00:00
|
|
|
return pass(f)
|
2017-07-19 20:08:40 +00:00
|
|
|
} else if f.TagDrop != nil {
|
2017-07-21 17:53:57 +00:00
|
|
|
return drop(f)
|
2017-07-19 20:08:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// filterFields removes fields according to fieldpass/fielddrop.
|
|
|
|
func (f *Filter) filterFields(metric telegraf.Metric) {
|
|
|
|
filterKeys := []string{}
|
|
|
|
for _, field := range metric.FieldList() {
|
|
|
|
if !f.shouldFieldPass(field.Key) {
|
|
|
|
filterKeys = append(filterKeys, field.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range filterKeys {
|
|
|
|
metric.RemoveField(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterTags removes tags according to taginclude/tagexclude.
|
|
|
|
func (f *Filter) filterTags(metric telegraf.Metric) {
|
|
|
|
filterKeys := []string{}
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.tagInclude != nil {
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, tag := range metric.TagList() {
|
|
|
|
if !f.tagInclude.Match(tag.Key) {
|
|
|
|
filterKeys = append(filterKeys, tag.Key)
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, key := range filterKeys {
|
|
|
|
metric.RemoveTag(key)
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
if f.tagExclude != nil {
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, tag := range metric.TagList() {
|
|
|
|
if f.tagExclude.Match(tag.Key) {
|
|
|
|
filterKeys = append(filterKeys, tag.Key)
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, key := range filterKeys {
|
|
|
|
metric.RemoveTag(key)
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|