Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Elastic-Agent] Use data subfolder as default for process logs #17960

Merged
merged 13 commits into from
Apr 29, 2020
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
- Pack ECS metadata to request payload send to fleet {pull}17894[17894]
- Allow CLI overrides of paths {pull}17781[17781]
- Enable Filebeat input: S3, Azureeventhub, cloudfoundry, httpjson, netflow, o365audit. {pull}17909[17909]
- Use data subfolder as default for process logs {pull}17960[17960]
- Enable debug log level for Metricbeat and Filebeat when run under the Elastic Agent. {pull}17935[17935]
64 changes: 3 additions & 61 deletions x-pack/elastic-agent/pkg/agent/application/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,19 @@
package application

import (
"os"
"path/filepath"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
)

var (
homePath string
dataPath string
overwrites *common.Config
)

func init() {
homePath = retrieveExecutablePath()
dataPath = retrieveDataPath()
overwrites = common.NewConfig()
common.ConfigOverwriteFlag(nil, overwrites, "path.home", "path.home", "", "Agent root path")
common.ConfigOverwriteFlag(nil, overwrites, "path.data", "path.data", "", "Data path contains Agent managed binaries")
}

// HomePath returns home path where.
func HomePath() string {
if val, err := overwrites.String("path.home", -1); err == nil {
return val
}

return homePath
}

// DataPath returns data path where.
func DataPath() string {
if val, err := overwrites.String("path.data", -1); err == nil {
return val
}

return dataPath
}

// InjectAgentConfig injects config to a provided configuration.
func InjectAgentConfig(c *config.Config) error {
globalConfig := agentGlobalConfig()
if err := c.Merge(globalConfig); err != nil {
return errors.New("failed to inject agent global config", err, errors.TypeConfig)
}

return injectOverwrites(c)
}

func injectOverwrites(c *config.Config) error {
if err := c.Merge(overwrites); err != nil {
return errors.New("failed to inject agent overwrites", err, errors.TypeConfig)
}

return nil
}

Expand All @@ -68,24 +26,8 @@ func injectOverwrites(c *config.Config) error {
func agentGlobalConfig() map[string]interface{} {
return map[string]interface{}{
"path": map[string]interface{}{
"data": dataPath,
"home": homePath,
"data": paths.Data(),
"home": paths.Home(),
},
}
}

// retrieveExecutablePath returns a directory where binary lives
// Executable is not supported on nacl.
func retrieveExecutablePath() string {
execPath, err := os.Executable()
if err != nil {
panic(err)
}

return filepath.Dir(execPath)
}

// retrieveHomePath returns a home directory of current user
func retrieveDataPath() string {
return filepath.Join(retrieveExecutablePath(), "data")
}
45 changes: 45 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package paths

import (
"flag"
"os"
"path/filepath"
)

var (
homePath string
dataPath string
)

func init() {
exePath := retrieveExecutablePath()

fs := flag.CommandLine
fs.StringVar(&homePath, "path.home", exePath, "Agent root path")
fs.StringVar(&dataPath, "path.data", filepath.Join(exePath, "data"), "Data path contains Agent managed binaries")
}

// Home returns a directory where binary lives
// Executable is not supported on nacl.
func Home() string {
return homePath
}

// Data returns a home directory of current user
func Data() string {
return dataPath
}

func retrieveExecutablePath() string {

execPath, err := os.Executable()
if err != nil {
panic(err)
}

return filepath.Dir(execPath)
}
6 changes: 3 additions & 3 deletions x-pack/elastic-agent/pkg/agent/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/basecmd"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
)
Expand All @@ -27,8 +27,8 @@ type globalFlags struct {

// Config returns path which identifies configuration file.
func (f *globalFlags) Config() string {
if len(f.PathConfigFile) == 0 {
return filepath.Join(application.HomePath(), defaultConfig)
if len(f.PathConfigFile) == 0 || f.PathConfigFile == defaultConfig {
return filepath.Join(paths.Home(), defaultConfig)
}
return f.PathConfigFile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ type testCase struct {

func TestMonitoringDrops(t *testing.T) {
cases := []testCase{
testCase{`/var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`npipe://drop`, ""},
testCase{`http+npipe://drop`, ""},
testCase{`\\.\pipe\drop`, ""},
testCase{`unix:///var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`http+unix:///var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`file:///var/lib/drop/abc.sock`, "/var/lib/drop"},
testCase{`http://localhost/stats`, ""},
testCase{`localhost/stats`, ""},
testCase{`http://localhost:8080/stats`, ""},
testCase{`localhost:8080/stats`, ""},
testCase{`http://1.2.3.4/stats`, ""},
testCase{`http://1.2.3.4:5678/stats`, ""},
testCase{`1.2.3.4:5678/stats`, ""},
testCase{`http://hithere.com:5678/stats`, ""},
testCase{`hithere.com:5678/stats`, ""},
{`/var/lib/drop/abc.sock`, "/var/lib/drop"},
{`npipe://drop`, ""},
{`http+npipe://drop`, ""},
{`\\.\pipe\drop`, ""},
{`unix:///var/lib/drop/abc.sock`, "/var/lib/drop"},
{`http+unix:///var/lib/drop/abc.sock`, "/var/lib/drop"},
{`file:///var/lib/drop/abc.sock`, "/var/lib/drop"},
{`http://localhost/stats`, ""},
{`localhost/stats`, ""},
{`http://localhost:8080/stats`, ""},
{`localhost:8080/stats`, ""},
{`http://1.2.3.4/stats`, ""},
{`http://1.2.3.4:5678/stats`, ""},
{`1.2.3.4:5678/stats`, ""},
{`http://hithere.com:5678/stats`, ""},
{`hithere.com:5678/stats`, ""},
}

for _, c := range cases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ package beats

import (
"fmt"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
)

const (
// args: pipeline name, application name
logFileFormat = "/var/log/elastic-agent/%s/%s"
// args: install path, pipeline name, application name
logFileFormatWin = "%s\\logs\\elastic-agent\\%s\\%s"
// args: data path, pipeline name, application name
logFileFormat = "%s/logs/%s/%s"
// args: data path, install path, pipeline name, application name
logFileFormatWin = "%s\\logs\\%s\\%s"

// args: pipeline name, application name
mbEndpointFileFormat = "unix:///var/run/elastic-agent/%s/%s/%s.sock"
mbEndpointFileFormat = "unix://%s/run/%s/%s/%s.sock"
// args: pipeline name, application name
mbEndpointFileFormatWin = `npipe:///%s-%s`
)
Expand All @@ -25,13 +27,13 @@ func getMonitoringEndpoint(program, operatingSystem, pipelineID string) string {
return fmt.Sprintf(mbEndpointFileFormatWin, pipelineID, program)
}

return fmt.Sprintf(mbEndpointFileFormat, pipelineID, program, program)
return fmt.Sprintf(mbEndpointFileFormat, paths.Data(), pipelineID, program, program)
}

func getLoggingFile(program, operatingSystem, installPath, pipelineID string) string {
if operatingSystem == "windows" {
return fmt.Sprintf(logFileFormatWin, installPath, pipelineID, program)
return fmt.Sprintf(logFileFormatWin, paths.Data(), pipelineID, program)
}

return fmt.Sprintf(logFileFormat, pipelineID, program)
return fmt.Sprintf(logFileFormat, paths.Data(), pipelineID, program)
}
8 changes: 2 additions & 6 deletions x-pack/elastic-agent/pkg/core/plugin/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"gopkg.in/yaml.v2"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/authority"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/process"
Expand Down Expand Up @@ -210,12 +211,7 @@ func (a *Application) checkGrpcHTTP(ctx context.Context, address string, ca *aut
}

func injectDataPath(args []string, pipelineID, id string) []string {
wd := ""
if w, err := os.Getwd(); err == nil {
wd = w
}

dataPath := filepath.Join(wd, "data", pipelineID, id)
dataPath := filepath.Join(paths.Data(), pipelineID, id)
return append(args, "-E", "path.data="+dataPath)
}

Expand Down