Skip to main content

Monitoring Memory and Disk Metrics for Amazon EC2 Linux Instances Via AWS CloudWatch

A new multi-platform CloudWatch agent is available. You can use a single agent to collect both system metrics and log files from Amazon EC2 instances and on-premises servers. 

The new agent supports both Windows Server and Linux and enables you to select the metrics to be collected, including sub-resource metrics such as per-CPU core. 

We recommend you use the new agent instead of the older monitoring scripts to collect metrics and logs. For more information about the CloudWatch agent, see Collect Metrics from Amazon EC2 Instances and On-Premises Servers with the CloudWatch Agent in the Amazon CloudWatch User Guide

Package Contents
The package for the monitoring scripts contains the following files:
  • CloudWatchClient.pm – Shared Perl module that simplifies calling Amazon CloudWatch from other scripts.
  • mon-put-instance-data.pl – Collects system metrics on an Amazon EC2 instance (memory, swap, disk space utilization) and sends them to Amazon CloudWatch.
  • mon-get-instance-stats.pl – Queries Amazon CloudWatch and displays the most recent utilization statistics for the EC2 instance on which this script is executed.
  • awscreds.template – File template for AWS credentials that stores your access key ID and secret access key.
  • LICENSE.txt – Text file containing the Apache 2.0 license.
  • NOTICE.txt – Copyright notice.
To install the required packages
  1. Log on to your instance. For more information, see Connect to Your Linux Instance.
  2. At a command prompt, install packages as follows:
    sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64
    
  3. Also run :
    sudo yum install perl-DateTime perl-CPAN perl-Net-SSLeay perl-IO-Socket-SSL perl-Digest-SHA gcc -y
    sudo yum install zip unzip
    
Getting Started

The following steps show you how to download, uncompress, and configure the CloudWatch Monitoring Scripts on an EC2 Linux instance.


To download, install, and configure the monitoring scripts

At a command prompt, move to a folder where you want to store the monitoring scripts and run the following command to download them:

curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O

Run the following commands to install the monitoring scripts you downloaded:

unzip CloudWatchMonitoringScripts-1.2.2.zip
rm CloudWatchMonitoringScripts-1.2.2.zip 
cd aws-scripts-mon

Ensure that the scripts have permission to perform CloudWatch operations using one of the following options:
  • If you associated an IAM role (instance profile) with your instance, verify that it grants permissions to perform the following operations:
    • cloudwatch:PutMetricData
    • cloudwatch:GetMetricStatistics
    • cloudwatch:ListMetrics
    • ec2:DescribeTags

Specify your AWS credentials in a credentials file.

First, copy  the awscreds.template file included with the monitoring scripts to awscreds.conf as follows:
cp awscreds.template awscreds.conf

Add the following content to the awscreds.conf file:

AWSAccessKeyId=my-access-key-id
AWSSecretKey=my-secret-access-key

To perform a simple test run without posting data to CloudWatch

./mon-put-instance-data.pl --mem-util --verify --verbose

To collect all available memory metrics and send them to CloudWatch
/usr/local/bin/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-path=/dev/xvda1 --disk-space-util --disk-space-used --disk-space-avail

To set a cron schedule for metrics reported to CloudWatch

Start editing the crontab using the following command: crontab -e

Add the following command to report memory and disk space utilization to CloudWatch every five minutes:

*/5 * * * * /usr/local/bin/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-path=/dev/xvda1 --disk-space-util --disk-space-used --disk-space-avail

After you successfully run the mon-put-instance-data.pl script, you can view your custom metrics in the Amazon CloudWatch console.
To view custom metrics
  1. Run mon-put-instance-data.pl as described previously.
  2. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.
  3. Choose View Metrics.
  4. For Viewing, your custom metrics posted by the script are displayed with the prefix 

Comments

Popular posts from this blog

Kubernetes Basics Cheat sheet

Kubernetes doesn’t have to be that way, you can use this simple cheat sheet to understand the basics. Kubernetes Architecture Components Master Node : The control plane of the cluster, responsible for managing the state of the cluster, scheduling and managing workloads, and providing a centralized configuration. Worker Nodes : These are the machines (physical or virtual) that run your applications and services. Pods are scheduled on worker nodes, and the containers within those pods run on the worker nodes. etcd : A distributed key-value store that provides a source of truth for the cluster state and configuration. The master node communicates with etcd to ensure the desired state of the cluster is maintained. API Server : The front-end of the master node, responsible for serving the RESTful API used by all other components to interact with the cluster. Controller Manager : Monitors the state of the cluster and makes changes as necessary to ensure the desired state is maintained. Sched...