top of page

“AUTOMATED APACHE2 MONITORING: CUSTOM METRIC, SNS AND AWS CLOUDWATCH INTEGRATION USING PYTHON"

Updated: Sep 20, 2023


ree

step 1:

  • Launch AWS EC2 instance and connect through the SSH in your terminal !


step 2:

  • Update your Ec2 instance with this command

   sudo apt-get update	

step 3:

  • Install apache2 on ubuntu

   sudo apt-get install apache2

  • enable and start the apache2 service

   sudo systemctl enable apache2
   sudo systemctl start apache2

  • check the status of apache2 service

   sudo systemctl status apache2

step 4:

  • check python3 is available or not if it is not available then install first

   python3 --version
  • if python is not available

   sudo apt-get install python3
  • install pip for import additional python libraries

   sudo apt-get install python3-pip
  • install boto3 library for Cloudwatch

   pip install boto3

step 5:

  • install awscli and configure aws credential

   sudo apt-get install awscli
   aws configure

  • AWS Access Key ID [None]: <your access key>

  • AWS Secret Access Key [None]: <your secret key>

  • Default region name [None]: <your aws region>


step 6:

  • create a /bashcripts directory

   mkdir /bashcripts
   cd /bashcripts/

  • create a 3 file using touch command

   touch instance-id instance-id.sh apache2.py

  • if there is any permission issue please check it

   nano apache2.py

  • put this code into apache2.py and also make change according to your requirement

import subprocess
import os 
import boto3 

INSTANCE_ID = open('/bashcript/instance-id').read().strip() 

def check_apache2_status():
    process = subprocess.Popen(['systemctl', 'status', 'apache2'],          stdout=subprocess.PIPE)  
    output = process.communicate()[0]  
    status = output.decode()  
    
    if 'Active: active' in status:
        return 1  
    else:
        return 0 
        
status = check_apache2_status() 
print(status) 

cloudwatch = boto3.client('cloudwatch', region_name='ap-south-1') 

cloudwatch.put_metric_data(
    Namespace='Custom-metric-namespace',  
    MetricData=[
      {  'MetricName': 'apache2-monitor',  
         'Value': status,  
         'Dimensions': [
           {  
               'Name': 'InstanceId',  
               'Value': INSTANCE_ID  
           }  
       ]  
    }  
  ] 
) 

  • get instance id of your instance, paste as it is can’t change anything

   #!/bin/bash
   curl http://169.254.169.254/latest/meta-data/instance-id
  • change mode of this file

   chmod a+x *.sh

step 7:

  • set crontab for apache2.py and instance-id.sh

   crontab -e

   * * * * * sh /bashcripts/instance-id.sh > /bashcripts/instance-id

   * * * * * python3 /bashcripts/apache2.py


step 8:

  • now configure the Cloudwatch part:

  • first of all check our custom metric in All metrics in Cloudwatch


ree

  • now create a alarm for apache2 > select custom metric(apache2) > select instance id >

  • now select a lower for threshold and put threshold value 1


ree

  • create a SNS new topic

ree

  • Request and confirm subscription

ree

  • after completion of subscription create a alarm

  • Add name and description > create alarm

  • now check it’s work or not using stop the apache2 server









Comments


©2023 by CloudOpsSolution

bottom of page