#!/usr/bin/perl
#
#  $Id: sm_wheels 20038 2010-09-08 18:52:09Z teetov $
# -----------------------------------------------------------------------------
#  SM_WHEELS - small tool for analize wheel status
# -----------------------------------------------------------------------------
#  Author: Alex Titov
#  QA by:
#  Copyright: videoNEXT LLC
# -----------------------------------------------------------------------------
#
# for REQ see: Rally S623:Storage Manager TA902: SM tools
# notes:
#   1. command line tool
#
# usage: 
#  sm_wheels inspect
#  sm_wheels OFFLINE <uuid>
#  sm_wheels ONLINE  <uuid>
#  sm_wheels RELEASE <uuid> - TBD
#  sm_wheels SUSPEND <uuid> - TBD


use warnings;
use strict;
use Data::Dumper;
use File::Basename qw(dirname);    
use lib dirname(__FILE__).'/../lib';              # find  SM::Config here
use SM::Config ':all';

# CONS ------------------------------------------------------------------------
my $WHEELS    =(split(/\//,$0))[-1];	          # the actual name of the prog
# VARS ------------------------------------------------------------------------
my $vol=SM_Wheels(); #configs of active volumes ex: $vol->{$uuid}->{LIMIT_WRITE}
#my $dev;              #configs of storage consumers
# SUBS ------------------------------------------------------------------------

#print Dumper($vol);

my $head_long ="Volume-Id/uuid                        Label    Configured Status     Size GB Free GB[%%]  W-Limit W-now  await util\n"
              ."------------------------------------ --------  ---------- ---------  ------- -----------  ------- ------ ----- -----\n";
my $tail_long ="------------------------------------ --------  ---------- ---------  ------- -----------  ------- ------ ----- -----\n"
              ."Total:                                                                %4d  %4d [%2d%%]           %6.2f\n";
my $form_long ="%-36.36s %-8.8s   %-8.8s  %-8.8s    %4d  %4d [%2d%%]  %6d   %6.6s  %5.5s %5.5s\n";

my $head_short="shortID   CST    Status   Size GB Free GB[%%] Wlim W-now  await util\n"
              ."-------- ------- -------- ------- ----------- ---- ------ ----- -----\n";
my $tail_short="-------- ------- -------- ------- ----------- ---- ------ ----- -----\n"
              ."Total:                       %4d  %4d [%2d%%]      %6.2f\n";
my $form_short="%-8.8s %-0.0s%-7.7s %-8.8s    %4d  %4d [%2d%%] %4d %6.6s %5.5s %5.5s\n";

sub inspect {
 my $task=shift;
 my ($t_size,$t_free,$t_write)=(0,0,0);
 my ($head,$tail,$form)=($task eq 'ins')?($head_short,$tail_short,$form_short)
                                        :($head_long, $tail_long, $form_long);
 print $head;
 foreach(sort keys %$vol) {
   my $disk=$vol->{$_};
#   print Dumper($disk);
   my $space=int($disk->{stat_FREE}/1000);
   $space=-1 if $disk->{stat_FREE} == -1;                   # this is undef
   if($disk->{ost}=~/^(Online|Degraded|Full)$/) {           # include in total only active volumes
     $t_size +=$disk->{stat_SIZE};
     $t_free +=$disk->{stat_FREE}  if $space>=0;
     $t_write+=$disk->{stat_WRITE} if $space>=0;
   }
   printf $form,$_,$disk->{NAME},$disk->{cst},$disk->{ost},
          int($disk->{stat_SIZE}/1000),$space,$disk->{stat_freeprc},
          $disk->{LIMIT_WRITE},$disk->{stat_WRITE},$disk->{stat_AWAIT},$disk->{stat_UTIL};
 }
 printf $tail,int($t_size/1000),int($t_free/1000),int(100*$t_free/(($t_size)?$t_size:1)),$t_write;
 0;
}

sub switch {
  my ($state,$wheel)=@_;
  SM_info($wheel,"Console command $state for volume $wheel");
  my $name=SM_STAT."/$wheel.cst";
  my $temp=SM_STAT."/$wheel.tmp";
  if (open(CST,">$temp")) {
    print CST "$state\n";
    close CST;
    unlink($name) if -f $name;
    rename($temp,$name);
  } else {
    print "ERR: Cannot open $name for writing\n";
    SM_LOG->error("ERR: Cannot open $name for writing\n");
    return 2;
  } 
  0;
}

sub get_task {
 return ('') if not defined $ARGV[0];
 my $task=$ARGV[0];
 return ($task) if $task=~/^(inspect|ins)$/;
 return ('') if not $task=~/^(OFFLINE|ONLINE|SUSPEND)$/;
 return ('') if not defined $ARGV[1];
 my $uuid=$ARGV[1];
 foreach(keys %$vol) { 
   $uuid=$_,last if /^$uuid/; # short format match found
   $uuid=$_,last if $vol->{$_}->{NAME} eq $uuid; # name matches
 }
 print "Note: '$ARGV[1]' substituted by $uuid\n" if $uuid ne $ARGV[1];
 if(not defined $vol->{$uuid}) {
   print "Volume '$uuid' does not found in wheels group\n";
   return ('');
 }
 return ($task,$uuid);
}

# MAIN =======================================================================


my ($task,$uuid)=get_task;
if(not $task) {
  print "\n\nUsage:\n\t$WHEELS inspect\n\t$WHEELS OFFLINE <uuid>\n"
       ."\t$WHEELS ONLINE <uuid>\n\t$WHEELS SUSPEND <uuid>\n\n";
  exit 1;
}

exit(inspect($task))      if $task=~/^(inspect|ins)$/;
exit(switch($task,$uuid)) if $task=~/^(OFFLINE|ONLINE|SUSPEND)$/;

