Skip to content

Instantly share code, notes, and snippets.

@gaulatti
Last active November 30, 2024 23:27
Show Gist options
  • Save gaulatti/ae6654a428232a4a4ce15b04d4dd20e3 to your computer and use it in GitHub Desktop.
Save gaulatti/ae6654a428232a4a4ce15b04d4dd20e3 to your computer and use it in GitHub Desktop.
Amazon CloudWatch Agent on Ubuntu Server

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

Create CloudWatch Agent Config

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}"
                    }
                ]
            }
        }
    }
}

Set CloudWatch Agent as Service

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment