July 17, 2020 / Nirav Shah
As per AWS “Amazon CloudWatch is a monitoring and observability service built for DevOps engineers, developers, site reliability engineers (SREs), and IT managers.” CloudWatch provides you with data and actionable insights to monitor your applications, respond to system-wide performance changes, optimize resource utilization, and get a unified view of operational health. CloudWatch collects monitoring and operational data in the form of logs, metrics, and events, providing you with a unified view of AWS resources, applications, and services that run on AWS and on-premises servers. You can use CloudWatch to detect anomalous behaviour in your environments, set alarms, visualize logs and metrics side by side, take automated actions, troubleshoot issues, and discover insights to keep your applications running smoothly.
For in detail blog to view custom instance metrics of an instance in AWS CloudWatch follow the link.
Step 1
Launch an instance
Step 2
Click on services and Select Cloudwatch
Step 3
Create a new Dashboard
Step 4
Select a widget type to configure
Step 5
Select EC2 in the matric
Step 6
Select Pre-instance in the matric
Step 7
Click on CPUUtilization
Step 8
Login to the server and install apache2
$ sudo apt install apache2 -y
Step 9
Check the status that apache2 is active or not
$ sudo service apache2 status
Step 10
Go to server’s public IP address or domain attached to the server and there should be apache2 default page as soon in the.
Cloudwatch Agent Installation
Step 11
Wget the cloudwatch agent setup with the help of the code below.
$ wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
Output
Step 12
Now install the downloaded setup with the help of the code below.
$ sudo dpkg -i amazon-cloudwatch-agent.deb
Step 13
Step 14
$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
Step 15
Once the configuration is finished we will start the cloudwatch agent with the following command.
$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
Step 16
Step 17
Step 18
Click on Create Widget
Step 19
$ sudo mkdir /bashcripts
Step 20
Navigate to the folder
$ cd /bashcripts
We are going to manage the widget with the help of scripts so to create and run the script steps are as follows.
Step 21
Create 3 files instance-id instance-id.sh apache-monitor.sh with the help of the following command.
$ touch instance-id instance-id.sh apache-monitor.sh
Step 22
Make the created file .sh executable with the following command.
$ chmod a+x *.sh
Step 23
Edit the create file instance-id.sh with the help of vim.
$ sudo vim instance-id.sh
Step 24
Copy the code from below and paste the code as it is.
#!/bin/bash curl http://169.254.169.254/latest/meta-data/instance-id
This code will curl the instance id of the server from the metadata.
Step 25
$ sudo crontab -e
Step 26
@reboot /bin/bash /bashcripts/instance-id.sh > /bashcripts/instance-id
Step 27
Now edit the apache-monitor.sh to get the status of the apache2 service is running or not.
$ sudo vim apache-monitor.sh
Step 28
Paste the script as it is in the apache-monitor.sh then replace
#!/bin/bash #set -x INSTANCE_ID=$(cat /bashcripts/instance-id) checkApacheStatus(){ counter=0 ps -ef | grep /usr/sbin/apache2 | grep -v grep | while read -r LINE do read PROCESS_ID <<< $LINE counter=$((counter+1)) echo $counter done } i=$(checkApacheStatus) #echo $i if [ "$i" == "" ] then aws --regioncloudwatch put-metric-data --metric-name --apache_status --value 0 --namespace --apache_status --dimensions InstanceId=$INSTANCE_ID else aws --region cloudwatch put-metric-data --metric-name --apache_status --value 1 --namespace --apache_status --dimensions InstanceId=$INSTANCE_ID fi
Step 29
We need to run this script in every one minute to check whether the apache2 service is running or not. To do this we are going to run this script via cron.
$ sudo crontab -e
Step 30
Paste the command at the end of the command
* * * * * /bin/bash /bashcripts/apache-monitor.sh
Step 31
Install AWS CLI
$ sudo apt install awscli -y
Step 32
How to create CloudWatch ALarm if the apache2 service status fails
Step 33
Go to Cloudwatch
Step 34
Click on Alarm
Step 35
Click on Create Alarm
Step 36
Click on Select Metric
Step 37
Select
Step 38
Click on the Instance ID
Step 39
Step 40
Apply the condition if it is lower than 1 then the alarm should trigger
Step 41
Give the endpoint if you have SNS topic than select that if not then click on create new SNS topic as you can follow the steps below.
Step 42
Input the name and description
Step 43
Click on create Alarm
The Alarm will be created and will trigger if the apache service fails.
As a Director of Eternal Web Private Ltd an AWS consulting partner company, Nirav is responsible for its operations. AWS, cloud-computing and digital transformation are some of his favorite topics to talk about. His key focus is to help enterprises adopt technology, to solve their business problem with the right cloud solutions.
Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.