When managing EC2 instances or other AWS resources, monitoring free disk space is crucial for preventing operational issues. AWS CloudWatch provides built-in metrics for this purpose through its CWAgent
namespace.
The primary metric for disk space monitoring is FreeStorageSpace
, which represents the available disk space in bytes. This metric is available under the AWS/EC2
namespace for instance store volumes and the AWS/EBS
namespace for EBS volumes.
For more detailed monitoring, you'll need to install and configure the CloudWatch agent. Here's a sample configuration for Linux:
{
"metrics": {
"metrics_collected": {
"disk": {
"measurement": [
"free",
"used",
"used_percent"
],
"resources": [
"*"
]
}
}
}
}
You can retrieve free disk space metrics using the AWS CLI:
aws cloudwatch get-metric-statistics \
--namespace CWAgent \
--metric-name disk_free \
--dimensions Name=path,Value=/ Name=InstanceId,Value=i-1234567890abcdef0 \
--statistics Average \
--period 300 \
--start-time 2023-01-01T00:00:00Z \
--end-time 2023-01-01T23:59:59Z
Set up alerts when disk space falls below a threshold (10% in this example):
aws cloudwatch put-metric-alarm \
--alarm-name "LowDiskSpace" \
--alarm-description "Alarm when disk space is low" \
--metric-name disk_used_percent \
--namespace CWAgent \
--statistic Average \
--period 300 \
--threshold 90 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:my-sns-topic
Create a dashboard widget to monitor disk space:
{
"widgets": [
{
"type": "metric",
"x": 0,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": [
["CWAgent", "disk_free", "path", "/", "InstanceId", "i-1234567890abcdef0"]
],
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "Free Disk Space (/)"
}
}
]
}
- Ensure IAM permissions for the CloudWatch agent include
cloudwatch:PutMetricData
- Verify the agent is running with
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
- Check agent logs at
/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log
When working with AWS EC2 instances, monitoring free disk space is crucial for maintaining system health. CloudWatch provides several metrics through its agent that can help track this information. The key metric for free disk space is disk_used_percent
, which indirectly gives you free space information when you know the total disk capacity.
First, you'll need to install and configure the CloudWatch agent on your EC2 instance. Here's a basic configuration example for disk monitoring:
{ "metrics": { "append_dimensions": { "InstanceId": "${aws:InstanceId}" }, "metrics_collected": { "disk": { "measurement": [ "used_percent", "free", "total" ], "metrics_collection_interval": 60, "resources": [ "*" ] } } } }
Once the agent is running, you can query the metrics using AWS CLI or the CloudWatch console. Here's how to get the free disk space metric for the past hour:
aws cloudwatch get-metric-statistics \ --namespace "CWAgent" \ --metric-name "disk_free" \ --dimensions Name=InstanceId,Value=i-1234567890abcdef0 Name=device,Value=xvda1 Name=fstype,Value=ext4 \ --statistics Average \ --start-time $(date -u -v-1H +%Y-%m-%dT%H:%M:%SZ) \ --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \ --period 60
To proactively monitor disk space, set up a CloudWatch alarm that triggers when free space falls below a threshold:
aws cloudwatch put-metric-alarm \ --alarm-name "LowDiskSpace" \ --alarm-description "Alarm when disk free space is less than 10%" \ --metric-name "disk_used_percent" \ --namespace "CWAgent" \ --statistic "Average" \ --period 300 \ --threshold 90 \ --comparison-operator "GreaterThanOrEqualToThreshold" \ --dimensions Name=InstanceId,Value=i-1234567890abcdef0 Name=device,Value=xvda1 \ --evaluation-periods 1 \ --alarm-actions "arn:aws:sns:us-east-1:123456789012:MyTopic"
Create a dashboard to visualize disk metrics. Here's a sample dashboard configuration snippet:
{ "widgets": [ { "type": "metric", "x": 0, "y": 0, "width": 12, "height": 6, "properties": { "metrics": [ ["CWAgent", "disk_free", "InstanceId", "i-1234567890abcdef0", "device", "xvda1"], [".", "disk_total", ".", ".", ".", "."] ], "period": 300, "stat": "Average", "region": "us-east-1", "title": "Disk Space Utilization" } } ] }
Remember that CloudWatch agent metrics have some limitations:
- Standard resolution metrics have a 1-minute minimum granularity
- There are costs associated with storing custom metrics
- The agent must be running on the instance to collect metrics
- You need proper IAM permissions for the agent to publish metrics