Skip to content

Commit

Permalink
Add opt-in default metrics to CloudWatch agent
Browse files Browse the repository at this point in the history
Report on memory and disk usage
  • Loading branch information
ouranos committed Mar 4, 2022
1 parent 509802d commit 5756f14
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
24 changes: 24 additions & 0 deletions packer/linux/conf/bin/bk-configure-cloudwatch-agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# shellcheck disable=SC2094
set -euo pipefail

if [[ "${CLOUDWATCH_ENABLE_METRICS:-false}" == "true" ]]; then
cw_config="/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json"
cat <<<"$(jq \
--arg queue "$BUILDKITE_QUEUE" \
'. + {
metrics: {
metrics_collected: {
mem: {measurement: ["mem_used_percent"], append_dimensions: {BuildkiteQueue: $queue}},
disk: {measurement: ["used_percent"], resources: ["*"], append_dimensions: {BuildkiteQueue: $queue}}
},
append_dimensions: {
AutoScalingGroupName: "${aws:AutoScalingGroupName}"
}
}
}' $cw_config)" >$cw_config
fi

# Enable and start amazon-cloudwatch-agent
systemctl enable amazon-cloudwatch-agent
systemctl start amazon-cloudwatch-agent
4 changes: 0 additions & 4 deletions packer/linux/scripts/install-cloudwatch-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

set -eu -o pipefail


echo "Installing cloudwatch agent..."
sudo yum install -y amazon-cloudwatch-agent

echo "Adding amazon-cloudwatch-agent config..."
sudo cp /tmp/conf/cloudwatch-agent/config.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

echo "Configuring amazon-cloudwatch-agent to start at boot"
sudo systemctl enable amazon-cloudwatch-agent

# These will send some systemctl service logs (like the buildkite agent and docker) to logfiles
echo "Adding rsyslogd configs..."
sudo cp /tmp/conf/cloudwatch-agent/rsyslog.d/* /etc/rsyslog.d/
14 changes: 14 additions & 0 deletions templates/aws-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Metadata:
- ManagedPolicyARN
- InstanceRoleName
- IMDSv2Tokens
- EnableCloudWatchMetrics

- Label:
default: Auto-scaling Configuration
Expand Down Expand Up @@ -417,6 +418,14 @@ Parameters:
- "false"
Default: "false"

EnableCloudWatchMetrics:
Type: String
Description: Enable CloudWatch agent metrics
AllowedValues:
- "true"
- "false"
Default: "false"

EnableCostAllocationTags:
Type: String
Description: Enables AWS Cost Allocation tags for all resources in the stack. See https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html
Expand Down Expand Up @@ -1016,6 +1025,11 @@ Resources:
BUILDKITE_ENABLE_INSTANCE_STORAGE="${EnableInstanceStorage}" \
/usr/local/bin/bk-configure-docker.sh
--==BOUNDARY==
Content-Type: text/cloud-boothook; charset="us-ascii"
CLOUDWATCH_ENABLE_METRICS="${EnableCloudWatchMetrics}" \
BUILDKITE_QUEUE="${BuildkiteQueue}" \
/usr/local/bin/bk-configure-cloudwatch-agent.sh
--==BOUNDARY==
Content-Type: text/x-shellscript; charset="us-ascii"
#!/bin/bash -v
BUILDKITE_STACK_NAME="${AWS::StackName}" \
Expand Down

0 comments on commit 5756f14

Please sign in to comment.