#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2006,2008 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)01   1.1   src/csm/install/noderpminstallstatus.perl, setup, csm_rgar2h, rgar2hs001a 12/11/06 01:45:17
#-----------------------------------------------------------------

=head1    noderpminstallstatus 
    Get the node's rpm installation real time status.
        Unreachable
        Alive
        <rpm is being installed> <Total percentage>
=cut
#------------------------------------------------------------------

use strict;

BEGIN { $::csmpm = $ENV{'CSM_PM'} ? $ENV{'CSM_PM'} : '/opt/csm/pm'; }
use lib $::csmpm;

use Getopt::Long;
use NodeUtils;
use MessageUtils;

# message subsystem
$::MSGCAT     = 'csmInstall.cat';
$::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : '/opt/csm/msgmaps';
$::MSGSET     = 'csminstall';


#--------------------------------------------------------------------------------

=head3	usage

	Displays usage information

        Notes:

=cut

#--------------------------------------------------------------------------------

sub usage
{
	MessageUtils->message('I', 'IMsgGetnodertstatUsage');
	exit 1;
}


#--------------------------------------------------------------------------------
sub getArgs
{
    $Getopt::Long::ignorecase = 0;
    Getopt::Long::Configure("bundling");
        if (
            !GetOptions(
                                    'd=s'   => \$::NODERTSTATAWK_DIR,
                                    'n=s'   => \$::NODE_LIST,
                                    'h'     => \$::HELP,
                        )
        )
        {
           &usage; 
           exit 2;
        }

        if ($::HELP) 
        { 
            &usage; 
            exit 0;
        }

        if (!$::NODERTSTATAWK_DIR)
        {
            $::NODERTSTATAWK_DIR = "/opt/csm/csmbin";
        }

        return $::NODE_LIST;
}

sub nodestat
{
    my $nodes = shift;
    my $port = 3001;

    my @nodesarr = split(",", $nodes);

    my $pid;
    my @children = ();
    my $pid_count;
    my $threshold = 64;
    foreach my $n (sort @nodesarr){
        # This script might be run on install server,
        # on which fping is not available.
        if ($pid = fork)
        {
            #parent, store children pid
            push @children, $pid;
            $pid_count++;
        }
        elsif (defined $pid)
        {
            # Children, get the node real time status
            my $cmd;
            if (-e "/usr/sbin/fping")
            {
                $cmd = "fping -c 1 $n >/dev/null 2>&1";
            }
            else
            {
                # set the time out value to 1 second
                $cmd = "ping -W 1 -c 1 $n >/dev/null 2>&1";
            }
            system($cmd);
            my $rc = $? >> 8;
            if(!$rc){
                chomp(my $stat = `$::NODERTSTATAWK_DIR/noderpminstallstatus.awk $n $port 2>/dev/null | tail -n 1`);
                if($stat ne ""){
                    if($stat =~ /\%(.?)*installing/){
                        chomp($stat);
                        my ($percent, $package) =  split(":",$stat);
                        print "$n: ";
                        chop($percent);
                        $percent =~ s/\%//g;
                        $percent = int($percent);
                        if ($percent > 100)
                        {
                           $percent = 100;
                        }
                        my $bars = 10 * ($percent / 100);
                        print "<";
                        print "|" x $bars;
                        print " " x (10 - $bars);
                        print "> ";
                        print "$percent%    ";
                        print "$package\n\n";

                    }else{
                        print "$n: $stat\n";
                    }
                }else{
                    print "$n: Reachable\n";
                }
            }else{
                print "$n: Unreachable\n";
            }
            exit;
        }
        else
        {
            # can not fork. can not use msg catalog
            # no sub process is available
            printf "noderpminstallstatus: Unable to fork child process.\n";
        }
        
        if ($pid_count == $threshold)
        {
            foreach $pid (@children)
            {
                waitpid($pid, 0);
            }
            undef @children;
            $pid_count = 0;
        }
    }
    # wait for last child process to finish
    foreach $pid (@children)
    {
        waitpid($pid, 0);
    }
}

#--------------------------------------------------------------------------------

=head3  Main

        Notes:

=cut

#--------------------------------------------------------------------------------

# get the node list, stored in $::NODE_LIST
my $nodes = getArgs();

if ($nodes)
{
    nodestat($nodes);
}
