#!/usr/bin/perl 

#ModulePurpose: Find when access7 users last logined in, and find old account 
#ModuleName:  user_login_stats 
#TimeRequied: 1 min

# Details Report
#   --- access7 user login stats collected 08.15.2009 
#       Total number of a7user home directories: 544 
#       Summary of last logins
#         Users last logged in past 30 days  (1 month): 125
#               last logged in past 30 - 60 days  (1-2 months): 42
#               last logged in past 60 - 180 days (2-6 months): 60
#               not logged in over 180 days (greater than 6 months): 286
#               who have never logged in: 30

#   access7 users in ETR:497
#   access7 users in passwd file:538
#   access7 users home directories:544

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

use English;
use Carp;
use Cwd;
use Date::Pcalc qw(Delta_Days); 
use File::Basename;
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 {
# List when users last logged in.  Highlight old accounts

     print "user last accessed check \n";

     my $etrfile = "/h/bmetzger/s/tmp/all/wm.etr";  

     my ($onemonth, $twomonth, $sixmonth, $old, $never) = 0 ; #login counters 
     my @dirlist = ();
     my $local_problems = "$TmpDir/users_problems";
     my $local_details  = "$TmpDir/users_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";

   my @today = (localtime)[5,4,3];
   $today[0] += 1900;
   $today[1]++;  #jan is 0


     @dirlist = qx[ find /home/a7users/*  -type d -prune]; 
     my $numbdirs = @dirlist;

     foreach my $dir (@dirlist){
         chomp $dir;
         my $file = "$dir/.dt/startlog";

         if ( -e $file ) {
              #turn mtime into something readable
              # startlog   mtime:1242648907  05/18/09
              $mtime = (stat$file)[9];
              my @t =  (localtime $mtime)[5,4,3];
              $t[0] += 1900;
              $t[1]++; 
#             $date = sprintf "%02u.%02u.%02u ", $t[4] + 1, $t[3], $t[5] % 100;
              my $days= Delta_Days(@t, @today);            
              my ($junk1,$junk2,$junk3,$user) = split(/\//,$dir);

              if ( $days < 31) { 
                    $onemonth++;      
              }
              if ( ($days > 30) && ($days < 61)) { 
                    $twomonth++;      
              }
              if ( ($days > 60) && ($days < 180)) { 
                    $sixmonth++;      
              }
              if ( $days > 180) { 
                    $old++;      
              }

         } else { #no startlog.  Never logged in
              my ($junk1,$junk2,$junk3,$user) = split(/\//,$dir);
             $never++;
#              print "$user never logged in\n";
         }
     }
     printf DETAILS " ";
     printf DETAILS "   Summary of last logins\n"; 
     printf DETAILS "      Users last logged in past 30 days  (1 month): $onemonth\n";
     printf DETAILS "            last logged in past 30 - 60 days  (1-2 months): $twomonth\n";
     printf DETAILS "            last logged in past 60 - 180 days (2-6 months): $sixmonth\n";
     printf DETAILS "            not logged in over 180 days (greater than 6 months): $old\n";
     printf DETAILS "            who have never logged in: $never\n";

    # count various access7 users
    chomp($etr_count = qx[grep "C C C C" $etrfile | wc -l]);

    chomp($passwd_count = qx[grep ":500:" /etc/passwd | wc -l]);
    chomp($homedir_count = qx[cd /home/a7users; ls -l | grep ^d | wc -l]);
    chomp($incomplete_passwd_entries = qx[grep ":500:" /etc/passwd | grep ",," |wc -l]);

    print DETAILS "\n";
    print DETAILS "    access7 users in ETR:$etr_count\n";
    print DETAILS "    access7 users in passwd file:$passwd_count\n";
    print DETAILS "    access7 users home directories:$homedir_count\n";
    print DETAILS "    incomplete access7 user passwd entries:$incomplete_passwd_entries\n";

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

    my $badcount = line_count($local_problems);
    my $detailcount = line_count($local_details);

    # 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--- access7 user login stats collected -$today \n";

    #  Format local_details, if needed
    if ( $detailcount > 0 ){
         printf JUNK2 $header;
         qx[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

