Skip to content

Commit 9506a40

Browse files
committed
Auto-start CloudWatch agent if not running
When metrics are enabled, the action now checks if the CloudWatch agent is already running before applying configuration. If the agent is not running, it starts the agent with the configuration using `fetch-config`. If the agent is already running, it appends the configuration using `append-config` (previous behavior). This ensures backwards compatibility as the native OTEL integration removes the default agent startup from RunsOn instances. Fixes #25
1 parent b1d0648 commit 9506a40

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

‎internal/monitoring/agent.go‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,56 @@ func GenerateCloudWatchConfig(action *githubactions.Action, metrics []string, ne
126126
action.Infof("Config file: %s", configPath)
127127
action.Infof("Config content: %s", string(configJSON))
128128

129-
// Append the config to the running CloudWatch agent
130-
return appendCloudWatchConfig(action, configPath)
129+
// Apply the config to the CloudWatch agent (start if needed, or append if already running)
130+
return applyCloudWatchConfig(action, configPath)
131131
}
132132

133-
func appendCloudWatchConfig(action *githubactions.Action, configPath string) error {
133+
const agentCtl = "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl"
134+
135+
// isCloudWatchAgentRunning checks if the CloudWatch agent is currently running
136+
func isCloudWatchAgentRunning() bool {
137+
cmd := exec.Command("sudo", agentCtl, "-a", "status")
138+
output, err := cmd.CombinedOutput()
139+
if err != nil {
140+
return false
141+
}
142+
// The status command returns JSON with "status": "running" if the agent is running
143+
return strings.Contains(string(output), `"status": "running"`)
144+
}
145+
146+
func applyCloudWatchConfig(action *githubactions.Action, configPath string) error {
134147
// Check if CloudWatch agent is available
135-
agentCtl := "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl"
136148
if _, err := os.Stat(agentCtl); os.IsNotExist(err) {
137149
action.Warningf("CloudWatch agent not found at %s, skipping metrics configuration", agentCtl)
138150
return nil
139151
}
140152

141-
// Append the configuration to the running agent
142-
cmd := exec.Command("sudo", agentCtl,
143-
"-a", "append-config",
144-
"-m", "ec2",
145-
"-s",
146-
"-c", fmt.Sprintf("file:%s", configPath))
153+
var cmd *exec.Cmd
154+
if isCloudWatchAgentRunning() {
155+
// Agent is running, append the configuration
156+
action.Infof("CloudWatch agent is running, appending configuration...")
157+
cmd = exec.Command("sudo", agentCtl,
158+
"-a", "append-config",
159+
"-m", "ec2",
160+
"-s",
161+
"-c", fmt.Sprintf("file:%s", configPath))
162+
} else {
163+
// Agent is not running, start it with the configuration
164+
action.Infof("CloudWatch agent is not running, starting it with configuration...")
165+
cmd = exec.Command("sudo", agentCtl,
166+
"-a", "fetch-config",
167+
"-m", "ec2",
168+
"-s",
169+
"-c", fmt.Sprintf("file:%s", configPath))
170+
}
147171

148172
output, err := cmd.CombinedOutput()
149173
if err != nil {
150-
action.Warningf("Failed to append CloudWatch config: %v\nOutput: %s", err, string(output))
174+
action.Warningf("Failed to configure CloudWatch agent: %v\nOutput: %s", err, string(output))
151175
return err
152176
}
153177

154-
action.Infof("Successfully appended CloudWatch metrics configuration")
178+
action.Infof("Successfully configured CloudWatch agent")
155179
action.Infof("CloudWatch agent output: %s", string(output))
156180

157181
return nil

0 commit comments

Comments
 (0)