Annotation of loncom/loncron, revision 1.105

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

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