Annotation of loncom/loncron, revision 1.109

1.1       albertel    1: #!/usr/bin/perl
                      2: 
1.47      albertel    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
                      4: #
1.109   ! raeburn     5: # $Id: loncron,v 1.108 2018/08/08 11:49:48 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.72      albertel   37: use Apache::lonnet;
1.79      raeburn    38: use Apache::loncommon;
1.26      harris41   39: 
1.1       albertel   40: use IO::File;
                     41: use IO::Socket;
1.48      albertel   42: use HTML::Entities;
1.49      albertel   43: use Getopt::Long;
1.104     raeburn    44: use GDBM_File;
                     45: use Storable qw(thaw);
1.46      albertel   46: #globals
                     47: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
                     48: 
                     49: my $statusdir="/home/httpd/html/lon-status";
                     50: 
1.1       albertel   51: 
                     52: # --------------------------------------------------------- Output error status
                     53: 
1.46      albertel   54: sub log {
                     55:     my $fh=shift;
                     56:     if ($fh) {	print $fh @_  }
                     57: }
                     58: 
1.1       albertel   59: sub errout {
                     60:    my $fh=shift;
1.46      albertel   61:    &log($fh,(<<ENDERROUT));
1.48      albertel   62:      <table border="2" bgcolor="#CCCCCC">
1.1       albertel   63:      <tr><td>Notices</td><td>$notices</td></tr>
                     64:      <tr><td>Warnings</td><td>$warnings</td></tr>
                     65:      <tr><td>Errors</td><td>$errors</td></tr>
1.48      albertel   66:      </table><p><a href="#top">Top</a></p>
1.1       albertel   67: ENDERROUT
                     68: }
                     69: 
1.73      albertel   70: sub rotate_logfile {
                     71:     my ($file,$fh,$description) = @_;
                     72:     my $size=(stat($file))[7];
                     73:     if ($size>40000) {
                     74: 	&log($fh,"<p>Rotating $description ...</p>");
                     75: 	rename("$file.2","$file.3");
                     76: 	rename("$file.1","$file.2");
                     77: 	rename("$file","$file.1");
                     78:     } 
                     79: }
                     80: 
1.42      albertel   81: sub start_daemon {
1.50      albertel   82:     my ($fh,$daemon,$pidfile,$args) = @_;
1.44      albertel   83:     my $progname=$daemon;
1.71      albertel   84:     if ($daemon eq 'lonc') {
1.44      albertel   85: 	$progname='loncnew'; 
                     86:     }
1.51      albertel   87:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
1.73      albertel   88:     &rotate_logfile($error_fname,$fh,'error logs');
1.74      albertel   89:     if ($daemon eq 'lonc') {
                     90: 	&clean_sockets($fh);
                     91:     }
1.51      albertel   92:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60      albertel   93:     sleep 1;
1.42      albertel   94:     if (-e $pidfile) {
1.48      albertel   95: 	&log($fh,"<p>Seems like it started ...</p>");
1.42      albertel   96: 	my $lfh=IO::File->new("$pidfile");
                     97: 	my $daemonpid=<$lfh>;
                     98: 	chomp($daemonpid);
1.62      albertel   99: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42      albertel  100: 	    return 1;
                    101: 	} else {
                    102: 	    return 0;
                    103: 	}
                    104:     }
1.48      albertel  105:     &log($fh,"<p>Seems like that did not work!</p>");
1.42      albertel  106:     $errors++;
                    107:     return 0;
                    108: }
                    109: 
                    110: sub checkon_daemon {
1.59      albertel  111:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42      albertel  112: 
1.63      albertel  113:     my $result;
1.48      albertel  114:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57      albertel  115:     printf("%-15s ",$daemon);
1.42      albertel  116:     if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
                    117: 	open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46      albertel  118: 	while (my $line=<DFH>) { 
                    119: 	    &log($fh,"$line");
1.42      albertel  120: 	    if ($line=~/INFO/) { $notices++; }
                    121: 	    if ($line=~/WARNING/) { $notices++; }
                    122: 	    if ($line=~/CRITICAL/) { $warnings++; }
                    123: 	};
                    124: 	close (DFH);
                    125:     }
1.48      albertel  126:     &log($fh,"</tt></p>");
1.42      albertel  127:     
                    128:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
                    129:     
                    130:     my $restartflag=1;
1.46      albertel  131:     my $daemonpid;
1.42      albertel  132:     if (-e $pidfile) {
                    133: 	my $lfh=IO::File->new("$pidfile");
1.46      albertel  134: 	$daemonpid=<$lfh>;
1.42      albertel  135: 	chomp($daemonpid);
1.62      albertel  136: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46      albertel  137: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59      albertel  138: 	    if ($send) { &log($fh,", sending $send"); }
1.46      albertel  139: 	    &log($fh,"</h3>");
1.59      albertel  140: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
                    141: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42      albertel  142: 	    $restartflag=0;
1.59      albertel  143: 	    if ($send eq 'USR2') {
1.63      albertel  144: 		$result = 'reloaded';
1.59      albertel  145: 		print "reloaded\n";
                    146: 	    } else {
1.63      albertel  147: 		$result = 'running';
1.59      albertel  148: 		print "running\n";
                    149: 	    }
1.42      albertel  150: 	} else {
                    151: 	    $errors++;
1.46      albertel  152: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42      albertel  153: 	    $restartflag=1;
1.46      albertel  154: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42      albertel  155: 	}
                    156:     }
                    157:     if ($restartflag==1) {
                    158: 	$simplestatus{$daemon}='off';
                    159: 	$errors++;
1.57      albertel  160: 	my $kadaemon=$daemon;
                    161: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.101     raeburn   162: 	&log($fh,'<br /><font color="red">Killall '.$daemon.': '.
1.57      albertel  163: 	    `killall $kadaemon 2>&1`.' - ');
1.60      albertel  164: 	sleep 1;
1.46      albertel  165: 	&log($fh,unlink($pidfile).' - '.
1.57      albertel  166: 	    `killall -9 $kadaemon 2>&1`.
1.101     raeburn   167: 	    '</font><br />');
1.107     raeburn   168:         if ($kadaemon eq 'loncnew') {
                    169:             &clean_lonc_childpids();
                    170:         }
1.46      albertel  171: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
1.42      albertel  172: 	
1.50      albertel  173: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  174: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  175: 	    $simplestatus{$daemon}='restarted';
1.63      albertel  176: 	    $result = 'started';
1.42      albertel  177: 	    print "started\n";
                    178: 	} else {
                    179: 	    $errors++;
1.46      albertel  180: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  181: 	    &log($fh,"<p>Give it one more try ...</p>");
1.42      albertel  182: 	    print " ";
1.50      albertel  183: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  184: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  185: 		$simplestatus{$daemon}='restarted';
1.63      albertel  186: 		$result = 'started';
1.42      albertel  187: 		print "started\n";
                    188: 	    } else {
1.63      albertel  189: 		$result = 'failed';
1.42      albertel  190: 		print " failed\n";
                    191: 		$simplestatus{$daemon}='failed';
                    192: 		$errors++; $errors++;
1.46      albertel  193: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  194: 		&log($fh,"<p>Unable to start $daemon</p>");
1.42      albertel  195: 	    }
                    196: 	}
                    197: 
                    198: 	if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
1.46      albertel  199: 	    &log($fh,"<p><pre>");
1.42      albertel  200: 	    open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46      albertel  201: 	    while (my $line=<DFH>) { 
                    202: 		&log($fh,"$line");
1.42      albertel  203: 		if ($line=~/WARNING/) { $notices++; }
                    204: 		if ($line=~/CRITICAL/) { $notices++; }
                    205: 	    };
                    206: 	    close (DFH);
1.48      albertel  207: 	    &log($fh,"</pre></p>");
1.42      albertel  208: 	}
                    209:     }
                    210:     
1.46      albertel  211:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.73      albertel  212:     &rotate_logfile($fname,$fh,'logs');
1.42      albertel  213: 
                    214:     &errout($fh);
1.63      albertel  215:     return $result;
1.42      albertel  216: }
1.1       albertel  217: 
1.46      albertel  218: # --------------------------------------------------------------------- Machine
                    219: sub log_machine_info {
                    220:     my ($fh)=@_;
1.48      albertel  221:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46      albertel  222:     &log($fh,"<h3>loadavg</h3>");
                    223: 	
                    224:     open (LOADAVGH,"/proc/loadavg");
                    225:     my $loadavg=<LOADAVGH>;
                    226:     close (LOADAVGH);
                    227:     
                    228:     &log($fh,"<tt>$loadavg</tt>");
                    229:     
                    230:     my @parts=split(/\s+/,$loadavg);
                    231:     if ($parts[1]>4.0) {
                    232: 	$errors++;
                    233:     } elsif ($parts[1]>2.0) {
                    234: 	$warnings++;
                    235:     } elsif ($parts[1]>1.0) {
                    236: 	$notices++;
                    237:     }
1.13      harris41  238: 
1.46      albertel  239:     &log($fh,"<h3>df</h3>");
                    240:     &log($fh,"<pre>");
1.14      harris41  241: 
1.46      albertel  242:     open (DFH,"df|");
                    243:     while (my $line=<DFH>) { 
1.48      albertel  244: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  245: 	@parts=split(/\s+/,$line);
                    246: 	my $usage=$parts[4];
                    247: 	$usage=~s/\W//g;
                    248: 	if ($usage>90) { 
                    249: 	    $warnings++;
                    250: 	    $notices++; 
                    251: 	} elsif ($usage>80) {
                    252: 	    $warnings++;
                    253: 	} elsif ($usage>60) {
                    254: 	    $notices++;
1.31      albertel  255: 	}
1.46      albertel  256: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1       albertel  257:     }
1.46      albertel  258:     close (DFH);
                    259:     &log($fh,"</pre>");
1.1       albertel  260: 
                    261: 
1.46      albertel  262:     &log($fh,"<h3>ps</h3>");
                    263:     &log($fh,"<pre>");
                    264:     my $psproc=0;
1.1       albertel  265: 
1.53      albertel  266:     open (PSH,"ps aux --cols 140 |");
1.46      albertel  267:     while (my $line=<PSH>) { 
1.48      albertel  268: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  269: 	$psproc++;
                    270:     }
                    271:     close (PSH);
                    272:     &log($fh,"</pre>");
1.1       albertel  273: 
1.46      albertel  274:     if ($psproc>200) { $notices++; }
                    275:     if ($psproc>250) { $notices++; }
1.1       albertel  276: 
1.61      albertel  277:     &log($fh,"<h3>distprobe</h3>");
                    278:     &log($fh,"<pre>");
1.98      raeburn   279:     &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
1.61      albertel  280:     &log($fh,"</pre>");
                    281: 
1.46      albertel  282:     &errout($fh);
                    283: }
1.1       albertel  284: 
1.46      albertel  285: sub start_logging {
1.43      albertel  286:     my $fh=IO::File->new(">$statusdir/newstatus.html");
                    287:     my %simplestatus=();
1.46      albertel  288:     my $now=time;
                    289:     my $date=localtime($now);
1.43      albertel  290:     
1.46      albertel  291: 
                    292:     &log($fh,(<<ENDHEADERS));
1.101     raeburn   293: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    294: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1.1       albertel  295: <head>
                    296: <title>LON Status Report $perlvar{'lonHostID'}</title>
1.101     raeburn   297: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1.1       albertel  298: </head>
1.3       www       299: <body bgcolor="#AAAAAA">
1.48      albertel  300: <a name="top" />
1.1       albertel  301: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
                    302: <h2>$date ($now)</h2>
                    303: <ol>
1.48      albertel  304: <li><a href="#configuration">Configuration</a></li>
                    305: <li><a href="#machine">Machine Information</a></li>
                    306: <li><a href="#tmp">Temporary Files</a></li>
                    307: <li><a href="#tokens">Session Tokens</a></li>
1.102     raeburn   308: <li><a href="#webdav">WebDAV Session Tokens</a></li>
1.48      albertel  309: <li><a href="#httpd">httpd</a></li>
                    310: <li><a href="#lonsql">lonsql</a></li>
                    311: <li><a href="#lond">lond</a></li>
                    312: <li><a href="#lonc">lonc</a></li>
                    313: <li><a href="#lonnet">lonnet</a></li>
                    314: <li><a href="#connections">Connections</a></li>
                    315: <li><a href="#delayed">Delayed Messages</a></li>
                    316: <li><a href="#errcount">Error Count</a></li>
1.1       albertel  317: </ol>
1.48      albertel  318: <hr />
                    319: <a name="configuration" />
1.1       albertel  320: <h2>Configuration</h2>
                    321: <h3>PerlVars</h3>
1.48      albertel  322: <table border="2">
1.1       albertel  323: ENDHEADERS
                    324: 
1.46      albertel  325:     foreach my $varname (sort(keys(%perlvar))) {
1.48      albertel  326: 	&log($fh,"<tr><td>$varname</td><td>".
                    327: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43      albertel  328:     }
1.48      albertel  329:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72      albertel  330:     my %hostname = &Apache::lonnet::all_hostnames();
                    331:     foreach my $id (sort(keys(%hostname))) {
                    332: 	my $role = (&Apache::lonnet::is_library($id) ? 'library'
                    333: 		                                     : 'access');
1.46      albertel  334: 	&log($fh,
1.72      albertel  335: 	    "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
                    336: 	    "</td><td>".$role.
                    337: 	    "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
                    338:     }
1.101     raeburn   339:     &log($fh,"</table><h3>Spare Hosts</h3>");
                    340:     if (keys(%Apache::lonnet::spareid) > 0) {
                    341:         &log($fh,"<ul>");
                    342:         foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
                    343: 	    &log($fh,"<li>$type\n<ol>");
                    344: 	    foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
                    345: 	        &log($fh,"<li>$id</li>\n");
                    346: 	    }
                    347: 	    &log($fh,"</ol>\n</li>\n");
                    348:         }
                    349:         &log($fh,"</ul>\n");
                    350:     } else {
                    351:         &log($fh,"No spare hosts specified<br />\n");
1.43      albertel  352:     }
1.46      albertel  353:     return $fh;
                    354: }
1.11      www       355: 
                    356: # --------------------------------------------------------------- clean out tmp
1.46      albertel  357: sub clean_tmp {
                    358:     my ($fh)=@_;
1.48      albertel  359:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.82      raeburn   360:     my ($cleaned,$old,$removed) = (0,0,0);
                    361:     my %errors = (
                    362:                      dir       => [],
                    363:                      file      => [],
                    364:                      failopen  => [],
                    365:                  );
                    366:     my %error_titles = (
                    367:                          dir       => 'failed to remove empty directory:',
                    368:                          file      => 'failed to unlike stale file',
                    369:                          failopen  => 'failed to open file or directory'
                    370:                        );
                    371:     ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
                    372:     &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
                    373:     foreach my $key (sort(keys(%errors))) {
                    374:         if (ref($errors{$key}) eq 'ARRAY') {
                    375:             if (@{$errors{$key}} > 0) {
                    376:                 &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
                    377:                      join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
                    378:             }
                    379:         }
                    380:     }
                    381: }
                    382: 
                    383: sub recursive_clean_tmp {
                    384:     my ($subdir,$cleaned,$old,$removed,$errors) = @_;
                    385:     my $base = "$perlvar{'lonDaemons'}/tmp";
                    386:     my $path = $base;
                    387:     next if ($subdir =~ m{\.\./});
                    388:     next unless (ref($errors) eq 'HASH');
                    389:     unless ($subdir eq '') {
                    390:         $path .= '/'.$subdir;
                    391:     }
                    392:     if (opendir(my $dh,"$path")) {
                    393:         while (my $file = readdir($dh)) {
                    394:             next if ($file =~ /^\.\.?$/);
                    395:             my $fname = "$path/$file";
                    396:             if (-d $fname) {
                    397:                 my $innerdir;
                    398:                 if ($subdir eq '') {
                    399:                     $innerdir = $file;
                    400:                 } else {
                    401:                     $innerdir = $subdir.'/'.$file;
                    402:                 }
                    403:                 ($cleaned,$old,$removed) = 
                    404:                      &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
                    405:                 my @doms = &Apache::lonnet::current_machine_domains();
                    406:                 
                    407:                 if (open(my $dirhandle,$fname)) {
                    408:                     unless (($innerdir eq 'helprequests') ||
                    409:                             (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
                    410:                         my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
                    411:                                       join('&&',@contents)."\n";    
                    412:                         if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
                    413:                             closedir($dirhandle);
                    414:                             if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
                    415:                                 if (rmdir($fname)) {
                    416:                                     $removed ++;
                    417:                                 } elsif (ref($errors->{dir}) eq 'ARRAY') {
                    418:                                     push(@{$errors->{dir}},$fname);
                    419:                                 }
                    420:                             }
                    421:                         }
                    422:                     } else {
                    423:                         closedir($dirhandle);
                    424:                     }
                    425:                 }
                    426:             } else {
                    427:                 my ($dev,$ino,$mode,$nlink,
                    428:                     $uid,$gid,$rdev,$size,
                    429:                     $atime,$mtime,$ctime,
                    430:                     $blksize,$blocks)=stat($fname);
                    431:                 my $now=time;
                    432:                 my $since=$now-$mtime;
                    433:                 if ($since>$perlvar{'lonExpire'}) {
                    434:                     if ($subdir eq '') {
                    435:                         my $line='';
                    436:                         if ($fname =~ /\.db$/) {
                    437:                             if (unlink($fname)) {
                    438:                                 $cleaned++;
                    439:                             } elsif (ref($errors->{file}) eq 'ARRAY') {
                    440:                                 push(@{$errors->{file}},$fname);
                    441:                             }
                    442:                         } elsif (open(PROBE,$fname)) {
                    443:                             my $line='';
                    444:                             $line=<PROBE>;
                    445:                             close(PROBE);
                    446:                             if ($line=~/^CHECKOUTTOKEN\&/) {
                    447:                                 if ($since>365*$perlvar{'lonExpire'}) {
                    448:                                     if (unlink($fname)) {
                    449:                                         $cleaned++; 
                    450:                                     } elsif (ref($errors->{file}) eq 'ARRAY') {
                    451:                                         push(@{$errors->{file}},$fname);
                    452:                                     }
                    453:                                 } else {
                    454:                                     $old++;
                    455:                                 }
                    456:                             } else {
                    457:                                 if (unlink($fname)) {
                    458:                                     $cleaned++;
                    459:                                 } elsif (ref($errors->{file}) eq 'ARRAY') {
                    460:                                     push(@{$errors->{file}},$fname);
                    461:                                 }
                    462:                             }
                    463:                         } elsif (ref($errors->{failopen}) eq 'ARRAY') {
                    464:                             push(@{$errors->{failopen}},$fname); 
                    465:                         }
                    466:                     } else {
                    467:                         if (unlink($fname)) {
                    468:                             $cleaned++;
                    469:                         } elsif (ref($errors->{file}) eq 'ARRAY') {
                    470:                             push(@{$errors->{file}},$fname);
                    471:                         }
                    472:                     }
                    473:                 }
                    474:             }
                    475:         }
                    476:         closedir($dh);
                    477:     } elsif (ref($errors->{failopen}) eq 'ARRAY') {
                    478:         push(@{$errors->{failopen}},$path);
1.43      albertel  479:     }
1.82      raeburn   480:     return ($cleaned,$old,$removed);
1.46      albertel  481: }
1.11      www       482: 
                    483: # ------------------------------------------------------------ clean out lonIDs
1.46      albertel  484: sub clean_lonIDs {
                    485:     my ($fh)=@_;
1.48      albertel  486:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46      albertel  487:     my $cleaned=0;
                    488:     my $active=0;
                    489:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.43      albertel  490: 	my ($dev,$ino,$mode,$nlink,
                    491: 	    $uid,$gid,$rdev,$size,
                    492: 	    $atime,$mtime,$ctime,
                    493: 	    $blksize,$blocks)=stat($fname);
1.46      albertel  494: 	my $now=time;
                    495: 	my $since=$now-$mtime;
1.43      albertel  496: 	if ($since>$perlvar{'lonExpire'}) {
                    497: 	    $cleaned++;
1.101     raeburn   498: 	    &log($fh,"Unlinking $fname<br />");
1.43      albertel  499: 	    unlink("$fname");
                    500: 	} else {
                    501: 	    $active++;
                    502: 	}
1.46      albertel  503:     }
1.48      albertel  504:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46      albertel  505:     &log($fh,"<h3>$active open session(s)</h3>");
                    506: }
1.43      albertel  507: 
1.102     raeburn   508: # ------------------------------------------------ clean out webDAV Session IDs
                    509: sub clean_webDAV_sessionIDs {
                    510:     my ($fh)=@_;
                    511:     if ($perlvar{'lonRole'} eq 'library') {
                    512:         &log($fh,'<hr /><a name="webdav" /><h2>WebDAV Session Tokens</h2>');
                    513:         my $cleaned=0;
                    514:         my $active=0;
                    515:         my $now = time;
                    516:         if (-d $perlvar{'lonDAVsessDir'}) {
                    517:             while (my $fname=<$perlvar{'lonDAVsessDir'}/*>) {
                    518:                 my @stats = stat($fname);
                    519:                 my $since=$now-$stats[9];
                    520:                 if ($since>$perlvar{'lonExpire'}) {
                    521:                     $cleaned++;
                    522:                     &log($fh,"Unlinking $fname<br />");
                    523:                     unlink("$fname");
                    524:                 } else {
                    525:                     $active++;
                    526:                 }
                    527:             }
                    528:             &log($fh,"<p>Cleaned up ".$cleaned." stale webDAV session token(s).</p>");
                    529:             &log($fh,"<h3>$active open webDAV session(s)</h3>");
                    530:         }
                    531:     }
                    532: }
                    533: 
1.74      albertel  534: # ----------------------------------------------------------- clean out sockets
                    535: sub clean_sockets {
                    536:     my ($fh)=@_;
                    537:     my $cleaned=0;
                    538:     opendir(SOCKETS,$perlvar{'lonSockDir'});
                    539:     while (my $fname=readdir(SOCKETS)) {
                    540: 	next if (-d $fname 
1.80      www       541: 		 || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
1.74      albertel  542: 	$cleaned++;
                    543: 	&log($fh,"Unlinking $fname<br />");
                    544: 	unlink("/home/httpd/sockets/$fname");
                    545:     }
                    546:     &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
                    547: }
                    548: 
1.11      www       549: 
1.1       albertel  550: # ----------------------------------------------------------------------- httpd
1.46      albertel  551: sub check_httpd_logs {
                    552:     my ($fh)=@_;
1.94      raeburn   553:     if (open(PIPE,"./lchttpdlogs|")) {
1.93      raeburn   554:         while (my $line=<PIPE>) {
                    555:             &log($fh,$line);
                    556:             if ($line=~/\[error\]/) { $notices++; }
                    557:         }
                    558:         close(PIPE);
1.46      albertel  559:     }
1.43      albertel  560:     &errout($fh);
1.46      albertel  561: }
1.1       albertel  562: 
                    563: # ---------------------------------------------------------------------- lonnet
                    564: 
1.48      albertel  565: sub rotate_lonnet_logs {
1.46      albertel  566:     my ($fh)=@_;
1.48      albertel  567:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.100     bisitz    568:     print "Checking logs.\n";
1.43      albertel  569:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
                    570: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.46      albertel  571: 	while (my $line=<DFH>) { 
1.48      albertel  572: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  573: 	}
1.43      albertel  574: 	close (DFH);
                    575:     }
1.46      albertel  576:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.43      albertel  577:     
                    578:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    579: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.46      albertel  580: 	while (my $line=<DFH>) { 
1.48      albertel  581: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  582: 	}
1.43      albertel  583: 	close (DFH);
1.46      albertel  584:     } else { &log($fh,"No perm log\n") }
1.43      albertel  585: 
1.46      albertel  586:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73      albertel  587:     &rotate_logfile($fname,$fh,'lonnet log');
1.1       albertel  588: 
1.46      albertel  589:     &log($fh,"</pre>");
1.43      albertel  590:     &errout($fh);
1.46      albertel  591: }
                    592: 
1.73      albertel  593: sub rotate_other_logs {
                    594:     my ($fh) = @_;
1.83      raeburn   595:     my %logs = (
                    596:                   autoenroll          => 'Auto Enroll log',
                    597:                   autocreate          => 'Create Course log',
                    598:                   searchcat           => 'Search Cataloguing log',
                    599:                   autoupdate          => 'Auto Update log',
                    600:                   refreshcourseids_db => 'Refresh CourseIDs db log',
                    601:                );
                    602:     foreach my $item (keys(%logs)) {
                    603:         my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
                    604:         &rotate_logfile($fname,$fh,$logs{$item});
                    605:     }
1.73      albertel  606: }
                    607: 
1.43      albertel  608: # ----------------------------------------------------------------- Connections
1.46      albertel  609: sub test_connections {
1.72      albertel  610:     my ($fh)=@_;
1.48      albertel  611:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.100     bisitz    612:     print "Testing connections.\n";
1.48      albertel  613:     &log($fh,"<table border='2'>");
1.49      albertel  614:     my ($good,$bad)=(0,0);
1.72      albertel  615:     my %hostname = &Apache::lonnet::all_hostnames();
                    616:     foreach my $tryserver (sort(keys(%hostname))) {
1.43      albertel  617: 	print(".");
1.46      albertel  618: 	my $result;
1.72      albertel  619: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43      albertel  620: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
                    621: 	    $result="<b>ok</b>";
1.49      albertel  622: 	    $good++;
1.43      albertel  623: 	} else {
                    624: 	    $result=$answer;
                    625: 	    $warnings++;
1.49      albertel  626: 	    if ($answer eq 'con_lost') {
                    627: 		$bad++;
                    628: 		$warnings++;
1.50      albertel  629: 	    } else {
                    630: 		$good++; #self connection
1.49      albertel  631: 	    }
1.43      albertel  632: 	}
                    633: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46      albertel  634: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1       albertel  635:     }
1.46      albertel  636:     &log($fh,"</table>");
1.49      albertel  637:     print "\n$good good, $bad bad connections\n";
1.43      albertel  638:     &errout($fh);
1.46      albertel  639: }
                    640: 
                    641: 
1.1       albertel  642: # ------------------------------------------------------------ Delayed messages
1.46      albertel  643: sub check_delayed_msg {
1.72      albertel  644:     my ($fh)=@_;
1.48      albertel  645:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.100     bisitz    646:     print "Checking buffers.\n";
1.46      albertel  647:     
                    648:     &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1       albertel  649: 
1.46      albertel  650:     my $unsend=0;
1.1       albertel  651: 
1.105     raeburn   652:     my %hostname = &Apache::lonnet::all_hostnames();
                    653:     my $numhosts = scalar(keys(%hostname));
                    654: 
1.46      albertel  655:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
                    656:     while (my $line=<$dfh>) {
                    657: 	my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
1.105     raeburn   658:         if ($numhosts) {
                    659:             next unless ($hostname{$dserv});
                    660:         }
1.46      albertel  661: 	if ($sdf eq 'F') { 
                    662: 	    my $local=localtime($time);
1.101     raeburn   663: 	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
1.46      albertel  664: 	    $warnings++;
1.43      albertel  665: 	}
1.46      albertel  666: 	if ($sdf eq 'S') { $unsend--; }
                    667: 	if ($sdf eq 'D') { $unsend++; }
1.1       albertel  668:     }
1.46      albertel  669: 
1.48      albertel  670:     &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.95      raeburn   671:     if ($unsend > 0) {
                    672:         $warnings=$warnings+5*$unsend;
                    673:     }
1.1       albertel  674: 
1.43      albertel  675:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48      albertel  676:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68      www       677: # list directory with delayed messages and remember offline servers
                    678:     my %servers=();
1.43      albertel  679:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68      www       680:     while (my $line=<DFH>) {
                    681:         my ($server)=($line=~/\.(\w+)$/);
                    682:         if ($server) { $servers{$server}=1; }
1.48      albertel  683: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  684:     }
1.48      albertel  685:     &log($fh,"</pre>\n");
1.43      albertel  686:     close (DFH);
1.68      www       687: # pong to all servers that have delayed messages
                    688: # this will trigger a reverse connection, which should flush the buffers
1.95      raeburn   689:     foreach my $tryserver (sort(keys(%servers))) {
                    690:         if ($hostname{$tryserver} || !$numhosts) {
                    691:             my $answer;
                    692:             eval {
                    693:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    694:                 alarm(20);
                    695:                 $answer = &Apache::lonnet::reply("pong",$tryserver);
                    696:                 alarm(0);
                    697:             };
                    698:             if ($@ && $@ =~ m/TIMEOUT/) {
                    699:                 &log($fh,"Attempted pong to $tryserver timed out<br />");
1.100     bisitz    700:                 print "Time out while contacting: $tryserver for pong.\n";
1.95      raeburn   701:             } else {
                    702:                 &log($fh,"Pong to $tryserver: $answer<br />");
                    703:             }
1.91      raeburn   704:         } else {
1.95      raeburn   705:             &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
1.91      raeburn   706:         }
1.68      www       707:     }
1.46      albertel  708: }
1.1       albertel  709: 
1.46      albertel  710: sub finish_logging {
                    711:     my ($fh)=@_;
1.48      albertel  712:     &log($fh,"<a name='errcount' />\n");
1.43      albertel  713:     $totalcount=$notices+4*$warnings+100*$errors;
                    714:     &errout($fh);
1.46      albertel  715:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
                    716:     my $now=time;
                    717:     my $date=localtime($now);
1.48      albertel  718:     &log($fh,"<hr />$date ($now)</body></html>\n");
1.100     bisitz    719:     print "lon-status webpage updated.\n";
1.43      albertel  720:     $fh->close();
1.46      albertel  721: 
                    722:     if ($errors) { $simplestatus{'errors'}=$errors; }
                    723:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
                    724:     if ($notices) { $simplestatus{'notices'}=$notices; }
                    725:     $simplestatus{'time'}=time;
1.1       albertel  726: }
                    727: 
1.46      albertel  728: sub log_simplestatus {
1.73      albertel  729:     rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46      albertel  730:     
1.43      albertel  731:     my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
                    732:     foreach (keys %simplestatus) {
                    733: 	print $sfh $_.'='.$simplestatus{$_}.'&';
                    734:     }
                    735:     print $sfh "\n";
                    736:     $sfh->close();
1.41      www       737: }
1.46      albertel  738: 
1.84      raeburn   739: sub write_loncaparevs {
1.100     bisitz    740:     print "Retrieving LON-CAPA version information.\n";
1.99      raeburn   741:     my %hostname = &Apache::lonnet::all_hostnames();
                    742:     my $output;
                    743:     foreach my $id (sort(keys(%hostname))) {
                    744:         if ($id ne '') {
                    745:             my $loncaparev;
                    746:             eval {
                    747:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    748:                 alarm(10);
                    749:                 $loncaparev =
                    750:                     &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
                    751:                 alarm(0);
                    752:             };
                    753:             if ($@ && $@ =~ m/TIMEOUT/) {
1.100     bisitz    754:                 print "Time out while contacting lonHost: $id for version.\n";   
1.99      raeburn   755:             }
                    756:             if ($loncaparev =~ /^[\w.\-]+$/) {
                    757:                 $output .= $id.':'.$loncaparev."\n";
                    758:             }
                    759:         }
                    760:     }
                    761:     if ($output) {
                    762:         if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                    763:             print $fh $output;
                    764:             close($fh);
                    765:             &Apache::lonnet::load_loncaparevs();
                    766:         }
                    767:     }
                    768:     return;
                    769: }
                    770: 
                    771: sub write_serverhomeIDs {
1.100     bisitz    772:     print "Retrieving LON-CAPA lonHostID information.\n";
1.99      raeburn   773:     my %name_to_host = &Apache::lonnet::all_names();
                    774:     my $output;
                    775:     foreach my $name (sort(keys(%name_to_host))) {
                    776:         if ($name ne '') {
                    777:             if (ref($name_to_host{$name}) eq 'ARRAY') {
                    778:                 my $serverhomeID;
1.90      raeburn   779:                 eval {
                    780:                     local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    781:                     alarm(10);
1.99      raeburn   782:                     $serverhomeID = 
                    783:                         &Apache::lonnet::get_server_homeID($name,1,'loncron');
1.90      raeburn   784:                     alarm(0);
                    785:                 };
                    786:                 if ($@ && $@ =~ m/TIMEOUT/) {
1.99      raeburn   787:                     print "Time out while contacting server: $name\n"; 
1.90      raeburn   788:                 }
1.99      raeburn   789:                 if ($serverhomeID ne '') {
                    790:                     $output .= $name.':'.$serverhomeID."\n";
                    791:                 } else {
                    792:                     $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1.84      raeburn   793:                 }
                    794:             }
                    795:         }
                    796:     }
1.99      raeburn   797:     if ($output) {
                    798:         if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                    799:             print $fh $output;
                    800:             close($fh);
                    801:             &Apache::lonnet::load_serverhomeIDs();
1.85      raeburn   802:         }
                    803:     }
                    804:     return;
                    805: }
                    806: 
1.96      raeburn   807: sub write_checksums {
1.98      raeburn   808:     my $distro = &LONCAPA::distro();
1.96      raeburn   809:     if ($distro) {
                    810:         print "Retrieving file version and checksumming.\n";
1.97      raeburn   811:         my $numchksums = 0;
1.96      raeburn   812:         my ($chksumsref,$versionsref) =
1.97      raeburn   813:             &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
                    814:                                                   $perlvar{'lonLib'},
                    815:                                                   $perlvar{'lonIncludes'},
                    816:                                                   $perlvar{'lonTabDir'});
1.96      raeburn   817:         if (ref($chksumsref) eq 'HASH') {
                    818:             $numchksums = scalar(keys(%{$chksumsref}));
                    819:         }
                    820:         print "File version retrieved and checksumming completed for $numchksums files.\n";
                    821:     } else {
                    822:         print "File version retrieval and checksumming skipped - could not determine Linux distro.\n"; 
                    823:     }
1.97      raeburn   824:     return;
1.96      raeburn   825: }
                    826: 
1.107     raeburn   827: sub clean_nosslverify {
                    828:     my ($fh) = @_;
                    829:     my %unlinked; 
                    830:     if (-d "$perlvar{'lonSockDir'}/nosslverify") {
                    831:         if (opendir(my $dh,"$perlvar{'lonSockDir'}/nosslverify")) {
                    832:             while (my $fname=readdir($dh)) {
                    833:                 next if ($fname =~ /^\.+$/);
                    834:                 if (unlink("/home/httpd/sockets/nosslverify/$fname")) {
                    835:                     &log($fh,"Unlinking $fname<br />");
                    836:                     $unlinked{$fname} = 1;
                    837:                 }
                    838:             }
                    839:             closedir($dh);
                    840:         }
                    841:     }
                    842:     &log($fh,"<p>Removed ".scalar(keys(%unlinked))." nosslverify clients</p>");
                    843:     return %unlinked;
                    844: }
                    845: sub clean_lonc_childpids {
                    846:     my $childpiddir = "$perlvar{'lonDocRoot'}/lon-status/loncchld";
                    847:     if (-d $childpiddir) {
                    848:         if (opendir(my $dh,$childpiddir)) {
                    849:             while (my $fname=readdir($dh)) {
                    850:                 next if ($fname =~ /^\.+$/);
                    851:                 unlink("$childpiddir/$fname");
                    852:             }
                    853:             closedir($dh);
                    854:         }
                    855:     }
                    856: }
                    857: 
1.104     raeburn   858: sub write_connection_config {
1.107     raeburn   859:     my ($isprimary,$domconf,$url,%connectssl,%changes);
1.104     raeburn   860:     my $primaryLibServer = &Apache::lonnet::domain($perlvar{'lonDefDomain'},'primary');
                    861:     if ($primaryLibServer eq $perlvar{'lonHostID'}) {
                    862:         $isprimary = 1;
                    863:     } elsif ($primaryLibServer ne '') {
                    864:         my $protocol = $Apache::lonnet::protocol{$primaryLibServer};
                    865:         my $hostname = &Apache::lonnet::hostname($primaryLibServer);
                    866:         unless ($protocol eq 'https') {
                    867:             $protocol = 'http';
                    868:         }
                    869:         $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl';
                    870:     }
                    871:     my $domconf = &get_domain_config($perlvar{'lonDefDomain'},$primaryLibServer,$isprimary,
                    872:                                      $url);
                    873:     if (ref($domconf) eq 'HASH') {
                    874:         if (ref($domconf->{'ssl'}) eq 'HASH') {
                    875:             foreach my $connect ('connto','connfrom') {
                    876:                 if (ref($domconf->{'ssl'}->{$connect}) eq 'HASH') {
                    877:                     my ($sslreq,$sslnoreq,$currsetting);
                    878:                     my %contypes;
                    879:                     foreach my $type ('dom','intdom','other') {
                    880:                         $connectssl{$connect.'_'.$type} = $domconf->{'ssl'}->{$connect}->{$type};
                    881:                     }
                    882:                 }
                    883:             }
                    884:         }
                    885:         if (keys(%connectssl)) {
1.107     raeburn   886:             my %currconf; 
                    887:             if (open(my $fh,'<',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
                    888:                 while (my $line = <$fh>) {
                    889:                     chomp($line);
                    890:                     my ($name,$value) = split(/=/,$line);
                    891:                     if ($value =~ /^(?:no|yes|req)$/) {
                    892:                         if ($name =~ /^conn(to|from)_(dom|intdom|other)$/) {
                    893:                             $currconf{$name} = $value;
                    894:                         }
                    895:                     }
                    896:                 }
                    897:                 close($fh);
                    898:             }
                    899:             if (open(my $fh,'>',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
1.104     raeburn   900:                 my $count = 0;
                    901:                 foreach my $key (sort(keys(%connectssl))) { 
                    902:                     print $fh "$key=$connectssl{$key}\n";
1.107     raeburn   903:                     if (exists($currconf{$key})) {
                    904:                         unless ($currconf{$key} eq $connectssl{$key}) {
                    905:                             $changes{$key} = 1;
                    906:                         }
                    907:                     } else {
                    908:                         $changes{$key} = 1;
                    909:                     }
1.104     raeburn   910:                     $count ++;
                    911:                 }
                    912:                 close($fh);
                    913:                 print "Completed writing SSL options for lonc/lond for $count items.\n";
                    914:             }
                    915:         } else {
                    916:             print "Writing of SSL options skipped - no connection rules in domain configuration.\n";
                    917:         }
                    918:     } else {
                    919:         print "Retrieval of SSL options for lonc/lond skipped - no configuration data available for domain.\n";
                    920:     }
1.107     raeburn   921:     return %changes;
1.104     raeburn   922: }
                    923: 
                    924: sub get_domain_config {
                    925:     my ($dom,$primlibserv,$isprimary,$url) = @_;
                    926:     my %confhash;
                    927:     if ($isprimary) {
                    928:         my $lonusersdir = $perlvar{'lonUsersDir'};
                    929:         my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
                    930:         if (-e $fname) {
                    931:             my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
                    932:             if (ref($dbref) eq 'HASH') {
                    933:                 foreach my $key (sort(keys(%{$dbref}))) {
                    934:                     my $value = $dbref->{$key};
                    935:                     if ($value =~ s/^__FROZEN__//) {
                    936:                         $value = thaw(&LONCAPA::unescape($value));
                    937:                     } else {
                    938:                         $value = &LONCAPA::unescape($value);
                    939:                     }
                    940:                     $confhash{$key} = $value;
                    941:                 }
                    942:                 &LONCAPA::locking_hash_untie($dbref);
                    943:             }
                    944:         }
                    945:     } else {
                    946:         if (open(PIPE,"wget --no-check-certificate '$url?primary=$primlibserv&format=raw' |")) {
                    947:             my $config = '';
                    948:             while (<PIPE>) {
                    949:                 $config .= $_;
                    950:             }
                    951:             close(PIPE);
                    952:             if ($config) {
                    953:                 my @pairs=split(/\&/,$config);
                    954:                 foreach my $item (@pairs) {
                    955:                     my ($key,$value)=split(/=/,$item,2);
                    956:                     my $what = &LONCAPA::unescape($key);
                    957:                     if ($value =~ s/^__FROZEN__//) {
                    958:                         $value = thaw(&LONCAPA::unescape($value));
                    959:                     } else {
                    960:                         $value = &LONCAPA::unescape($value);
                    961:                     }
                    962:                     $confhash{$what}=$value;
                    963:                 }
                    964:             }
                    965:         }
                    966:     }
                    967:     return \%confhash;
                    968: }
                    969: 
                    970: sub write_hosttypes {
                    971:     my %intdom = &Apache::lonnet::all_host_intdom();
                    972:     my %hostdom = &Apache::lonnet::all_host_domain();
                    973:     my $dom = $hostdom{$perlvar{'lonHostID'}};
                    974:     my $internetdom = $intdom{$perlvar{'lonHostID'}};
1.107     raeburn   975:     my %changes;
1.104     raeburn   976:     if (($dom ne '') && ($internetdom ne '')) {
                    977:         if (keys(%hostdom)) {
1.107     raeburn   978:             my %currhosttypes;
                    979:             if (open(my $fh,'<',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
                    980:                 while (my $line = <$fh>) {
                    981:                     chomp($line);
                    982:                     my ($name,$value) = split(/:/,$line);
                    983:                     if (($name ne '') && ($value =~ /^(dom|intdom|other)$/)) {
                    984:                         $currhosttypes{$name} = $value;
                    985:                     }
                    986:                 }
                    987:                 close($fh);
                    988:             }
                    989:             if (open(my $fh,'>',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
1.104     raeburn   990:                 my $count = 0;
                    991:                 foreach my $lonid (sort(keys(%hostdom))) {
                    992:                     my $type = 'other';
                    993:                     if ($hostdom{$lonid} eq $dom) {
                    994:                         $type = 'dom'; 
                    995:                     } elsif ($intdom{$lonid} eq $internetdom) {
                    996:                         $type = 'intdom';
                    997:                     }
                    998:                     print $fh "$lonid:$type\n";
1.107     raeburn   999:                     if (exists($currhosttypes{$lonid})) {
                   1000:                         if ($type ne $currhosttypes{$lonid}) {
                   1001:                             $changes{$lonid} = 1;
                   1002:                         }
                   1003:                     } else {
                   1004:                         $changes{$lonid} = 1;
                   1005:                     }
1.104     raeburn  1006:                     $count ++;
                   1007:                 }
                   1008:                 close($fh);
                   1009:                 print "Completed writing host type data for $count hosts.\n";
                   1010:             }
                   1011:         } else {
                   1012:             print "Writing of host types skipped - no hosts found.\n";
                   1013:         }
                   1014:     } else {
                   1015:         print "Writing of host types skipped - could not determine this host's LON-CAPA domain or 'internet' domain.\n";
                   1016:     }
1.107     raeburn  1017:     return %changes;
1.104     raeburn  1018: }
                   1019: 
1.106     raeburn  1020: sub update_revocation_list {
1.107     raeburn  1021:     my ($result,$changed) = &Apache::lonnet::fetch_crl_pemfile();
                   1022:     if ($result eq 'ok') {
1.106     raeburn  1023:         print "Certificate Revocation List (from CA) updated.\n";
                   1024:     } else {
                   1025:         print "Certificate Revocation List from (CA) not updated.\n";
                   1026:     }
1.107     raeburn  1027:     return $changed;
                   1028: }
                   1029: 
                   1030: sub reset_nosslverify_pids {
1.108     raeburn  1031:     my ($fh,%sslrem) = @_;
1.107     raeburn  1032:     &checkon_daemon($fh,'lond',40000,'USR2');
                   1033:     my $loncpidfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
                   1034:     my $loncppid;
                   1035:     if ((-e $loncpidfile) && (open(my $pfh,'<',$loncpidfile))) {
                   1036:         $loncppid=<$pfh>;
                   1037:         chomp($loncppid);
                   1038:         close($pfh);
                   1039:         if ($loncppid =~ /^\d+$/) {
                   1040:             my %pids_by_host;
                   1041:             my $docdir = $perlvar{'lonDocRoot'};
                   1042:             if (-d "$docdir/lon-status/loncchld") {
                   1043:                 if (opendir(my $dh,"$docdir/lon-status/loncchld")) {
                   1044:                     while (my $file = readdir($dh)) {
                   1045:                         next if ($file =~ /^\./);
                   1046:                         if (open(my $fh,'<',"$docdir/lon-status/loncchld/$file")) {
                   1047:                             my $record = <$fh>;
                   1048:                             chomp($record);
                   1049:                             close($fh);
                   1050:                             my ($remotehost,$authmode) = split(/:/,$record);
                   1051:                             $pids_by_host{$remotehost}{$authmode}{$file} = 1;
                   1052:                         }
                   1053:                     }
                   1054:                     closedir($dh);
                   1055:                     if (keys(%pids_by_host)) {
                   1056:                         foreach my $host (keys(%pids_by_host)) {
                   1057:                             if ($sslrem{$host}) {
                   1058:                                 if (ref($pids_by_host{$host}) eq 'HASH') {
                   1059:                                     if (ref($pids_by_host{$host}{'insecure'}) eq 'HASH') {
1.109   ! raeburn  1060:                                         if (keys(%{$pids_by_host{$host}{'insecure'}})) {
        !          1061:                                             foreach my $pid (keys(%{$pids_by_host{$host}{'insecure'}})) {
1.107     raeburn  1062:                                                 if (open(PIPE,"ps -o ppid= -p $pid |")) {
                   1063:                                                     my $ppid = <PIPE>;
                   1064:                                                     chomp($ppid);
                   1065:                                                     close(PIPE);
                   1066:                                                     $ppid =~ s/(^\s+|\s+$)//g;
                   1067:                                                     if (($ppid == $loncppid) && (kill 0 => $pid)) {
                   1068:                                                         kill QUIT => $pid;
                   1069:                                                     }
                   1070:                                                 }
                   1071:                                             }
                   1072:                                         }
                   1073:                                     }
                   1074:                                 }
                   1075:                             }
                   1076:                         }
                   1077:                     }
                   1078:                 }
                   1079:             }
                   1080:         }
                   1081:     }
                   1082:     return;
1.106     raeburn  1083: }
                   1084: 
1.46      albertel 1085: sub send_mail {
1.79      raeburn  1086:     my $defdom = $perlvar{'lonDefDomain'};
                   1087:     my $origmail = $perlvar{'lonAdmEMail'};
1.78      raeburn  1088:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
                   1089:                                    'lonstatusmail',$defdom,$origmail);
1.54      www      1090:     if ($totalcount>2500) {
1.43      albertel 1091: 	$emailto.=",$perlvar{'lonSysEMail'}";
                   1092:     }
1.101     raeburn  1093:     my $from;
                   1094:     my $hostname=`/bin/hostname`;
                   1095:     chop($hostname);
                   1096:     $hostname=~s/[^\w\.]//g;
                   1097:     if ($hostname) {
                   1098:         $from = 'www@'.$hostname;
                   1099:     }
                   1100:     my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
                   1101:     my $loncronmail = "To: $emailto\n".
                   1102:                       "From: $from\n".
                   1103:                       "Subject: ".$subj."\n".
                   1104:                       "Content-type: text/html\; charset=UTF-8\n".
                   1105:                       "MIME-Version: 1.0\n\n";
                   1106:     if (open(my $fh,"<$statusdir/index.html")) {
                   1107:         while (<$fh>) {
                   1108:             $loncronmail .= $_;
                   1109:         }
                   1110:         close($fh);
                   1111:     } else {
                   1112:         $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
                   1113:     }
                   1114:     $loncronmail .= "\n\n";
                   1115:     if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
                   1116:         print $mailh $loncronmail;
                   1117:         close($mailh);
                   1118:         print "Sending mail.\n";
                   1119:     } else {
                   1120:         print "Sending mail failed.\n";
1.52      albertel 1121:     }
1.1       albertel 1122: }
1.46      albertel 1123: 
1.49      albertel 1124: sub usage {
                   1125:     print(<<USAGE);
1.100     bisitz   1126: loncron - housekeeping program that checks up on various parts of LON-CAPA
1.49      albertel 1127: 
                   1128: Options:
1.71      albertel 1129:    --help     Display 
1.49      albertel 1130:    --noemail  Do not send the status email
                   1131:    --justcheckconnections  Only check the current status of the lonc/d
                   1132:                                 connections, do not send emails do not
                   1133:                                 check if the daemons are running, do not
                   1134:                                 generate lon-status
                   1135:    --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
                   1136:                                 running, do not send emails do not
                   1137:                                 check the lonc/d connections, do not
                   1138:                                 generate lon-status
1.59      albertel 1139:    --justreload            Only tell the daemons to reload the config files,
                   1140: 				do not send emails do not
                   1141:                                 check if the daemons are running, do not
                   1142:                                 generate lon-status
1.49      albertel 1143:                            
                   1144: USAGE
                   1145: }
                   1146: 
1.46      albertel 1147: # ================================================================ Main Program
                   1148: sub main () {
1.71      albertel 1149:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.59      albertel 1150: 	$justreload);
1.49      albertel 1151:     &GetOptions("help"                 => \$help,
                   1152: 		"justcheckdaemons"     => \$justcheckdaemons,
                   1153: 		"noemail"              => \$noemail,
1.59      albertel 1154: 		"justcheckconnections" => \$justcheckconnections,
                   1155: 		"justreload"           => \$justreload
1.49      albertel 1156: 		);
                   1157:     if ($help) { &usage(); return; }
1.46      albertel 1158: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
                   1159:     my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
                   1160:     %perlvar=%{$perlvarref};
                   1161:     undef $perlvarref;
                   1162:     delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
                   1163:     delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.75      albertel 1164:     chdir($perlvar{'lonDaemons'});
1.46      albertel 1165: # --------------------------------------- Make sure that LON-CAPA is configured
                   1166: # I only test for one thing here (lonHostID).  This is just a safeguard.
                   1167:     if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
                   1168: 	print("Unconfigured machine.\n");
                   1169: 	my $emailto=$perlvar{'lonSysEMail'};
                   1170: 	my $hostname=`/bin/hostname`;
                   1171: 	chop $hostname;
                   1172: 	$hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
                   1173: 	my $subj="LON: Unconfigured machine $hostname";
                   1174: 	system("echo 'Unconfigured machine $hostname.' |\
                   1175:  mailto $emailto -s '$subj' > /dev/null");
                   1176: 	exit 1;
                   1177:     }
                   1178: 
                   1179: # ----------------------------- Make sure this process is running from user=www
                   1180:     my $wwwid=getpwnam('www');
                   1181:     if ($wwwid!=$<) {
1.100     bisitz   1182: 	print("User ID mismatch. This program must be run as user 'www'.\n");
1.46      albertel 1183: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                   1184: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.100     bisitz   1185: 	system("echo 'User ID mismatch. loncron must be run as user www.' |\
1.46      albertel 1186:  mailto $emailto -s '$subj' > /dev/null");
                   1187: 	exit 1;
                   1188:     }
                   1189: 
1.72      albertel 1190: # -------------------------------------------- Force reload of host information
1.103     raeburn  1191:     my $nomemcache;
                   1192:     if ($justcheckdaemons) {
                   1193:         $nomemcache=1;
                   1194:         my $memcachepidfile="$perlvar{'lonDaemons'}/logs/memcached.pid";
                   1195:         my $memcachepid;
                   1196:         if (-e $memcachepidfile) {
                   1197:             my $memfh=IO::File->new($memcachepidfile);
                   1198:             $memcachepid=<$memfh>;
                   1199:             chomp($memcachepid);
                   1200:             if ($memcachepid =~ /^\d+$/ && kill 0 => $memcachepid) {
                   1201:                 undef($nomemcache);
                   1202:             }
                   1203:         }
                   1204:     }
                   1205:     &Apache::lonnet::load_hosts_tab(1,$nomemcache);
                   1206:     &Apache::lonnet::load_domain_tab(1,$nomemcache);
                   1207:     &Apache::lonnet::get_iphost(1,$nomemcache);
1.46      albertel 1208: 
1.81      raeburn  1209: # ----------------------------------------- Force firewall update for lond port  
                   1210: 
                   1211:     if ((!$justcheckdaemons) && (!$justreload)) {
                   1212:         my $now = time;
                   1213:         my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
                   1214:                       $now.$$.int(rand(10000));
                   1215:         if (open(my $fh,">$tmpfile")) {
                   1216:             my %iphosts = &Apache::lonnet::get_iphost();
                   1217:             foreach my $key (keys(%iphosts)) {
                   1218:                 print $fh "$key\n";
                   1219:             }
                   1220:             close($fh);
1.89      raeburn  1221:             if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
                   1222:                 my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
                   1223:                 system("$execpath $tmpfile");
                   1224:                 unlink('/tmp/lock_lciptables');  # Remove the lock file. 
                   1225:             }
1.88      raeburn  1226:             unlink($tmpfile);
1.81      raeburn  1227:         }
                   1228:     }
                   1229: 
1.46      albertel 1230: # ---------------------------------------------------------------- Start report
                   1231: 
                   1232:     $errors=0;
                   1233:     $warnings=0;
                   1234:     $notices=0;
                   1235: 
                   1236: 	
1.49      albertel 1237:     my $fh;
1.59      albertel 1238:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72      albertel 1239: 	$fh=&start_logging();
1.49      albertel 1240: 
                   1241: 	&log_machine_info($fh);
                   1242: 	&clean_tmp($fh);
                   1243: 	&clean_lonIDs($fh);
1.102     raeburn  1244:         &clean_webDAV_sessionIDs($fh);
1.49      albertel 1245: 	&check_httpd_logs($fh);
                   1246: 	&rotate_lonnet_logs($fh);
1.73      albertel 1247: 	&rotate_other_logs($fh);
1.49      albertel 1248:     }
1.59      albertel 1249:     if (!$justcheckconnections && !$justreload) {
1.76      albertel 1250: 	&checkon_daemon($fh,'lonmemcached',40000);
1.49      albertel 1251: 	&checkon_daemon($fh,'lonsql',200000);
1.63      albertel 1252: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
                   1253: 	    &checkon_daemon($fh,'lond',40000,'USR2');
                   1254: 	}
1.71      albertel 1255: 	&checkon_daemon($fh,'lonc',40000,'USR1');
1.70      raeburn  1256:         &checkon_daemon($fh,'lonmaxima',40000);
1.80      www      1257:         &checkon_daemon($fh,'lonr',40000);
1.49      albertel 1258:     }
1.59      albertel 1259:     if ($justreload) {
1.107     raeburn  1260:         &clean_nosslverify($fh);
1.104     raeburn  1261:         &write_connection_config();
                   1262:         &write_hosttypes();
1.107     raeburn  1263:         &update_revocation_list(); 
1.59      albertel 1264: 	&checkon_daemon($fh,'lond',40000,'USR2');
1.71      albertel 1265: 	&checkon_daemon($fh,'lonc',40000,'USR2');
1.59      albertel 1266:     }
1.63      albertel 1267:     if ($justcheckconnections) {
1.72      albertel 1268: 	&test_connections($fh);
1.49      albertel 1269:     }
1.59      albertel 1270:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72      albertel 1271: 	&check_delayed_msg($fh);
1.49      albertel 1272: 	&log_simplestatus();
1.87      raeburn  1273:         &write_loncaparevs();
                   1274:         &write_serverhomeIDs();
1.97      raeburn  1275: 	&write_checksums();
1.107     raeburn  1276:         my %sslrem = &clean_nosslverify($fh);
                   1277:         my %conchgs = &write_connection_config();
                   1278:         my %hosttypechgs = &write_hosttypes();
                   1279:         my $hadcrlchg = &update_revocation_list();
1.108     raeburn  1280:         if ((keys(%conchgs) > 0) || (keys(%hosttypechgs) > 0) ||
1.107     raeburn  1281:             $hadcrlchg || (keys(%sslrem) > 0)) {
                   1282:             &checkon_daemon($fh,'lond',40000,'USR2');
1.108     raeburn  1283:             &reset_nosslverify_pids($fh,%sslrem);
1.107     raeburn  1284:         }
1.108     raeburn  1285:         &finish_logging($fh);
1.49      albertel 1286: 	if ($totalcount>200 && !$noemail) { &send_mail(); }
                   1287:     }
1.46      albertel 1288: }
                   1289: 
                   1290: &main();
1.1       albertel 1291: 1;
                   1292: 

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