#!/usr/bin/perl 

#ModulePurpose: Does /tmp/wm exist on RSP, WST, CSs
# If not, create directory.

#ModuleName: checkfor_standard_tmp_dirs 
#TimeRequired: 

#Problem Report
# --- 4 problems with /tmp dir or standard files
# m001                /tmp/wm   drwxr-xr-x   ne   drwxrwxrwx -- wrong permissions
# m007                /tmp/wm   drwxr-xr-x   ne   drwxrwxrwx -- wrong permissions
# m008                /tmp/wm   drwxr-xr-x   ne   drwxrwxrwx -- wrong permissions
# m7server            /tmp/wm   drwxrwxr-x   ne   drwxrwxrwx -- wrong permissions

use lib '/h/bmetzger/s/p/hc/';
use HC qw(:ALL);  #functions available to all modules 

use English;
use Carp;
use Cwd;
use DBI;
use Net::Ping;
use File::Basename;
use File::Path;
use File::Glob;
use File::Copy;

###############3###
# define location of, and open files
##################

  my ($TmpDir,$sitelist,$global_problems,$global_details,$global_summary) = InitVars();

  $SSH_OPTIONS=" -o BatchMode=yes -o ConnectTimeout=2";
  $ENV{'PATH'} = 'misc_tools:/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:/opt/platform7/lbin/:.';
  &initvars();

  #create list of boxes to check
  qx[list_processors -sw > $sitelist];
  qx[cat /etc/opt/OSSInstall/servers.conf >> $sitelist];

### checks
 ($header, $local_problems, $local_details)= &check(); 
  summarize($header, $local_problems, $local_details);

###################33
sub initvars {

   ($sys_day,$sys_mon,$sys_year) = (localtime)[3,4,5];
   $sys_mon += 1;  #jan is 0
   $sys_year += 1900;
   $today = sprintf "%02d.%02d.%d",$sys_mon,$sys_day,$sys_year;  # zero fill date

   $SSH_OPTIONS=" -o BatchMode=yes -o ConnectTimeout=2";
   $ENV{'PATH'} = '/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:.';
} #initvars

####################
sub line_count {
# perform a  wc -l

  my $file = shift;
    open my $fh, "<", $file or die "could not open $file:$!";
    my $lines = 0;
    $lines++ while <$fh>;
    return $lines;
    close ($fh);
} #line_count

#########
sub check {
# check that /tmp/wm exists. If not, create it

     print "/tmp/wm dir check \n";

     my ($p, $box);
     my @serverlist = ();
     my $local_problems = "$TmpDir/wmdir_problems";
     my $local_details  = "$TmpDir/wmdir_details";
     my $local_junk1    = "$TmpDir/junk1";    # temp file for problems
     my $local_junk2    = "$TmpDir/junk2";    # temp file for details

    # open local output files
    open PROBLEMS, ">>$local_problems" or croak "Can't open file $local_problems for write: $!\n";
    open DETAILS, ">>$local_details" or croak "Can't open file $local_details for write: $!\n";

    open IN, "<${sitelist}" or die $! ;

   # check each site 
    foreach my $box (<IN>) {
        chomp($box);

        $p = Net::Ping->new("icmp");
        if ( $p->ping($box) ) {
            printf "   %-12s \n",$box ;

            #see if /tmp/wm dir exists
            my $dir_count = qx[remsh ${box} "ls -Ald /tmp/wm | grep -v No | wc -l" 2> /dev/null];
            $dir_count =~ s/\s+//g;

            if ($dir_count > 0) { # /tmp/wm exists 
                  #is it a directory and 7777 ?
                  # check permissions for sprint standard 777
                      my $junk1=qx[remsh ${box} "cd  /tmp;ls -Ald /tmp/wm " ];
                      my @arr = split(/\s+/,$junk1);
                      my $permissions = $arr[0];

                      if ($permissions ne "drwxrwxrwx" ){
                          printf PROBLEMS  "%-18s  /tmp/wm   $permissions   ne   drwxrwxrwx -- wrong permissions\n", $box, $permissions;
                      }
            } else {   # need to create directory
                  printf PROBLEMS  "%-18s  /tmp/wm dir missing\n", $box;
                  qx[remsh ${box} "cd  /tmp; mkdir /tmp/wm; chmod 777 /tmp/wm " ];
            }

         } else {
              printf PROBLEMS "$box  NOT pingable\n" ;
         }
       $p->close();
  } #for each box in sitelist

    #gotta close, so lines can be counted
    close(DETAILS);  
    close(PROBLEMS); 

    my $badcount = line_count($local_problems);
    my $detailcount = line_count($local_details);
    my $box_count = scalar(@sitelist);

    # open local output files
    open JUNK1, ">>$local_junk1" or croak "Can't open file $local_junk1 for write: $!\n";
    open JUNK2, ">>$local_junk2" or croak "Can't open file $local_junk2 for write: $!\n";

    my $header = "\n--- $badcount problems with /tmp dir -$today\n";

    if ( $badcount > 0 ){  # at least one problem

         #format and header 
         print JUNK1 $header;
         my @problem_list = qw(missing permissions crontab ping);
         foreach my $f (@problem_list) {
             qx[grep $f $local_problems >> $local_junk1];
         }
         close(JUNK1);
    }

    #  Format local_details, if needed
    if ( $detailcount > 0 ){
         print JUNK2 $header;
         system("cat $local_details >> $local_junk2");
    }
    close(JUNK2);  

   copy($local_junk1,$local_problems) or die "junk1 cannot be copied";
   copy($local_junk2,$local_details) or die "junk2 cannot be copied";

   unlink("$local_junk1") || print $!;
   unlink("$local_junk2") || print $!;

  return($header, $local_problems, $local_details);
} #check

