From 476500150cb2e5e8b4b94c96bbd8c54e4997a0b5 Mon Sep 17 00:00:00 2001 From: Adam <90734270+adam-mateen@users.noreply.github.com> Date: Fri, 6 Oct 2023 15:21:37 -0500 Subject: [PATCH 1/5] Make -otelconfig optional by adding a default. --- .../amazon-cloudwatch-agent.go | 3 +- cmd/start-amazon-cloudwatch-agent/path.go | 38 +++++---------- .../path_windows.go | 30 ------------ .../start-amazon-cloudwatch-agent.go | 46 ++++--------------- tool/paths/paths.go | 27 +++++++++++ tool/paths/paths_unix.go | 26 ++++++++--- tool/paths/paths_windows.go | 38 ++++++++++++--- 7 files changed, 103 insertions(+), 105 deletions(-) create mode 100644 tool/paths/paths.go diff --git a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go index ff6a3f0c99..e2ee07d1a3 100644 --- a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go +++ b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go @@ -42,6 +42,7 @@ import ( "github.com/aws/amazon-cloudwatch-agent/service/configprovider" "github.com/aws/amazon-cloudwatch-agent/service/defaultcomponents" "github.com/aws/amazon-cloudwatch-agent/service/registry" + "github.com/aws/amazon-cloudwatch-agent/tool/paths" ) const ( @@ -58,7 +59,7 @@ var fTest = flag.Bool("test", false, "enable test mode: gather metrics, print th var fTestWait = flag.Int("test-wait", 0, "wait up to this many seconds for service inputs to complete in test mode") var fSchemaTest = flag.Bool("schematest", false, "validate the toml file schema") var fConfig = flag.String("config", "", "configuration file to load") -var fOtelConfig = flag.String("otelconfig", "", "YAML configuration file to run OTel pipeline") +var fOtelConfig = flag.String("otelconfig", paths.YamlConfigPath, "YAML configuration file to run OTel pipeline") var fEnvConfig = flag.String("envconfig", "", "env configuration file to load") var fConfigDirectory = flag.String("config-directory", "", "directory containing additional *.conf files") diff --git a/cmd/start-amazon-cloudwatch-agent/path.go b/cmd/start-amazon-cloudwatch-agent/path.go index 3383b06777..bf17673c52 100644 --- a/cmd/start-amazon-cloudwatch-agent/path.go +++ b/cmd/start-amazon-cloudwatch-agent/path.go @@ -24,13 +24,13 @@ func startAgent(writer io.WriteCloser) error { if os.Getenv(config.RUN_IN_CONTAINER) == config.RUN_IN_CONTAINER_TRUE { // Use exec so PID 1 changes to agent from start-agent. execArgs := []string{ - agentBinaryPath, // when using syscall.Exec, must pass binary name as args[0] - "-config", tomlConfigPath, - "-envconfig", envConfigPath, - "-otelconfig", yamlConfigPath, + paths.AgentBinaryPath, // when using syscall.Exec, must pass binary name as args[0] + "-config", paths.TomlConfigPath, + "-envconfig", paths.EnvConfigPath, + "-otelconfig", paths.YamlConfigPath, "-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid", } - if err := syscall.Exec(agentBinaryPath, execArgs, os.Environ()); err != nil { + if err := syscall.Exec(paths.AgentBinaryPath, execArgs, os.Environ()); err != nil { return fmt.Errorf("error exec as agent binary: %w", err) } // We should never reach this line but the compiler doesn't know... @@ -49,7 +49,7 @@ func startAgent(writer io.WriteCloser) error { return err } - name, err := exec.LookPath(agentBinaryPath) + name, err := exec.LookPath(paths.AgentBinaryPath) if err != nil { log.Printf("E! Failed to lookpath: %v ", err) return err @@ -62,10 +62,10 @@ func startAgent(writer io.WriteCloser) error { // linux command has pid passed while windows does not agentCmd := []string{ - agentBinaryPath, - "-config", tomlConfigPath, - "-envconfig", envConfigPath, - "-otelconfig", yamlConfigPath, + paths.AgentBinaryPath, + "-config", paths.TomlConfigPath, + "-envconfig", paths.EnvConfigPath, + "-otelconfig", paths.YamlConfigPath, "-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid", } if err = syscall.Exec(name, agentCmd, os.Environ()); err != nil { @@ -80,22 +80,8 @@ func startAgent(writer io.WriteCloser) error { func generateMergedJsonConfigMap() (map[string]interface{}, error) { ctx := context.CurrentContext() setCTXOS(ctx) - ctx.SetInputJsonFilePath(jsonConfigPath) - ctx.SetInputJsonDirPath(jsonDirPath) + ctx.SetInputJsonFilePath(paths.JsonConfigPath) + ctx.SetInputJsonDirPath(paths.JsonDirPath) ctx.SetMultiConfig("remove") return cmdutil.GenerateMergedJsonConfigMap(ctx) } - -func init() { - jsonConfigPath = paths.AgentDir + "/etc/" + JSON - jsonDirPath = paths.AgentDir + "/etc/" + paths.JsonDir - envConfigPath = paths.AgentDir + "/etc/" + ENV - tomlConfigPath = paths.AgentDir + "/etc/" + TOML - commonConfigPath = paths.AgentDir + "/etc/" + COMMON_CONFIG - yamlConfigPath = paths.AgentDir + "/etc/" + YAML - - agentLogFilePath = paths.AgentDir + "/logs/" + AGENT_LOG_FILE - - translatorBinaryPath = paths.AgentDir + "/bin/" + paths.TranslatorBinaryName - agentBinaryPath = paths.AgentDir + "/bin/" + paths.AgentBinaryName -} diff --git a/cmd/start-amazon-cloudwatch-agent/path_windows.go b/cmd/start-amazon-cloudwatch-agent/path_windows.go index 549058b5b4..5e37e1109a 100644 --- a/cmd/start-amazon-cloudwatch-agent/path_windows.go +++ b/cmd/start-amazon-cloudwatch-agent/path_windows.go @@ -10,10 +10,7 @@ import ( "fmt" "io" "log" - "os" "os/exec" - - "github.com/aws/amazon-cloudwatch-agent/tool/paths" ) func startAgent(writer io.WriteCloser) error { @@ -33,30 +30,3 @@ func startAgent(writer io.WriteCloser) error { fmt.Printf("%s \n", stdoutStderr) return err } - -func init() { - programFiles := os.Getenv("ProgramFiles") - var programData string - if _, ok := os.LookupEnv("ProgramData"); ok { - programData = os.Getenv("ProgramData") - } else { - // Windows 2003 - programData = os.Getenv("ALLUSERSPROFILE") + "\\Application Data" - } - - agentRootDir := programFiles + paths.AgentDir - agentConfigDir := programData + paths.AgentDir - - jsonConfigPath = agentConfigDir + "\\" + JSON - jsonDirPath = agentConfigDir + paths.JsonDir - envConfigPath = agentConfigDir + "\\" + ENV - tomlConfigPath = agentConfigDir + "\\" + TOML - yamlConfigPath = agentConfigDir + "\\" + YAML - - commonConfigPath = agentConfigDir + "\\" + COMMON_CONFIG - - agentLogFilePath = agentConfigDir + "\\Logs\\" + AGENT_LOG_FILE - - translatorBinaryPath = agentRootDir + "\\" + paths.TranslatorBinaryName - agentBinaryPath = agentRootDir + "\\" + paths.AgentBinaryName -} diff --git a/cmd/start-amazon-cloudwatch-agent/start-amazon-cloudwatch-agent.go b/cmd/start-amazon-cloudwatch-agent/start-amazon-cloudwatch-agent.go index d6fa6f0457..0c23787f06 100644 --- a/cmd/start-amazon-cloudwatch-agent/start-amazon-cloudwatch-agent.go +++ b/cmd/start-amazon-cloudwatch-agent/start-amazon-cloudwatch-agent.go @@ -14,47 +14,21 @@ import ( "gopkg.in/natefinch/lumberjack.v2" + "github.com/aws/amazon-cloudwatch-agent/tool/paths" "github.com/aws/amazon-cloudwatch-agent/translator/config" ) -const ( - COMMON_CONFIG = "common-config.toml" - JSON = "amazon-cloudwatch-agent.json" - TOML = "amazon-cloudwatch-agent.toml" - YAML = "amazon-cloudwatch-agent.yaml" - ENV = "env-config.json" - - AGENT_LOG_FILE = "amazon-cloudwatch-agent.log" - - //TODO this CONFIG_DIR_IN_CONTAINER should change to something indicate dir, keep it for now to avoid break testing - CONFIG_DIR_IN_CONTAINER = "/etc/cwagentconfig" -) - -var ( - jsonConfigPath string - jsonDirPath string - envConfigPath string - tomlConfigPath string - commonConfigPath string - yamlConfigPath string - - agentLogFilePath string - - translatorBinaryPath string - agentBinaryPath string -) - // We use an environment variable here because we need this condition before the translator reads agent config json file. var runInContainer = os.Getenv(config.RUN_IN_CONTAINER) func translateConfig() error { - args := []string{"--output", tomlConfigPath, "--mode", "auto"} + args := []string{"--output", paths.TomlConfigPath, "--mode", "auto"} if runInContainer == config.RUN_IN_CONTAINER_TRUE { - args = append(args, "--input-dir", CONFIG_DIR_IN_CONTAINER) + args = append(args, "--input-dir", paths.CONFIG_DIR_IN_CONTAINER) } else { - args = append(args, "--input", jsonConfigPath, "--input-dir", jsonDirPath, "--config", commonConfigPath) + args = append(args, "--input", paths.JsonConfigPath, "--input-dir", paths.JsonDirPath, "--config", paths.CommonConfigPath) } - cmd := exec.Command(translatorBinaryPath, args...) + cmd := exec.Command(paths.TranslatorBinaryPath, args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stdout err := cmd.Run() @@ -83,7 +57,7 @@ func main() { if runInContainer != config.RUN_IN_CONTAINER_TRUE { writer = &lumberjack.Logger{ - Filename: agentLogFilePath, + Filename: paths.AgentLogFilePath, MaxSize: 100, //MB MaxBackups: 5, //backup files MaxAge: 7, //days @@ -96,10 +70,10 @@ func main() { if err := translateConfig(); err != nil { log.Fatalf("E! Cannot translate JSON, ERROR is %v \n", err) } - log.Printf("I! Config has been translated into TOML %s \n", tomlConfigPath) - printFileContents(tomlConfigPath) - log.Printf("I! Config has been translated into YAML %s \n", yamlConfigPath) - printFileContents(yamlConfigPath) + log.Printf("I! Config has been translated into TOML %s \n", paths.TomlConfigPath) + printFileContents(paths.TomlConfigPath) + log.Printf("I! Config has been translated into YAML %s \n", paths.YamlConfigPath) + printFileContents(paths.YamlConfigPath) if err := startAgent(writer); err != nil { log.Printf("E! Error when starting Agent, Error is %v \n", err) diff --git a/tool/paths/paths.go b/tool/paths/paths.go new file mode 100644 index 0000000000..78668ccacc --- /dev/null +++ b/tool/paths/paths.go @@ -0,0 +1,27 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT + +package paths + +const ( + COMMON_CONFIG = "common-config.toml" + JSON = "amazon-cloudwatch-agent.json" + TOML = "amazon-cloudwatch-agent.toml" + YAML = "amazon-cloudwatch-agent.yaml" + ENV = "env-config.json" + AGENT_LOG_FILE = "amazon-cloudwatch-agent.log" + //TODO this CONFIG_DIR_IN_CONTAINER should change to something indicate dir, keep it for now to avoid break testing + CONFIG_DIR_IN_CONTAINER = "/etc/cwagentconfig" +) + +var ( + JsonConfigPath string + JsonDirPath string + EnvConfigPath string + TomlConfigPath string + CommonConfigPath string + YamlConfigPath string + AgentLogFilePath string + TranslatorBinaryPath string + AgentBinaryPath string +) \ No newline at end of file diff --git a/tool/paths/paths_unix.go b/tool/paths/paths_unix.go index 111b0d622f..8c88a3664d 100644 --- a/tool/paths/paths_unix.go +++ b/tool/paths/paths_unix.go @@ -7,11 +7,25 @@ package paths const ( - AgentDir = "/opt/aws/amazon-cloudwatch-agent" - BinaryDir = "bin" - JsonDir = "amazon-cloudwatch-agent.d" + AgentDir = "/opt/aws/amazon-cloudwatch-agent" + BinaryDir = "bin" + JsonDir = "amazon-cloudwatch-agent.d" TranslatorBinaryName = "config-translator" - AgentBinaryName = "amazon-cloudwatch-agent" - WizardBinaryName = "amazon-cloudwatch-agent-config-wizard" - AgentStartName = "amazon-cloudwatch-agent-ctl" + AgentBinaryName = "amazon-cloudwatch-agent" + WizardBinaryName = "amazon-cloudwatch-agent-config-wizard" + AgentStartName = "amazon-cloudwatch-agent-ctl" ) + +func init() { + JsonConfigPath = AgentDir + "/etc/" + JSON + JsonDirPath = AgentDir + "/etc/" + JsonDir + EnvConfigPath = AgentDir + "/etc/" + ENV + TomlConfigPath = AgentDir + "/etc/" + TOML + CommonConfigPath = AgentDir + "/etc/" + COMMON_CONFIG + YamlConfigPath = AgentDir + "/etc/" + YAML + + AgentLogFilePath = AgentDir + "/logs/" + AGENT_LOG_FILE + + TranslatorBinaryPath = AgentDir + "/bin/" + TranslatorBinaryName + AgentBinaryPath = AgentDir + "/bin/" + AgentBinaryName +} \ No newline at end of file diff --git a/tool/paths/paths_windows.go b/tool/paths/paths_windows.go index ae96449be2..30ac0e4991 100644 --- a/tool/paths/paths_windows.go +++ b/tool/paths/paths_windows.go @@ -6,12 +6,38 @@ package paths +import "os" + const ( - AgentDir = "\\Amazon\\AmazonCloudWatchAgent\\" - JsonDir = "\\Configs" - BinaryDir = "bin" + AgentDir = "\\Amazon\\AmazonCloudWatchAgent\\" + JsonDir = "\\Configs" + BinaryDir = "bin" TranslatorBinaryName = "config-translator.exe" - AgentBinaryName = "amazon-cloudwatch-agent.exe" - WizardBinaryName = "amazon-cloudwatch-agent-config-wizard.exe" - AgentStartName = "amazon-cloudwatch-agent-ctl.ps1" + AgentBinaryName = "amazon-cloudwatch-agent.exe" + WizardBinaryName = "amazon-cloudwatch-agent-config-wizard.exe" + AgentStartName = "amazon-cloudwatch-agent-ctl.ps1" ) + +func init() { + programFiles := os.Getenv("ProgramFiles") + var programData string + if _, ok := os.LookupEnv("ProgramData"); ok { + programData = os.Getenv("ProgramData") + } else { + // Windows 2003 + programData = os.Getenv("ALLUSERSPROFILE") + "\\Application Data" + } + + AgentRootDir := programFiles + paths.AgentDir + AgentConfigDir := programData + paths.AgentDir + + JsonConfigPath = agentConfigDir + "\\" + JSON + JsonDirPath = agentConfigDir + paths.JsonDir + EnvConfigPath = agentConfigDir + "\\" + ENV + TomlConfigPath = agentConfigDir + "\\" + TOML + YamlConfigPath = agentConfigDir + "\\" + YAML + CommonConfigPath = agentConfigDir + "\\" + COMMON_CONFIG + AgentLogFilePath = agentConfigDir + "\\Logs\\" + AGENT_LOG_FILE + TranslatorBinaryPath = agentRootDir + "\\" + paths.TranslatorBinaryName + AgentBinaryPath = agentRootDir + "\\" + paths.AgentBinaryName +} \ No newline at end of file From bdd6cd435207bffed551407cf398d9e65f6f300b Mon Sep 17 00:00:00 2001 From: Adam <90734270+adam-mateen@users.noreply.github.com> Date: Fri, 6 Oct 2023 15:35:46 -0500 Subject: [PATCH 2/5] fix var names and gofmt --- .../amazon-cloudwatch-agent.go | 1 + .../path_windows.go | 9 ++--- tool/paths/paths.go | 26 +++++++------- tool/paths/paths_unix.go | 14 ++++---- tool/paths/paths_windows.go | 36 +++++++++---------- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go index e2ee07d1a3..1a94924912 100644 --- a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go +++ b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go @@ -259,6 +259,7 @@ func runAgent(ctx context.Context, logger.SetupLogging(logConfig) log.Printf("I! Starting AmazonCloudWatchAgent %s\n", agentinfo.FullVersion()) + log.Printf("I! config: %v, %v",*fConfig, *fOtelConfig) // Need to set SDK log level before plugins get loaded. // Some aws.Config objects get created early and live forever which means // we cannot change the sdk log level without restarting the Agent. diff --git a/cmd/start-amazon-cloudwatch-agent/path_windows.go b/cmd/start-amazon-cloudwatch-agent/path_windows.go index 5e37e1109a..eed4f8afc8 100644 --- a/cmd/start-amazon-cloudwatch-agent/path_windows.go +++ b/cmd/start-amazon-cloudwatch-agent/path_windows.go @@ -11,6 +11,8 @@ import ( "io" "log" "os/exec" + + "github.com/aws/amazon-cloudwatch-agent/tool/paths" ) func startAgent(writer io.WriteCloser) error { @@ -20,10 +22,9 @@ func startAgent(writer io.WriteCloser) error { } cmd := exec.Command( - agentBinaryPath, - "-config", tomlConfigPath, - "-envconfig", envConfigPath, - "-otelconfig", yamlConfigPath, + paths.AgentBinaryPath, + "-config", paths.TomlConfigPath, + "-envconfig", paths.EnvConfigPath, ) stdoutStderr, err := cmd.CombinedOutput() // log file is closed, so use fmt here diff --git a/tool/paths/paths.go b/tool/paths/paths.go index 78668ccacc..26543934fb 100644 --- a/tool/paths/paths.go +++ b/tool/paths/paths.go @@ -4,24 +4,24 @@ package paths const ( - COMMON_CONFIG = "common-config.toml" - JSON = "amazon-cloudwatch-agent.json" - TOML = "amazon-cloudwatch-agent.toml" - YAML = "amazon-cloudwatch-agent.yaml" - ENV = "env-config.json" + COMMON_CONFIG = "common-config.toml" + JSON = "amazon-cloudwatch-agent.json" + TOML = "amazon-cloudwatch-agent.toml" + YAML = "amazon-cloudwatch-agent.yaml" + ENV = "env-config.json" AGENT_LOG_FILE = "amazon-cloudwatch-agent.log" //TODO this CONFIG_DIR_IN_CONTAINER should change to something indicate dir, keep it for now to avoid break testing CONFIG_DIR_IN_CONTAINER = "/etc/cwagentconfig" ) var ( - JsonConfigPath string - JsonDirPath string - EnvConfigPath string - TomlConfigPath string - CommonConfigPath string - YamlConfigPath string - AgentLogFilePath string + JsonConfigPath string + JsonDirPath string + EnvConfigPath string + TomlConfigPath string + CommonConfigPath string + YamlConfigPath string + AgentLogFilePath string TranslatorBinaryPath string AgentBinaryPath string -) \ No newline at end of file +) diff --git a/tool/paths/paths_unix.go b/tool/paths/paths_unix.go index 8c88a3664d..b6272581fd 100644 --- a/tool/paths/paths_unix.go +++ b/tool/paths/paths_unix.go @@ -7,13 +7,13 @@ package paths const ( - AgentDir = "/opt/aws/amazon-cloudwatch-agent" - BinaryDir = "bin" - JsonDir = "amazon-cloudwatch-agent.d" + AgentDir = "/opt/aws/amazon-cloudwatch-agent" + BinaryDir = "bin" + JsonDir = "amazon-cloudwatch-agent.d" TranslatorBinaryName = "config-translator" - AgentBinaryName = "amazon-cloudwatch-agent" - WizardBinaryName = "amazon-cloudwatch-agent-config-wizard" - AgentStartName = "amazon-cloudwatch-agent-ctl" + AgentBinaryName = "amazon-cloudwatch-agent" + WizardBinaryName = "amazon-cloudwatch-agent-config-wizard" + AgentStartName = "amazon-cloudwatch-agent-ctl" ) func init() { @@ -28,4 +28,4 @@ func init() { TranslatorBinaryPath = AgentDir + "/bin/" + TranslatorBinaryName AgentBinaryPath = AgentDir + "/bin/" + AgentBinaryName -} \ No newline at end of file +} diff --git a/tool/paths/paths_windows.go b/tool/paths/paths_windows.go index 30ac0e4991..009696a3ae 100644 --- a/tool/paths/paths_windows.go +++ b/tool/paths/paths_windows.go @@ -9,13 +9,13 @@ package paths import "os" const ( - AgentDir = "\\Amazon\\AmazonCloudWatchAgent\\" - JsonDir = "\\Configs" - BinaryDir = "bin" + AgentDir = "\\Amazon\\AmazonCloudWatchAgent\\" + JsonDir = "\\Configs" + BinaryDir = "bin" TranslatorBinaryName = "config-translator.exe" - AgentBinaryName = "amazon-cloudwatch-agent.exe" - WizardBinaryName = "amazon-cloudwatch-agent-config-wizard.exe" - AgentStartName = "amazon-cloudwatch-agent-ctl.ps1" + AgentBinaryName = "amazon-cloudwatch-agent.exe" + WizardBinaryName = "amazon-cloudwatch-agent-config-wizard.exe" + AgentStartName = "amazon-cloudwatch-agent-ctl.ps1" ) func init() { @@ -28,16 +28,16 @@ func init() { programData = os.Getenv("ALLUSERSPROFILE") + "\\Application Data" } - AgentRootDir := programFiles + paths.AgentDir - AgentConfigDir := programData + paths.AgentDir + AgentRootDir := programFiles + AgentDir + AgentConfigDir := programData + AgentDir - JsonConfigPath = agentConfigDir + "\\" + JSON - JsonDirPath = agentConfigDir + paths.JsonDir - EnvConfigPath = agentConfigDir + "\\" + ENV - TomlConfigPath = agentConfigDir + "\\" + TOML - YamlConfigPath = agentConfigDir + "\\" + YAML - CommonConfigPath = agentConfigDir + "\\" + COMMON_CONFIG - AgentLogFilePath = agentConfigDir + "\\Logs\\" + AGENT_LOG_FILE - TranslatorBinaryPath = agentRootDir + "\\" + paths.TranslatorBinaryName - AgentBinaryPath = agentRootDir + "\\" + paths.AgentBinaryName -} \ No newline at end of file + JsonConfigPath = AgentConfigDir + "\\" + JSON + JsonDirPath = AgentConfigDir + JsonDir + EnvConfigPath = AgentConfigDir + "\\" + ENV + TomlConfigPath = AgentConfigDir + "\\" + TOML + YamlConfigPath = AgentConfigDir + "\\" + YAML + CommonConfigPath = AgentConfigDir + "\\" + COMMON_CONFIG + AgentLogFilePath = AgentConfigDir + "\\Logs\\" + AGENT_LOG_FILE + TranslatorBinaryPath = AgentRootDir + "\\" + TranslatorBinaryName + AgentBinaryPath = AgentRootDir + "\\" + AgentBinaryName +} From 68163d29f0f195943ce918bd6e467c4f3173bc70 Mon Sep 17 00:00:00 2001 From: Adam <90734270+adam-mateen@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:03:25 -0500 Subject: [PATCH 3/5] remove unnecessary param passing --- .../amazon-cloudwatch-agent.go | 1 - cmd/start-amazon-cloudwatch-agent/path.go | 2 -- tool/paths/paths_unix.go | 22 ++++++++-------- tool/paths/paths_windows.go | 26 ++++++++++--------- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go index 1a94924912..e2ee07d1a3 100644 --- a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go +++ b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go @@ -259,7 +259,6 @@ func runAgent(ctx context.Context, logger.SetupLogging(logConfig) log.Printf("I! Starting AmazonCloudWatchAgent %s\n", agentinfo.FullVersion()) - log.Printf("I! config: %v, %v",*fConfig, *fOtelConfig) // Need to set SDK log level before plugins get loaded. // Some aws.Config objects get created early and live forever which means // we cannot change the sdk log level without restarting the Agent. diff --git a/cmd/start-amazon-cloudwatch-agent/path.go b/cmd/start-amazon-cloudwatch-agent/path.go index bf17673c52..c898844dcc 100644 --- a/cmd/start-amazon-cloudwatch-agent/path.go +++ b/cmd/start-amazon-cloudwatch-agent/path.go @@ -27,7 +27,6 @@ func startAgent(writer io.WriteCloser) error { paths.AgentBinaryPath, // when using syscall.Exec, must pass binary name as args[0] "-config", paths.TomlConfigPath, "-envconfig", paths.EnvConfigPath, - "-otelconfig", paths.YamlConfigPath, "-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid", } if err := syscall.Exec(paths.AgentBinaryPath, execArgs, os.Environ()); err != nil { @@ -65,7 +64,6 @@ func startAgent(writer io.WriteCloser) error { paths.AgentBinaryPath, "-config", paths.TomlConfigPath, "-envconfig", paths.EnvConfigPath, - "-otelconfig", paths.YamlConfigPath, "-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid", } if err = syscall.Exec(name, agentCmd, os.Environ()); err != nil { diff --git a/tool/paths/paths_unix.go b/tool/paths/paths_unix.go index b6272581fd..c59c70232a 100644 --- a/tool/paths/paths_unix.go +++ b/tool/paths/paths_unix.go @@ -6,6 +6,8 @@ package paths +import "path/filepath" + const ( AgentDir = "/opt/aws/amazon-cloudwatch-agent" BinaryDir = "bin" @@ -17,15 +19,13 @@ const ( ) func init() { - JsonConfigPath = AgentDir + "/etc/" + JSON - JsonDirPath = AgentDir + "/etc/" + JsonDir - EnvConfigPath = AgentDir + "/etc/" + ENV - TomlConfigPath = AgentDir + "/etc/" + TOML - CommonConfigPath = AgentDir + "/etc/" + COMMON_CONFIG - YamlConfigPath = AgentDir + "/etc/" + YAML - - AgentLogFilePath = AgentDir + "/logs/" + AGENT_LOG_FILE - - TranslatorBinaryPath = AgentDir + "/bin/" + TranslatorBinaryName - AgentBinaryPath = AgentDir + "/bin/" + AgentBinaryName + JsonConfigPath = filepath.Join(AgentDir, "etc", JSON) + JsonDirPath = filepath.Join(AgentDir, "etc", JsonDir) + EnvConfigPath = filepath.Join(AgentDir, "etc", ENV) + TomlConfigPath = filepath.Join(AgentDir, "etc", TOML) + CommonConfigPath = filepath.Join(AgentDir, "etc", COMMON_CONFIG) + YamlConfigPath = filepath.Join(AgentDir, "etc", YAML) + AgentLogFilePath = filepath.Join(AgentDir, "logs", AGENT_LOG_FILE) + TranslatorBinaryPath = filepath.Join(AgentDir, "bin", TranslatorBinaryName) + AgentBinaryPath = filepath.Join(AgentDir, "bin", AgentBinaryName) } diff --git a/tool/paths/paths_windows.go b/tool/paths/paths_windows.go index 009696a3ae..1234cfe77d 100644 --- a/tool/paths/paths_windows.go +++ b/tool/paths/paths_windows.go @@ -6,7 +6,10 @@ package paths -import "os" +import ( + "os" + "path/filepath" +) const ( AgentDir = "\\Amazon\\AmazonCloudWatchAgent\\" @@ -25,19 +28,18 @@ func init() { programData = os.Getenv("ProgramData") } else { // Windows 2003 - programData = os.Getenv("ALLUSERSPROFILE") + "\\Application Data" + programData = filepath.Join(os.Getenv("ALLUSERSPROFILE"), "Application Data") } AgentRootDir := programFiles + AgentDir AgentConfigDir := programData + AgentDir - - JsonConfigPath = AgentConfigDir + "\\" + JSON - JsonDirPath = AgentConfigDir + JsonDir - EnvConfigPath = AgentConfigDir + "\\" + ENV - TomlConfigPath = AgentConfigDir + "\\" + TOML - YamlConfigPath = AgentConfigDir + "\\" + YAML - CommonConfigPath = AgentConfigDir + "\\" + COMMON_CONFIG - AgentLogFilePath = AgentConfigDir + "\\Logs\\" + AGENT_LOG_FILE - TranslatorBinaryPath = AgentRootDir + "\\" + TranslatorBinaryName - AgentBinaryPath = AgentRootDir + "\\" + AgentBinaryName + JsonConfigPath = filepath.Join(AgentConfigDir, JSON) + JsonDirPath = filepath.Join(AgentConfigDir, JsonDir) + EnvConfigPath = filepath.Join(AgentConfigDir, ENV) + TomlConfigPath = filepath.Join(AgentConfigDir, TOML) + YamlConfigPath = filepath.Join(AgentConfigDir, YAML) + CommonConfigPath = filepath.Join(AgentConfigDir, COMMON_CONFIG) + AgentLogFilePath = filepath.Join(AgentConfigDir, AGENT_LOG_FILE) + TranslatorBinaryPath = filepath.Join(AgentRootDir, TranslatorBinaryName) + AgentBinaryPath = filepath.Join(AgentRootDir, AgentBinaryName) } From a1fb4dc028d816c52dd4f462e00f616daf3748c7 Mon Sep 17 00:00:00 2001 From: Adam <90734270+adam-mateen@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:10:05 -0500 Subject: [PATCH 4/5] rename fConfig to fTomlConfig and pass the -otelconfig from start-amazon-cloudwatch-agent to amazon-cloudwatch-agent. --- .../amazon-cloudwatch-agent.go | 14 +++++++------- cmd/start-amazon-cloudwatch-agent/path.go | 2 ++ cmd/start-amazon-cloudwatch-agent/path_windows.go | 1 + 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go index e2ee07d1a3..0cae50afe0 100644 --- a/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go +++ b/cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go @@ -58,7 +58,7 @@ var fQuiet = flag.Bool("quiet", false, var fTest = flag.Bool("test", false, "enable test mode: gather metrics, print them out, and exit") var fTestWait = flag.Int("test-wait", 0, "wait up to this many seconds for service inputs to complete in test mode") var fSchemaTest = flag.Bool("schematest", false, "validate the toml file schema") -var fConfig = flag.String("config", "", "configuration file to load") +var fTomlConfig = flag.String("config", "", "configuration file to load") var fOtelConfig = flag.String("otelconfig", paths.YamlConfigPath, "YAML configuration file to run OTel pipeline") var fEnvConfig = flag.String("envconfig", "", "env configuration file to load") var fConfigDirectory = flag.String("config-directory", "", @@ -136,7 +136,7 @@ func reloadLoop( } }(ctx) - if envConfigPath, err := getEnvConfigPath(*fConfig, *fEnvConfig); err == nil { + if envConfigPath, err := getEnvConfigPath(*fTomlConfig, *fEnvConfig); err == nil { // Reloads environment variables when file is changed go func(ctx context.Context, envConfigPath string) { var previousModTime time.Time @@ -216,7 +216,7 @@ func runAgent(ctx context.Context, inputFilters []string, outputFilters []string, ) error { - envConfigPath, err := getEnvConfigPath(*fConfig, *fEnvConfig) + envConfigPath, err := getEnvConfigPath(*fTomlConfig, *fEnvConfig) if err != nil { return err } @@ -555,8 +555,8 @@ func main() { // Handle the --service flag here to prevent any issues with tooling that // may not have an interactive session, e.g. installing from Ansible. if *fService != "" { - if *fConfig != "" { - svcConfig.Arguments = []string{"--config", *fConfig} + if *fTomlConfig != "" { + svcConfig.Arguments = []string{"--config", *fTomlConfig} } if *fConfigDirectory != "" { svcConfig.Arguments = append(svcConfig.Arguments, "--config-directory", *fConfigDirectory) @@ -607,7 +607,7 @@ func windowsRunAsService() bool { } func loadTomlConfigIntoAgent(c *config.Config) error { - err := c.LoadConfig(*fConfig) + err := c.LoadConfig(*fTomlConfig) if err != nil { return err } @@ -638,7 +638,7 @@ func validateAgentFinalConfigAndPlugins(c *config.Config) error { if *fSchemaTest { //up to this point, the given config file must be valid fmt.Println(agentinfo.FullVersion()) - fmt.Printf("The given config: %v is valid\n", *fConfig) + fmt.Printf("The given config: %v is valid\n", *fTomlConfig) os.Exit(0) } diff --git a/cmd/start-amazon-cloudwatch-agent/path.go b/cmd/start-amazon-cloudwatch-agent/path.go index c898844dcc..bf17673c52 100644 --- a/cmd/start-amazon-cloudwatch-agent/path.go +++ b/cmd/start-amazon-cloudwatch-agent/path.go @@ -27,6 +27,7 @@ func startAgent(writer io.WriteCloser) error { paths.AgentBinaryPath, // when using syscall.Exec, must pass binary name as args[0] "-config", paths.TomlConfigPath, "-envconfig", paths.EnvConfigPath, + "-otelconfig", paths.YamlConfigPath, "-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid", } if err := syscall.Exec(paths.AgentBinaryPath, execArgs, os.Environ()); err != nil { @@ -64,6 +65,7 @@ func startAgent(writer io.WriteCloser) error { paths.AgentBinaryPath, "-config", paths.TomlConfigPath, "-envconfig", paths.EnvConfigPath, + "-otelconfig", paths.YamlConfigPath, "-pidfile", paths.AgentDir + "/var/amazon-cloudwatch-agent.pid", } if err = syscall.Exec(name, agentCmd, os.Environ()); err != nil { diff --git a/cmd/start-amazon-cloudwatch-agent/path_windows.go b/cmd/start-amazon-cloudwatch-agent/path_windows.go index eed4f8afc8..19c01a2ef7 100644 --- a/cmd/start-amazon-cloudwatch-agent/path_windows.go +++ b/cmd/start-amazon-cloudwatch-agent/path_windows.go @@ -25,6 +25,7 @@ func startAgent(writer io.WriteCloser) error { paths.AgentBinaryPath, "-config", paths.TomlConfigPath, "-envconfig", paths.EnvConfigPath, + "-otelconfig", paths.YamlConfigPath, ) stdoutStderr, err := cmd.CombinedOutput() // log file is closed, so use fmt here From 2d697a21071619ca54a6ec74624d09fb0edc5d37 Mon Sep 17 00:00:00 2001 From: Adam <90734270+adam-mateen@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:15:27 -0500 Subject: [PATCH 5/5] More filepath.Join --- tool/paths/paths_windows.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tool/paths/paths_windows.go b/tool/paths/paths_windows.go index 1234cfe77d..69c51ac889 100644 --- a/tool/paths/paths_windows.go +++ b/tool/paths/paths_windows.go @@ -31,8 +31,8 @@ func init() { programData = filepath.Join(os.Getenv("ALLUSERSPROFILE"), "Application Data") } - AgentRootDir := programFiles + AgentDir - AgentConfigDir := programData + AgentDir + AgentRootDir := filepath.Join(programFiles, AgentDir) + AgentConfigDir := filepath.Join(programData, AgentDir) JsonConfigPath = filepath.Join(AgentConfigDir, JSON) JsonDirPath = filepath.Join(AgentConfigDir, JsonDir) EnvConfigPath = filepath.Join(AgentConfigDir, ENV)