{"id":7138,"date":"2017-11-28T08:41:29","date_gmt":"2017-11-28T00:41:29","guid":{"rendered":"http:\/\/rmohan.com\/?p=7138"},"modified":"2017-11-28T08:41:29","modified_gmt":"2017-11-28T00:41:29","slug":"monitoring-memory-utilisation-of-aws-ec2-instance-in-cloudwatch","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7138","title":{"rendered":"Monitoring Memory Utilisation of aws EC2 instance in\u00a0CloudWatch"},"content":{"rendered":"<div id=\"page\" class=\"hfeed\">\n<div id=\"main\">\n<div id=\"primary\">\n<div id=\"content\" role=\"main\">\n<article id=\"post-224\" class=\"post-224 post type-post status-publish format-standard hentry category-aws tag-aws tag-cloudwatch tag-disk tag-iam tag-memory tag-user tag-utilisation\">\n<div class=\"entry-content\">\n<p>Connect to your EC2 instance using ssh.<br \/>\n<code>ssh -i ec2-user@my_ec2_ip_address<\/code><\/p>\n<p>Install following perl packages<br \/>\n<code>sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https<\/code><\/p>\n<p>Download the monitoring scripts<br \/>\n<code>curl <a href=\"http:\/\/aws-cloudwatch.s3.amazonaws.com\/downloads\/CloudWatchMonitoringScripts-1.2.1.zip\" rel=\"nofollow\">http:\/\/aws-cloudwatch.s3.amazonaws.com\/downloads\/CloudWatchMonitoringScripts-1.2.1.zip<\/a> -O<\/code><\/p>\n<p>Install the monitoring scripts<br \/>\n<code>unzip CloudWatchMonitoringScripts-1.2.1.zip<br \/>\nrm CloudWatchMonitoringScripts-1.2.1.zip<br \/>\ncd aws-scripts-mon<\/code><\/p>\n<p>run the following command to check the memory utilisation.<br \/>\n<code>.\/mon-get-instance-stats.pl --recent-hours=12<\/code><\/p>\n<p>In order to push this data periodically to cloud watch, we need to create an IAM user with relevant permissions, then schedule a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cron\">cron job<\/a> to periodically push the data to aws cloud watch.<\/p>\n<p><a href=\"http:\/\/docs.aws.amazon.com\/IAM\/latest\/UserGuide\/id_users_create.html\">Create a new IAM user<\/a> and provide him access to the ec2 instance and cloud watch. Make a note of the aws\u00a0credentials i.e. access key and secret key. You need this later. Make sure the IAM user has following access.<\/p>\n<blockquote><p>cloudwatch:PutMetricData<br \/>\ncloudwatch:GetMetricStatistics<br \/>\ncloudwatch:ListMetrics<br \/>\nec2:DescribeTags<\/p><\/blockquote>\n<p>For the purpose of the demo, you can provide full access to EC2 and CloudWatch (This is not recommended for production.)<\/p>\n<p>run the following command<br \/>\n<code>cp awscreds.template awscreds.con<\/code><\/p>\n<p>open the file \u00a0<span class=\"s1\"><em><span class=\"skimlinks-unlinked\">awscreds.conf<\/span><\/em> and enter the key &amp; secret.<\/span><br \/>\nThe content of the file should look something like this<\/p>\n<blockquote><p>AWSAccessKeyId=AKAWSACESSKEYSA<br \/>\nAWSSecretKey=z\/NOAWSSECRETkeyzt<\/p><\/blockquote>\n<p>Run the following command to push the data to cloud watch<br \/>\n<code>.\/mon-put-instance-data.pl --mem-util --mem-used --mem-avail<\/code><\/p>\n<p>Now, configure the cron tab to automate this process at eveny 5 minutes interval.<code><br \/>\ncrontab -e<br \/>\n*\/5 * * * * ~\/aws-scripts-mon\/mon-put-instance-data.pl --mem-util --disk-space-util --disk-path=\/ --from-cron<br \/>\n<\/code><\/p>\n<p>In your aws console, go to cloud watch section, and select Metrics =&gt; custom namespaces =&gt; Linux System. Select the required metrics.<\/p>\n<\/div>\n<\/article>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/11\/aws_cloud_watch_memory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7139\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/11\/aws_cloud_watch_memory.png\" alt=\"\" width=\"584\" height=\"292\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/11\/aws_cloud_watch_memory.png 584w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/11\/aws_cloud_watch_memory-300x150.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/11\/aws_cloud_watch_memory-150x75.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/11\/aws_cloud_watch_memory-400x200.png 400w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\n<p>Connect to your EC2 instance using ssh. ssh -i ec2-user@my_ec2_ip_address<\/p>\n<p>Install following perl packages sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https<\/p>\n<p>Download the monitoring scripts curl http:\/\/aws-cloudwatch.s3.amazonaws.com\/downloads\/CloudWatchMonitoringScripts-1.2.1.zip -O<\/p>\n<p>Install the monitoring scripts unzip CloudWatchMonitoringScripts-1.2.1.zip rm CloudWatchMonitoringScripts-1.2.1.zip cd aws-scripts-mon<\/p>\n<p>run the following command to check the memory utilisation. .\/mon-get-instance-stats.pl &#8211;recent-hours=12<\/p>\n<p>In order to push [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7138"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7138"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7138\/revisions"}],"predecessor-version":[{"id":7140,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7138\/revisions\/7140"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}