Annotation of loncom/loncron, revision 1.128

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

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