Thursday 10 March 2016

Custom CloudWatch Tomcat JVM Metrics

If you use tomcat as your usual Servlet container for your Java web applications on EC2 then you may wish to monitor Java Virtual Machine stats and apply notification alarms which can be triggered should the application run into trouble. There are many JVM monitoring tools out there which range from free to expensive, simple to complex. I decided to try and create a simple and free solution using CloudWatch and the AWS Command Line Interface, (AWS CLI), to send JVM stats to CloudWatch as custom metrics. Once the custom metrics have been configured CloudWatch does the rest and provides a monitoring platform on which alarms and notifications can be easily created and managed.

To set this up you will need to do the following:

  • Have an AWS account
  • Set up a IAM Role to be applied to a running instance which you want to monitor
  • Start EC2 instance hosting Tomcat which has outbound access to the internet
  • Download the AWS CLI package
  • Create a script to extract the JVM stats and publish to CloudWatch using the CLI
  • Set execution of the script in cron to publish the metrics at your desired frequency

Assuming you already have an AWS account and know your way around, create an IAM role which will be assigned to the EC2 instance you want to monitor. This should allow applications on the instance to manage metric data

Next, ensure you have a VPC with a subnet allowing access to the outside internet. You will need a subnet which assigns a public IP to launched instances, an internet gateway and a route table which directs all outbound traffic from instances within the subet to that gateway - 0.0.0.0/0 - igw-my-gateway-id.

Launch an instance into the subnet as usual and assign the new IAM role. If you already have a base image containing a tomcat installation then use that. Otherwise, install tomcat as you wish. I won't cover this in this post. Once running open a session and install the AWS CLI package.


You can confirm the installation is good by running aws help which will show the manual. Now create a script to get the JVM stats from your running tomcat instance and use the CLI to send to CloudWatch. You may well want to run this on multiple instances so it makes sense to ascertain the instance id at runtime and use this when you define the metric name in the CLI command. To discover the instance id you'll need to make an HTTP request and interrogate the meta data, assign this to a variable in your script which will be used in the CLI command later.


Locate the catalina PID file which contains the process ID of your running Tomcat instance. Depending on your version and installation this can be in various places. The PID file contains the id of the running Tomcat process. There are various other ways you could ascertain this using the jps or ps commands.


Use the jstat command to print a snapshot of the JVM memory performance statistics. These are pretty printed so there has to be a bit of String manipulation to pick them out. Use the AWS CLI command to invoke the CloudWatch service which makes a PUT request with the substituted data. In the example below I've created a metric name for each jstat value from this EC2 instance, identified by the instance id. Here's the full script -


It's then a simple case of adding this script to a cron schedule. You'll then be able to login to the AWS console, navigate to CloudWatch and select the metrics. From here, you can easily create the alarms and notifications for different threshold values of the metric data.

21 comments:

  1. Thanks John for the details its Informative

    ReplyDelete
    Replies
    1. Hi John,

      Such vivid info on the Custom CloudWatch Tomcat JVM Metrics! Flabbergasted! Thank you for making the read a smooth sail!

      I am new to Linux, and I am hoping to have just a leisurely conversation about switching from windows. I will find answers to specific questions hopefully with a search here, but I may still ask anyway.
      Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.

      Thank you,
      Kevin

      Delete
  2. Thank you, this helped a lot.

    ReplyDelete
  3. hi John,

    I need to develop similar solution in Windows.
    Please if you can help

    ReplyDelete
  4. Really nice blog provided by you thanks for providing your information. For more updates AWS Online Training Hyderabad Get Trained and Get Job

    ReplyDelete
  5. Letter commercial create remember issue. Mrs beyond go like kitchen. Including report official not.health

    ReplyDelete
  6. Bravo! Your article was a pleasure to read. The real-life examples you shared made it easy to grasp the concepts. Also, it's great to see you promoting responsible trading practices. Speaking of which, does Radhe exchange user IDoffer any risk management tools?

    ReplyDelete
  7. "Your blog has given me such insightful information on the subject that it has deepened my understanding. Thank you for sharing!"
    SAP Analytics Cloud Training

    ReplyDelete
  8. "I really enjoyed reading your blog post; it was very well-written and extremely informative."
    Salesforce CPQ Certification

    ReplyDelete
  9. "I appreciate you sharing such a useful topic.
    Salesforce CPQ Course

    ReplyDelete
  10. I adored your blog post very much! Your observations are so insightful and energizing. It's obvious that you spent a lot of time and effort writing and researching this essay. Your writing is interesting and simple to read, making even difficult subjects seem understandable. Readers like myself who are looking for insightful and well-informed information appreciate you sharing your expertise and viewpoint on this subject. Hopefully you'll write more informative stuff in the future. Continue your excellent job!
    Salesforce CPQ Training

    ReplyDelete
  11. I appreciate you spending the time to share your knowledge and skills. You have not only increased my knowledge of subject but also my interest in it. Keep up the great work, and I'm looking forward to your upcoming posts!
    Golang Certification

    ReplyDelete
  12. Great great post; it definitely improved my understanding of the subject.
    Golang Course

    ReplyDelete
  13. "I couldn't agree more with your points; this blog really opened my eyes to new ideas!
    Mulesoft Course

    ReplyDelete
  14. "Great post, I found it to be really insightful and stimulating!"
    CCSP Training

    ReplyDelete
  15. "I totally concur with your points; this blog post is spot on!"
    CCSP Certification

    ReplyDelete
  16. Go India Tour and Cabs leads in providing Taxi service in Jaipur that offers the most luxurious and reasonably priced taxi trips in world-class vehicles. We offer cab in Jaipur travel, outstation trips, weddings, corporate events, airport services, and sightseeing in Jaipur.

    ReplyDelete