Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container stats in cloudwatch

I am using the Amazon AWS free tier for a proof of concept. I have docker running on a EC2 instance with a few containers. I'm aware you can run some scripts to monitor the memory of the EC2 instance but was wondering if there were scripts available to monitor the CPU/memory of each of the docker containers and send the results to CloudWatch.

Any help would be appreciated.

like image 880
Swordfish Avatar asked May 22 '26 18:05

Swordfish


1 Answers

After looking for options. I didn't find any specific solution for shipping container level stats to cloud watch.

Nonetheless, you can reuse the Aws mon scripts and parse the command docker stats, roughly something like this drift:

docker ps | tail -n +2 > dockerps.txt #all lines except the firts one.
running_containers = cat dockerps.txt | wc -l
for (i=0;i=running_containers;i++) {
    container_id = cat dockerps.txt |  tr -s ' ' | cut -d' ' -f1 | sed -n -e $ip
    image_name = cat dockerps.txt |  tr -s ' ' | cut -d' ' -f2 | sed -n -e $ip
    used_mem = docker stats --no-stream | grep $(cat dockerps.txt |  tr -s ' ' | cut -d' ' -f1 |  sed -n -e 3p) |  tr -s ' ' | cut -d' ' -f3
    #and now send it to cloudwatch using the mon script 
} 

Or go for cAdvisor + prometheus.io + Grafana (its what I am doing)

To track the memory use by a specific container, for example, celery. I am running this every minute (with crontab):

docker ps | tail -n +2 > dockerps.txt
(echo -n  $(date)" " && docker stats --no-stream | grep $(cat dockerps.txt | grep celery |  tr -s ' ' | cut -d' ' -f1 ) |  tr -s ' ' | cut -d' ' -f3)

and then filebeat is shipping all the outputs to elasticsearch cluster where I have a graphic for this value, also a grok in logstash is necesary to parse it. Or you can ship it to prometheus.

I hope this helps somebody

like image 138
NicoKowe Avatar answered May 24 '26 06:05

NicoKowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!