File:  [LON-CAPA] / loncom / loncron
Revision 1.109: download - view: text, annotated - select for diffs
Thu Oct 25 02:48:56 2018 UTC (5 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Dereference hashref.

    1: #!/usr/bin/perl
    2: 
    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
    4: #
    5: # $Id: loncron,v 1.109 2018/10/25 02:48:56 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: $|=1;
   31: use strict;
   32: 
   33: use lib '/home/httpd/lib/perl/';
   34: use LONCAPA::Configuration;
   35: use LONCAPA::Checksumming;
   36: use LONCAPA;
   37: use Apache::lonnet;
   38: use Apache::loncommon;
   39: 
   40: use IO::File;
   41: use IO::Socket;
   42: use HTML::Entities;
   43: use Getopt::Long;
   44: use GDBM_File;
   45: use Storable qw(thaw);
   46: #globals
   47: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
   48: 
   49: my $statusdir="/home/httpd/html/lon-status";
   50: 
   51: 
   52: # --------------------------------------------------------- Output error status
   53: 
   54: sub log {
   55:     my $fh=shift;
   56:     if ($fh) {	print $fh @_  }
   57: }
   58: 
   59: sub errout {
   60:    my $fh=shift;
   61:    &log($fh,(<<ENDERROUT));
   62:      <table border="2" bgcolor="#CCCCCC">
   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>
   66:      </table><p><a href="#top">Top</a></p>
   67: ENDERROUT
   68: }
   69: 
   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: 
   81: sub start_daemon {
   82:     my ($fh,$daemon,$pidfile,$args) = @_;
   83:     my $progname=$daemon;
   84:     if ($daemon eq 'lonc') {
   85: 	$progname='loncnew'; 
   86:     }
   87:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
   88:     &rotate_logfile($error_fname,$fh,'error logs');
   89:     if ($daemon eq 'lonc') {
   90: 	&clean_sockets($fh);
   91:     }
   92:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
   93:     sleep 1;
   94:     if (-e $pidfile) {
   95: 	&log($fh,"<p>Seems like it started ...</p>");
   96: 	my $lfh=IO::File->new("$pidfile");
   97: 	my $daemonpid=<$lfh>;
   98: 	chomp($daemonpid);
   99: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
  100: 	    return 1;
  101: 	} else {
  102: 	    return 0;
  103: 	}
  104:     }
  105:     &log($fh,"<p>Seems like that did not work!</p>");
  106:     $errors++;
  107:     return 0;
  108: }
  109: 
  110: sub checkon_daemon {
  111:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
  112: 
  113:     my $result;
  114:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
  115:     printf("%-15s ",$daemon);
  116:     if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
  117: 	open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
  118: 	while (my $line=<DFH>) { 
  119: 	    &log($fh,"$line");
  120: 	    if ($line=~/INFO/) { $notices++; }
  121: 	    if ($line=~/WARNING/) { $notices++; }
  122: 	    if ($line=~/CRITICAL/) { $warnings++; }
  123: 	};
  124: 	close (DFH);
  125:     }
  126:     &log($fh,"</tt></p>");
  127:     
  128:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
  129:     
  130:     my $restartflag=1;
  131:     my $daemonpid;
  132:     if (-e $pidfile) {
  133: 	my $lfh=IO::File->new("$pidfile");
  134: 	$daemonpid=<$lfh>;
  135: 	chomp($daemonpid);
  136: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
  137: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
  138: 	    if ($send) { &log($fh,", sending $send"); }
  139: 	    &log($fh,"</h3>");
  140: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
  141: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
  142: 	    $restartflag=0;
  143: 	    if ($send eq 'USR2') {
  144: 		$result = 'reloaded';
  145: 		print "reloaded\n";
  146: 	    } else {
  147: 		$result = 'running';
  148: 		print "running\n";
  149: 	    }
  150: 	} else {
  151: 	    $errors++;
  152: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
  153: 	    $restartflag=1;
  154: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
  155: 	}
  156:     }
  157:     if ($restartflag==1) {
  158: 	$simplestatus{$daemon}='off';
  159: 	$errors++;
  160: 	my $kadaemon=$daemon;
  161: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
  162: 	&log($fh,'<br /><font color="red">Killall '.$daemon.': '.
  163: 	    `killall $kadaemon 2>&1`.' - ');
  164: 	sleep 1;
  165: 	&log($fh,unlink($pidfile).' - '.
  166: 	    `killall -9 $kadaemon 2>&1`.
  167: 	    '</font><br />');
  168:         if ($kadaemon eq 'loncnew') {
  169:             &clean_lonc_childpids();
  170:         }
  171: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
  172: 	
  173: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
  174: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
  175: 	    $simplestatus{$daemon}='restarted';
  176: 	    $result = 'started';
  177: 	    print "started\n";
  178: 	} else {
  179: 	    $errors++;
  180: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
  181: 	    &log($fh,"<p>Give it one more try ...</p>");
  182: 	    print " ";
  183: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
  184: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
  185: 		$simplestatus{$daemon}='restarted';
  186: 		$result = 'started';
  187: 		print "started\n";
  188: 	    } else {
  189: 		$result = 'failed';
  190: 		print " failed\n";
  191: 		$simplestatus{$daemon}='failed';
  192: 		$errors++; $errors++;
  193: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
  194: 		&log($fh,"<p>Unable to start $daemon</p>");
  195: 	    }
  196: 	}
  197: 
  198: 	if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
  199: 	    &log($fh,"<p><pre>");
  200: 	    open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
  201: 	    while (my $line=<DFH>) { 
  202: 		&log($fh,"$line");
  203: 		if ($line=~/WARNING/) { $notices++; }
  204: 		if ($line=~/CRITICAL/) { $notices++; }
  205: 	    };
  206: 	    close (DFH);
  207: 	    &log($fh,"</pre></p>");
  208: 	}
  209:     }
  210:     
  211:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
  212:     &rotate_logfile($fname,$fh,'logs');
  213: 
  214:     &errout($fh);
  215:     return $result;
  216: }
  217: 
  218: # --------------------------------------------------------------------- Machine
  219: sub log_machine_info {
  220:     my ($fh)=@_;
  221:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
  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:     }
  238: 
  239:     &log($fh,"<h3>df</h3>");
  240:     &log($fh,"<pre>");
  241: 
  242:     open (DFH,"df|");
  243:     while (my $line=<DFH>) { 
  244: 	&log($fh,&encode_entities($line,'<>&"')); 
  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++;
  255: 	}
  256: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
  257:     }
  258:     close (DFH);
  259:     &log($fh,"</pre>");
  260: 
  261: 
  262:     &log($fh,"<h3>ps</h3>");
  263:     &log($fh,"<pre>");
  264:     my $psproc=0;
  265: 
  266:     open (PSH,"ps aux --cols 140 |");
  267:     while (my $line=<PSH>) { 
  268: 	&log($fh,&encode_entities($line,'<>&"')); 
  269: 	$psproc++;
  270:     }
  271:     close (PSH);
  272:     &log($fh,"</pre>");
  273: 
  274:     if ($psproc>200) { $notices++; }
  275:     if ($psproc>250) { $notices++; }
  276: 
  277:     &log($fh,"<h3>distprobe</h3>");
  278:     &log($fh,"<pre>");
  279:     &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
  280:     &log($fh,"</pre>");
  281: 
  282:     &errout($fh);
  283: }
  284: 
  285: sub start_logging {
  286:     my $fh=IO::File->new(">$statusdir/newstatus.html");
  287:     my %simplestatus=();
  288:     my $now=time;
  289:     my $date=localtime($now);
  290:     
  291: 
  292:     &log($fh,(<<ENDHEADERS));
  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">
  295: <head>
  296: <title>LON Status Report $perlvar{'lonHostID'}</title>
  297: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  298: </head>
  299: <body bgcolor="#AAAAAA">
  300: <a name="top" />
  301: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
  302: <h2>$date ($now)</h2>
  303: <ol>
  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>
  308: <li><a href="#webdav">WebDAV Session Tokens</a></li>
  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>
  317: </ol>
  318: <hr />
  319: <a name="configuration" />
  320: <h2>Configuration</h2>
  321: <h3>PerlVars</h3>
  322: <table border="2">
  323: ENDHEADERS
  324: 
  325:     foreach my $varname (sort(keys(%perlvar))) {
  326: 	&log($fh,"<tr><td>$varname</td><td>".
  327: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
  328:     }
  329:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
  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');
  334: 	&log($fh,
  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:     }
  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");
  352:     }
  353:     return $fh;
  354: }
  355: 
  356: # --------------------------------------------------------------- clean out tmp
  357: sub clean_tmp {
  358:     my ($fh)=@_;
  359:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
  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);
  479:     }
  480:     return ($cleaned,$old,$removed);
  481: }
  482: 
  483: # ------------------------------------------------------------ clean out lonIDs
  484: sub clean_lonIDs {
  485:     my ($fh)=@_;
  486:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
  487:     my $cleaned=0;
  488:     my $active=0;
  489:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
  490: 	my ($dev,$ino,$mode,$nlink,
  491: 	    $uid,$gid,$rdev,$size,
  492: 	    $atime,$mtime,$ctime,
  493: 	    $blksize,$blocks)=stat($fname);
  494: 	my $now=time;
  495: 	my $since=$now-$mtime;
  496: 	if ($since>$perlvar{'lonExpire'}) {
  497: 	    $cleaned++;
  498: 	    &log($fh,"Unlinking $fname<br />");
  499: 	    unlink("$fname");
  500: 	} else {
  501: 	    $active++;
  502: 	}
  503:     }
  504:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
  505:     &log($fh,"<h3>$active open session(s)</h3>");
  506: }
  507: 
  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: 
  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 
  541: 		 || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
  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: 
  549: 
  550: # ----------------------------------------------------------------------- httpd
  551: sub check_httpd_logs {
  552:     my ($fh)=@_;
  553:     if (open(PIPE,"./lchttpdlogs|")) {
  554:         while (my $line=<PIPE>) {
  555:             &log($fh,$line);
  556:             if ($line=~/\[error\]/) { $notices++; }
  557:         }
  558:         close(PIPE);
  559:     }
  560:     &errout($fh);
  561: }
  562: 
  563: # ---------------------------------------------------------------------- lonnet
  564: 
  565: sub rotate_lonnet_logs {
  566:     my ($fh)=@_;
  567:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
  568:     print "Checking logs.\n";
  569:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
  570: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
  571: 	while (my $line=<DFH>) { 
  572: 	    &log($fh,&encode_entities($line,'<>&"'));
  573: 	}
  574: 	close (DFH);
  575:     }
  576:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
  577:     
  578:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
  579: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
  580: 	while (my $line=<DFH>) { 
  581: 	    &log($fh,&encode_entities($line,'<>&"'));
  582: 	}
  583: 	close (DFH);
  584:     } else { &log($fh,"No perm log\n") }
  585: 
  586:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
  587:     &rotate_logfile($fname,$fh,'lonnet log');
  588: 
  589:     &log($fh,"</pre>");
  590:     &errout($fh);
  591: }
  592: 
  593: sub rotate_other_logs {
  594:     my ($fh) = @_;
  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:     }
  606: }
  607: 
  608: # ----------------------------------------------------------------- Connections
  609: sub test_connections {
  610:     my ($fh)=@_;
  611:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
  612:     print "Testing connections.\n";
  613:     &log($fh,"<table border='2'>");
  614:     my ($good,$bad)=(0,0);
  615:     my %hostname = &Apache::lonnet::all_hostnames();
  616:     foreach my $tryserver (sort(keys(%hostname))) {
  617: 	print(".");
  618: 	my $result;
  619: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
  620: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
  621: 	    $result="<b>ok</b>";
  622: 	    $good++;
  623: 	} else {
  624: 	    $result=$answer;
  625: 	    $warnings++;
  626: 	    if ($answer eq 'con_lost') {
  627: 		$bad++;
  628: 		$warnings++;
  629: 	    } else {
  630: 		$good++; #self connection
  631: 	    }
  632: 	}
  633: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
  634: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
  635:     }
  636:     &log($fh,"</table>");
  637:     print "\n$good good, $bad bad connections\n";
  638:     &errout($fh);
  639: }
  640: 
  641: 
  642: # ------------------------------------------------------------ Delayed messages
  643: sub check_delayed_msg {
  644:     my ($fh)=@_;
  645:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
  646:     print "Checking buffers.\n";
  647:     
  648:     &log($fh,'<h3>Scanning Permanent Log</h3>');
  649: 
  650:     my $unsend=0;
  651: 
  652:     my %hostname = &Apache::lonnet::all_hostnames();
  653:     my $numhosts = scalar(keys(%hostname));
  654: 
  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);
  658:         if ($numhosts) {
  659:             next unless ($hostname{$dserv});
  660:         }
  661: 	if ($sdf eq 'F') { 
  662: 	    my $local=localtime($time);
  663: 	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
  664: 	    $warnings++;
  665: 	}
  666: 	if ($sdf eq 'S') { $unsend--; }
  667: 	if ($sdf eq 'D') { $unsend++; }
  668:     }
  669: 
  670:     &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
  671:     if ($unsend > 0) {
  672:         $warnings=$warnings+5*$unsend;
  673:     }
  674: 
  675:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
  676:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
  677: # list directory with delayed messages and remember offline servers
  678:     my %servers=();
  679:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
  680:     while (my $line=<DFH>) {
  681:         my ($server)=($line=~/\.(\w+)$/);
  682:         if ($server) { $servers{$server}=1; }
  683: 	&log($fh,&encode_entities($line,'<>&"'));
  684:     }
  685:     &log($fh,"</pre>\n");
  686:     close (DFH);
  687: # pong to all servers that have delayed messages
  688: # this will trigger a reverse connection, which should flush the buffers
  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 />");
  700:                 print "Time out while contacting: $tryserver for pong.\n";
  701:             } else {
  702:                 &log($fh,"Pong to $tryserver: $answer<br />");
  703:             }
  704:         } else {
  705:             &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
  706:         }
  707:     }
  708: }
  709: 
  710: sub finish_logging {
  711:     my ($fh)=@_;
  712:     &log($fh,"<a name='errcount' />\n");
  713:     $totalcount=$notices+4*$warnings+100*$errors;
  714:     &errout($fh);
  715:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
  716:     my $now=time;
  717:     my $date=localtime($now);
  718:     &log($fh,"<hr />$date ($now)</body></html>\n");
  719:     print "lon-status webpage updated.\n";
  720:     $fh->close();
  721: 
  722:     if ($errors) { $simplestatus{'errors'}=$errors; }
  723:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
  724:     if ($notices) { $simplestatus{'notices'}=$notices; }
  725:     $simplestatus{'time'}=time;
  726: }
  727: 
  728: sub log_simplestatus {
  729:     rename("$statusdir/newstatus.html","$statusdir/index.html");
  730:     
  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();
  737: }
  738: 
  739: sub write_loncaparevs {
  740:     print "Retrieving LON-CAPA version information.\n";
  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/) {
  754:                 print "Time out while contacting lonHost: $id for version.\n";   
  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 {
  772:     print "Retrieving LON-CAPA lonHostID information.\n";
  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;
  779:                 eval {
  780:                     local $SIG{ ALRM } = sub { die "TIMEOUT" };
  781:                     alarm(10);
  782:                     $serverhomeID = 
  783:                         &Apache::lonnet::get_server_homeID($name,1,'loncron');
  784:                     alarm(0);
  785:                 };
  786:                 if ($@ && $@ =~ m/TIMEOUT/) {
  787:                     print "Time out while contacting server: $name\n"; 
  788:                 }
  789:                 if ($serverhomeID ne '') {
  790:                     $output .= $name.':'.$serverhomeID."\n";
  791:                 } else {
  792:                     $output .= $name.':'.$name_to_host{$name}->[0]."\n";
  793:                 }
  794:             }
  795:         }
  796:     }
  797:     if ($output) {
  798:         if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
  799:             print $fh $output;
  800:             close($fh);
  801:             &Apache::lonnet::load_serverhomeIDs();
  802:         }
  803:     }
  804:     return;
  805: }
  806: 
  807: sub write_checksums {
  808:     my $distro = &LONCAPA::distro();
  809:     if ($distro) {
  810:         print "Retrieving file version and checksumming.\n";
  811:         my $numchksums = 0;
  812:         my ($chksumsref,$versionsref) =
  813:             &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
  814:                                                   $perlvar{'lonLib'},
  815:                                                   $perlvar{'lonIncludes'},
  816:                                                   $perlvar{'lonTabDir'});
  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:     }
  824:     return;
  825: }
  826: 
  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: 
  858: sub write_connection_config {
  859:     my ($isprimary,$domconf,$url,%connectssl,%changes);
  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)) {
  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")) {
  900:                 my $count = 0;
  901:                 foreach my $key (sort(keys(%connectssl))) { 
  902:                     print $fh "$key=$connectssl{$key}\n";
  903:                     if (exists($currconf{$key})) {
  904:                         unless ($currconf{$key} eq $connectssl{$key}) {
  905:                             $changes{$key} = 1;
  906:                         }
  907:                     } else {
  908:                         $changes{$key} = 1;
  909:                     }
  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:     }
  921:     return %changes;
  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'}};
  975:     my %changes;
  976:     if (($dom ne '') && ($internetdom ne '')) {
  977:         if (keys(%hostdom)) {
  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")) {
  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";
  999:                     if (exists($currhosttypes{$lonid})) {
 1000:                         if ($type ne $currhosttypes{$lonid}) {
 1001:                             $changes{$lonid} = 1;
 1002:                         }
 1003:                     } else {
 1004:                         $changes{$lonid} = 1;
 1005:                     }
 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:     }
 1017:     return %changes;
 1018: }
 1019: 
 1020: sub update_revocation_list {
 1021:     my ($result,$changed) = &Apache::lonnet::fetch_crl_pemfile();
 1022:     if ($result eq 'ok') {
 1023:         print "Certificate Revocation List (from CA) updated.\n";
 1024:     } else {
 1025:         print "Certificate Revocation List from (CA) not updated.\n";
 1026:     }
 1027:     return $changed;
 1028: }
 1029: 
 1030: sub reset_nosslverify_pids {
 1031:     my ($fh,%sslrem) = @_;
 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') {
 1060:                                         if (keys(%{$pids_by_host{$host}{'insecure'}})) {
 1061:                                             foreach my $pid (keys(%{$pids_by_host{$host}{'insecure'}})) {
 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;
 1083: }
 1084: 
 1085: sub send_mail {
 1086:     my $defdom = $perlvar{'lonDefDomain'};
 1087:     my $origmail = $perlvar{'lonAdmEMail'};
 1088:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
 1089:                                    'lonstatusmail',$defdom,$origmail);
 1090:     if ($totalcount>2500) {
 1091: 	$emailto.=",$perlvar{'lonSysEMail'}";
 1092:     }
 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";
 1121:     }
 1122: }
 1123: 
 1124: sub usage {
 1125:     print(<<USAGE);
 1126: loncron - housekeeping program that checks up on various parts of LON-CAPA
 1127: 
 1128: Options:
 1129:    --help     Display 
 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
 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
 1143:                            
 1144: USAGE
 1145: }
 1146: 
 1147: # ================================================================ Main Program
 1148: sub main () {
 1149:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
 1150: 	$justreload);
 1151:     &GetOptions("help"                 => \$help,
 1152: 		"justcheckdaemons"     => \$justcheckdaemons,
 1153: 		"noemail"              => \$noemail,
 1154: 		"justcheckconnections" => \$justcheckconnections,
 1155: 		"justreload"           => \$justreload
 1156: 		);
 1157:     if ($help) { &usage(); return; }
 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
 1164:     chdir($perlvar{'lonDaemons'});
 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!=$<) {
 1182: 	print("User ID mismatch. This program must be run as user 'www'.\n");
 1183: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
 1184: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
 1185: 	system("echo 'User ID mismatch. loncron must be run as user www.' |\
 1186:  mailto $emailto -s '$subj' > /dev/null");
 1187: 	exit 1;
 1188:     }
 1189: 
 1190: # -------------------------------------------- Force reload of host information
 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);
 1208: 
 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);
 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:             }
 1226:             unlink($tmpfile);
 1227:         }
 1228:     }
 1229: 
 1230: # ---------------------------------------------------------------- Start report
 1231: 
 1232:     $errors=0;
 1233:     $warnings=0;
 1234:     $notices=0;
 1235: 
 1236: 	
 1237:     my $fh;
 1238:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
 1239: 	$fh=&start_logging();
 1240: 
 1241: 	&log_machine_info($fh);
 1242: 	&clean_tmp($fh);
 1243: 	&clean_lonIDs($fh);
 1244:         &clean_webDAV_sessionIDs($fh);
 1245: 	&check_httpd_logs($fh);
 1246: 	&rotate_lonnet_logs($fh);
 1247: 	&rotate_other_logs($fh);
 1248:     }
 1249:     if (!$justcheckconnections && !$justreload) {
 1250: 	&checkon_daemon($fh,'lonmemcached',40000);
 1251: 	&checkon_daemon($fh,'lonsql',200000);
 1252: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
 1253: 	    &checkon_daemon($fh,'lond',40000,'USR2');
 1254: 	}
 1255: 	&checkon_daemon($fh,'lonc',40000,'USR1');
 1256:         &checkon_daemon($fh,'lonmaxima',40000);
 1257:         &checkon_daemon($fh,'lonr',40000);
 1258:     }
 1259:     if ($justreload) {
 1260:         &clean_nosslverify($fh);
 1261:         &write_connection_config();
 1262:         &write_hosttypes();
 1263:         &update_revocation_list(); 
 1264: 	&checkon_daemon($fh,'lond',40000,'USR2');
 1265: 	&checkon_daemon($fh,'lonc',40000,'USR2');
 1266:     }
 1267:     if ($justcheckconnections) {
 1268: 	&test_connections($fh);
 1269:     }
 1270:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
 1271: 	&check_delayed_msg($fh);
 1272: 	&log_simplestatus();
 1273:         &write_loncaparevs();
 1274:         &write_serverhomeIDs();
 1275: 	&write_checksums();
 1276:         my %sslrem = &clean_nosslverify($fh);
 1277:         my %conchgs = &write_connection_config();
 1278:         my %hosttypechgs = &write_hosttypes();
 1279:         my $hadcrlchg = &update_revocation_list();
 1280:         if ((keys(%conchgs) > 0) || (keys(%hosttypechgs) > 0) ||
 1281:             $hadcrlchg || (keys(%sslrem) > 0)) {
 1282:             &checkon_daemon($fh,'lond',40000,'USR2');
 1283:             &reset_nosslverify_pids($fh,%sslrem);
 1284:         }
 1285:         &finish_logging($fh);
 1286: 	if ($totalcount>200 && !$noemail) { &send_mail(); }
 1287:     }
 1288: }
 1289: 
 1290: &main();
 1291: 1;
 1292: 

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