Showomem.pl

Page last edited hace 2208 días 8 horas
De Solaris-ES
Saltar a: navegación, buscar
#!/usr/bin/perl

# *********************************************************************** #
#                                                                         #
#  SCRIPT: showomem                                                       #
#  AUTHOR: mariano.obarrio@gmail.com                                      #
#    DATE: 11/05/10                                                       #
#     REV: 0.1                                                            #
#                                                                         #
#  PLATFORM: Solaris 10                                                   #
#                                                                         #
#  PROPOSITO: Analizar la memoria de los procesos de Oracle.              #
#                                                                         #
#  ESTE ESCRIPT ES LLAMADO DESDE:                                         #
#                                                                         #
#     Linea de comando                                                    #
#                                                                         #
#                                                                         #
#  EXIT CODES:                                                            #
#                                                                         #
#  NOTE: You must have root privileges to execute this script.            #
#                                                                         #
# Historico:                                                              #
#    11/05/2010 Rev. 0.1                                                  #
#    Mariano    -> Listado de ORACLE_SID                                  #
#               -> Opciones -a -q                                         #
#    10/06/2010 -> Opcion -t                                              #
#                                                                         #
# *********************************************************************** #

use Switch;
use Getopt::Std;
use IO;

getopts("p:s:d:hlaqt", \%args);

my $DEBUG     = ($args{d}?$args{d}:0);
my $all       = ($args{a}?1:0);
my $osid      = ($args{s}?$args{s}:"ALL");
my $pid       = ($args{p}?$args{p}:-1);
my $Pmap      = "/usr/bin/pmap";
my $Grep      = "/usr/bin/grep";
my $Egrep     = "/usr/bin/egrep";
my $Awk       = "/usr/bin/awk";
my $Ps        = "/usr/bin/ps";
my $Ps2       = "/usr/ucb/ps";
my $Sort      = "/usr/bin/sort";
my $TotPriv   = 0;
my $TotShar   = 0;
my $Total     = 0;


if($args{h}){ usage(); exit; }
print "DEBUG      : ON  LEVEL: [$DEBUG]\n" if($DEBUG>=1);
print "PID        : $pid\n" if($DEBUG>=1);
print "ORACLE_SID : $osid\n" if($DEBUG>=1);

if($args{l}) { ShowSIDs(); exit; }
if($args{t}) { 
   printf("         PID         PRIVATE          SHARED           TOTAL    ARGUMENTOS\n"); 
   print "-"x90 if(!$args{q});
   print "\n" if(!$args{q});
   my $ps2cmd = "$Ps2 -auxw|sort -k4nr|head -25|awk \'{print \$2}\'";
   @psout = qx($ps2cmd);
   foreach $pid (@psout)
   {
     chomp($pid);
     ($PidPriv,$PidShar,$Gtotal) = split(/:/,ShowByPID($pid));
     $argumentos = `$Ps -fo args -p $pid|grep -v COMMAND`;
     chomp($argumentos);
     printf("%12d %12.2f Mb %12.2f Mb %12.2f Mb    %s\n",$pid,$PidPriv,$PidShar,$Gtotal,$argumentos);
   }
   exit;
}

if($pid != -1)
{
   ($PidPriv,$PidShar,$Gtotal) = split(/:/,ShowByPID($pid));
   print "-"x35 if(!$args{q});
   print "\n" if(!$args{q});
   print "Non Shared Memory by PID\n" if(!$args{q});
   print "-"x35 if(!$args{q});
   print "\n" if(!$args{q});
   printf("Pid       : %12d\nPrivate   : %12.2f Mb\nShared    : %12.2f Mb\n",$pid,$PidPriv,$PidShar);
   printf("Total     : %12.2f Mb\n",$Gtotal);
   print "-"x35 if(!$args{q});
   print "\n" if(!$args{q});
   printf("Verify    : ps -ef |egrep -i $pid|grep -v grep\n") if(!$args{q});
   #printf("KILL      : kill -9 $pid\n");
   print "-"x35 if(!$args{q});
   print "\n";
}
else 
{ 
   if($osid !~ /ALL/) { ShowByOSID($osid); }
   else               { usage();           }
}
exit;
;

sub ShowSIDs
{
   print "-"x35 if(!$args{q});
   print "\n" if(!$args{q});
   print "Listado de los SIDs disponibles\n" if(!$args{q});
   print "-"x35 if(!$args{q});
   print "\n" if(!$args{q});
   my $pmapcmd = "$Ps -efo args|$Grep _smon_|$Grep -v grep|$Sort -u";
   @psout = qx($pmapcmd);
   foreach $line (@psout)
   {
     $line =~ s/.*_//g;
     print $line;
   }
   print "-"x35 if(!$args{q});
   print "\n" if(!$args{q});
}

sub ShowByOSID
{
   my $osid = shift;
   print "Procesando por SID\n" if($DEBUG>=1);
   print "-"x83 if(!$args{q});
   print "\n" if(!$args{q});
   print "Non Shared Memory by ORACLE_SID\n" if(!$args{q});
   print "-"x83 if(!$args{q});
   print "\n" if(!$args{q});
   print "       Pid        ORACLE_SID         Private          Shared           Total\n";
   print "-"x83 if(!$args{q});
   print "\n" if(!$args{q});
   my $pmapcmd = "$Ps -ef";
   @psout = qx($pmapcmd);
   foreach $line (@psout)
   {
     chomp($line);
     if($args{a}){ $string = "oracle${osid}"; }
     else        { $string = "oracle${osid}.*LOCAL=NO"; }
     if($line =~ m/$string/)
     {
        #  oracle  6038     1   0 09:01:27 ?           0:01 oracleDNEWTESE (LOCAL=NO)
        $line =~ s/\ +/\ /g;
        $line =~ s/^\ //;
        ($nada,$pid,$nada) = split(/ /,$line,3);
        ($PidPriv,$PidShar,$Gtotal) = split(/:/,ShowByPID($pid));
        $TotPriv += $PidPriv;
        $TotShar += $PidShar;
        $Total   += $Gtotal;
        printf("%10d %17s %12.2f Mb %12.2f Mb %12.2f Mb\n",$pid,$osid,$PidPriv,$PidShar,$Gtotal);
     }
   }
   print "-"x83 if(!$args{q});
   print "\n" if(!$args{q});
   printf("           %17s %12.2f Mb %12.2f Mb %12.2f Mb\n",$osid,$TotPriv,$TotShar,$Total);
   print "-"x83 if(!$args{q});
   print "\n" if(!$args{q});
}

sub ShowByPID
{
  my $pid     = shift;
  my $PidPriv = 0;
  my $PidShar = 0;
  my $Gtotal  = 0;
  my $pmapcmd = "$Pmap $pid 2>/dev/null";
  # read/write
  @pmapout = qx($pmapcmd);
  foreach $line (@pmapout)
  {
    chomp($line);                  # no newline
    $line =~ s/K//g;
    if($line =~ m/r-x../)
    {
       $line =~ s/.*(\ )(\d+)(\ ).*$/$2/g;
       $PidShar += $line;
       next;
    }
    next if($line =~ m/shmid/);
    if($line =~ m/rw.../)
    {
       $line =~ s/.*(\ )(\d+)(\ ).*$/$2/g;
       $PidPriv += $line;
    }
  } 
  $PidPriv = $PidPriv/1024;
  $PidShar = $PidShar/1024;
  $Gtotal  = $PidPriv + $PidShar;
  return("$PidPriv:$PidShar:$Gtotal");
}

sub PrivShMem
{
  my $pid = shift;
  my $PidPriv   = 0;
  my $pmapcmd = "$Pmap $pid | $Grep 'rw...' | $Grep -v shmid | $Awk \'{print \$2}\' | $Awk -FK \'{print \$1}\'";
  # read/write
  @pmapout = qx($pmapcmd);
  foreach $line (@pmapout)
  {
    chomp($line);                  # no newline
    $PidPriv += $line;
  } 
  return(($PidPriv/1024));
}

sub TotShMem
{
  my $pid = shift;
  my $PidShar   = 0;
  my $pmapcmd   = "$Pmap $pid | $Grep 'r-x..' | $Awk \'{print \$2}\' | $Awk -FK \'{print \$1}\'";
  #read/exec
  @pmapout = qx($pmapcmd);
  foreach $line (@pmapout)
  {
    chomp($line);                  # no newline
    $PidShar += $line;
  } 
  return(($PidShar/1024));
}

sub usage{
        print "\n-- showomem.pl: Show Oracle Memory v1r0.1 by Mariano J. Obarrio Miles ( http://groups.google.es/group/solaris-es?hl=es )\n\n";
        print "Usage:\n";
        print "\n";
        print "$0 -h \n";
        print "       -h: Help\n";
        print "       -a: All Oracle Process.\n";
        print "       -l: Lista SIDs Disponibles\n";
        print "       -p: PID\n";
        print "       -t: Top 25 de consumo\n";
        print "       -s: ORACLE_SID\n";
        print "\nEj.: $0 -p 3166\n\n";
        exit;
}