%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /data/old/etc/munin/plugins/
Upload File :
Create Path :
Current File : //data/old/etc/munin/plugins/tomcat_access

#!/usr/bin/perl
# -*- perl -*-

=head1 NAME

tomcat_access - Plugin to monitor the number of accesses to Tomcat
servers.

=head1 CONFIGURATION

The following environment variables are used by this plugin

 timeout   - Connection timeout
 url       - Override default status-url
 port      - HTTP port number
 user      - Manager username
 password  - Manager password
 connector - Override connector to monitor

=head1 USAGE

Requirements:

Needs access to
http://<user>:<password>@localhost:8080/manager/status?XML=true (or
modify the address for another host).

Tomcat 5.0 or higher.

A munin-user in $CATALINA_HOME/conf/tomcat-users.xml should be set up
for this to work.

Tip: To see if it's already set up correctly, just run this plugin
with the parameter "autoconf". If you get a "yes", everything should
work like a charm already.

tomcat-users.xml example:
 <user username="munin" password="<set this>" roles="standard,manager"/>

=head1 AUTHOR

Rune Nordbøe Skillingstad <runesk@linpro.no>

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=manual
 #%# capabilities=autoconf

=cut

use strict;
use overload;
use Munin::Plugin;
use XML::Simple;

sub objectToString {
    my $obj    = shift; # object to convert to string
    my $indent = shift; # indentation level. Defaults to 0.

    $indent = 0 unless( defined( $indent ));
    return 'undef' unless( defined( $obj ));

    my $ret = '';

    my $strval = overload::StrVal( $obj );

    my ($realpack, $realtype, $id) = ($strval =~ /^(?:(.*)\=)?([^=]*)\(([^\(]*)\)$/);
    $realpack = '' unless( $realpack );
    $realtype = '' unless( $realtype );
    $id = '' unless( $id );

    if( $realpack eq "Math::BigInt" ) {
        $ret .= ref( $obj ) . " { ";
        $ret .= $obj->bstr;
        $ret .= " }";
    } elsif( $realtype eq "ARRAY" ) {
        $ret .= ref( $obj ) . " {\n";
        # check whether this is a pseudo-hash
        if( @{$obj} && overload::StrVal( @{$obj}[0] ) =~ m!^pseudohash=! ) {
            my $pseudo = @{$obj}[0];
            foreach my $k ( keys %{$pseudo} ) {
                $ret .= indent( $indent+1 );
                $ret .= sprintf( "%-32s => %s\n", $k, objectToString( @{$obj}[ $pseudo->{$k} ], $indent+1 ));
            }
        } else {
            foreach my $o ( @{$obj} ) {
                $ret .= indent( $indent+1 );
                if( ref( $o ) eq "HASH" || ref( $o ) eq "ARRAY" ) {
                    $ret .= objectToString( $o, $indent+1 ) . "\n";
                } else {
                    if( defined( $o )) {
                        $ret .= objectToString( $o, $indent+1 ) . "\n";
                    } else {
                        $ret .= "<<undef>>\n";
                    }
                }
            }
        }
        $ret .= indent( $indent ) . "}\n";

    } elsif( $realtype eq "HASH" ) {
        $ret .= ref( $obj ) . " {\n";
        foreach my $k ( keys %{$obj} ) {
            $ret .= indent( $indent+1 );
            $ret .= sprintf( "%-32s => %s\n", $k, objectToString( $obj->{$k}, $indent+1 ));
        }
        $ret .= indent( $indent ) . "}";
    } else {
        $ret .= $obj;
    }
    return $ret;
}

#####
# indent so many times
sub indent {
    my $indent = shift;

    $indent = 0 unless( defined( $indent ));
    my $ret = '';
    for( my $i=0 ; $i<$indent ; ++$i ) {
        $ret .= '    ';
    }
    return $ret;
}



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\@%s:%d/manager/status?XML=true";
my $PORT     = exists $ENV{'port'}     ? $ENV{'port'}     : exists $ENV{'ports'} ? $ENV{'ports'} : 8080;
my $HOST     = exists $ENV{'host'}     ? $ENV{'host'}     : "127.0.0.1";
my $USER     = exists $ENV{'user'}     ? $ENV{'user'}     : "munin";
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : "munin";
my $TIMEOUT  = exists $ENV{'timeout'}  ? $ENV{'timeout'}  : 30;
my $CONNECTOR= exists $ENV{'connector'}? $ENV{'connector'}: "http-".$PORT;

my $url = sprintf $URL, $USER, $PASSWORD, $HOST, $PORT;

if(exists $ARGV[0] and $ARGV[0] eq "autoconf") {
    if($ret) {
	print "no ($ret)\n";
	exit 0;
    }
    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 "yes\n";
	exit 0;
    } else {
	print "no (no tomcat status)\n";
	exit 0;
    }
}

if(exists $ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Tomcat accesses\n";
    print "graph_args --base 1000\n";
    print "graph_vlabel accesses / \${graph_period}\n";
    print "graph_category tomcat\n";
    print "accesses.label Accesses\n";
    print "accesses.type DERIVE\n";
    print "accesses.max 1000000\n";
    print "accesses.min 0\n";
    exit 0;
}

my $ua = LWP::UserAgent->new(timeout => $TIMEOUT);
my $xs = new XML::Simple;
my $response = $ua->request(HTTP::Request->new('GET',$url));
my %options = ( KeyAttr => { connector => 'name' }, ForceArray => 1 );
my $xml = $xs->XMLin($response->content, %options);

if($xml->{'connector'}->{$CONNECTOR}->{'requestInfo'}->[0]->{'requestCount'}) {
    print "accesses.value " . $xml->{'connector'}->{$CONNECTOR}->{'requestInfo'}->[0]->{'requestCount'} . "\n";
} else {
    print "accesses.value U\n";
}

# vim:syntax=perl

Zerion Mini Shell 1.0