{"id":27062,"date":"2015-09-29T16:47:54","date_gmt":"2015-09-29T11:17:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=27062"},"modified":"2016-01-19T13:31:30","modified_gmt":"2016-01-19T08:01:30","slug":"monitoring-docker-container-status-using-aws-cloudwatch","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/monitoring-docker-container-status-using-aws-cloudwatch\/","title":{"rendered":"Monitoring Docker container status using AWS CloudWatch"},"content":{"rendered":"<p>There are no such tools till now to monitor whether your Docker container is running or not. Now, I had a Docker container running in production and I could not afford not knowing whenever it went down or rather stopped. So, I decided to do something about it. The blog is about the same.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-26635 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/09\/cicd-with-docker-on-aws-1-638.jpg\" alt=\"cicd-with-docker-on-aws-1-638\" width=\"549\" height=\"353\" \/><br \/>\nWhat we will be doing is checking whether the required Docker container is up using a simple bash script. We will put this script in cron running at every minute. Every minute if the container is up, the count remains 1 for single container in my use-case and is pushed to AWS CloudWatch. In AWS CloudWatch we will put an alarm for count &lt;1 and an SNS notification to follow.<\/p>\n<h3><span style=\"color: #000000\">Prerequisites<\/span><\/h3>\n<ol>\n<li>AWS Linux server<\/li>\n<li>Docker service and a Docker container running on that server<\/li>\n<li>The server should have AWS CLI installed and have appropriate IAM keys or a role attached to it. The policy attached to the role should have CloudWatch access.<\/li>\n<\/ol>\n<h3><span style=\"color: #000000\">Steps<\/span><\/h3>\n<h4><span style=\"color: #000000\">1. Understand the bash script<\/span><\/h4>\n<p>You need a bash script which will check whether your Docker container is up and running or not. Below is the bash script you can use. If you have multiple Docker containers you simply need to grep that as well and change the count. Else you can simply create a different metric for different container.<\/p>\n<p>[js]<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>INST_ID=i-9dee2454<br \/>\ncontainer=asa-prod<br \/>\n$<br \/>\nmetric1()<br \/>\n{<\/p>\n<p>docker ps | grep -i &quot;$container&quot;<\/p>\n<p>if [ &quot;$?&quot; -ne 0 ];then<br \/>\ncount=0<br \/>\nelse<br \/>\ncount=1<br \/>\nfi<br \/>\n\/usr\/local\/bin\/aws\u00a0cloudwatch put-metric-data &#8211;metric-name &quot;Docker Container $container is down on `hostname`&quot; &#8211;unit Percent &#8211;value &quot;$count&quot; &#8211;dimensions InstanceId=$INST_ID &#8211;namespace System\/Linux<br \/>\n}<\/p>\n<p>alarm1()<br \/>\n{<br \/>\n\/usr\/local\/bin\/aws cloudwatch put-metric-alarm &#8211;alarm-name Docker-Prod-Container-Down\u00a0&#8211;alarm-description &quot;If the named container is down this alarm is triggered&quot; &#8211;metric-name Docker Container is down on `hostname`\u00a0&#8211;namespace System\/Linux &#8211;statistic Average &#8211;period 60 &#8211;threshold $count\u00a0&#8211;comparison-operator LessThanThreshold &#8211;dimensions Name=InstanceId,Value=$INST_ID &#8211;evaluation-periods 1 &#8211;alarm-actions arn:aws:sns:us-east-1:069016302557:ProdContainer\u00a0&#8211;unit Count<br \/>\n}<br \/>\n$1<br \/>\n[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #000000\">Explanation of the above script<\/span><\/h4>\n<ul>\n<li>container variable stores the container id which you want to monitor.<\/li>\n<li><strong>docker ps<\/strong> will list all the running instances and grep will check if container with that name is present or not.<\/li>\n<li>Now, we check if grep found the container name or not. If no then count will be 0, else will be 1. <strong>count == 0<\/strong> will mean container is not running and <strong>count == 1<\/strong> will mean that container is running.<\/li>\n<li>Next, we create a CloudWatch metric pushing the value of count to AWS CloudWatch metric in the namespace System\/Linux.<\/li>\n<li>Last is creating an alarm for the metric. Once the metric data starts getting pushed you can put an alarm either via AWS console or through the command in the alarm1 function in the script.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #000000\">2. To execute the script first time just run the following command:<\/span><\/h4>\n<p>[js]sudo bash dockermonitor.sh metric1<br \/>\nsudo bash dockermonitor.sh alarm1[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #000000\">3. Put the script in Cron.<\/span><\/h4>\n<p>Now, just put the first command in Cron using<strong> crontab -e<\/strong> and appending the command there as shown below. You don&#8217;t need to run the alarm1 function again.<\/p>\n<p style=\"text-align: left\"><code>* * * * * bash \/home\/dockermonitor.sh metric1<\/code><\/p>\n<p>This script will do all for you. It will push the data to CloudWatch and monitor your Docker Container. It will also trigger any alarms when your Docker container is down. This might also be helpful in situations where you might have just restarted your container and it may have never come up and you getting to know about it after sometime.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are no such tools till now to monitor whether your Docker container is running or not. Now, I had a Docker container running in production and I could not afford not knowing whenever it went down or rather stopped. So, I decided to do something about it. The blog is about the same. What [&hellip;]<\/p>\n","protected":false},"author":174,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":128},"categories":[1174,2348],"tags":[248,2413,2414,1883,2412,1499],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/27062"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/174"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=27062"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/27062\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=27062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=27062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=27062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}