How to monitor disk space on EC2
For anyone working with production systems or I suppose test systems for that matter, monitoring is highly important, if something goes down how will we be alerted right? For an EC2 instance the main metrics which probably spring to mind are CPU, Mem and Disk.
What if I told you out of the box disk usage is not monitored!!! 😱
Fear not in just a few short steps we will show how this can be achieved and the best part, its very straightforward!
On any EC2 instance for which we want to monitor for disk usage we need to install the CloudWatch agent, this can be downloaded by running:
wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
We then need to install the agent:
sudo rpm -U amazon-cloudwatch-agent.rpm
If your installing a DEB package or running on windows server the command will be:
DEB: sudo dpkg -i -E ./amazon-cloudwatch-agent.debWindows: msiexec /i amazon-cloudwatch-agent.msi
*And of course you would have downloaded the correct package for the install 😜
It’s now time to configure the agent config, create a file in the following location:
sudo vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
Paste the following into the file:
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "cwagent"
},
"metrics": {
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"metrics_collected": {
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"/"
]
}
}
}
}
Its now time to restart the CW Agent so as to pickup the new configuration:
sudo systemctl restart amazon-cloudwatch-agent
Sweet we now have the instance configured to alert on disk usage 😎
Now its time to configure the alarm in CloudWatch.
Follow the below steps to configure the alarm in CloudWatch:
- Login to the AWS console
- Navigate to CloudWatch and click on Create alarm
- Select metric
- Under custom namespaces you should see CWAgent
- Select the metric to monitor
- The list of instance ids which are being monitored by the agent will be shown, select the instances you want to monitor
- Set the threshold for which you want to alert on, for example setting to 80% will alert when there is only 20% of disk space left
- Setup the notification so that you get notified once the threshold is hit. This is done by creating a new SNS topic, provide a list of emails who should receive the alert.
- The last step is to name the alarm
- We now have a CW alarm in place to monitor disk usage on the selected instances 🎉
Its also possible to send alerts to the likes of Slack and Teams etc. This will just depend on the method used within your company.
Thanks for reading, I hope this was helpful 😃