Ad Widget

Collapse

Tomcat monitoring with the manager-app

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • renke
    Junior Member
    • Sep 2009
    • 3

    #1

    Tomcat monitoring with the manager-app

    Hi all,

    I read about the monitoring with jmx but imo it is boring to use the same way every time - so here's an approach with the manager application of tomcat:

    1. add something like
    Code:
    <user username="zabbix" password="zabbix" roles="manager"/>
    to the tomcat-users.xml file

    2. copy the following in a file and mark it as executable (the values for port, user and password should be changed according to your environment):
    Code:
    #!/usr/bin/perl -w
    use strict;
    my $ret = undef;
    
    if(!eval "require LWP::UserAgent;") {
        $ret = "LWP::UserAgent not found";
    }
    
    if(!eval "require XML::Simple;") {
        $ret .= "XML::Simple not found";
    } 
    
    my $URL      = exists $ENV{'url'}      ? $ENV{'url'}      : "http://%s:%s\@127.0.0.1:%d/manager/status?XML=true";
    my $PORT     = exists $ENV{'ports'}    ? $ENV{'ports'}    : 8080;
    my $USER     = exists $ENV{'user'}     ? $ENV{'user'}     : "zabbix";
    my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : "zabbix";
    my $TIMEOUT  = exists $ENV{'timeout'}  ? $ENV{'timeout'}  : 30;
    
    my $url = sprintf $URL, $USER, $PASSWORD, $PORT;
    
    
    if(exists $ARGV[0] and $ARGV[0] eq "running") {
        my $au = LWP::UserAgent->new(timeout => $TIMEOUT);
        my $repsonse = $au->request(HTTP::Request->new('GET',$url));
        if($repsonse->is_success and $repsonse->content =~ /<status>.*<\/status>/im) {
            print "1\n";
            exit 0;
        } else {
            print "0\n";
            exit 1;
        }
    }
    
    if(exists $ARGV[0] and $ARGV[0] eq "mem") {
        my $ua = LWP::UserAgent->new(timeout => $TIMEOUT);
        my $xs = new XML::Simple;
        my $response = $ua->request(HTTP::Request->new('GET',$url));
        my $xml = $xs->XMLin($response->content);
        if($xml->{'jvm'}->{'memory'}->{'free'} && $xml->{'jvm'}->{'memory'}->{'total'}) {
            print "" . ($xml->{'jvm'}->{'memory'}->{'total'} - $xml->{'jvm'}->{'memory'}->{'free'}) . "\n";
        }
    }
    
    if(exists $ARGV[0] and $ARGV[0] eq "maxmem") {
        my $ua = LWP::UserAgent->new(timeout => $TIMEOUT);
        my $xs = new XML::Simple;
        my $response = $ua->request(HTTP::Request->new('GET',$url));
        my $xml = $xs->XMLin($response->content);
        if($xml->{'jvm'}->{'memory'}->{'max'}) {
            print "" . ($xml->{'jvm'}->{'memory'}->{'max'}) . "\n";
        }
    }
    
    if(exists $ARGV[0] and $ARGV[0] eq "freemem") {
        my $ua = LWP::UserAgent->new(timeout => $TIMEOUT);
        my $xs = new XML::Simple;
        my $response = $ua->request(HTTP::Request->new('GET',$url));
        my $xml = $xs->XMLin($response->content);
        if($xml->{'jvm'}->{'memory'}->{'free'}) {
            print "" . ($xml->{'jvm'}->{'memory'}->{'free'}) . "\n";
        }
    }
    4. add some userdefined items to your zabbix_agentd.conf, I use this:
    Code:
    UserParameter=tomcat.running,/opt/zabbix-scripts/tomcat_running.pl running
    UserParameter=tomcat.mem,/opt/zabbix-scripts/tomcat_running.pl mem
    UserParameter=tomcat.maxmem,/opt/zabbix-scripts/tomcat_running.pl maxmem
    UserParameter=tomcat.freemem,/opt/zabbix-scripts/tomcat_running.pl freemem
    5. add the items to your zabbix host configuration and start to monitor the state of your tomcat container

    the status page of the manager application supports much more infos about the tomcat server - feel free to extend the script.

    disclaimer: I "stole" some of the code from a munin plugin
  • maniat1k
    Junior Member
    • Feb 2015
    • 2

    #2
    hello! A couple of small questions ...

    First: Where I put the code you're giving? outside the tomcat / opt for instance?
    Second: The file name does not interest me, right?

    Where can I get the zabbix-scripts?

    greetings,
    maniat1k

    Comment

    • renke
      Junior Member
      • Sep 2009
      • 3

      #3
      Hi maniat1k,

      I totally forgot about this account - and I'm not actively using Zabbix anymore (different employer, different environment) so take this with a grain of salt

      > First: Where I put the code you're giving? outside the tomcat / opt for instance?
      The perl script and Zabbix agent can be run everywhere, as long as the manager app of Tomcat is reachable via http(s).

      I ran the script locally on the Tomcat server, see $URL (the host part is 127.0.0.1, i.e. localhost)

      > Second: The file name does not interest me, right?
      > Where can I get the zabbix-scripts?
      Those are related - the Zabbix config uses tomcat_running.pl as script name, if you save the perl listing with this file name it should work.

      Renke.

      Comment

      • Maximus22
        Junior Member
        • Mar 2015
        • 1

        #4
        Infirmation

        Thanks for information.

        Comment

        Working...