Iostat.pl

Page last edited hace 2081 días 9 horas
De Solaris-ES
Saltar a: navegación, buscar
#!/usr/bin/perl
# *********************************************************************** #
#                                                                         #
#  SCRIPT: iostat.pl                                                      #
#  AUTHOR: mariano.obarrio@gmail.com                                      #
#    DATE: 22/03/10                                                       #
#     REV: 0.1                                                            #
#                                                                         #
#  PLATFORM: Solaris 10                                                   #
#                                                                         #
#  PROPOSITO: Analizar las IOPS, Throughput y de un device.               #
#                                                                         #
#  ESTE ESCRIPT ES LLAMADO DESDE:                                         #
#                                                                         #
#     Linea de comando                                                    #
#                                                                         #
#                                                                         #
#  EXIT CODES:                                                            #
#                                                                         #
#  NOTE: You must have root privileges to execute this script.            #
#                                                                         #
# Historico:                                                              #
#    23/03/2010 Rev. 0.2                                                  #
#    Mariano -> open(IOSTAT, "iostat -zxn $interval|");                   #
#               open(IOSTAT, "/usr/bin/iostat -zxn $interval|");          #
#                                                                         #
# *********************************************************************** #
use POSIX qw/ strftime /;
use Getopt::Std;

### -------- Variables
getopts("i:d:c:hqpf:", \%args);
if($args{h}){ usage(); exit 1; }
if(!$args{d}) { $all = 1;      }
else          { $d = $args{d}; }
my $interval = ($args{i}?$args{i}:1);;
my $filename = ($args{f}?$args{f}:"");;
my $count    = ($args{c}?$args{c}:0);;
my $cc       = 0;
my $first    = 0;

### -------- Encabezado
if(!$args{q})
{
   print "-"x130,"\n";
   printf("Start    : %s\n",strftime("%Y-%m-%d %H:%M:%S",localtime(time)));
   printf("Interval : %s Seg\n",$interval);
   printf("Count    : %s\n",($count?$count:"Infinito"));
   printf("Device   : %s\n",($all?"ALL":$d)) if(!$args{q});
   printf("Filename : %s\n",$filename) if($filename);
   print "-"x130,"\n";
   print "                                                                              Kthr   CPU\n";
   print "Date       Time           IOPS ThPut (k/s)       KR/s       KW/s ASVC_T   %B  r b w Idle Device\n";
   print "-"x130,"\n";
}

### -------- Main
open(DAT,">$filename") || die("Cannot Open File") if($filename);
open(IOSTAT, "/usr/bin/iostat -zxn $interval|");
while(<IOSTAT>)
{
     next if($_ =~ /extended|device/);
     ($rs,$ws,$krs,$kws,$wait,$actv,$wsvc_t,$asvc_t,$w,$b,$device) = split;
     if($device =~ /$d/ || $all)
     {
      $vmstat = `/usr/bin/vmstat 1 2|/usr/bin/tail -1` if(!$all);
      $tt = strftime("%Y-%m-%d".($args{p}?";":" ")."%H:%M:%S",localtime(time));
      $vmstat =~ s/^ +//;
      $vmstat =~ s/ +/ /g;
      $vmstat =~ s/\n//g;
      ($vm_r, $vm_b, $vm_w, $vm_swap, $vm_free, 
       $vm_re, $vm_mf, $vm_pi, $vm_po, $vm_fr, 
       $vm_de, $vm_sr, $vm_m1_1, $vm_m1_2, 
       $vm_m1_3, $vm_m2_4, $vm_in, $vm_sy, 
       $vm_cs, $vm_us, $vm_sy, $vm_id) = split(/\ /,$vmstat);
      $iops  = $rs+$ws;
      $thput = ($krs+$kws);
      # $iops =~ s/\./\,/g;
      # $thput =~ s/\./\,/g;
      if($first == 1) # Descarta la primer medicion
      {
         printf("%19s %10.2f %11.2f %10.2f %10.2f %6.2f %4d %2d %1d %1d %4d %s\n",
                $tt,$iops,$thput,$krs,$kws,$asvc_t,$b,$vm_r,$vm_b,$vm_w,$vm_id,$device) if(!$args{q} && !$args{p});
         printf("%19s;%10.2f;%11.2f;%10.2f;%10.2f;%6.2f;%4d;%2d;%1d;%1d;%4d;%s\n",
                $tt,$iops,$thput,$krs,$kws,$asvc_t,$b,$vm_r,$vm_b,$vm_w,$vm_id,$device) if(!$args{q} && $args{p});
         printf(DAT "%19s %10.2f %11.2f %10.2f %10.2f %6.2f %4d %2d %1d %1d %4d %s\n",
                $tt,$iops,$thput,$krs,$kws,$asvc_t,$b,$vm_r,$vm_b,$vm_w,$vm_id,$device) if($filename);
         if($count != 0) { $cc++; exit 0 if($cc >= $count); }
      } else { $first = 1; }
     }
}
close(IOSTAT);
close(DAT) if($filename);
exit 0;


sub usage{
        print "Usage:\n";
        print "\n";
        print "usage: $0 -i 30 -d c1t0d0 -f /tmp/c1t0d0.out\n";
        print "   -i <Intervalo>\n";
        print "   -d <Device>\n";
        print "   -f <FileName> Envia la salida a un archivo\n";
        print "   -q Quiet\n";
        exit;
}