November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Get the java info perl

#!/usr/bin/perl

# Fast script to grab jstack / jinfo and jmap of processes running on server.
#

### Update paths here for java_base.
$java_base='/usr/java/bin';
$jstack='jstack';
$jinfo='jinfo';
$jmap='jmap';
$script=$0;
$runuser=`/usr/xpg4/bin/id -nu`;
chomp($runuser);
#$outdir='/tmp';
($min, $hour, $day, $month, $year) = (localtime)[1,2,3,4,5];
$year=$year+1900;
if ( $month < 10 ) { $month="0$month"; }
if ( $day < 10 ) { $day="0$day"; }
if ( $hour < 10 ) { $hour="0$hour"; }
if ( $min < 10 ) { $min="0$min"; }

$tarfile="$script-$year$month$day-$hour$min.tar";

sub options
{
        use Getopt::Long;
        if ($#ARGV lt 0)
        {
                &usage;
        }
        GetOptions ("debug|d"           => \$debug,
                    "verbose|v"         => \$verbose,
                    "quiet|q"           => \$quiet,
                    "outdir=s"          => \$outdir,
                    "o=s"               => \$outdir,
                    "remove|r"          => \$remove,
                    "help|h"              => \$help);
        &usage if $help;
}

# sub routine to print out the usage if the no command line arguments are passed.
sub usage
{
        print "This script will generate jstack / jinfo and jmap of all java processes on the server that are currently running\n";
        print "It will create a tar file of all output.\n";
        print "Optionally, the script will remove the output directory\n";
        print "\n";
        print "-outdir or -o: Specify an output directory for the traces.\n";
        print "-remove or -r: Remove the output directory after creating the tar file.\n";
        print "-debug or -d: Debug Output.\n";
        print "-verbose or -v: Verbose Output.\n";
        print "-help or -h: Help message.\n";
        print "\n";
        print "Example Usage:\n";
        print "\t$script -o foo -v\n";

        exit 1;
}

&options;

if ( $runuser ne "root" )
{
        print "You need to be root to use this tool\n";
        exit 1;
}

if ( ! $outdir )
{
        print "Output directory is required\n";
        exit 2;
}
else
{
        if ( -d $outdir )
        {
                print "Output directory $outdir already exists - exiting\n";
                exit 3;
        }
        if ( ! -d $outdir )
        {
                system("mkdir $outdir");
        }
        if ( $verbose )
        {
                print "Created $outdir\n";
        }
        if ( $debug )
        {
                print "DEBUG: mkdir $outdir\n";
        }

        system("/usr/bin/logger -p local0.info \"$script : invoked by $runuser \"");

        @jps=`$java_base/jps | grep -v Jps`;

        foreach (@jps)
        {
                ($pid,$name)=split(/ /, $_);
                chomp ($name);
                if ( $debug )
                {
                        print "DEBUG: pid: $pid\n";
                        print "DEBUG: name: $name\n";
                }
                $is_64_bit=`$java_base/$jstack $pid 2>&1 | grep -c 'debuggee is 64 bit'`;
                if ( $is_64_bit == 1 ) # we need to use 64 bit option here.
                {
                        $proc_bits=64;
                }
                else
                {
                        $proc_bits=32;
                }
                if ( $debug )
                {
                        print "DEBUG: $java_base/$jstack -d$proc_bits $pid 2>&1 > $outdir/jstack.$name.wri\n";
                }
                $cmd=`$java_base/$jstack -d$proc_bits $pid 2>&1 > $outdir/jstack.$name.wri`;
                if ( $verbose )
                {
                        print "Creating stack trace for $name\n";
                }


                if ( $debug )
                {
                        print "DEBUG: $java_base/$jinfo -d$proc_bits $pid 2>&1 > $outdir/jinfo.$name.wri\n";
                }
                $cmd=`$java_base/$jinfo -d$proc_bits $pid 2>&1 > $outdir/jinfo.$name.wri`;
                if ( $verbose )
                {
                        print "Creating info trace for $name\n";
                }

                if ( $debug )
                {
                        print "DEBUG: $java_base/$jmap -d$proc_bits $pid 2>&1 > $outdir/jmap.$name.wri\n";
                }
                $cmd=`$java_base/$jmap -d$proc_bits $pid 2>&1 > $outdir/jmap.$name.wri`;
                if ( $verbose )
                {
                        print "Creating jmap trace for $name\n";
                }
        } # end foreach
        $cmd=`tar cvf $tarfile $outdir`;
        $cmd=`gzip $tarfile`;
        if ( $remove )
        {
                $cmd=`rm -rf $outdir`;
                if ( $verbose )
                {
                        print "Removing directory $outdir\n";
                }
                if ( $debug )
                {
                        print "DEBUG: rm -rf $outdir\n";
                }
        }
} # end else.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>