Skip to content

Commit 95fbdd2

Browse files
add cloudwatch agent for custom metrics (#1670)
1 parent e33ba7f commit 95fbdd2

File tree

3 files changed

+180
-1
lines changed

3 files changed

+180
-1
lines changed

Vagrantfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ num_workers = 0 # Generic workers that get the code, but don't start any service
3131
ram_megabytes = 1280
3232
base_box = "ubuntu/trusty64"
3333

34+
semaphore_job_id = ENV['SEMAPHORE_JOB_ID']
35+
3436
# EC2
3537
ec2_access_key = ENV['AWS_ACCESS_KEY']
3638
ec2_secret_key = ENV['AWS_SECRET_KEY']
@@ -50,7 +52,7 @@ ec2_subnet_id = nil
5052
# Only override this by setting it to false if you're running in a VPC and you
5153
# are running Vagrant from within that VPC as well.
5254
ec2_associate_public_ip = nil
53-
ec2_iam_instance_profile_name = nil
55+
ec2_iam_instance_profile_name = "semaphore-access"
5456

5557
ebs_volume_type = 'gp3'
5658

@@ -212,6 +214,8 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
212214
name_node(worker, name, ec2_instance_name_prefix)
213215
ip_address = "192.168.50." + (100 + i).to_s
214216
assign_local_ip(worker, ip_address)
217+
worker.vm.provision "file", source: "vagrant/cloudwatch-agent-configuration.json", destination: "/tmp/cloudwatch-agent-configuration.json"
218+
worker.vm.provision "shell", path: "vagrant/cloudwatch-agent-setup.sh", env: {"SEMAPHORE_JOB_ID" => semaphore_job_id}
215219
worker.vm.provision "shell", path: "vagrant/base.sh", env: {"JDK_MAJOR" => jdk_major, "JDK_FULL" => jdk_full}
216220
end
217221
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"agent": {
3+
"metrics_collection_interval": 60,
4+
"run_as_user": "root"
5+
},
6+
"metrics": {
7+
"namespace": "system-test-ccs-kafka",
8+
"aggregation_dimensions": [
9+
[
10+
"InstanceId"
11+
]
12+
],
13+
"append_dimensions": {
14+
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
15+
"ImageId": "${aws:ImageId}",
16+
"InstanceId": "${aws:InstanceId}",
17+
"InstanceType": "${aws:InstanceType}"
18+
},
19+
"metrics_collected": {
20+
"cpu": {
21+
"append_dimensions": {
22+
"SemaphoreJobId": "${SEMAPHORE_JOB_ID}"
23+
},
24+
"measurement": [
25+
"usage_idle",
26+
"usage_iowait",
27+
"usage_user"
28+
],
29+
"metrics_collection_interval": 60,
30+
"totalcpu": true
31+
},
32+
"disk": {
33+
"append_dimensions": {
34+
"SemaphoreJobId": "${SEMAPHORE_JOB_ID}"
35+
},
36+
"measurement": [
37+
"used_percent",
38+
"inodes_free"
39+
],
40+
"metrics_collection_interval": 60,
41+
"resources": [
42+
"*"
43+
]
44+
},
45+
"diskio": {
46+
"append_dimensions": {
47+
"SemaphoreJobId": "${SEMAPHORE_JOB_ID}"
48+
},
49+
"measurement": [
50+
"io_time",
51+
"write_bytes",
52+
"read_bytes",
53+
"writes",
54+
"reads"
55+
],
56+
"metrics_collection_interval": 60,
57+
"resources": [
58+
"*"
59+
]
60+
},
61+
"mem": {
62+
"append_dimensions": {
63+
"SemaphoreJobId": "${SEMAPHORE_JOB_ID}"
64+
},
65+
"measurement": [
66+
"mem_used_percent"
67+
],
68+
"metrics_collection_interval": 60
69+
},
70+
"netstat": {
71+
"append_dimensions": {
72+
"SemaphoreJobId": "${SEMAPHORE_JOB_ID}"
73+
},
74+
"measurement": [
75+
"tcp_established",
76+
"tcp_time_wait"
77+
],
78+
"metrics_collection_interval": 60
79+
},
80+
"statsd": {
81+
"metrics_aggregation_interval": 60,
82+
"metrics_collection_interval": 60,
83+
"service_address": ":8125"
84+
},
85+
"swap": {
86+
"append_dimensions": {
87+
"SemaphoreJobId": "${SEMAPHORE_JOB_ID}"
88+
},
89+
"measurement": [
90+
"swap_used_percent"
91+
],
92+
"metrics_collection_interval": 60
93+
}
94+
}
95+
}
96+
}

vagrant/cloudwatch-agent-setup.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# This shell script installs Amazon CloudWatch Agent on all EC2 instances that
4+
# are provisioned and spinned-off as worker-nodes for system-test runs.
5+
#
6+
# This helps in collecting and monitoring useful system-level metrics from these EC2
7+
# instances aka worker-nodes to identify performance patterns and anomalies,
8+
# and devise methods to address them.
9+
#
10+
# Example : Disk Space Utilization and Memory Utilization Metrics.
11+
12+
set -ex
13+
14+
architecture=arm64
15+
arch=$(uname -m)
16+
17+
if [ "$arch" == "x86_64" ]; then
18+
architecture=amd64
19+
fi
20+
21+
downloadUrl=https://amazoncloudwatch-agent.s3.amazonaws.com/ubuntu/$architecture/latest/amazon-cloudwatch-agent.deb
22+
23+
echo "Installing amazon-cloudwatch-agent...."
24+
wget -nv $downloadUrl
25+
26+
27+
dpkg -i -E ./amazon-cloudwatch-agent.deb
28+
apt-get -y update && apt-get -y install collectd
29+
30+
31+
pushd /tmp
32+
33+
JQ_URL="https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64"
34+
35+
# Download the binary
36+
wget "$JQ_URL" -O jq
37+
38+
# Make it executable
39+
chmod +x jq
40+
41+
# Copy it to a directory in your PATH (e.g., /usr/local/bin)
42+
sudo cp jq /usr/local/bin/
43+
44+
jq --version
45+
46+
popd
47+
48+
CONFIG_FILE="/tmp/cloudwatch-agent-configuration.json"
49+
TEMP_FILE="cloudwatch-agent-configuration.json.tmp"
50+
51+
# traverse the JSON file and update the SemaphoreJobId value
52+
jq \
53+
--arg job_id "$SEMAPHORE_JOB_ID" \
54+
'
55+
walk(
56+
if type == "object" and has("append_dimensions") and (.append_dimensions | has("SemaphoreJobId")) then
57+
.append_dimensions.SemaphoreJobId = $job_id
58+
else
59+
# Otherwise, return the element unchanged
60+
.
61+
end
62+
)
63+
' \
64+
"$CONFIG_FILE" > "$TEMP_FILE"
65+
66+
67+
# Check if the jq command was successful
68+
if [ $? -eq 0 ]; then
69+
cp "$TEMP_FILE" /opt/aws/amazon-cloudwatch-agent/bin/config.json
70+
cat /opt/aws/amazon-cloudwatch-agent/bin/config.json
71+
else
72+
echo "Error: Failed to update '$CONFIG_FILE' using jq."
73+
rm -f "$TEMP_FILE"
74+
exit 1
75+
fi
76+
77+
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
78+
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
79+
echo "Installation completed for amazon-cloudwatch-agent !!!"

0 commit comments

Comments
 (0)