--- loncom/loncron 2018/12/22 20:05:16 1.116 +++ loncom/loncron 2019/03/17 23:23:21 1.117 @@ -2,7 +2,7 @@ # Housekeeping program, started by cron, loncontrol and loncron.pl # -# $Id: loncron,v 1.116 2018/12/22 20:05:16 raeburn Exp $ +# $Id: loncron,v 1.117 2019/03/17 23:23:21 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -45,6 +45,7 @@ use Getopt::Long; use GDBM_File; use Storable qw(thaw); use File::ReadBackwards; +use File::Copy; #globals use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount); @@ -1027,6 +1028,94 @@ sub write_checksums { return; } +sub write_hostips { + my $lontabdir = $perlvar{'lonTabDir'}; + my $defdom = $perlvar{'lonDefDomain'}; + my $lonhost = $perlvar{'lonHostID'}; + my $newfile = "$lontabdir/currhostips.tab"; + my $oldfile = "$lontabdir/prevhostips.tab"; + my (%prevhosts,%currhosts,%ipchange); + if ((-e $newfile) && (-s $newfile)) { + move($newfile,$oldfile); + chmod(0644,$oldfile); + if (open(my $fh,'<',$oldfile)) { + while (my $line=<$fh>) { + chomp($line); + if ($line =~ /^([^:]+):([\d.]+)$/) { + $prevhosts{$1} = $2; + } + } + close($fh); + } + } + my ($ip_info,$cached) = + &Apache::lonnet::is_cached_new('iphost','iphost'); + if (!$cached) { + &Apache::lonnet::get_iphost(); + ($ip_info,$cached) = + &Apache::lonnet::is_cached_new('iphost','iphost'); + } + if (ref($ip_info) eq 'ARRAY') { + %currhosts = %{$ip_info->[1]}; + if (open(my $fh,'>',$newfile)) { + foreach my $key (keys(%currhosts)) { + print $fh "$key:$currhosts{$key}\n"; + } + close($fh); + chmod(0644,$newfile); + } + } + if (keys(%prevhosts) && keys(%currhosts)) { + foreach my $key (keys(%prevhosts)) { + unless ($currhosts{$key} eq $prevhosts{$key}) { + $ipchange{$key} = $prevhosts{$key}.'|'.$currhosts{$key}; + } + } + foreach my $key (keys(%currhosts)) { + unless ($currhosts{$key} eq $prevhosts{$key}) { + $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key}; + } + } + } + if (&Apache::lonnet::domain($defdom,'primary') eq $lonhost) { + if (keys(%ipchange)) { + if (open(my $fh,'>>',$perlvar{'lonDaemons'}.'/logs/hostip.log')) { + print $fh "********************\n".localtime(time).' Changes --'."\n". + "Hostname | Previous IP | New IP\n". + "--------------------------------\n"; + foreach my $hostname (sort(keys(%ipchange))) { + print $fh "$hostname | $ipchange{$hostname}\n"; + } + print $fh "\n*******************\n\n"; + close($fh); + } + my $emailto = &Apache::loncommon::build_recipient_list(undef, + 'hostipmail',$defdom); + if ($emailto) { + my $subject = "LON-CAPA Hostname to IP change ($perlvar{'lonHostID'})"; + my $chgmail = "To: $emailto\n". + "Subject: $subject\n". + "Content-type: text/plain\; charset=UTF-8\n". + "MIME-Version: 1.0\n\n". + "Host/IP changes\n". + " \n". + "Hostname | Previous IP | New IP\n". + "--------------------------------\n"; + foreach my $hostname (sort(keys(%ipchange))) { + $chgmail .= "$hostname | $ipchange{$hostname}\n"; + } + $chgmail .= "\n\n"; + if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) { + print $mailh $chgmail; + close($mailh); + print "Sending mail notification of hostname/IP changes.\n"; + } + } + } + } + return; +} + sub clean_nosslverify { my ($fh) = @_; my %unlinked; @@ -1533,6 +1622,7 @@ sub main () { &write_loncaparevs(); &write_serverhomeIDs(); &write_checksums(); + &write_hostips(); my %sslrem = &clean_nosslverify($fh); my %conchgs = &write_connection_config(); my %hosttypechgs = &write_hosttypes();