This is for getting metrics from the host that aren’t normally reported by EC2 such as RAM usage.
# Update Package List
sudo apt-get update
# Install CollectD (required for some metrics)
sudo apt install -y collectd
# Install the Agent
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
This file should be saved at /opt/aws/amazon-cloudwatch-agent/bin/config.json.
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "cwagent"
},
"metrics": {
"aggregation_dimensions": [
[
"InstanceId"
]
],
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"collectd": {
"metrics_aggregation_interval": 60
},
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"statsd": {
"metrics_aggregation_interval": 60,
"metrics_collection_interval": 10,
"service_address": ":8125"
}
}
},
"logs": {
"metrics_collected": {
"log": {
"files": {
"log_level": "debug"
}
}
}
}
}
If it's on-premises
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "cwagent"
},
"metrics": {
"aggregation_dimensions": [
[]
],
"append_dimensions": {
"Host": "${host}"
},
"metrics_collected": {
"collectd": {
"metrics_aggregation_interval": 60
},
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"statsd": {
"metrics_aggregation_interval": 60,
"metrics_collection_interval": 10,
"service_address": ":8125"
}
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/syslog",
"log_group_name": "on-prem-logs",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/auth.log",
"log_group_name": "on-prem-logs",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
This is important to keep keep the agent running on the background and also initialize it on start.
sudo nano /etc/systemd/system/cloudwatch-agent.service
This will allow to create the service, which should have the following content:
[Unit]
Description=Amazon CloudWatch Agent
After=network.target
[Service]
Type=simple
ExecStart=/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a start
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Then we should launch reload the daemons and start the service.
sudo systemctl daemon-reload
sudo systemctl enable cloudwatch-agent.service
sudo systemctl start cloudwatch-agent.service
To check the status of the service we can do:
sudo systemctl status cloudwatch-agent.service