File:  [LON-CAPA] / loncom / loncron
Revision 1.128: download - view: text, annotated - select for diffs
Tue Feb 2 21:14:36 2021 UTC (3 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Reverse changes in 1.127, as they duplicated changes in rev. 1.104.

    1: #!/usr/bin/perl
    2: 
    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
    4: #
    5: # $Id: loncron,v 1.128 2021/02/02 21:14:36 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: $|=1;
   31: use strict;
   32: 
   33: use lib '/home/httpd/lib/perl/';
   34: use LONCAPA::Configuration;
   35: use LONCAPA::Checksumming;
   36: use LONCAPA;
   37: use LONCAPA::LWPReq;
   38: use Apache::lonnet;
   39: use Apache::loncommon;
   40: 
   41: use IO::File;
   42: use IO::Socket;
   43: use HTML::Entities;
   44: use Getopt::Long;
   45: use GDBM_File qw(GDBM_READER);
   46: use Storable qw(thaw);
   47: use File::ReadBackwards;
   48: use File::Copy;
   49: use Sys::Hostname::FQDN();
   50: 
   51: #globals
   52: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
   53: 
   54: my $statusdir="/home/httpd/html/lon-status";
   55: 
   56: 
   57: # --------------------------------------------------------- Output error status
   58: 
   59: sub log {
   60:     my $fh=shift;
   61:     if ($fh) {	print $fh @_  }
   62: }
   63: 
   64: sub errout {
   65:    my $fh=shift;
   66:    &log($fh,(<<ENDERROUT));
   67:      <table border="2" bgcolor="#CCCCCC">
   68:      <tr><td>Notices</td><td>$notices</td></tr>
   69:      <tr><td>Warnings</td><td>$warnings</td></tr>
   70:      <tr><td>Errors</td><td>$errors</td></tr>
   71:      </table><p><a href="#top">Top</a></p>
   72: ENDERROUT
   73: }
   74: 
   75: sub rotate_logfile {
   76:     my ($file,$fh,$description) = @_;
   77:     my $size=(stat($file))[7];
   78:     if ($size>40000) {
   79: 	&log($fh,"<p>Rotating $description ...</p>");
   80: 	rename("$file.2","$file.3");
   81: 	rename("$file.1","$file.2");
   82: 	rename("$file","$file.1");
   83:     }
   84: }
   85: 
   86: sub start_daemon {
   87:     my ($fh,$daemon,$pidfile,$args) = @_;
   88:     my $progname=$daemon;
   89:     if ($daemon eq 'lonc') {
   90: 	$progname='loncnew';
   91:     }
   92:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
   93:     &rotate_logfile($error_fname,$fh,'error logs');
   94:     if ($daemon eq 'lonc') {
   95: 	&clean_sockets($fh);
   96:     }
   97:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
   98:     sleep 1;
   99:     if (-e $pidfile) {
  100: 	&log($fh,"<p>Seems like it started ...</p>");
  101: 	my $lfh=IO::File->new("$pidfile");
  102: 	my $daemonpid=<$lfh>;
  103: 	chomp($daemonpid);
  104: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
  105: 	    return 1;
  106: 	} else {
  107: 	    return 0;
  108: 	}
  109:     }
  110:     &log($fh,"<p>Seems like that did not work!</p>");
  111:     $errors++;
  112:     return 0;
  113: }
  114: 
  115: sub checkon_daemon {
  116:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
  117: 
  118:     my $result;
  119:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
  120:     printf("%-15s ",$daemon);
  121:     if ($fh) {
  122:         if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
  123: 	    if (open(DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
  124: 	        while (my $line=<DFH>) {
  125: 	            &log($fh,"$line");
  126: 	            if ($line=~/INFO/) { $notices++; }
  127: 	            if ($line=~/WARNING/) { $notices++; }
  128: 	            if ($line=~/CRITICAL/) { $warnings++; }
  129: 	        }
  130: 	        close (DFH);
  131:             }
  132:         }
  133:         &log($fh,"</tt></p>");
  134:     }
  135: 
  136:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
  137:  
  138:     my $restartflag=1;
  139:     my $daemonpid;
  140:     if (-e $pidfile) {
  141: 	my $lfh=IO::File->new("$pidfile");
  142: 	$daemonpid=<$lfh>;
  143: 	chomp($daemonpid);
  144: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
  145: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
  146: 	    if ($send) { &log($fh,", sending $send"); }
  147: 	    &log($fh,"</h3>");
  148: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
  149: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
  150: 	    $restartflag=0;
  151: 	    if ($send eq 'USR2') {
  152: 		$result = 'reloaded';
  153: 		print "reloaded\n";
  154: 	    } else {
  155: 		$result = 'running';
  156: 		print "running\n";
  157: 	    }
  158: 	} else {
  159: 	    $errors++;
  160: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
  161: 	    $restartflag=1;
  162: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
  163: 	}
  164:     }
  165:     if ($restartflag==1) {
  166: 	$simplestatus{$daemon}='off';
  167: 	$errors++;
  168: 	my $kadaemon=$daemon;
  169: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
  170: 	&log($fh,'<br /><font color="red">Killall '.$daemon.': '.
  171: 	    `killall $kadaemon 2>&1`.' - ');
  172: 	sleep 1;
  173: 	&log($fh,unlink($pidfile).' - '.
  174: 	    `killall -9 $kadaemon 2>&1`.
  175: 	    '</font><br />');
  176:         if ($kadaemon eq 'loncnew') {
  177:             &clean_lonc_childpids();
  178:         }
  179: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
  180: 
  181: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
  182: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
  183: 	    $simplestatus{$daemon}='restarted';
  184: 	    $result = 'started';
  185: 	    print "started\n";
  186: 	} else {
  187: 	    $errors++;
  188: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
  189: 	    &log($fh,"<p>Give it one more try ...</p>");
  190: 	    print " ";
  191: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
  192: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
  193: 		$simplestatus{$daemon}='restarted';
  194: 		$result = 'started';
  195: 		print "started\n";
  196: 	    } else {
  197: 		$result = 'failed';
  198: 		print " failed\n";
  199: 		$simplestatus{$daemon}='failed';
  200: 		$errors++; $errors++;
  201: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
  202: 		&log($fh,"<p>Unable to start $daemon</p>");
  203: 	    }
  204: 	}
  205:         if ($fh) {
  206: 	    if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
  207: 	        &log($fh,"<p><pre>");
  208: 	        if (open(DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
  209: 	            while (my $line=<DFH>) { 
  210: 		        &log($fh,"$line");
  211: 		        if ($line=~/WARNING/) { $notices++; }
  212: 		        if ($line=~/CRITICAL/) { $notices++; }
  213: 	            }
  214: 	            close (DFH);
  215:                 }
  216: 	        &log($fh,"</pre></p>");
  217:             }
  218: 	}
  219:     }
  220: 
  221:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
  222:     &rotate_logfile($fname,$fh,'logs');
  223: 
  224:     &errout($fh);
  225:     return $result;
  226: }
  227: 
  228: # --------------------------------------------------------------------- Machine
  229: sub log_machine_info {
  230:     my ($fh)=@_;
  231:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
  232:     &log($fh,"<h3>loadavg</h3>");
  233: 
  234:     my $cpucount;
  235:     if (open(PIPE,"lscpu |grep '^CPU(s)' 2>&1 |")) {
  236:         my $info = <PIPE>;
  237:         chomp($info);
  238:         ($cpucount) = ($info =~ /^\QCPU(s):\E\s+(\d+)$/);
  239:         close(PIPE);
  240:     }
  241:     if (!$cpucount) {
  242:         $cpucount = 1;
  243:     }
  244:     my %loadtarget = (
  245:                         error => 4.0*$cpucount,
  246:                         warn  => 2.0*$cpucount,
  247:                         note  => 1.0*$cpucount,
  248:                      );
  249:     open (LOADAVGH,"/proc/loadavg");
  250:     my $loadavg=<LOADAVGH>;
  251:     close (LOADAVGH);
  252:  
  253:     &log($fh,"<tt>$loadavg</tt>");
  254: 
  255:     my @parts=split(/\s+/,$loadavg);
  256:     if ($parts[1]>$loadtarget{'error'}) {
  257: 	$errors++;
  258:     } elsif ($parts[1]>$loadtarget{'warn'}) {
  259: 	$warnings++;
  260:     } elsif ($parts[1]>$loadtarget{'note'}) {
  261: 	$notices++;
  262:     }
  263: 
  264:     &log($fh,"<h3>df</h3>");
  265:     &log($fh,"<pre>");
  266: 
  267:     open (DFH,"df|");
  268:     while (my $line=<DFH>) {
  269: 	&log($fh,&encode_entities($line,'<>&"'));
  270: 	@parts=split(/\s+/,$line);
  271: 	my $usage=$parts[4];
  272: 	$usage=~s/\W//g;
  273: 	if ($usage>90) {
  274: 	    $warnings++;
  275: 	    $notices++;
  276: 	} elsif ($usage>80) {
  277: 	    $warnings++;
  278: 	} elsif ($usage>60) {
  279: 	    $notices++;
  280: 	}
  281: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
  282:     }
  283:     close (DFH);
  284:     &log($fh,"</pre>");
  285: 
  286: 
  287:     &log($fh,"<h3>ps</h3>");
  288:     &log($fh,"<pre>");
  289:     my $psproc=0;
  290: 
  291:     open (PSH,"ps aux --cols 140 |");
  292:     while (my $line=<PSH>) {
  293: 	&log($fh,&encode_entities($line,'<>&"'));
  294: 	$psproc++;
  295:     }
  296:     close (PSH);
  297:     &log($fh,"</pre>");
  298: 
  299:     if ($psproc>200) { $notices++; }
  300:     if ($psproc>250) { $notices++; }
  301: 
  302:     &log($fh,"<h3>distprobe</h3>");
  303:     &log($fh,"<pre>");
  304:     &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
  305:     &log($fh,"</pre>");
  306: 
  307:     &errout($fh);
  308: }
  309: 
  310: sub start_logging {
  311:     my $fh=IO::File->new(">$statusdir/newstatus.html");
  312:     my %simplestatus=();
  313:     my $now=time;
  314:     my $date=localtime($now);
  315:  
  316: 
  317:     &log($fh,(<<ENDHEADERS));
  318: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  319: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  320: <head>
  321: <title>LON Status Report $perlvar{'lonHostID'}</title>
  322: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  323: </head>
  324: <body bgcolor="#AAAAAA">
  325: <a name="top" />
  326: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
  327: <h2>$date ($now)</h2>
  328: <ol>
  329: <li><a href="#configuration">Configuration</a></li>
  330: <li><a href="#machine">Machine Information</a></li>
  331: <li><a href="#tmp">Temporary Files</a></li>
  332: <li><a href="#tokens">Session Tokens</a></li>
  333: <li><a href="#webdav">WebDAV Session Tokens</a></li>
  334: <li><a href="#httpd">httpd</a></li>
  335: <li><a href="#lonsql">lonsql</a></li>
  336: <li><a href="#lond">lond</a></li>
  337: <li><a href="#lonc">lonc</a></li>
  338: <li><a href="#lonnet">lonnet</a></li>
  339: <li><a href="#connections">Connections</a></li>
  340: <li><a href="#delayed">Delayed Messages</a></li>
  341: <li><a href="#errcount">Error Count</a></li>
  342: </ol>
  343: <hr />
  344: <a name="configuration" />
  345: <h2>Configuration</h2>
  346: <h3>PerlVars</h3>
  347: <table border="2">
  348: ENDHEADERS
  349: 
  350:     foreach my $varname (sort(keys(%perlvar))) {
  351: 	&log($fh,"<tr><td>$varname</td><td>".
  352: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
  353:     }
  354:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
  355:     my %hostname = &Apache::lonnet::all_hostnames();
  356:     foreach my $id (sort(keys(%hostname))) {
  357: 	my $role = (&Apache::lonnet::is_library($id) ? 'library'
  358: 		                                     : 'access');
  359: 	&log($fh,
  360: 	    "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
  361: 	    "</td><td>".$role.
  362: 	    "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
  363:     }
  364:     &log($fh,"</table><h3>Spare Hosts</h3>");
  365:     if (keys(%Apache::lonnet::spareid) > 0) {
  366:         &log($fh,"<ul>");
  367:         foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
  368: 	    &log($fh,"<li>$type\n<ol>");
  369: 	    foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
  370: 	        &log($fh,"<li>$id</li>\n");
  371: 	    }
  372: 	    &log($fh,"</ol>\n</li>\n");
  373:         }
  374:         &log($fh,"</ul>\n");
  375:     } else {
  376:         &log($fh,"No spare hosts specified<br />\n");
  377:     }
  378:     return $fh;
  379: }
  380: 
  381: # --------------------------------------------------------------- clean out tmp
  382: sub clean_tmp {
  383:     my ($fh)=@_;
  384:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
  385:     my ($cleaned,$old,$removed) = (0,0,0);
  386:     my %errors = (
  387:                      dir       => [],
  388:                      file      => [],
  389:                      failopen  => [],
  390:                  );
  391:     my %error_titles = (
  392:                          dir       => 'failed to remove empty directory:',
  393:                          file      => 'failed to unlike stale file',
  394:                          failopen  => 'failed to open file or directory'
  395:                        );
  396:     ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
  397:     &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
  398:     foreach my $key (sort(keys(%errors))) {
  399:         if (ref($errors{$key}) eq 'ARRAY') {
  400:             if (@{$errors{$key}} > 0) {
  401:                 &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
  402:                      join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
  403:             }
  404:         }
  405:     }
  406: }
  407: 
  408: sub recursive_clean_tmp {
  409:     my ($subdir,$cleaned,$old,$removed,$errors) = @_;
  410:     my $base = "$perlvar{'lonDaemons'}/tmp";
  411:     my $path = $base;
  412:     next if ($subdir =~ m{\.\./});
  413:     next unless (ref($errors) eq 'HASH');
  414:     unless ($subdir eq '') {
  415:         $path .= '/'.$subdir;
  416:     }
  417:     if (opendir(my $dh,"$path")) {
  418:         while (my $file = readdir($dh)) {
  419:             next if ($file =~ /^\.\.?$/);
  420:             my $fname = "$path/$file";
  421:             if (-d $fname) {
  422:                 my $innerdir;
  423:                 if ($subdir eq '') {
  424:                     $innerdir = $file;
  425:                 } else {
  426:                     $innerdir = $subdir.'/'.$file;
  427:                 }
  428:                 ($cleaned,$old,$removed) = 
  429:                      &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
  430:                 my @doms = &Apache::lonnet::current_machine_domains();
  431: 
  432:                 if (open(my $dirhandle,$fname)) {
  433:                     unless (($innerdir eq 'helprequests') ||
  434:                             (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
  435:                         my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
  436:                                       join('&&',@contents)."\n";
  437:                         if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
  438:                             closedir($dirhandle);
  439:                             if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
  440:                                 if (rmdir($fname)) {
  441:                                     $removed ++;
  442:                                 } elsif (ref($errors->{dir}) eq 'ARRAY') {
  443:                                     push(@{$errors->{dir}},$fname);
  444:                                 }
  445:                             }
  446:                         }
  447:                     } else {
  448:                         closedir($dirhandle);
  449:                     }
  450:                 }
  451:             } else {
  452:                 my ($dev,$ino,$mode,$nlink,
  453:                     $uid,$gid,$rdev,$size,
  454:                     $atime,$mtime,$ctime,
  455:                     $blksize,$blocks)=stat($fname);
  456:                 my $now=time;
  457:                 my $since=$now-$mtime;
  458:                 if ($since>$perlvar{'lonExpire'}) {
  459:                     if ($subdir eq '') {
  460:                         my $line='';
  461:                         if ($fname =~ /\.db$/) {
  462:                             if (unlink($fname)) {
  463:                                 $cleaned++;
  464:                             } elsif (ref($errors->{file}) eq 'ARRAY') {
  465:                                 push(@{$errors->{file}},$fname);
  466:                             }
  467:                         } elsif (open(PROBE,$fname)) {
  468:                             my $line='';
  469:                             $line=<PROBE>;
  470:                             close(PROBE);
  471:                             if ($line=~/^CHECKOUTTOKEN\&/) {
  472:                                 if ($since>365*$perlvar{'lonExpire'}) {
  473:                                     if (unlink($fname)) {
  474:                                         $cleaned++; 
  475:                                     } elsif (ref($errors->{file}) eq 'ARRAY') {
  476:                                         push(@{$errors->{file}},$fname);
  477:                                     }
  478:                                 } else {
  479:                                     $old++;
  480:                                 }
  481:                             } else {
  482:                                 if (unlink($fname)) {
  483:                                     $cleaned++;
  484:                                 } elsif (ref($errors->{file}) eq 'ARRAY') {
  485:                                     push(@{$errors->{file}},$fname);
  486:                                 }
  487:                             }
  488:                         } elsif (ref($errors->{failopen}) eq 'ARRAY') {
  489:                             push(@{$errors->{failopen}},$fname);
  490:                         }
  491:                     } else {
  492:                         if (unlink($fname)) {
  493:                             $cleaned++;
  494:                         } elsif (ref($errors->{file}) eq 'ARRAY') {
  495:                             push(@{$errors->{file}},$fname);
  496:                         }
  497:                     }
  498:                 }
  499:             }
  500:         }
  501:         closedir($dh);
  502:     } elsif (ref($errors->{failopen}) eq 'ARRAY') {
  503:         push(@{$errors->{failopen}},$path);
  504:     }
  505:     return ($cleaned,$old,$removed);
  506: }
  507: 
  508: # ------------------------------------------------------------ clean out lonIDs
  509: sub clean_lonIDs {
  510:     my ($fh)=@_;
  511:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
  512:     my $cleaned=0;
  513:     my $active=0;
  514:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
  515:         my $now=time;
  516:         if (-l $fname) {
  517:             my $linkfname = readlink($fname);
  518:             if (-f $linkfname) {
  519:                 if ($linkfname =~ m{^$perlvar{'lonIDsDir'}/[^/]+\.id$}) {
  520:                     my @data = stat($linkfname);
  521:                     my $mtime = $data[9];
  522:                     my $since=$now-$mtime;
  523:                     if ($since>$perlvar{'lonExpire'}) {
  524:                         if (unlink($linkfname)) {
  525:                             $cleaned++;
  526:                             &log($fh,"Unlinking $linkfname<br />");
  527:                             unlink($fname);
  528:                         }
  529:                     }
  530:                 }
  531:             } else {
  532:                unlink($fname);
  533:             }
  534:         } elsif (-f $fname) {
  535:             my @data = stat($fname);
  536:             my $mtime = $data[9];
  537:             my $since=$now-$mtime;
  538:             if ($since>$perlvar{'lonExpire'}) {
  539:                 if (unlink($fname)) {
  540:                     $cleaned++;
  541:                     &log($fh,"Unlinking $fname<br />");
  542:                 }
  543:             } else {
  544:                 $active++;
  545:             }
  546:         }
  547:     }
  548:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
  549:     &log($fh,"<h3>$active open session(s)</h3>");
  550: }
  551: 
  552: # -------------------------------------------------------- clean out balanceIDs
  553: 
  554: sub clean_balanceIDs {
  555:     my ($fh)=@_;
  556:     &log($fh,'<hr /><a name="balcookies" /><h2>Session Tokens</h2>');
  557:     my $cleaned=0;
  558:     my $active=0;
  559:     if (-d $perlvar{'lonBalanceDir'}) {
  560:         while (my $fname=<$perlvar{'lonBalanceDir'}/*.id>) {
  561:             my ($dev,$ino,$mode,$nlink,
  562:                 $uid,$gid,$rdev,$size,
  563:                 $atime,$mtime,$ctime,
  564:                 $blksize,$blocks)=stat($fname);
  565:             my $now=time;
  566:             my $since=$now-$mtime;
  567:             if ($since>$perlvar{'lonExpire'}) {
  568:                 $cleaned++;
  569:                 &log($fh,"Unlinking $fname<br />");
  570:                 unlink("$fname");
  571:             } else {
  572:                 $active++;
  573:             }
  574:         }
  575:     }
  576:     &log($fh,"<p>Cleaned up ".$cleaned." stale balancer files</p>");
  577:     &log($fh,"<h3>$active unexpired balancer files</h3>");
  578: }
  579: 
  580: # ------------------------------------------------ clean out webDAV Session IDs
  581: sub clean_webDAV_sessionIDs {
  582:     my ($fh)=@_;
  583:     if ($perlvar{'lonRole'} eq 'library') {
  584:         &log($fh,'<hr /><a name="webdav" /><h2>WebDAV Session Tokens</h2>');
  585:         my $cleaned=0;
  586:         my $active=0;
  587:         my $now = time;
  588:         if (-d $perlvar{'lonDAVsessDir'}) {
  589:             while (my $fname=<$perlvar{'lonDAVsessDir'}/*>) {
  590:                 my @stats = stat($fname);
  591:                 my $since=$now-$stats[9];
  592:                 if ($since>$perlvar{'lonExpire'}) {
  593:                     $cleaned++;
  594:                     &log($fh,"Unlinking $fname<br />");
  595:                     unlink("$fname");
  596:                 } else {
  597:                     $active++;
  598:                 }
  599:             }
  600:             &log($fh,"<p>Cleaned up ".$cleaned." stale webDAV session token(s).</p>");
  601:             &log($fh,"<h3>$active open webDAV session(s)</h3>");
  602:         }
  603:     }
  604: }
  605: 
  606: # ------------------------------------------------------------ clean out ltiIDs
  607: 
  608: sub clean_ltiIDs {
  609:     my ($fh)=@_;
  610:     &log($fh,'<hr /><a name="ltisessions" /><h2>LTI Session Pointers</h2>');
  611:     my $cleaned=0;
  612:     my $active=0;
  613:     if (-d $perlvar{'ltiIDsDir'}) {
  614:         while (my $fname=<$perlvar{'ltiIDsDir'}/*>) {
  615:             my ($dev,$ino,$mode,$nlink,
  616:                 $uid,$gid,$rdev,$size,
  617:                 $atime,$mtime,$ctime,
  618:                 $blksize,$blocks)=stat($fname);
  619:             my $now=time;
  620:             my $since=$now-$mtime;
  621:             if ($since>$perlvar{'lonExpire'}) {
  622:                 $cleaned++;
  623:                 &log($fh,"Unlinking $fname<br />");
  624:                 unlink("$fname");
  625:             } else {
  626:                 $active++;
  627:             }
  628:         }
  629:     }
  630:     &log($fh,"<p>Cleaned up ".$cleaned." old LTI session pointers.</p>");
  631:     &log($fh,"<h3>$active unexpired LTI session pointers</h3>");
  632: }
  633: 
  634: # ----------------------------------------------------------- clean out sockets
  635: sub clean_sockets {
  636:     my ($fh)=@_;
  637:     my $cleaned=0;
  638:     opendir(SOCKETS,$perlvar{'lonSockDir'});
  639:     while (my $fname=readdir(SOCKETS)) {
  640: 	next if (-d $fname 
  641: 		 || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
  642: 	$cleaned++;
  643: 	&log($fh,"Unlinking $fname<br />");
  644: 	unlink("/home/httpd/sockets/$fname");
  645:     }
  646:     &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
  647: }
  648: 
  649: 
  650: # ----------------------------------------------------------------------- httpd
  651: sub check_httpd_logs {
  652:     my ($fh)=@_;
  653:     if (open(PIPE,"./lchttpdlogs|")) {
  654:         while (my $line=<PIPE>) {
  655:             &log($fh,$line);
  656:             if ($line=~/\[error\]/) { $notices++; }
  657:         }
  658:         close(PIPE);
  659:     }
  660:     &errout($fh);
  661: }
  662: 
  663: # ---------------------------------------------------------------------- lonnet
  664: 
  665: sub rotate_lonnet_logs {
  666:     my ($fh)=@_;
  667:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
  668:     print "Checking logs.\n";
  669:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
  670: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
  671: 	while (my $line=<DFH>) {
  672: 	    &log($fh,&encode_entities($line,'<>&"'));
  673: 	}
  674: 	close (DFH);
  675:     }
  676:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
  677: 
  678:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
  679: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
  680: 	while (my $line=<DFH>) {
  681: 	    &log($fh,&encode_entities($line,'<>&"'));
  682: 	}
  683: 	close (DFH);
  684:     } else { &log($fh,"No perm log\n") }
  685: 
  686:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
  687:     &rotate_logfile($fname,$fh,'lonnet log');
  688: 
  689:     &log($fh,"</pre>");
  690:     &errout($fh);
  691: }
  692: 
  693: sub rotate_other_logs {
  694:     my ($fh) = @_;
  695:     my %logs = (
  696:                   autoenroll          => 'Auto Enroll log',
  697:                   autocreate          => 'Create Course log',
  698:                   searchcat           => 'Search Cataloguing log',
  699:                   autoupdate          => 'Auto Update log',
  700:                   refreshcourseids_db => 'Refresh CourseIDs db log',
  701:                );
  702:     foreach my $item (keys(%logs)) {
  703:         my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
  704:         &rotate_logfile($fname,$fh,$logs{$item});
  705:     }
  706: }
  707: 
  708: # ----------------------------------------------------------------- Connections
  709: sub test_connections {
  710:     my ($fh)=@_;
  711:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
  712:     print "Testing connections.\n";
  713:     &log($fh,"<table border='2'>");
  714:     my ($good,$bad)=(0,0);
  715:     my %hostname = &Apache::lonnet::all_hostnames();
  716:     foreach my $tryserver (sort(keys(%hostname))) {
  717: 	print(".");
  718: 	my $result;
  719: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
  720: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
  721: 	    $result="<b>ok</b>";
  722: 	    $good++;
  723: 	} else {
  724: 	    $result=$answer;
  725: 	    $warnings++;
  726: 	    if ($answer eq 'con_lost') {
  727: 		$bad++;
  728: 		$warnings++;
  729: 	    } else {
  730: 		$good++; #self connection
  731: 	    }
  732: 	}
  733: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
  734: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
  735:     }
  736:     &log($fh,"</table>");
  737:     print "\n$good good, $bad bad connections\n";
  738:     &errout($fh);
  739: }
  740: 
  741: 
  742: # ------------------------------------------------------------ Delayed messages
  743: sub check_delayed_msg {
  744:     my ($fh,$weightsref,$exclusionsref)=@_;
  745:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
  746:     print "Checking buffers.\n";
  747:     
  748:     &log($fh,'<h3>Scanning Permanent Log</h3>');
  749: 
  750:     my $unsend=0;
  751:     my $ignored=0;
  752: 
  753:     my %hostname = &Apache::lonnet::all_hostnames();
  754:     my $numhosts = scalar(keys(%hostname));
  755:     my $checkbackwards = 0;
  756:     my $checkfrom = 0;
  757:     my $checkexcluded = 0;
  758:     my (%bymachine,%weights,%exclusions,%serverhomes);
  759:     if (ref($weightsref) eq 'HASH') {
  760:         %weights = %{$weightsref};
  761:     }
  762:     if (ref($exclusionsref) eq 'HASH') {
  763:         %exclusions = %{$exclusionsref};
  764:         if (keys(%exclusions)) {
  765:             $checkexcluded = 1;
  766:             %serverhomes = &read_serverhomeIDs();
  767:         }
  768:     }
  769: 
  770: #
  771: # For LON-CAPA 1.2.0 to 2.1.3 (release dates: 8/31/2004 and 3/31/2006) any
  772: # entry logged in lonnet.perm.log for completion of a delayed (critical)
  773: # transaction lacked the hostID for the remote node to which the command
  774: # to be completed was sent.
  775: #
  776: # Because of this, exclusion of items in lonnet.perm.log for nodes which are
  777: # no longer part of the cluster from adding to the overall "unsend" count
  778: # needs additional effort besides the changes made in loncron rev. 1.105.
  779: #
  780: # For "S" (completion) events logging in LON-CAPA 1.2.0 through 2.1.3 included
  781: # "LondTransaction=HASH(hexadecimal)->getClient() :$cmd, where the hexadecimal
  782: # is a memory location, and $cmd is the command sent to the remote node.
  783: #
  784: # Starting with 2.2.0 (released 8/21/2006) logging for "S" (completion) events
  785: # had sethost:$host_id:$cmd after LondTransaction=HASH(hexadecimal)->getClient()
  786: #
  787: # Starting with 2.4.1 (released 6/13/2007) logging for "S" replaced echoing the
  788: # getClient() call with the result of the Transaction->getClient() call itself
  789: # undef for completion of delivery of a delayed message.
  790: #
  791: # The net effect of these changes is that lonnet.perm.log is now accessed three
  792: # times: (a) oldest record is checked, if earlier than release date for 2.5.0
  793: # then (b) file is read backwards, with timestamp recorded for most recent
  794: # instance of logged "S" event for "update" command without "sethost:$host_id:"
  795: # then (c) file is read forward with records ignored which predate the timestamp
  796: # recorded in (b), if one was found.
  797: #
  798: # In (c), when calculating the unsend total, i.e., the difference between delayed
  799: # transactions ("D") and sent transactions ("S"), transactions are ignored if the
  800: # target node is no longer in the cluster, and also (for "update" commands), if
  801: # the target node is in the list of nodes excluded from the count, in the domain
  802: # configuration for this machine's default domain.  The idea here is to remove
  803: # delayed "update" commands for nodes for which inbound access to port 5663,
  804: # is blocked, but are still part of the LON-CAPA network, (i.e., they can still
  805: # replicate content from other nodes).
  806: #
  807: 
  808:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
  809:     if (defined($dfh)) {
  810:         while (my $line=<$dfh>) {
  811:             my ($time,$sdf,$rest)=split(/:/,$line,3);
  812:             if ($time < 1541185772) {
  813:                 $checkbackwards = 1;
  814:             }
  815:             last;
  816:         }
  817:         undef $dfh;
  818:     } 
  819: 
  820:     if ($checkbackwards) {
  821:         if (tie *BW, 'File::ReadBackwards', "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
  822:             while(my $line=<BW>) {
  823:                 if ($line =~ /\QLondTransaction=HASH\E[^:]+:update:/) {
  824:                     ($checkfrom) = split(/:/,$line,2);
  825:                     last;
  826:                 }
  827:             }
  828:             close(BW);
  829:         }
  830:     }
  831:     $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
  832:     if (defined($dfh)) {
  833:         while (my $line=<$dfh>) {
  834:             my ($time,$sdf,$rest)=split(/:/,$line,3);
  835:             next unless (($sdf eq 'F') || ($sdf eq 'S') || ($sdf eq 'D'));
  836:             next if (($checkfrom) && ($time <= $checkfrom));
  837:             my ($dserv,$dcmd);
  838:             if ($sdf eq 'S') {
  839:                 my ($serva,$cmda,$servb,$cmdb) = split(/:/,$rest);
  840:                 if ($cmda eq 'sethost') {
  841:                     chomp($cmdb);
  842:                     $dcmd = $cmdb;
  843:                 } else {
  844:                     $dcmd = $cmda;
  845:                 }
  846:                 if (($serva =~ /^LondTransaction/) || ($serva eq '')) {
  847:                     unless (($servb eq '') || ($servb =~ m{^/})) {
  848:                         $dserv = $servb;
  849:                     }
  850:                 } else {
  851:                     $dserv = $serva;
  852:                 }
  853:             } else {
  854:                 ($dserv,$dcmd) = split(/:/,$rest);
  855:             }
  856:             if ($sdf eq 'F') {
  857:                 my $local=localtime($time);
  858:                 &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
  859:                 $warnings++;
  860:             }
  861:             next if ((($dserv eq '') || ($dcmd eq '')) && ($sdf ne 'F'));
  862:             if ($sdf eq 'S') {
  863:                 if ($dcmd eq 'update') {
  864:                     if ($hostname{$dserv}) {
  865:                         if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
  866:                             $ignored --;
  867:                         } else {
  868:                             $unsend --;
  869:                         }
  870:                     }
  871:                     if (exists($bymachine{$dserv})) {
  872:                         $bymachine{$dserv} --;
  873:                     } else {
  874:                         $bymachine{$dserv} = -1;
  875:                     }
  876:                 } else {
  877:                     if ($hostname{$dserv}) {
  878:                         $unsend --;
  879:                     }
  880:                 }
  881:             } elsif ($sdf eq 'D') {
  882:                 if ($dcmd eq 'update') {
  883:                     if ($hostname{$dserv}) {
  884:                         if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
  885:                             $ignored ++;
  886:                         } else {
  887:                             $unsend ++;
  888:                         }
  889:                     }
  890:                     if (exists($bymachine{$dserv})) {
  891:                         $bymachine{$dserv} ++;
  892:                     } else {
  893:                         $bymachine{$dserv} = 1;
  894:                     }
  895:                 } else {
  896:                     if ($hostname{$dserv}) {
  897:                         $unsend ++;
  898:                     }
  899:                 }
  900:             }
  901:         }
  902:         undef $dfh;
  903:         my $nodest = 0;
  904:         my $retired = 0;
  905:         my %active;
  906:         if (keys(%bymachine)) {
  907:             unless ($checkexcluded) {
  908:                 %serverhomes = &read_serverhomeIDs();
  909:             }
  910:             foreach my $key (keys(%bymachine)) {
  911:                 if ($bymachine{$key} > 0) {
  912:                     if ($hostname{$key}) {
  913:                         $active{$serverhomes{$hostname{$key}}} += $bymachine{$key};
  914:                     } else {
  915:                         $retired ++;
  916:                         $nodest += $bymachine{$key};
  917:                     }
  918:                 }
  919:             }
  920:         }
  921:         if (keys(%active)) {
  922:             &log($fh,"<p>Unsend messages by node, active (undegraded) nodes in cluster</p>\n");
  923:             foreach my $key (sort(keys(%active))) {
  924:                 &log($fh,&encode_entities("$key => $active{$key}",'<>&"')."\n");
  925:             }
  926:         }
  927:         &log($fh,"<p>Total unsend messages: <b>$unsend</b> for ".scalar(keys(%active))." active (undegraded) nodes in cluster.</p>\n");
  928:         if (keys(%exclusions) > 0) {
  929:             &log($fh,"<p>Total incomplete updates <b>$ignored</b> for ".scalar(keys(%exclusions))." degraded nodes in cluster.</p>\n");
  930:         }
  931:         if ($retired) {
  932:             &log($fh,"<p>Total unsent <b>$nodest</b> for $retired nodes no longer in cluster.</p>\n");
  933:         }
  934:         if ($unsend > 0) {
  935:             $warnings=$warnings+$weights{'U'}*$unsend;
  936:         }
  937:     }
  938: 
  939:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
  940:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
  941: # list directory with delayed messages and remember offline servers
  942:     my %servers=();
  943:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
  944:     while (my $line=<DFH>) {
  945:         my ($server)=($line=~/\.(\w+)$/);
  946:         if ($server) { $servers{$server}=1; }
  947: 	&log($fh,&encode_entities($line,'<>&"'));
  948:     }
  949:     &log($fh,"</pre>\n");
  950:     close (DFH);
  951: # pong to all servers that have delayed messages
  952: # this will trigger a reverse connection, which should flush the buffers
  953:     foreach my $tryserver (sort(keys(%servers))) {
  954:         if ($hostname{$tryserver} || !$numhosts) {
  955:             my $answer;
  956:             eval {
  957:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
  958:                 alarm(20);
  959:                 $answer = &Apache::lonnet::reply("pong",$tryserver);
  960:                 alarm(0);
  961:             };
  962:             if ($@ && $@ =~ m/TIMEOUT/) {
  963:                 &log($fh,"Attempted pong to $tryserver timed out<br />");
  964:                 print "Time out while contacting: $tryserver for pong.\n";
  965:             } else {
  966:                 &log($fh,"Pong to $tryserver: $answer<br />");
  967:             }
  968:         } else {
  969:             &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
  970:         }
  971:     }
  972: }
  973: 
  974: sub finish_logging {
  975:     my ($fh,$weightsref)=@_;
  976:     my %weights;
  977:     if (ref($weightsref) eq 'HASH') {
  978:         %weights = %{$weightsref};
  979:     }
  980:     &log($fh,"<a name='errcount' />\n");
  981:     $totalcount=($weights{'N'}*$notices)+($weights{'W'}*$warnings)+($weights{'E'}*$errors);
  982:     &errout($fh);
  983:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
  984:     my $now=time;
  985:     my $date=localtime($now);
  986:     &log($fh,"<hr />$date ($now)</body></html>\n");
  987:     print "lon-status webpage updated.\n";
  988:     $fh->close();
  989: 
  990:     if ($errors) { $simplestatus{'errors'}=$errors; }
  991:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
  992:     if ($notices) { $simplestatus{'notices'}=$notices; }
  993:     $simplestatus{'time'}=time;
  994: }
  995: 
  996: sub log_simplestatus {
  997:     rename("$statusdir/newstatus.html","$statusdir/index.html");
  998:     
  999:     my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
 1000:     foreach (keys %simplestatus) {
 1001: 	print $sfh $_.'='.$simplestatus{$_}.'&';
 1002:     }
 1003:     print $sfh "\n";
 1004:     $sfh->close();
 1005: }
 1006: 
 1007: sub write_loncaparevs {
 1008:     print "Retrieving LON-CAPA version information.\n";
 1009:     my %hostname = &Apache::lonnet::all_hostnames();
 1010:     my $output;
 1011:     foreach my $id (sort(keys(%hostname))) {
 1012:         if ($id ne '') {
 1013:             my $loncaparev;
 1014:             eval {
 1015:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
 1016:                 alarm(10);
 1017:                 $loncaparev =
 1018:                     &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
 1019:                 alarm(0);
 1020:             };
 1021:             if ($@ && $@ =~ m/TIMEOUT/) {
 1022:                 print "Time out while contacting lonHost: $id for version.\n";   
 1023:             }
 1024:             if ($loncaparev =~ /^[\w.\-]+$/) {
 1025:                 $output .= $id.':'.$loncaparev."\n";
 1026:             }
 1027:         }
 1028:     }
 1029:     if ($output) {
 1030:         if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
 1031:             print $fh $output;
 1032:             close($fh);
 1033:             &Apache::lonnet::load_loncaparevs();
 1034:         }
 1035:     }
 1036:     return;
 1037: }
 1038: 
 1039: sub write_serverhomeIDs {
 1040:     print "Retrieving LON-CAPA lonHostID information.\n";
 1041:     my %name_to_host = &Apache::lonnet::all_names();
 1042:     my $output;
 1043:     foreach my $name (sort(keys(%name_to_host))) {
 1044:         if ($name ne '') {
 1045:             if (ref($name_to_host{$name}) eq 'ARRAY') {
 1046:                 my $serverhomeID;
 1047:                 eval {
 1048:                     local $SIG{ ALRM } = sub { die "TIMEOUT" };
 1049:                     alarm(10);
 1050:                     $serverhomeID =
 1051:                         &Apache::lonnet::get_server_homeID($name,1,'loncron');
 1052:                     alarm(0);
 1053:                 };
 1054:                 if ($@ && $@ =~ m/TIMEOUT/) {
 1055:                     print "Time out while contacting server: $name\n"; 
 1056:                 }
 1057:                 if ($serverhomeID ne '') {
 1058:                     $output .= $name.':'.$serverhomeID."\n";
 1059:                 } else {
 1060:                     $output .= $name.':'.$name_to_host{$name}->[0]."\n";
 1061:                 }
 1062:             }
 1063:         }
 1064:     }
 1065:     if ($output) {
 1066:         if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
 1067:             print $fh $output;
 1068:             close($fh);
 1069:             &Apache::lonnet::load_serverhomeIDs();
 1070:         }
 1071:     }
 1072:     return;
 1073: }
 1074: 
 1075: sub write_checksums {
 1076:     my $distro = &LONCAPA::distro();
 1077:     if ($distro) {
 1078:         print "Retrieving file version and checksumming.\n";
 1079:         my $numchksums = 0;
 1080:         my ($chksumsref,$versionsref) =
 1081:             &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
 1082:                                                   $perlvar{'lonLib'},
 1083:                                                   $perlvar{'lonIncludes'},
 1084:                                                   $perlvar{'lonTabDir'});
 1085:         if (ref($chksumsref) eq 'HASH') {
 1086:             $numchksums = scalar(keys(%{$chksumsref}));
 1087:         }
 1088:         print "File version retrieved and checksumming completed for $numchksums files.\n";
 1089:     } else {
 1090:         print "File version retrieval and checksumming skipped - could not determine Linux distro.\n"; 
 1091:     }
 1092:     return;
 1093: }
 1094: 
 1095: sub write_hostips {
 1096:     my $lontabdir = $perlvar{'lonTabDir'};
 1097:     my $defdom = $perlvar{'lonDefDomain'};
 1098:     my $lonhost = $perlvar{'lonHostID'};
 1099:     my $newfile = "$lontabdir/currhostips.tab";
 1100:     my $oldfile = "$lontabdir/prevhostips.tab";
 1101:     my (%prevhosts,%currhosts,%ipchange);
 1102:     if ((-e $newfile) && (-s $newfile)) {
 1103:         move($newfile,$oldfile);
 1104:         chmod(0644,$oldfile);
 1105:         if (open(my $fh,'<',$oldfile)) {
 1106:             while (my $line=<$fh>) {
 1107:                 chomp($line);
 1108:                 if ($line =~ /^([^:]+):([\d.]+)$/) {
 1109:                     $prevhosts{$1} = $2;
 1110:                 }
 1111:             }
 1112:             close($fh);
 1113:         }
 1114:     }
 1115:     my ($ip_info,$cached) =
 1116:         &Apache::lonnet::is_cached_new('iphost','iphost');
 1117:     if (!$cached) {
 1118:         &Apache::lonnet::get_iphost();
 1119:         ($ip_info,$cached) =
 1120:         &Apache::lonnet::is_cached_new('iphost','iphost');
 1121:     }
 1122:     if (ref($ip_info) eq 'ARRAY') {
 1123:         %currhosts = %{$ip_info->[1]};
 1124:         if (open(my $fh,'>',$newfile)) {
 1125:             foreach my $key (keys(%currhosts)) {
 1126:                 print $fh "$key:$currhosts{$key}\n";
 1127:             }
 1128:             close($fh);
 1129:             chmod(0644,$newfile);
 1130:         }
 1131:     }
 1132:     if (keys(%prevhosts) && keys(%currhosts)) {
 1133:         foreach my $key (keys(%prevhosts)) {
 1134:             unless ($currhosts{$key} eq $prevhosts{$key}) {
 1135:                 $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
 1136:             }
 1137:         }
 1138:         foreach my $key (keys(%currhosts)) {
 1139:             unless ($currhosts{$key} eq $prevhosts{$key}) {
 1140:                 $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
 1141:             }
 1142:         }
 1143:     }
 1144:     if (&Apache::lonnet::domain($defdom,'primary') eq $lonhost) {
 1145:         if (keys(%ipchange)) {
 1146:             if (open(my $fh,'>>',$perlvar{'lonDaemons'}.'/logs/hostip.log')) {
 1147:                print $fh "********************\n".localtime(time).' Changes --'."\n".
 1148:                          "| Hostname | Previous IP | New IP |\n".
 1149:                          " --------------------------------- \n";
 1150:                foreach my $hostname (sort(keys(%ipchange))) {
 1151:                     print $fh "| $hostname | $ipchange{$hostname} |\n";
 1152:                 }
 1153:                 print $fh "\n*******************\n\n";
 1154:                 close($fh);
 1155:             }
 1156:             my $emailto = &Apache::loncommon::build_recipient_list(undef,
 1157:                                    'hostipmail',$defdom);
 1158:             if ($emailto) {
 1159:                 my $subject = "LON-CAPA Hostname to IP change ($perlvar{'lonHostID'})";
 1160:                 my $chgmail = "To: $emailto\n".
 1161:                               "Subject: $subject\n".
 1162:                               "Content-type: text/plain\; charset=UTF-8\n".
 1163:                               "MIME-Version: 1.0\n\n".
 1164:                               "Host/IP changes\n".
 1165:                               " \n".
 1166:                               "| Hostname | Previous IP | New IP |\n".
 1167:                               " --------------------------------- \n";
 1168:                 foreach my $hostname (sort(keys(%ipchange))) {
 1169:                     $chgmail .= "| $hostname | $ipchange{$hostname} |\n";
 1170:                 }
 1171:                 $chgmail .= "\n\n";
 1172:                 if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
 1173:                     print $mailh $chgmail;
 1174:                     close($mailh);
 1175:                     print "Sending mail notification of hostname/IP changes.\n";
 1176:                 }
 1177:             }
 1178:         }
 1179:     }
 1180:     return;
 1181: }
 1182: 
 1183: sub clean_nosslverify {
 1184:     my ($fh) = @_;
 1185:     my %unlinked;
 1186:     if (-d "$perlvar{'lonSockDir'}/nosslverify") {
 1187:         if (opendir(my $dh,"$perlvar{'lonSockDir'}/nosslverify")) {
 1188:             while (my $fname=readdir($dh)) {
 1189:                 next if ($fname =~ /^\.+$/);
 1190:                 if (unlink("/home/httpd/sockets/nosslverify/$fname")) {
 1191:                     &log($fh,"Unlinking $fname<br />");
 1192:                     $unlinked{$fname} = 1;
 1193:                 }
 1194:             }
 1195:             closedir($dh);
 1196:         }
 1197:     }
 1198:     &log($fh,"<p>Removed ".scalar(keys(%unlinked))." nosslverify clients</p>");
 1199:     return %unlinked;
 1200: }
 1201: sub clean_lonc_childpids {
 1202:     my $childpiddir = "$perlvar{'lonDocRoot'}/lon-status/loncchld";
 1203:     if (-d $childpiddir) {
 1204:         if (opendir(my $dh,$childpiddir)) {
 1205:             while (my $fname=readdir($dh)) {
 1206:                 next if ($fname =~ /^\.+$/);
 1207:                 unlink("$childpiddir/$fname");
 1208:             }
 1209:             closedir($dh);
 1210:         }
 1211:     }
 1212: }
 1213: 
 1214: sub write_connection_config {
 1215:     my ($domconf,%connectssl,%changes);
 1216:     $domconf = &get_domain_config();
 1217:     if (ref($domconf) eq 'HASH') {
 1218:         if (ref($domconf->{'ssl'}) eq 'HASH') {
 1219:             foreach my $connect ('connto','connfrom') {
 1220:                 if (ref($domconf->{'ssl'}->{$connect}) eq 'HASH') {
 1221:                     my ($sslreq,$sslnoreq,$currsetting);
 1222:                     my %contypes;
 1223:                     foreach my $type ('dom','intdom','other') {
 1224:                         $connectssl{$connect.'_'.$type} = $domconf->{'ssl'}->{$connect}->{$type};
 1225:                     }
 1226:                 }
 1227:             }
 1228:         }
 1229:         if (keys(%connectssl)) {
 1230:             my %currconf; 
 1231:             if (open(my $fh,'<',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
 1232:                 while (my $line = <$fh>) {
 1233:                     chomp($line);
 1234:                     my ($name,$value) = split(/=/,$line);
 1235:                     if ($value =~ /^(?:no|yes|req)$/) {
 1236:                         if ($name =~ /^conn(to|from)_(dom|intdom|other)$/) {
 1237:                             $currconf{$name} = $value;
 1238:                         }
 1239:                     }
 1240:                 }
 1241:                 close($fh);
 1242:             }
 1243:             if (open(my $fh,'>',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
 1244:                 my $count = 0;
 1245:                 foreach my $key (sort(keys(%connectssl))) { 
 1246:                     print $fh "$key=$connectssl{$key}\n";
 1247:                     if (exists($currconf{$key})) {
 1248:                         unless ($currconf{$key} eq $connectssl{$key}) {
 1249:                             $changes{$key} = 1;
 1250:                         }
 1251:                     } else {
 1252:                         $changes{$key} = 1;
 1253:                     }
 1254:                     $count ++;
 1255:                 }
 1256:                 close($fh);
 1257:                 print "Completed writing SSL options for lonc/lond for $count items.\n";
 1258:             }
 1259:         } else {
 1260:             print "Writing of SSL options skipped - no connection rules in domain configuration.\n";
 1261:         }
 1262:     } else {
 1263:         print "Retrieval of SSL options for lonc/lond skipped - no configuration data available for domain.\n";
 1264:     }
 1265:     return %changes;
 1266: }
 1267: 
 1268: sub get_domain_config {
 1269:     my ($dom,$primlibserv,$isprimary,$url,%confhash);
 1270:     $dom = $perlvar{'lonDefDomain'};
 1271:     $primlibserv = &Apache::lonnet::domain($dom,'primary');
 1272:     if ($primlibserv eq $perlvar{'lonHostID'}) {
 1273:         $isprimary = 1;
 1274:     } elsif ($primlibserv ne '') {
 1275:         my $protocol = $Apache::lonnet::protocol{$primlibserv};
 1276:         my $hostname = &Apache::lonnet::hostname($primlibserv);
 1277:         unless ($protocol eq 'https') {
 1278:             $protocol = 'http';
 1279:         }
 1280:         $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl?primary='.$primlibserv.'&format=raw';
 1281:     }
 1282:     if ($isprimary) {
 1283:         my $lonusersdir = $perlvar{'lonUsersDir'};
 1284:         my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
 1285:         if (-e $fname) {
 1286:             my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
 1287:             if (ref($dbref) eq 'HASH') {
 1288:                 foreach my $key (sort(keys(%{$dbref}))) {
 1289:                     my $value = $dbref->{$key};
 1290:                     if ($value =~ s/^__FROZEN__//) {
 1291:                         $value = thaw(&LONCAPA::unescape($value));
 1292:                     } else {
 1293:                         $value = &LONCAPA::unescape($value);
 1294:                     }
 1295:                     $confhash{$key} = $value;
 1296:                 }
 1297:                 &LONCAPA::locking_hash_untie($dbref);
 1298:             }
 1299:         }
 1300:     } else {
 1301:         my $request=new HTTP::Request('GET',$url);
 1302:         my $response=&LONCAPA::LWPReq::makerequest($primlibserv,$request,'',\%perlvar,5);
 1303:         unless ($response->is_error()) {
 1304:             my $content = $response->content;
 1305:             if ($content) {
 1306:                 my @pairs=split(/\&/,$content);
 1307:                 foreach my $item (@pairs) {
 1308:                     my ($key,$value)=split(/=/,$item,2);
 1309:                     my $what = &LONCAPA::unescape($key);
 1310:                     if ($value =~ s/^__FROZEN__//) {
 1311:                         $value = thaw(&LONCAPA::unescape($value));
 1312:                     } else {
 1313:                         $value = &LONCAPA::unescape($value);
 1314:                     }
 1315:                     $confhash{$what}=$value;
 1316:                 }
 1317:             }
 1318:         }
 1319:     }
 1320:     return \%confhash;
 1321: }
 1322: 
 1323: sub write_hosttypes {
 1324:     my %intdom = &Apache::lonnet::all_host_intdom();
 1325:     my %hostdom = &Apache::lonnet::all_host_domain();
 1326:     my $dom = $hostdom{$perlvar{'lonHostID'}};
 1327:     my $internetdom = $intdom{$perlvar{'lonHostID'}};
 1328:     my %changes;
 1329:     if (($dom ne '') && ($internetdom ne '')) {
 1330:         if (keys(%hostdom)) {
 1331:             my %currhosttypes;
 1332:             if (open(my $fh,'<',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
 1333:                 while (my $line = <$fh>) {
 1334:                     chomp($line);
 1335:                     my ($name,$value) = split(/:/,$line);
 1336:                     if (($name ne '') && ($value =~ /^(dom|intdom|other)$/)) {
 1337:                         $currhosttypes{$name} = $value;
 1338:                     }
 1339:                 }
 1340:                 close($fh);
 1341:             }
 1342:             if (open(my $fh,'>',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
 1343:                 my $count = 0;
 1344:                 foreach my $lonid (sort(keys(%hostdom))) {
 1345:                     my $type = 'other';
 1346:                     if ($hostdom{$lonid} eq $dom) {
 1347:                         $type = 'dom';
 1348:                     } elsif ($intdom{$lonid} eq $internetdom) {
 1349:                         $type = 'intdom';
 1350:                     }
 1351:                     print $fh "$lonid:$type\n";
 1352:                     if (exists($currhosttypes{$lonid})) {
 1353:                         if ($type ne $currhosttypes{$lonid}) {
 1354:                             $changes{$lonid} = 1;
 1355:                         }
 1356:                     } else {
 1357:                         $changes{$lonid} = 1;
 1358:                     }
 1359:                     $count ++;
 1360:                 }
 1361:                 close($fh);
 1362:                 print "Completed writing host type data for $count hosts.\n";
 1363:             }
 1364:         } else {
 1365:             print "Writing of host types skipped - no hosts found.\n";
 1366:         }
 1367:     } else {
 1368:         print "Writing of host types skipped - could not determine this host's LON-CAPA domain or 'internet' domain.\n";
 1369:     }
 1370:     return %changes;
 1371: }
 1372: 
 1373: sub update_revocation_list {
 1374:     my ($result,$changed) = &Apache::lonnet::fetch_crl_pemfile();
 1375:     if ($result eq 'ok') {
 1376:         print "Certificate Revocation List (from CA) updated.\n";
 1377:     } else {
 1378:         print "Certificate Revocation List from (CA) not updated.\n";
 1379:     }
 1380:     return $changed;
 1381: }
 1382: 
 1383: sub reset_nosslverify_pids {
 1384:     my ($fh,%sslrem) = @_;
 1385:     &checkon_daemon($fh,'lond',40000,'USR2');
 1386:     my $loncpidfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
 1387:     my $loncppid;
 1388:     if ((-e $loncpidfile) && (open(my $pfh,'<',$loncpidfile))) {
 1389:         $loncppid=<$pfh>;
 1390:         chomp($loncppid);
 1391:         close($pfh);
 1392:         if ($loncppid =~ /^\d+$/) {
 1393:             my %pids_by_host;
 1394:             my $docdir = $perlvar{'lonDocRoot'};
 1395:             if (-d "$docdir/lon-status/loncchld") {
 1396:                 if (opendir(my $dh,"$docdir/lon-status/loncchld")) {
 1397:                     while (my $file = readdir($dh)) {
 1398:                         next if ($file =~ /^\./);
 1399:                         if (open(my $fh,'<',"$docdir/lon-status/loncchld/$file")) {
 1400:                             my $record = <$fh>;
 1401:                             chomp($record);
 1402:                             close($fh);
 1403:                             my ($remotehost,$authmode) = split(/:/,$record);
 1404:                             $pids_by_host{$remotehost}{$authmode}{$file} = 1;
 1405:                         }
 1406:                     }
 1407:                     closedir($dh);
 1408:                     if (keys(%pids_by_host)) {
 1409:                         foreach my $host (keys(%pids_by_host)) {
 1410:                             if ($sslrem{$host}) {
 1411:                                 if (ref($pids_by_host{$host}) eq 'HASH') {
 1412:                                     if (ref($pids_by_host{$host}{'insecure'}) eq 'HASH') {
 1413:                                         if (keys(%{$pids_by_host{$host}{'insecure'}})) {
 1414:                                             foreach my $pid (keys(%{$pids_by_host{$host}{'insecure'}})) {
 1415:                                                 if (open(PIPE,"ps -o ppid= -p $pid |")) {
 1416:                                                     my $ppid = <PIPE>;
 1417:                                                     chomp($ppid);
 1418:                                                     close(PIPE);
 1419:                                                     $ppid =~ s/(^\s+|\s+$)//g;
 1420:                                                     if (($ppid == $loncppid) && (kill 0 => $pid)) {
 1421:                                                         kill QUIT => $pid;
 1422:                                                     }
 1423:                                                 }
 1424:                                             }
 1425:                                         }
 1426:                                     }
 1427:                                 }
 1428:                             }
 1429:                         }
 1430:                     }
 1431:                 }
 1432:             }
 1433:         }
 1434:     }
 1435:     return;
 1436: }
 1437: 
 1438: sub get_permcount_settings {
 1439:     my ($domconf) = @_;
 1440:     my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 1441:     my (%weights,$threshold,$sysmail,$reportstatus,%exclusions);
 1442:     foreach my $type ('E','W','N','U') {
 1443:         $weights{$type} = $defaults->{$type};
 1444:     }
 1445:     $threshold = $defaults->{'threshold'};
 1446:     $sysmail = $defaults->{'sysmail'};
 1447:     $reportstatus = 1;
 1448:     if (ref($domconf) eq 'HASH') {
 1449:         if (ref($domconf->{'contacts'}) eq 'HASH') {
 1450:             if ($domconf->{'contacts'}{'reportstatus'} == 0) {
 1451:                 $reportstatus = 0;
 1452:             }
 1453:             if (ref($domconf->{'contacts'}{'lonstatus'}) eq 'HASH') {
 1454:                 if (ref($domconf->{'contacts'}{'lonstatus'}{weights}) eq 'HASH') {
 1455:                     foreach my $type ('E','W','N','U') {
 1456:                         if (exists($domconf->{'contacts'}{'lonstatus'}{weights}{$type})) {
 1457:                             $weights{$type} = $domconf->{'contacts'}{'lonstatus'}{weights}{$type};
 1458:                         }
 1459:                     }
 1460:                 }
 1461:                 if (ref($domconf->{'contacts'}{'lonstatus'}{'excluded'}) eq 'ARRAY') {
 1462:                     my @excluded = @{$domconf->{'contacts'}{'lonstatus'}{'excluded'}};
 1463:                     if (@excluded) {
 1464:                         map { $exclusions{$_} = 1; } @excluded;
 1465:                     }
 1466:                 }
 1467:                 if (exists($domconf->{'contacts'}{'lonstatus'}{'threshold'})) {
 1468:                     $threshold = $domconf->{'contacts'}{'lonstatus'}{'threshold'};
 1469:                 }
 1470:                 if (exists($domconf->{'contacts'}{'lonstatus'}{'sysmail'})) {
 1471:                     $sysmail = $domconf->{'contacts'}{'lonstatus'}{'sysmail'};
 1472:                 }
 1473:             }
 1474:         }
 1475:     }
 1476:     return ($threshold,$sysmail,$reportstatus,\%weights,\%exclusions);
 1477: }
 1478: 
 1479: sub read_serverhomeIDs {
 1480:     my %server;
 1481:     if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
 1482:         if (open(my $fh,'<',"$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
 1483:             while (<$fh>) {
 1484:                 my($host,$id) = split(/:/);
 1485:                 chomp($id);
 1486:                 $server{$host} = $id;
 1487:             }
 1488:             close($fh);
 1489:         }
 1490:     }
 1491:     return %server;
 1492: }
 1493: 
 1494: sub send_mail {
 1495:     my ($sysmail,$reportstatus) = @_;
 1496:     my $defdom = $perlvar{'lonDefDomain'};
 1497:     my $origmail = $perlvar{'lonAdmEMail'};
 1498:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
 1499:                                    'lonstatusmail',$defdom,$origmail);
 1500:     if (($totalcount>$sysmail) && ($reportstatus)) {
 1501: 	$emailto.=",$perlvar{'lonSysEMail'}";
 1502:     }
 1503:     my $from;
 1504:     my $hostname=`/bin/hostname`;
 1505:     chop($hostname);
 1506:     $hostname=~s/[^\w\.]//g;
 1507:     if ($hostname) {
 1508:         $from = 'www@'.$hostname;
 1509:     }
 1510:     my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
 1511:     my $loncronmail = "To: $emailto\n".
 1512:                       "From: $from\n".
 1513:                       "Subject: ".$subj."\n".
 1514:                       "Content-type: text/html\; charset=UTF-8\n".
 1515:                       "MIME-Version: 1.0\n\n";
 1516:     if (open(my $fh,"<$statusdir/index.html")) {
 1517:         while (<$fh>) {
 1518:             $loncronmail .= $_;
 1519:         }
 1520:         close($fh);
 1521:     } else {
 1522:         $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
 1523:     }
 1524:     $loncronmail .= "\n\n";
 1525:     if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
 1526:         print $mailh $loncronmail;
 1527:         close($mailh);
 1528:         print "Sending mail.\n";
 1529:     } else {
 1530:         print "Sending mail failed.\n";
 1531:     }
 1532: }
 1533: 
 1534: sub usage {
 1535:     print(<<USAGE);
 1536: loncron - housekeeping program that checks up on various parts of LON-CAPA
 1537: 
 1538: Options:
 1539:    --help     Display 
 1540:    --noemail  Do not send the status email
 1541:    --justcheckconnections  Only check the current status of the lonc/d
 1542:                                 connections, do not send emails do not
 1543:                                 check if the daemons are running, do not
 1544:                                 generate lon-status
 1545:    --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
 1546:                                 running, do not send emails do not
 1547:                                 check the lonc/d connections, do not
 1548:                                 generate lon-status
 1549:    --justreload            Only tell the daemons to reload the config files,
 1550: 				do not send emails do not
 1551:                                 check if the daemons are running, do not
 1552:                                 generate lon-status
 1553:    --justiptables          Only update the dynamic iptables rules for the
 1554:                                 lond port; do not send emails, do not
 1555:                                 check if the daemons are running, do not
 1556:                                 generate lon-status
 1557: USAGE
 1558: }
 1559: 
 1560: # ================================================================ Main Program
 1561: sub main () {
 1562:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
 1563: 	$justreload,$justiptables);
 1564:     &GetOptions("help"                 => \$help,
 1565: 		"justcheckdaemons"     => \$justcheckdaemons,
 1566: 		"noemail"              => \$noemail,
 1567: 		"justcheckconnections" => \$justcheckconnections,
 1568: 		"justreload"           => \$justreload,
 1569:                 "justiptables"         => \$justiptables
 1570: 		);
 1571:     if ($help) { &usage(); return; }
 1572: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
 1573:     my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
 1574:     %perlvar=%{$perlvarref};
 1575:     undef $perlvarref;
 1576:     delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
 1577:     delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
 1578:     chdir($perlvar{'lonDaemons'});
 1579: # --------------------------------------- Make sure that LON-CAPA is configured
 1580: # I only test for one thing here (lonHostID).  This is just a safeguard.
 1581:     if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
 1582: 	print("Unconfigured machine.\n");
 1583: 	my $emailto=$perlvar{'lonSysEMail'};
 1584: 	my $hostname = Sys::Hostname::FQDN::fqdn();
 1585: 	$hostname=~s/\.+/./g;
 1586: 	$hostname=~s/\-+/-/g;
 1587: 	$hostname=~s/[^\w\.-]//g; # make sure is safe to pass through shell
 1588: 	my $subj="LON: Unconfigured machine $hostname";
 1589: 	system("echo 'Unconfigured machine $hostname.' |".
 1590:                " mail -s '$subj' $emailto > /dev/null");
 1591: 	exit 1;
 1592:     }
 1593: 
 1594: # ----------------------------- Make sure this process is running from user=www
 1595:     my $wwwid=getpwnam('www');
 1596:     if ($wwwid!=$<) {
 1597: 	print("User ID mismatch. This program must be run as user 'www'.\n");
 1598: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
 1599: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
 1600: 	system("echo 'User ID mismatch. loncron must be run as user www.' |".
 1601:                " mail -s '$subj' $emailto > /dev/null");
 1602: 	exit 1;
 1603:     }
 1604: 
 1605: # -------------------------------------------- Force reload of host information
 1606:     my $nomemcache;
 1607:     if ($justcheckdaemons) {
 1608:         $nomemcache=1;
 1609:         my $memcachepidfile="$perlvar{'lonDaemons'}/logs/memcached.pid";
 1610:         my $memcachepid;
 1611:         if (-e $memcachepidfile) {
 1612:             my $memfh=IO::File->new($memcachepidfile);
 1613:             $memcachepid=<$memfh>;
 1614:             chomp($memcachepid);
 1615:             if ($memcachepid =~ /^\d+$/ && kill 0 => $memcachepid) {
 1616:                 undef($nomemcache);
 1617:             }
 1618:         }
 1619:     }
 1620:     if (!$justiptables) {
 1621:         &Apache::lonnet::load_hosts_tab(1,$nomemcache);
 1622:         &Apache::lonnet::load_domain_tab(1,$nomemcache);
 1623:         &Apache::lonnet::get_iphost(1,$nomemcache);
 1624:     }
 1625: 
 1626: # ----------------------------------------- Force firewall update for lond port
 1627: 
 1628:     if ((!$justcheckdaemons) && (!$justreload)) {
 1629:         my $now = time;
 1630:         my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
 1631:                       $now.$$.int(rand(10000));
 1632:         if (open(my $fh,">$tmpfile")) {
 1633:             my %iphosts = &Apache::lonnet::get_iphost();
 1634:             foreach my $key (keys(%iphosts)) {
 1635:                 print $fh "$key\n";
 1636:             }
 1637:             close($fh);
 1638:             if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
 1639:                 my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
 1640:                 system("$execpath $tmpfile");
 1641:                 unlink('/tmp/lock_lciptables');  # Remove the lock file.
 1642:             }
 1643:             unlink($tmpfile);
 1644:         }
 1645:     }
 1646: 
 1647: # ---------------------------------------------------------------- Start report
 1648: 
 1649:     $errors=0;
 1650:     $warnings=0;
 1651:     $notices=0;
 1652: 
 1653: 
 1654:     my $fh;
 1655:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
 1656: 	$fh=&start_logging();
 1657: 
 1658: 	&log_machine_info($fh);
 1659: 	&clean_tmp($fh);
 1660: 	&clean_lonIDs($fh);
 1661:         &clean_balanceIDs($fh);
 1662:         &clean_webDAV_sessionIDs($fh);
 1663:         &clean_ltiIDs($fh);
 1664: 	&check_httpd_logs($fh);
 1665: 	&rotate_lonnet_logs($fh);
 1666: 	&rotate_other_logs($fh);
 1667:     }
 1668:     if (!$justcheckconnections && !$justreload && !$justiptables) {
 1669: 	&checkon_daemon($fh,'lonmemcached',40000);
 1670: 	&checkon_daemon($fh,'lonsql',200000);
 1671: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
 1672: 	    &checkon_daemon($fh,'lond',40000,'USR2');
 1673: 	}
 1674: 	&checkon_daemon($fh,'lonc',40000,'USR1');
 1675:         &checkon_daemon($fh,'lonmaxima',40000);
 1676:         &checkon_daemon($fh,'lonr',40000);
 1677:     }
 1678:     if ($justreload) {
 1679:         &clean_nosslverify($fh);
 1680:         &write_connection_config();
 1681:         &write_hosttypes();
 1682:         &update_revocation_list(); 
 1683: 	&checkon_daemon($fh,'lond',40000,'USR2');
 1684: 	&checkon_daemon($fh,'lonc',40000,'USR2');
 1685:     }
 1686:     if ($justcheckconnections) {
 1687: 	&test_connections($fh);
 1688:     }
 1689:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
 1690:         my $domconf = &get_domain_config();
 1691:         my ($threshold,$sysmail,$reportstatus,$weightsref,$exclusionsref) =
 1692:             &get_permcount_settings($domconf);
 1693: 	&check_delayed_msg($fh,$weightsref,$exclusionsref);
 1694:         &write_loncaparevs();
 1695:         &write_serverhomeIDs();
 1696: 	&write_checksums();
 1697:         &write_hostips();
 1698:         my %sslrem = &clean_nosslverify($fh);
 1699:         my %conchgs = &write_connection_config();
 1700:         my %hosttypechgs = &write_hosttypes();
 1701:         my $hadcrlchg = &update_revocation_list();
 1702:         if ((keys(%conchgs) > 0) || (keys(%hosttypechgs) > 0) ||
 1703:             $hadcrlchg || (keys(%sslrem) > 0)) {
 1704:             &checkon_daemon($fh,'lond',40000,'USR2');
 1705:             &reset_nosslverify_pids($fh,%sslrem);
 1706:         }
 1707:         &finish_logging($fh,$weightsref);
 1708:         &log_simplestatus();
 1709:         if ($totalcount>$threshold && !$noemail) { &send_mail($sysmail,$reportstatus); }
 1710:     }
 1711: }
 1712: 
 1713: &main();
 1714: 1;
 1715: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>