domenica 20 febbraio 2011

Expose Apache Tomcat statistics on Cacti

Cacti is an extremly usefully tool to plot any parameters on a system.
This came out with defaults sensors for SO and Networking in general, I tried to use cacti to plot some interesting parameters about JVM and Tomcat.

I use the jmxProxy servlet came with manager webapp from tomcat's distribution to get the value of some attributes from the MBean Server.

Let we start with trace the Request Count, Error Count and Processing Time on AJP port, I do it on a Fedora 13 machine:

  1. Requirements
    1. Default Tomcat installation with manager webapp.
    2. Default Cacti installation.
    3. Set the user with manager role in tomcat-users.xml:
      1. This is a piece of my TOMCAT_HOME/conf/tomcat-users.xml:
         <tomcat-users>  
          <role rolename="manager"/>  
          <user username="manager" password="changethispassword" roles="manager"/>  
          ....  
         </tomcat-users>  
        
      2. Check the jmxProxy servlet, point your browser to http://localhost:8080/manager/jmxproxy/?qry=Catalina:type=GlobalRequestProcessor,name=http-8080 and check the result page, this is an example:
         OK - Number of results: 1  
         Name: Catalina:type=GlobalRequestProcessor,name=http-8080  
         modelerType: org.apache.tomcat.util.modeler.BaseModelMBean  
         bytesSent: 168065  
         bytesReceived: 340  
         processingTime: 5310  
         errorCount: 0  
         maxTime: 3214  
         requestCount: 16  
        
  2. Create the script to get the output of jmxProxy servlet as input for Cacti:
    1. Log on the machine as cacti user and create the file CACTI_HOME/scripts/get_tomcat_globalrequestprocessor.sh:
       #!/bin/bash   
       OPTTIONS="--user $3:$4 -f -S http://$1:$2/manager/jmxproxy/?qry=Catalina:type=GlobalRequestProcessor,name=$5"  
       OUTPUT=$(curl $OPTTIONS 2>/dev/null)  
       PT=$(printf "$OUTPUT" | grep -e "^processingTime: " | grep -oEi 'processingTime: ([0-9]*)' | sed "s/processingTime: /processingtime:/g")  
       EC=$(printf "$OUTPUT" | grep -e "^errorCount: " | grep -oEi 'errorCount: ([0-9]*)' | sed "s/errorCount: /errorcount:/g")  
       RC=$(printf "$OUTPUT" | grep -e "^requestCount: " | grep -oEi 'requestCount: ([0-9]*)' | sed "s/requestCount: /requestcount:/g")  
       echo $PT $EC $RC  
      
    2. Change it's permission: chmod 775 scripts/get_tomcat_globalrequestprocessor.sh
    3. Check the script: ./scripts/get_tomcat_globalrequestprocessor.sh localhost 8080 manager changethispassword http-8080
       -bash-4.1$ ./get_tomcat_globalrequestprocessor.sh localhost 8080 manager changethispassword http-8080  
       processingtime:73631 errorcount:2 requestcount:18253  
       -bash-4.1$   
      
  3. Configure Cacti to plot this value:

    1. Open Cacti and create a new Data Input Method. Type in the name field Tomcat GlobalRequestProcessor,
      select Script/Command as Input Type and cut&paste the script call in Input String <path_cacti>/scripts/get_tomcat_globalrequestprocessor.sh <host> <port> <username> <password> <name>.
      The name in the script wrapped between < and > are parameters that are replaced with the values came from Data Sources view (see later). Click create.

    2. Complete the Data Input Method specifying the input and output of script. Add in input field the five descriptions of input defined for the script and add in output fields list the four output value of the script (with a short description and leave check the field Update RRD File):
      1. errorcount
      2. processingtime
      3. requestcount
      And save.



    3. Add a new Data Source. Select None in the "Selected Data Template" field and an host. I've created previously a devices called Java Virtual Machine to attach all my jvm's graph, but isn't necessary, is only for ordering graph.



      Click Create and complete the Data Source with a name (Tomcat http-8080 Global Request Processor) a Data Source Path (/localhost_tomcat_globalrequestprocessor_91.rrd) and select the just created Data Input Metod (Tomcat Global Request Processor). Leave Associated RRS's, Step and Data Source Active with default value and click Create.
      Cacti has saved the DataSource and now ask for Data Source Item. In this section we have to define all data parameters that come out with Data Input Methods and specify its type (http://www.cacti.net/downloads/docs/html/templates.html Table 13-2). Start with processingtime, that is a COUNTER



      Complete the Data Source Item with other two Data Source Item, all as COUNTER type and Maximum Value 0. Complete also the Custom Data section with input value that script needs.



    4. Create the Graph. Click on Add in Graph Management and select None as Template and an host. Click Create and insert the graph name, leaving all fields with default value. Click on Create.



      Add the three Graph Items. Click Add on Graph Items, select the first our data source (Tomcat Global Request Processor (errorcount)), select a color, AREA as Graph Item Type and MAX as Consolidation Function.



      You can also add Items that display the average/max/min/last value of sensors. I added Avg and Last values for each items.



    5. The result




  4. Troubleshooting

    When your graph don't appear try to do all the step that cacti do to display the graph:
    1. Check the script's paramenters that cacti use. Go in System Utility -> View Poller Cache and write down the Script call in Details Field. Check the parameters order and try to execute the script on the shell. Check the output.
    2. Check data samples in RRD file. Use the rddtool dump command to see the file content and check if your samples are out of range (if MIN or MAX aren't zero):
       -bash-4.1$ rrdtool dump /usr/share/cacti/rra/localhost_tomcat_globalrequestprocessor_91.rrd | less  
      
    3. Check the ERROR: the RRD does not contain an RRA matching the chosen CF This error means that the statistics collected from rrd don't match the type of Consolidation Function selected on Graph Items (Console -> Graph Management -> (Edit) -> Graph Items). To see which statistics are collected by rrd, open the rrd file with rrdtool dump (as above) and see the cf element of rra node.




  5. Enhancement

    In this graph I don't use the template for Data Source/Graph, but if you have to create the same graph for multiple machines, creating template is the right choose.







I hope this help to learn cacti, which hasn't a very flat learning curve.