But what if you want to take monitoring a step further? That’s where EC2 custom metrics come into play.
Why create custom metrics in CloudWatch?
CloudWatch is an effective cloud application monitoring tool, but built-in monitoring can’t do it all. For example, it does not monitor traffic on port 80 or port 443. It also does not monitor to see if the Nginx server is working as expected. A custom metric allows you to monitor a specific application binary or runtime. CloudWatch helps monitor the infrastructure portion of an EC2 instance, such as CPU, hard drive and network. However, if the application running on the instance is down or in a warning state, CloudWatch’s default monitoring won’t provide much information. When CloudWatch doesn’t have a metric for your specific use case, you want to implement a custom metric. Let’s see what it takes to create a custom metric in CloudWatch. In the following step by step tutorial, we have reviewed the steps to instruct CloudWatch to monitor a web server running on a Linux EC2 instance to confirm that port 443 is open for inbound HTTPS traffic. Before you begin, make sure you have an EC2 instance running Linux with a role membership for CloudWatch access. You also need EC2 SSH access to the instance.
Creating a custom metric
To create a custom metric in CloudWatch, you use the AWS Management Console or a script. IT teams should try to automate as much as possible in any environment, and a script is a great method of automation. The automation script in this example is native to Linux and presented in Bash, but you can use any programming language for this purpose. Create the automation script. To get started, SSH the EC2 instance. Create a new file called https.sh for the automation code. Open the https.sh file with Vim or Nano and copy/paste the following code: PORT_443 = $ (netstat -an | grep 443 | wc -l) aws cloudwatch put-metric-data –metric-name PORT_443_AVAILABILITY –dimensions Instance = i-0255e296e993b6df1 –namespace “port443” –value $ The automation code creates a variable that uses the netstat command to grep port 443 to ensure it is running. Use the AWS Command Line Interface with the cloudwatch command to create the custom metric using the put-metric-data option. The put-metric-data option contains four parameters: the name of the metric, the ID of the instance you want to monitor, the namespace, and the value of the metric, which is the grepping variable for port 443.
AWS CloudWatch Custom Metric Automation Code
The last step is to provide the proper permissions for the system to run the script. Use the following chmod command: chmod + x https.sh Create the cron job. Once the https.sh script is saved, it’s time to run it. Make sure the script runs continuously so that custom metrics are always updated in the CloudWatch console. To create a cron job, run the following command on the EC2 instance: crontab -e The cron job will open and you are now ready to define up the cron. To ensure that the CloudWatch custom metric extracts data from the EC2 instance reasonably, set the cron job to run the script continuously.
- / 1 * * * * /home/ec2-user/https.sh After saving the cron job, you will see output from the terminal stating that the cron is running. Cron job code metrics Check the custom metric on CloudWatch. For the last step, open the AWS Management Console and go to the CloudWatch service to check the metric. In the CloudWatch service, click Metrics. AWS Management Console CloudWatch Service Screens Under All Metrics, there is a new section for custom metrics. custom metrics Click Custom, and the new custom metric will be available. CloudWatch custom metric available
Custom metrics with other cloud providers
There are many options for making custom metrics for anyone using another cloud provider or third-party service. On Google Cloud Platform (GCP), for example, you can create custom metrics with the OpenCensus service, which is a set of libraries for various programming languages that allow you to collect metrics from applications. You can also use the GCP Cloud Monitoring API to create custom metrics that work with C#, Go, Java, Node.js, PHP, Python and Ruby. There are several monitoring tools available for Azure cloud users as well. Microsoft put custom metrics in preview mode :updated for Azure Monitor. The preview version allows you to send metrics to Azure in several ways: