Annotation of loncom/loncron, revision 1.103.2.6

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

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