HOW TO CREATE A CLOUDWATCH ALARM TO CHECK APACHE2 STATUS?

HOW TO CREATE A CLOUDWATCH ALARM TO CHECK APACHE2 STATUS?

What is AWS Cloudwatch?

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.

Let get started

For in detail blog to view custom instance metrics of an instance in AWS CloudWatch follow the link.

Step 1

Launch an instance

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 2

Click on services and Select Cloudwatch

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 3

Create a new Dashboard

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 4

Select a widget type to configure

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 5

Select EC2 in the matric

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 6

Select Pre-instance in the matric

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 7

Click on CPUUtilization

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 8

Login to the server and install apache2

$ sudo apt install apache2 -y 
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 9

Check the status that apache2 is active or not

$ sudo service apache2 status
how-to-create-a-cloudwatch-alarm-to-check-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.

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

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
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Output

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 12

Now install the downloaded setup with the help of the code below.

$ sudo dpkg -i amazon-cloudwatch-agent.deb
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 13

  • Now create an IAM role to connect the EC2 with Cloudwatch
  • The role should have the permission of “CloudWatchAgentServerPolicy ”
  • Attach the role to the running server
how-to-create-a-cloudwatch-alarm-to-check-apache2-status
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 14

  • Start the configuration of the agent with the following command.
  • For detail how to configure custom metrics and best configuration please follow the link
$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

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
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 16

  • Now go to CloudWatch dashboard and click on “Add Widget”.
  • You will see CWAgent a new matric is been added in the dialog box
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 17

  • Click on CWAgent
  • Different type of metrics will be shown. Select the metric that you desire.
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 18

Click on Create Widget

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Creating a Widget to show the Apache2 status

Step 19

  • Login to the server
  • Create a new folder with the following command
$ sudo mkdir /bashcripts
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 20

Navigate to the folder

$ cd /bashcripts
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

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
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 22

Make the created file .sh executable with the following command.

$ chmod a+x *.sh
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 23

Edit the create file instance-id.sh with the help of vim.

$ sudo vim instance-id.sh
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

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
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

This code will curl the instance id of the server from the metadata.

Step 25

  • An instance id is a unique number assigned to the server so to get the instance id of the server which is running the script, we will add the script to cron and will run the script curl to the instance id and will paste the instance in the file created “instance-id” which will be used by apache2 status script.
  • So to do this run the following command.
$ sudo crontab -e
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 26

  • We need to run this script every time the server reboots.
  • So add the following line at the end of the crontab.
@reboot /bin/bash /bashcripts/instance-id.sh > /bashcripts/instance-id
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 27

Now edit the apache-monitor.sh to get the status of whether the apache2 service is running or not.

$ sudo vim apache-monitor.sh
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 28

Paste the script as it is in the apache-monitor.sh then replace , in the script according to your configuration

#!/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 --region  cloudwatch 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
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

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
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 30

Paste the command at the end of the command

* * * * * /bin/bash /bashcripts/apache-monitor.sh
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 31

Install AWS CLI

$ sudo apt install awscli -y
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 32

  • Restart the server
  • When the server is restarted
  • Go to /bashcripts
  • $ cd /bashcripts
  • View the instance id file and check if the instance id is there or not
  • $ cat instance-id
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

How to create CloudWatch ALarm if the apache2 service status fails

Step 33

Go to Cloudwatch

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 34

Click on Alarm

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 35

Click on Create Alarm

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 36

Click on Select Metric

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 37

Select -apache-status

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 38

Click on the Instance ID

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 39

  • Select the matric
  • Click on select metric
how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 40

Apply the condition if it is lower than 1 then the alarm should trigger

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

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.

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 42

Input the name and description

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

Step 43

Click on Create Alarm

how-to-create-a-cloudwatch-alarm-to-check-apache2-status

The Alarm will be created and will trigger if the ApacheM service fails.

Techmandra Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *