Learn from your fellow PHP developers with our PHP blogs, or help share the knowledge you've gained by writing your own.
![Create Alarm and Monitoring on Custom Memory and Disk Metrics for Amazon EC2](https://images.ctfassets.net/vzl5fkwyme3u/2HgdCq2lZucMyuiYyYiQie/b7376c29a2f94799613e8c1cb8ff4d3b/AdobeStock_91111530.jpeg?w=1000)
Today I am going write a blog on how to Monitor Memory and Disk custom metrics and creating alarm in Ubuntu.
To do this, we can use Amazon CloudWatch, which provides a flexible, scalable and reliable solution for monitoring our server.
Amazon Cloud Watch will allow us to collect the custom metrics from our applications that we will monitor to troubleshoot any issues, spot trends, and configure operational performance. CloudWatch functions display alarms, graphs, custom metrics data and including statistics.
## Installing the Scripts
Before we start installing the scripts for monitoring, we should install all the dependent packages need to perform on Ubuntu.
First login to your AWS server, and from our terminal, install below packages
sudo apt-get update
sudo apt-get install unzip
sudo apt-get install libwww-perl libdatetime-perl
### Now Install the Monitoring Scripts
Following are the steps to download and then unzip we need to configure the Cloud Watch Monitoring scripts on our server:
**1. In the terminal, we need to change our directory and where we want to add our monitoring scripts.**
**2. Now run the below command and download the source:**
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
**3. Now uncompress the currently downloaded sources using the following commands**
unzip CloudWatchMonitoringScripts-1.2.2.zip && \
rm CloudWatchMonitoringScripts-1.2.2.zip && \
The directory will contain Perl scripts, because of the execution of these scripts only report memory run and disk space utilization metrics will run in our Ubuntu server.
Currently, our folder will contain the following files:
**mon-get-instance-stats.pl** - This Perl file is used to displaying the current utilization statistics reports for our AWS instance on which these file scripts will be executed.
**mon-put-instance-data.pl** - This Perl script file will be used for collecting the system metrics on our ubuntu server and which will send them to the Amazon Cloud Watch.
**awscreds.template** - This Perl script file will contain an example for AWS credentials keys and secret access key named with access key ID.
**CloudWatchClient.pm** - This Perl script file module will be used to simplify by calling Amazon Cloud Watch from using other scripts.
**LICENSE.txt** – This file contains the license details for Apache 2.0.
**NOTICE.txt** – This file contains will gives us information about Copyright notice.
**4. For performing the Cloud Watch operations, we need to confirm that whether our scripts have corresponding permissions for the actions:**
If we are associated with an IAM role with our EC2 Ubuntu instance, we need to verify that which will grant the permissions to perform the below-listed operations:
Now we need to copy the ‘awscreds.template’ file into ‘awscreds.conf’ by using the command below and which will update the file with details of the AWS credentials.
cp awscreds.template awscreds.conf
AWSAccessKeyId = my_access_key_id
AWSSecretKey = my_secret_access_key
Now we completed the configuration.
This Perl script file will collect memory, disk space utilization data and swap the current system details and then it makes handling a remote call to Amazon Cloud Watch to reports details to the collected cloud watch data as a custom metrics.
We can perform a simple test run, by running the below without sending data to Amazon CloudWatch
./mon-put-instance-data.pl --mem-util --verify --verbose
Now we are going to set a cron for scheduling our metrics and we will send them to Amazon CloudWatch
**1. Now we need to edit the crontab by using below command:**
**2. Now we will update the file using the following query which will disk space utilization and report memory for particular paths to Amazon CloudWatch in every five minutes:**
*/5 * * * * ~/STORAGE/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-avail --mem-used --disk-space-util --disk-space-avail --disk-space-used --disk-path=/ --disk-path=/STORAGE --from-cron
If there is an error, the scripts will write an error message in our system log.
### Use of Options
The above command will collect the information about used memory and which will send the details of the reports in MBs into the MemoryUsed metrics. This will give us information about the metric counts memory allocated by applications and the OS as used.
The above command will collect the information about memory utilization in percentages and which will send the details of the Memory Utilization metrics and it will count the usage of the memory applications and the OS.
The above command will collect the information to collect the current utilized disk space and which will send the reports in percentages to the DiskSpaceUtilization for the metric and for the selected disks.
The above command will collect the information about the available memory and which will send the reports in MBs to the MemoryAvailable metrics details. This is the metric counts memory allocated by the applications and the OS as used.
The above command will collect the information and will point out the which disk path to report disk space.
The above command will collect the information about the available disk space and which will send the reports in GBs to the DiskSpaceAvailable metric for the selected disks.
The above command will collect the information about the disk space used and which will send the reports in GBs to the DiskSpaceUsed metric for the selected disks.
The PATH can specify to point or any of the files can be located on which are mounted point for the filesystem which needs to be reported.
If we want to points to the multiple disks, then specify both of the disks like below:
## Setting an Alarm for Custom Metrics
Before we are going to running our Perl Scripts, then we need to create an alarm that will be listed in our default metrics except for the custom metrics. You can see some default metrics are listed in below image:
Once we completed setting the cron, then the custom metrics will be located in Linux System Metrics.
Now we are going to creating the alarm for our custom metrics
**1. We need to open the cloudwatch console panel at https://console.aws.amazon.com/cloudwatch/home**
**2. Now navigate to the navigation panel, we need to click on Alarm and we can Create Alarm.**
**3. This will open a popup which with the list of the CloudWatch metrics by category.**
**4. Now click on the Linux System Metrics . This will be listed out with custom metrics you can see in the below pictures**
**5. Now we need to select metric details and we need to click on the NEXT button. Now we need to navigate to Define Alarm step.**
**6. Now we need to define an Alarm with required fields**
Now we need to enter the Alarm name for identifying them. Then we need to give a description of our alarm.
Next, we need to give the condition with the maximum limit of bytes count or percentage when it notifies the alarm. If the condition satisfies, then the alarm will start trigger.
We need to provide a piece of additional information about for our alarm.
We need to define what are the actions to be taken when our alarm changes it state.
We need to select or create a new topic with emails needed for sending notification about alarm state.
**7. Finally, we need to choose the Create Alarm.**
So its completed. Now the alarm is created for our selected custom metrics.
Now the alarm will be listed out under the selected state in our AWS panel. Now we need to select an alarm from the list seen and we can see the details and history of our alarm.