Skip to content

Commit

Permalink
Add option to support moved filebeat modules (#9432)
Browse files Browse the repository at this point in the history
If a module needs to be moved, existing configuration will stop working,
this change adds an option to mark an old module as moved to other name.

This is being considered for the movement of apache2 module to apache to
be coherent with metricbeat module (#9402).
  • Loading branch information
jsoriano authored Dec 21, 2018
1 parent 8309a97 commit 1b83f5e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

- Added module for parsing Google Santa logs. {pull}9540[9540]
- Added netflow input type that supports NetFlow v1, v5, v6, v7, v8, v9 and IPFIX. {issue}9399[9399]
- Add option to modules.yml file to indicate that a module has been moved {pull}9432[9432].

*Heartbeat*

Expand Down
26 changes: 26 additions & 0 deletions filebeat/fileset/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"

"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/kibana"
Expand Down Expand Up @@ -57,6 +58,12 @@ func newModuleRegistry(modulesPath string,
continue
}

// Look for moved modules
if module, moved := getCurrentModuleName(modulesPath, mcfg.Module); moved {
logp.Warn("Using old name '%s' for module '%s', please update your configuration", mcfg.Module, module)
mcfg.Module = module
}

reg.registry[mcfg.Module] = map[string]*Fileset{}
moduleFilesets, err := getModuleFilesets(modulesPath, mcfg.Module)
if err != nil {
Expand Down Expand Up @@ -180,7 +187,26 @@ func mcfgFromConfig(cfg *common.Config) (*ModuleConfig, error) {
return &mcfg, nil
}

func getCurrentModuleName(modulePath, module string) (string, bool) {
moduleConfigPath := filepath.Join(modulePath, module, "module.yml")
d, err := ioutil.ReadFile(moduleConfigPath)
if err != nil {
return module, false
}

var moduleConfig struct {
MovedTo string `yaml:"movedTo"`
}
err = yaml.Unmarshal(d, &moduleConfig)
if err == nil && moduleConfig.MovedTo != "" {
return moduleConfig.MovedTo, true
}

return module, false
}

func getModuleFilesets(modulePath, module string) ([]string, error) {
module, _ = getCurrentModuleName(modulePath, module)
fileInfos, err := ioutil.ReadDir(filepath.Join(modulePath, module))
if err != nil {
return []string{}, err
Expand Down
18 changes: 18 additions & 0 deletions filebeat/fileset/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ func TestNewModuleRegistryConfig(t *testing.T) {
assert.NotContains(t, reg.registry["nginx"], "error")
}

func TestMovedModule(t *testing.T) {
modulesPath, err := filepath.Abs("./test/moved_module")
assert.NoError(t, err)

configs := []*ModuleConfig{
&ModuleConfig{
Module: "old",
Filesets: map[string]*FilesetConfig{
"test": {},
},
},
}

reg, err := newModuleRegistry(modulesPath, configs, nil, "5.2.0")
assert.NoError(t, err)
assert.NotNil(t, reg)
}

func TestApplyOverrides(t *testing.T) {
falseVar := false
trueVar := true
Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions filebeat/fileset/test/moved_module/old/module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
movedTo: new

0 comments on commit 1b83f5e

Please sign in to comment.