Annotation of loncom/loncron, revision 1.131

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

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