#!/opt/p7perl/bin/perl 

#  /opt/perl5/bin/perl  # for c.07
#  /opt/p7perl/bin/perl # for c.08

# ver. 28
#            uses ifpc/.rhosts
#            checks for IFPC ROM version
# 28 march - checks for tape drive
 
use Cwd;

$REPORT_FILE="vw.report.out";
$ETR_FILE="wm.etr";
$GF35_COPY="wm.gf35";

$REMOTE_SCRIPT=vw28a;   # generic script copied to remote processors
$IFPC_SCRIPT=vw25b;     # to gather info from IFPCs

#chop($WORKDIR=`pwd`);
$WORKDIR=cwd();

# define, create or clean local TMP_DIR
$TMP_DIR="$WORKDIR/tmp";
mkdir $TMP_DIR, 0777;

# dump etr file. Only needed for BPP's (maybe)
#if (-e '/var/opt/platform7/tmp/cf13_serverState') { #SVR processor
#    `/opt/platform7/bin/p7config -db $ETR_FILE`;
# } 

#test_UID();
test_for_required_files();
cleanup();
processor_capture();
#ifpc_capture();    # not used
determine_max_lengths();
create_header();
create_processor_report();
create_bpp_report();
create_ifpc_report();

sub cleanup {
      while (<$TMP_DIR/*.proc>) {
         unlink $_ ;
      }
    
      while (<$TMP_DIR/*.ifpc>) {
         unlink $_ ;
      }
      while (<$TMP_DIR/*.bpp>) {
         unlink $_ ;
      }
      unlink $REPORT_FILE;
}  # end cleanup

sub test_UID {
   # Verify user is root.  If not, bail.
    @password = getpwuid($<);
    $name = $password[0];
   
    if ($password[2] != 0) { # user is not root
        print "Sorry $name. Must be run as root.  Thanks for playing\n";
        exit;
    } 
} # end of test_UID

sub test_for_required_files {
   # Tests for required files

    @file_list=($GF35_COPY, node_info, $REMOTE_SCRIPT);

    foreach $x (@file_list) {

        if (! -e $x) { # does not exist
            print "       Script requires a copy of $x file\n";
            exit;
        }
    } # end of foreach
} # end of test_for_required_files


sub determine_max_lengths {
    # Data has been captured.
    # Now needs to be put into single spreadsheet file(fields seperated by tabs).

   open (REPORT,">>$REPORT_FILE") ;  #file to be used for final report

   # First, need to compute maximum number of fields for entries which could
   # have variable lengths.

   $diskcount=0;    # highest number of disks
   $lancount=0;     # highest number of lan cards
   $slots_used=0;  # highest number of memory slots used

   while (defined ( $file = <$TMP_DIR/*.proc> ) ) {
    open(INFILE, "< $file") || die "Cannot open $file:  $!";
          while (defined ($line = <INFILE>)) {
                if ( $line =~ /disk_count/ ) {
                     ($parm, $validinfo) = split (/:\s+/, $line);  #split on ":"
                     if ($diskcount >= $validinfo) {
                          next;
                     } else { 
                          $diskcount = $validinfo;
                     };
                }

                if ( $line =~ /lancard_count/ ) {
                     ($parm, $validinfo) = split (/:\s+/, $line);  #split on ":"
                     if ($lancount >= $validinfo) {
                          next;
                     } else { 
                          $lancount = $validinfo;
                     };
               }

                if ( $line =~ /slots_used/ ) {
                     ($parm, $validinfo) = split (/:\s+/, $line);  #split on ":"
                     if ($max_slots_used_count >= $validinfo) {  
                          next;
                     } else { 
                          $max_slots_used_count = $validinfo;
                     };
               }
         } # end of searching each line in all files for values
     close (INFILE);
  }  # end of globbing while
} # end of determine_max_lengths 


sub create_header {
   #create header line(s)
   # This need to be done after determining number of fields required for greatest number
   # of LAN and DISKs 
   # Address fields will have spaces.   Therefore fields will be seperated by  \t(tab).

   $lanheader1="LAN\tLAN\tLAN\tSubnet\t";
   $lanheader2="Card\tIP\tMAC\tMask\t";

   $diskheader1="SCSI\tDisk\tDisk\tDisk\t";
   $diskheader2="Address\tSize (GB)\tType\tVendor\t";

   # This is the top row
   chomp($date_string=`date`); 
   print REPORT "ver. $0 \tData of Report:\t$date_string\t\t\n";
   print REPORT "\n\tAccess7\tProcessor\t\tGateway\tSystem\tTotal\tNumber\tTape\tNumber\tNumber\tOS\tOS\tProcessor\tSoftware ID/\tCurrent\tSupported\t\t\t\t\t\t\t\tSite\tSite\tSerial\t";

   for ($i = 0; $i < $lancount ; $i++) {  ##number of fields depends on # of cards
      print REPORT $lanheader1;
   }

   for ($i = 0; $i < $diskcount ; $i++) {  ##number of fields depends on # of disks
      print REPORT $diskheader1;
   }

   print REPORT "Diags\t";
   print REPORT "PDC\t";
   print REPORT "RAM Slots\t";
   print REPORT "RAM Slots\t";
   print REPORT "RAM Slots\t";

   for ($i = 0; $i < $max_slots_used_count*2 ; $i++) {  # *2 to account for slot and size
      print REPORT "RAM\t";
   }

   # This is the second row
   print REPORT "\nHostname\tName\tType\tModel\tIP\tMemory (MB)\tDiskspace(GB)\tDisks\tDrive\tLancards\tProcessors\tType\tVersion\tSpeed\tROM Ver\tBits\tBits\tTimezone\tBay\tFloor\tStreet\tCity\tState\tZip\tContact\tPhone#\tNumber\t";

   for ($i = 0; $i < $lancount ; $i++) {  ##number of fields depends on # of cards
      print REPORT $lanheader2;
   }

   for ($i = 0; $i < $diskcount ; $i++) {  ##number of fields depends on # of disks
      print REPORT $diskheader2;
   }

   print REPORT "Installed\t";
   print REPORT "Rev\t";
   print REPORT "Total\t";
   print REPORT "Used\t";
   print REPORT "Avail\t";

   for ($i = 0; $i < $max_slots_used_count ; $i++) {  
      print  print REPORT "Slot\t" ; 
      print  print REPORT "Size(MB)\t" ;
   }
#   for ($i = 0; $i < $max_slots_used_count/2 ; $i++) {  # divide by 2 to account for A and B 0A/0B
#      print  REPORT "$i"; print REPORT "A\t" ; # gotta split $i[letter]
#      print  REPORT "$i"; print REPORT "B\t" ;
#   }

   print REPORT "\n\n";  # ready to inserting collected data

}  # end create_header

sub create_processor_report {
   # populate report file for info collected from HPUX boxes

   while (defined ( $file = <$TMP_DIR/*.pr*> ) ) {  # glob all .proc, and .probe data files
          open(INFILE, "< $file") || die "Cannot open $file:  $!";

               @lanarray=();  # zero out array for each file
               @diskarray=();  # zero out array for each file
               @ram_array=();  # zero out array for each file

          %PROCESSOR_Hash =(
                     slot => NA,
                     model => NA,
                     os_type => NA,
                     os_rev => NA,
                     memory => NA,
                     hostname => NA,
                     timezone => NA,
                     gateway_ip => NA,
                     disk_count => NA,
                     tape_drive => NA,
                     software_id => NA,
                     current_bits => NA,
                     lancard_count => NA,
                     supported_bits => NA,
                     processor_speed => NA,
                     a7processor_type => NA,
                     a7processor_name => NA,
                     number_processors => NA,
                     total_diskspace => NA,
                     Bay => NA,
                     Floor => NA,
                     Street => NA,
                     City => NA,
                     State => NA,
                     Zip => NA,
                     Contact => NA,
                     Number => NA,
                     SN => NA,
                     diags_installed => NA,
                     PDC_rev => NA,
                     total_slots => NA,
                     slots_used => NA,
                     slots_available => NA
           );

 
          while (defined ($line = <INFILE>)) {
               chomp $line;
               ($parm, $validinfo) = split (/:\s+/, $line);  # parameter collected is $parm.  Actual data is validinfo

                    if ( $line =~ /$parm/ ) {                  # load hash with  all available info for that processor
                            $PROCESSOR_Hash{$parm} = $validinfo;
                    } 

                    if ( $line =~ /LAN/ ) {
                            @tmparray = split (" ", $line);  # all lan info
                            shift @tmparray ;                # get rid of leading LAN label 
                            push @lanarray, @tmparray;
                    } 
                    
                    if ( $line =~ /DISK/ ) {                # SCSI devices 
                            @tmparray = split (" ", $line);      
                            shift @tmparray ;                # get rid of leading DISK label 
                            push @diskarray, @tmparray;
                    }

                    if ( $line =~ /RAM/ ) {                #  RAM info
                            @tmparray = split (" ", $line);      
                            shift @tmparray ;   # get rid of leading RAM label
                            push @ram_array, @tmparray;
                    }

          } # end of assigning variables while
          close (INFILE);  # file has been read and processed.


          # To have specific printing format, print each previously loaded variable

                print REPORT "$PROCESSOR_Hash{hostname}\t";       #leading \t
                print REPORT "$PROCESSOR_Hash{a7processor_name}\t";
                print REPORT "$PROCESSOR_Hash{a7processor_type}\t";
                print REPORT "$PROCESSOR_Hash{model}\t";
                print REPORT "$PROCESSOR_Hash{gateway_ip}\t";
                print REPORT "$PROCESSOR_Hash{memory}\t";
                print REPORT "$PROCESSOR_Hash{total_diskspace}\t";
                print REPORT "$PROCESSOR_Hash{disk_count}\t";
                print REPORT "$PROCESSOR_Hash{tape_drive}\t";
                print REPORT "$PROCESSOR_Hash{lancard_count}\t";
                print REPORT "$PROCESSOR_Hash{number_processors}\t";
                print REPORT "$PROCESSOR_Hash{os_type}\t";
                print REPORT "$PROCESSOR_Hash{os_rev}\t";
                print REPORT "$PROCESSOR_Hash{processor_speed}\t";
                print REPORT "$PROCESSOR_Hash{software_id}\t";
                print REPORT "$PROCESSOR_Hash{current_bits}\t";
                print REPORT "$PROCESSOR_Hash{supported_bits}\t";
                print REPORT "$PROCESSOR_Hash{timezone}\t";
                print REPORT "$PROCESSOR_Hash{Bay}\t";
                print REPORT "$PROCESSOR_Hash{Floor}\t";
                print REPORT "$PROCESSOR_Hash{Street}\t";
                print REPORT "$PROCESSOR_Hash{City}\t";
                print REPORT "$PROCESSOR_Hash{State}\t";
                print REPORT "$PROCESSOR_Hash{Zip}\t";
                print REPORT "$PROCESSOR_Hash{Contact}\t";
                print REPORT "$PROCESSOR_Hash{Number}\t";
                print REPORT "$PROCESSOR_Hash{SN}\t";


          # lanarray contains all lan values for specific processor.
          # Now for a consistent look, need to fill unused elements with tabs
          #
           for ($j=0; $j < ($lancount * 4); $j++) {  # 4 variables per lan card
                if ( !defined $lanarray[$j] )  {
                     $lanarray[$j] = " "; #blank if card doesn't exist(cleaner that using NA)
                }
                print REPORT "$lanarray[$j]\t";     
           }

          # diskarray contains all lan values for specific processor.

           for ($s=0; $s < ($diskcount * 4); $s++) {  # 4 variables per scsi device
                if ( !defined $diskarray[$s] )  {
                     $diskarray[$s] = " ";  #blank if disk doesn't exist
                }
                print REPORT "$diskarray[$s]\t";     
           }

          print REPORT "$PROCESSOR_Hash{diags_installed}\t";
          print REPORT "$PROCESSOR_Hash{PDC_rev}\t";
          print REPORT "$PROCESSOR_Hash{total_slots}\t";
          print REPORT "$PROCESSOR_Hash{slots_used}\t";
          print REPORT "$PROCESSOR_Hash{slots_available}\t";

          # memarray contains all memory slot info for specific processor.

           for ($w=0; $w < ($max_slots_used_count*2); $w++) { # x2 to print slot and size
                if ( !defined $max_slots_used_count[$w] )  {
                     $max_slots_used_count[$w] = " ";  #blank if nothing exists 
                }
                print REPORT "$ram_array[$w]\t";     
           }
           print REPORT "\n";     
   } # end of while glob
} #end create_proc_report

sub create_bpp_report {
   # populate report file for info collected from bpp's 

   while (defined ( $file = <$TMP_DIR/*.bpp> ) ) {  # glob all .bpp data files
          open(INFILE, "< $file") || die "Cannot open $file:  $!";

          %PROCESSOR_Hash =(
                     hostname => NA,
                     model => NA,
                     os_type => NA,
                     os_rev => NA,
                     memory => NA,
                     timezone => NA,
                     gateway_ip => NA,
                     lancard_count => NA,
                     processor_speed => NA,
                     a7processor_type => NA,
                     a7processor_name => NA,
                     Bay => NA,
                     Floor => NA,
                     Street => NA,
                     City => NA,
                     State => NA,
                     Zip => NA,
                     Contact => NA,
                     Number => NA,
                     SN => NA
           );

 
          while (defined ($line = <INFILE>)) {
               chomp $line;
               ($parm, $validinfo) = split (/:\s+/, $line);  # parameter collected is $parm.  Actual data is validinfo

                    if ( $line =~ /$parm/ ) {                  # load hash with  all available info for that processor
                            $PROCESSOR_Hash{$parm} = $validinfo;
                    } 

                    if ( $line =~ /LAN/ ) {
                            @tmparray = split (" ", $line);  # all lan info
                            shift @tmparray ;                # get rid of leading LAN label 
                            push @lanarray, @tmparray;
                    } 
                    
                    if ( $line =~ /DISK/ ) {                # SCSI devices 
                            @tmparray = split (" ", $line);      
                            shift @tmparray ;                # get rid of leading DISK label 
                            push @diskarray, @tmparray;
                    }
          } # end of assigning variables while
          close (INFILE);  # file has been read and processed.


          # To have specific printing format, print each previously loaded variable

                print REPORT "$PROCESSOR_Hash{hostname}\t";       #leading \t
                print REPORT "$PROCESSOR_Hash{a7processor_name}\t";
                print REPORT "$PROCESSOR_Hash{a7processor_type}\t";
                print REPORT "$PROCESSOR_Hash{model}\t";
                print REPORT "$PROCESSOR_Hash{gateway_ip}\t";
                print REPORT "$PROCESSOR_Hash{memory}\t\t\t\t\t";
                print REPORT "$PROCESSOR_Hash{os_type}\t";
                print REPORT "$PROCESSOR_Hash{os_rev}\t";
                print REPORT "$PROCESSOR_Hash{processor_speed}\t\t\t\t";
                print REPORT "$PROCESSOR_Hash{timezone}\t";
                print REPORT "$PROCESSOR_Hash{Bay}\t";
                print REPORT "$PROCESSOR_Hash{Floor}\t";
                print REPORT "$PROCESSOR_Hash{Street}\t";
                print REPORT "$PROCESSOR_Hash{City}\t";
                print REPORT "$PROCESSOR_Hash{State}\t";
                print REPORT "$PROCESSOR_Hash{Zip}\t";
                print REPORT "$PROCESSOR_Hash{Contact}\t";
                print REPORT "$PROCESSOR_Hash{Number}\t";
                print REPORT "$PROCESSOR_Hash{SN}\t";


          # lanarray contains all lan values for specific processor.
          # Now for a consistent look, need to fill unused elements with tabs
          #
           for ($j=0; $j < ($lancount * 4); $j++) {  # 4 variables per lan card
                if ( !defined $lanarray[$j] )  {
                     $lanarray[$j] = " "; #blank if card doesn't exist(cleaner that using NA)
                }
                print REPORT "$lanarray[$j]\t";     
           }

           print REPORT "\n";     
   } # end of while glob
} #end create_bpp_report

sub create_ifpc_report {

   # Purpose:  populate report file for info collected from IFPC's

local @lan_array = ();
local %IFPC_Hash = ();
local $cc_number = "1";

   while (defined ( $file = <$TMP_DIR/*.ifpc> ) ) {  # glob all .ifpc data files
          open(INFILE, "< $file") || die "Cannot open $file:  $!";

          %IFPC_Hash =(
                     hostname => NA,
                     model => NA,
                     os_type => NA,
                     os_rev => NA,
                     rom_rev => NA,
                     memory => NA,
                     gateway_ip => NA,
                     a7processor_type => NA,
           );

         #Assign variables to Hash

          while (defined ($line = <INFILE>)) {
               chomp $line;

               ($parm, $validinfo) = split (/:\s+/, $line);  # parameter collected is $parm.  Actual data is validinfo

                    if (( $line =~ /$parm/ ) && ( $line !~ /LAN/ )) {   # load hash 
                        $IFPC_Hash{$parm} = $validinfo;
                    }   

                    if ( $line =~ /LAN/ ) {
                         @lan_array = split (" ", $line);  # all lan info
                     }

          } # end of assigning variables while
          close (INFILE);  # file has been read and processed.

                print REPORT "$IFPC_Hash{hostname}\t\t";     
                print REPORT "$IFPC_Hash{a7processor_type}\t";
                print REPORT "$IFPC_Hash{model}\t";
                print REPORT "$IFPC_Hash{gateway_ip}\t";
                print REPORT "$IFPC_Hash{memory}\t\t\t\t\t\t";
                print REPORT "$IFPC_Hash{os_type}\t";
                print REPORT "$IFPC_Hash{os_rev}\t\t";
                print REPORT "$IFPC_Hash{rom_rev}\t\t\t\t\t\t\t\t\t\t\t\t\t";

                # @lan_array built in the serial_number lookup section
                shift @lan_array;     # get rid of leading LAN
                foreach $y (@lan_array) {
                      print REPORT "$y\t";
                }

                print REPORT "\n";               # newline after each ifpc

   } # end of while glob
} #end create_ifpc_report


sub processor_capture {
# Define processors to capture date on.
# Actually login to remote machines,capture data, and copy it back to working directory.


     chop($junk=`cat /etc/opt/p7install/servers.conf`);
     @server_list = split (/\s/, $junk);

     chop($junk=`/opt/platform7/lbin/list_processors -w`);
     @workstation_list = split (/\s/, $junk);     # $junk is multiline need to split on \r

     chop($junk=`/opt/platform7/lbin/list_processors -s `);
     @site_list = split (/\s/, $junk);   

     chop($junk=`/opt/platform7/lbin/list_processors -p`);
     @probe_list = split (/\s/, $junk);    

     chop($junk=`/opt/platform7/lbin/list_processors -d `);
     @dstore_list = split (/\s/, $junk); 

     @misc_list = (a7ignite);
#     @processor_list = (a7ignite);
#    @processor_list = (@workstation_list, @site_list, @probe_list, @server_list, @misc_list);
    @processor_list = (@workstation_list, @site_list, @server_list, @misc_list);

     foreach $node (@processor_list) {

       # test to see if node is alive
       $ping_junk1_value=`/etc/ping $node -n 1 | grep loss | sed s/%//g`;
       @ping_values = split (/\s+/, $ping_junk1_value); 
       $ping_loss_percent = int $ping_values[6]; 

       if ( $ping_loss_percent < 100 ) {   # if less than 100% loss, node is alive

            print "$node\n";
           `remsh $node -n "if [ ! -d /tmp/wm ]; then mkdir /tmp/wm ; fi"`;
           `remsh $node -n "if [ ! -d /tmp/wm/tmp ]; then mkdir /tmp/wm/tmp; fi"`;

           `rcp $GF35_COPY $node:/tmp/wm`;
           `rcp node_info $node:/tmp/wm`;
           `rcp $REMOTE_SCRIPT $node:/tmp/wm`;
           `remsh $node  -n "cd /tmp/wm/; ./$REMOTE_SCRIPT > /tmp/wm/vw.errors 2>&1"`;
           `rcp $node:/tmp/wm/*.proc $TMP_DIR`;
           `rcp $node:/tmp/wm/*.ifpc $TMP_DIR`;
           `rcp $node:/tmp/wm/*.bpp $TMP_DIR`;
           `remsh $node  -n "cd /tmp/wm/; rm $REMOTE_SCRIPT ;rm *.proc;rm *.ifpc;rm *.bpp;rm wm.*;rm node* >> /tmp/wm/vw.errors 2>&1"`;

       } else {
          open (TMPFILE,">$TMP_DIR/$node.proc");
          print TMPFILE "hostname: $node\n"; 
          print TMPFILE "a7processor_name: unreachable\n"; 
          close TMPFILE;

       } #end of node alive test

    } # end of foreach
}  #end processor_capture

sub ifpc_capture {
# 
     chop($junk=`/opt/platform7/lbin/list_processors -i `);
     @ifpc_list = split (/\s/, $junk); 
#     @ifpc_list = (s001c01p1); 

     foreach $node (@ifpc_list) {

       # test to see if node is alive
       $ping_junk1_value=`/etc/ping $node -n 1 | grep loss | sed s/%//g`;
       @ping_values = split (/\s+/, $ping_junk1_value); 
       $ping_loss_percent = int $ping_values[6]; 

       if ( $ping_loss_percent < 100 ) {   # if less than 100% loss, node is alive

            print "$node\n";
            `$IFPC_SCRIPT $node`;

       } else {
          open (TMPFILE,">$TMP_DIR/$node.proc");
          print TMPFILE "$node: unreachable\n"; 
          close TMPFILE;

       } #end of node alive test

    } # end of foreach


} # end ifpc_capture {
