Annotation of loncom/loncron, revision 1.95.10.1

1.1       albertel    1: #!/usr/bin/perl
                      2: 
1.47      albertel    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
                      4: #
1.95.10.1! raeburn     5: # $Id: loncron,v 1.95 2011/11/14 17:27:34 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.89      raeburn    35: use LONCAPA;
1.72      albertel   36: use Apache::lonnet;
1.79      raeburn    37: use Apache::loncommon;
1.26      harris41   38: 
1.1       albertel   39: use IO::File;
                     40: use IO::Socket;
1.48      albertel   41: use HTML::Entities;
1.49      albertel   42: use Getopt::Long;
1.46      albertel   43: #globals
                     44: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
                     45: 
                     46: my $statusdir="/home/httpd/html/lon-status";
                     47: 
1.1       albertel   48: 
                     49: # --------------------------------------------------------- Output error status
                     50: 
1.46      albertel   51: sub log {
                     52:     my $fh=shift;
                     53:     if ($fh) {	print $fh @_  }
                     54: }
                     55: 
1.1       albertel   56: sub errout {
                     57:    my $fh=shift;
1.46      albertel   58:    &log($fh,(<<ENDERROUT));
1.48      albertel   59:      <table border="2" bgcolor="#CCCCCC">
1.1       albertel   60:      <tr><td>Notices</td><td>$notices</td></tr>
                     61:      <tr><td>Warnings</td><td>$warnings</td></tr>
                     62:      <tr><td>Errors</td><td>$errors</td></tr>
1.48      albertel   63:      </table><p><a href="#top">Top</a></p>
1.1       albertel   64: ENDERROUT
                     65: }
                     66: 
1.73      albertel   67: sub rotate_logfile {
                     68:     my ($file,$fh,$description) = @_;
                     69:     my $size=(stat($file))[7];
                     70:     if ($size>40000) {
                     71: 	&log($fh,"<p>Rotating $description ...</p>");
                     72: 	rename("$file.2","$file.3");
                     73: 	rename("$file.1","$file.2");
                     74: 	rename("$file","$file.1");
                     75:     } 
                     76: }
                     77: 
1.42      albertel   78: sub start_daemon {
1.50      albertel   79:     my ($fh,$daemon,$pidfile,$args) = @_;
1.44      albertel   80:     my $progname=$daemon;
1.71      albertel   81:     if ($daemon eq 'lonc') {
1.44      albertel   82: 	$progname='loncnew'; 
                     83:     }
1.51      albertel   84:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
1.73      albertel   85:     &rotate_logfile($error_fname,$fh,'error logs');
1.74      albertel   86:     if ($daemon eq 'lonc') {
                     87: 	&clean_sockets($fh);
                     88:     }
1.51      albertel   89:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60      albertel   90:     sleep 1;
1.42      albertel   91:     if (-e $pidfile) {
1.48      albertel   92: 	&log($fh,"<p>Seems like it started ...</p>");
1.42      albertel   93: 	my $lfh=IO::File->new("$pidfile");
                     94: 	my $daemonpid=<$lfh>;
                     95: 	chomp($daemonpid);
1.62      albertel   96: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42      albertel   97: 	    return 1;
                     98: 	} else {
                     99: 	    return 0;
                    100: 	}
                    101:     }
1.48      albertel  102:     &log($fh,"<p>Seems like that did not work!</p>");
1.42      albertel  103:     $errors++;
                    104:     return 0;
                    105: }
                    106: 
                    107: sub checkon_daemon {
1.59      albertel  108:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42      albertel  109: 
1.63      albertel  110:     my $result;
1.48      albertel  111:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57      albertel  112:     printf("%-15s ",$daemon);
1.42      albertel  113:     if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
                    114: 	open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46      albertel  115: 	while (my $line=<DFH>) { 
                    116: 	    &log($fh,"$line");
1.42      albertel  117: 	    if ($line=~/INFO/) { $notices++; }
                    118: 	    if ($line=~/WARNING/) { $notices++; }
                    119: 	    if ($line=~/CRITICAL/) { $warnings++; }
                    120: 	};
                    121: 	close (DFH);
                    122:     }
1.48      albertel  123:     &log($fh,"</tt></p>");
1.42      albertel  124:     
                    125:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
                    126:     
                    127:     my $restartflag=1;
1.46      albertel  128:     my $daemonpid;
1.42      albertel  129:     if (-e $pidfile) {
                    130: 	my $lfh=IO::File->new("$pidfile");
1.46      albertel  131: 	$daemonpid=<$lfh>;
1.42      albertel  132: 	chomp($daemonpid);
1.62      albertel  133: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46      albertel  134: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59      albertel  135: 	    if ($send) { &log($fh,", sending $send"); }
1.46      albertel  136: 	    &log($fh,"</h3>");
1.59      albertel  137: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
                    138: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42      albertel  139: 	    $restartflag=0;
1.59      albertel  140: 	    if ($send eq 'USR2') {
1.63      albertel  141: 		$result = 'reloaded';
1.59      albertel  142: 		print "reloaded\n";
                    143: 	    } else {
1.63      albertel  144: 		$result = 'running';
1.59      albertel  145: 		print "running\n";
                    146: 	    }
1.42      albertel  147: 	} else {
                    148: 	    $errors++;
1.46      albertel  149: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42      albertel  150: 	    $restartflag=1;
1.46      albertel  151: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42      albertel  152: 	}
                    153:     }
                    154:     if ($restartflag==1) {
                    155: 	$simplestatus{$daemon}='off';
                    156: 	$errors++;
1.57      albertel  157: 	my $kadaemon=$daemon;
                    158: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.46      albertel  159: 	&log($fh,'<br><font color="red">Killall '.$daemon.': '.
1.57      albertel  160: 	    `killall $kadaemon 2>&1`.' - ');
1.60      albertel  161: 	sleep 1;
1.46      albertel  162: 	&log($fh,unlink($pidfile).' - '.
1.57      albertel  163: 	    `killall -9 $kadaemon 2>&1`.
1.46      albertel  164: 	    '</font><br>');
                    165: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
1.42      albertel  166: 	
1.50      albertel  167: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  168: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  169: 	    $simplestatus{$daemon}='restarted';
1.63      albertel  170: 	    $result = 'started';
1.42      albertel  171: 	    print "started\n";
                    172: 	} else {
                    173: 	    $errors++;
1.46      albertel  174: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  175: 	    &log($fh,"<p>Give it one more try ...</p>");
1.42      albertel  176: 	    print " ";
1.50      albertel  177: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  178: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  179: 		$simplestatus{$daemon}='restarted';
1.63      albertel  180: 		$result = 'started';
1.42      albertel  181: 		print "started\n";
                    182: 	    } else {
1.63      albertel  183: 		$result = 'failed';
1.42      albertel  184: 		print " failed\n";
                    185: 		$simplestatus{$daemon}='failed';
                    186: 		$errors++; $errors++;
1.46      albertel  187: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  188: 		&log($fh,"<p>Unable to start $daemon</p>");
1.42      albertel  189: 	    }
                    190: 	}
                    191: 
                    192: 	if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
1.46      albertel  193: 	    &log($fh,"<p><pre>");
1.42      albertel  194: 	    open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46      albertel  195: 	    while (my $line=<DFH>) { 
                    196: 		&log($fh,"$line");
1.42      albertel  197: 		if ($line=~/WARNING/) { $notices++; }
                    198: 		if ($line=~/CRITICAL/) { $notices++; }
                    199: 	    };
                    200: 	    close (DFH);
1.48      albertel  201: 	    &log($fh,"</pre></p>");
1.42      albertel  202: 	}
                    203:     }
                    204:     
1.46      albertel  205:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.73      albertel  206:     &rotate_logfile($fname,$fh,'logs');
1.42      albertel  207: 
                    208:     &errout($fh);
1.63      albertel  209:     return $result;
1.42      albertel  210: }
1.1       albertel  211: 
1.46      albertel  212: # --------------------------------------------------------------------- Machine
                    213: sub log_machine_info {
                    214:     my ($fh)=@_;
1.48      albertel  215:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46      albertel  216:     &log($fh,"<h3>loadavg</h3>");
                    217: 	
                    218:     open (LOADAVGH,"/proc/loadavg");
                    219:     my $loadavg=<LOADAVGH>;
                    220:     close (LOADAVGH);
                    221:     
                    222:     &log($fh,"<tt>$loadavg</tt>");
                    223:     
                    224:     my @parts=split(/\s+/,$loadavg);
                    225:     if ($parts[1]>4.0) {
                    226: 	$errors++;
                    227:     } elsif ($parts[1]>2.0) {
                    228: 	$warnings++;
                    229:     } elsif ($parts[1]>1.0) {
                    230: 	$notices++;
                    231:     }
1.13      harris41  232: 
1.46      albertel  233:     &log($fh,"<h3>df</h3>");
                    234:     &log($fh,"<pre>");
1.14      harris41  235: 
1.46      albertel  236:     open (DFH,"df|");
                    237:     while (my $line=<DFH>) { 
1.48      albertel  238: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  239: 	@parts=split(/\s+/,$line);
                    240: 	my $usage=$parts[4];
                    241: 	$usage=~s/\W//g;
                    242: 	if ($usage>90) { 
                    243: 	    $warnings++;
                    244: 	    $notices++; 
                    245: 	} elsif ($usage>80) {
                    246: 	    $warnings++;
                    247: 	} elsif ($usage>60) {
                    248: 	    $notices++;
1.31      albertel  249: 	}
1.46      albertel  250: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1       albertel  251:     }
1.46      albertel  252:     close (DFH);
                    253:     &log($fh,"</pre>");
1.1       albertel  254: 
                    255: 
1.46      albertel  256:     &log($fh,"<h3>ps</h3>");
                    257:     &log($fh,"<pre>");
                    258:     my $psproc=0;
1.1       albertel  259: 
1.53      albertel  260:     open (PSH,"ps aux --cols 140 |");
1.46      albertel  261:     while (my $line=<PSH>) { 
1.48      albertel  262: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  263: 	$psproc++;
                    264:     }
                    265:     close (PSH);
                    266:     &log($fh,"</pre>");
1.1       albertel  267: 
1.46      albertel  268:     if ($psproc>200) { $notices++; }
                    269:     if ($psproc>250) { $notices++; }
1.1       albertel  270: 
1.61      albertel  271:     &log($fh,"<h3>distprobe</h3>");
                    272:     &log($fh,"<pre>");
                    273:     open(DSH,"$perlvar{'lonDaemons'}/distprobe |");
                    274:     while (my $line=<DSH>) { 
                    275: 	&log($fh,&encode_entities($line,'<>&"')); 
                    276: 	$psproc++;
                    277:     }
                    278:     close(DSH);
                    279:     &log($fh,"</pre>");
                    280: 
1.46      albertel  281:     &errout($fh);
                    282: }
1.1       albertel  283: 
1.46      albertel  284: sub start_logging {
1.43      albertel  285:     my $fh=IO::File->new(">$statusdir/newstatus.html");
                    286:     my %simplestatus=();
1.46      albertel  287:     my $now=time;
                    288:     my $date=localtime($now);
1.43      albertel  289:     
1.46      albertel  290: 
                    291:     &log($fh,(<<ENDHEADERS));
1.1       albertel  292: <html>
                    293: <head>
                    294: <title>LON Status Report $perlvar{'lonHostID'}</title>
                    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>
                    305: <li><a href="#httpd">httpd</a></li>
                    306: <li><a href="#lonsql">lonsql</a></li>
                    307: <li><a href="#lond">lond</a></li>
                    308: <li><a href="#lonc">lonc</a></li>
                    309: <li><a href="#lonnet">lonnet</a></li>
                    310: <li><a href="#connections">Connections</a></li>
                    311: <li><a href="#delayed">Delayed Messages</a></li>
                    312: <li><a href="#errcount">Error Count</a></li>
1.1       albertel  313: </ol>
1.48      albertel  314: <hr />
                    315: <a name="configuration" />
1.1       albertel  316: <h2>Configuration</h2>
                    317: <h3>PerlVars</h3>
1.48      albertel  318: <table border="2">
1.1       albertel  319: ENDHEADERS
                    320: 
1.46      albertel  321:     foreach my $varname (sort(keys(%perlvar))) {
1.48      albertel  322: 	&log($fh,"<tr><td>$varname</td><td>".
                    323: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43      albertel  324:     }
1.48      albertel  325:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72      albertel  326:     my %hostname = &Apache::lonnet::all_hostnames();
                    327:     foreach my $id (sort(keys(%hostname))) {
                    328: 	my $role = (&Apache::lonnet::is_library($id) ? 'library'
                    329: 		                                     : 'access');
1.46      albertel  330: 	&log($fh,
1.72      albertel  331: 	    "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
                    332: 	    "</td><td>".$role.
                    333: 	    "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
                    334:     }
                    335:     &log($fh,"</table><h3>Spare Hosts</h3><ul>");
                    336:     foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
                    337: 	&log($fh,"<li>$type\n<ol>");
                    338: 	foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
                    339: 	    &log($fh,"<li>$id</li>\n");
                    340: 	}
                    341: 	&log($fh,"</ol>\n</li>\n");
1.43      albertel  342:     }
1.72      albertel  343:     &log($fh,"</ul>\n");
1.46      albertel  344:     return $fh;
                    345: }
1.11      www       346: 
                    347: # --------------------------------------------------------------- clean out tmp
1.46      albertel  348: sub clean_tmp {
                    349:     my ($fh)=@_;
1.48      albertel  350:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.82      raeburn   351:     my ($cleaned,$old,$removed) = (0,0,0);
                    352:     my %errors = (
                    353:                      dir       => [],
                    354:                      file      => [],
                    355:                      failopen  => [],
                    356:                  );
                    357:     my %error_titles = (
                    358:                          dir       => 'failed to remove empty directory:',
                    359:                          file      => 'failed to unlike stale file',
                    360:                          failopen  => 'failed to open file or directory'
                    361:                        );
                    362:     ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
                    363:     &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
                    364:     foreach my $key (sort(keys(%errors))) {
                    365:         if (ref($errors{$key}) eq 'ARRAY') {
                    366:             if (@{$errors{$key}} > 0) {
                    367:                 &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
                    368:                      join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
                    369:             }
                    370:         }
                    371:     }
                    372: }
                    373: 
                    374: sub recursive_clean_tmp {
                    375:     my ($subdir,$cleaned,$old,$removed,$errors) = @_;
                    376:     my $base = "$perlvar{'lonDaemons'}/tmp";
                    377:     my $path = $base;
                    378:     next if ($subdir =~ m{\.\./});
                    379:     next unless (ref($errors) eq 'HASH');
                    380:     unless ($subdir eq '') {
                    381:         $path .= '/'.$subdir;
                    382:     }
                    383:     if (opendir(my $dh,"$path")) {
                    384:         while (my $file = readdir($dh)) {
                    385:             next if ($file =~ /^\.\.?$/);
                    386:             my $fname = "$path/$file";
                    387:             if (-d $fname) {
                    388:                 my $innerdir;
                    389:                 if ($subdir eq '') {
                    390:                     $innerdir = $file;
                    391:                 } else {
                    392:                     $innerdir = $subdir.'/'.$file;
                    393:                 }
                    394:                 ($cleaned,$old,$removed) = 
                    395:                      &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
                    396:                 my @doms = &Apache::lonnet::current_machine_domains();
                    397:                 
                    398:                 if (open(my $dirhandle,$fname)) {
                    399:                     unless (($innerdir eq 'helprequests') ||
                    400:                             (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
                    401:                         my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
                    402:                                       join('&&',@contents)."\n";    
                    403:                         if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
                    404:                             closedir($dirhandle);
                    405:                             if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
                    406:                                 if (rmdir($fname)) {
                    407:                                     $removed ++;
                    408:                                 } elsif (ref($errors->{dir}) eq 'ARRAY') {
                    409:                                     push(@{$errors->{dir}},$fname);
                    410:                                 }
                    411:                             }
                    412:                         }
                    413:                     } else {
                    414:                         closedir($dirhandle);
                    415:                     }
                    416:                 }
                    417:             } else {
                    418:                 my ($dev,$ino,$mode,$nlink,
                    419:                     $uid,$gid,$rdev,$size,
                    420:                     $atime,$mtime,$ctime,
                    421:                     $blksize,$blocks)=stat($fname);
                    422:                 my $now=time;
                    423:                 my $since=$now-$mtime;
                    424:                 if ($since>$perlvar{'lonExpire'}) {
                    425:                     if ($subdir eq '') {
                    426:                         my $line='';
                    427:                         if ($fname =~ /\.db$/) {
                    428:                             if (unlink($fname)) {
                    429:                                 $cleaned++;
                    430:                             } elsif (ref($errors->{file}) eq 'ARRAY') {
                    431:                                 push(@{$errors->{file}},$fname);
                    432:                             }
                    433:                         } elsif (open(PROBE,$fname)) {
                    434:                             my $line='';
                    435:                             $line=<PROBE>;
                    436:                             close(PROBE);
                    437:                             if ($line=~/^CHECKOUTTOKEN\&/) {
                    438:                                 if ($since>365*$perlvar{'lonExpire'}) {
                    439:                                     if (unlink($fname)) {
                    440:                                         $cleaned++; 
                    441:                                     } elsif (ref($errors->{file}) eq 'ARRAY') {
                    442:                                         push(@{$errors->{file}},$fname);
                    443:                                     }
                    444:                                 } else {
                    445:                                     $old++;
                    446:                                 }
                    447:                             } else {
                    448:                                 if (unlink($fname)) {
                    449:                                     $cleaned++;
                    450:                                 } elsif (ref($errors->{file}) eq 'ARRAY') {
                    451:                                     push(@{$errors->{file}},$fname);
                    452:                                 }
                    453:                             }
                    454:                         } elsif (ref($errors->{failopen}) eq 'ARRAY') {
                    455:                             push(@{$errors->{failopen}},$fname); 
                    456:                         }
                    457:                     } else {
                    458:                         if (unlink($fname)) {
                    459:                             $cleaned++;
                    460:                         } elsif (ref($errors->{file}) eq 'ARRAY') {
                    461:                             push(@{$errors->{file}},$fname);
                    462:                         }
                    463:                     }
                    464:                 }
                    465:             }
                    466:         }
                    467:         closedir($dh);
                    468:     } elsif (ref($errors->{failopen}) eq 'ARRAY') {
                    469:         push(@{$errors->{failopen}},$path);
1.43      albertel  470:     }
1.82      raeburn   471:     return ($cleaned,$old,$removed);
1.46      albertel  472: }
1.11      www       473: 
                    474: # ------------------------------------------------------------ clean out lonIDs
1.46      albertel  475: sub clean_lonIDs {
                    476:     my ($fh)=@_;
1.48      albertel  477:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46      albertel  478:     my $cleaned=0;
                    479:     my $active=0;
                    480:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.43      albertel  481: 	my ($dev,$ino,$mode,$nlink,
                    482: 	    $uid,$gid,$rdev,$size,
                    483: 	    $atime,$mtime,$ctime,
                    484: 	    $blksize,$blocks)=stat($fname);
1.46      albertel  485: 	my $now=time;
                    486: 	my $since=$now-$mtime;
1.43      albertel  487: 	if ($since>$perlvar{'lonExpire'}) {
                    488: 	    $cleaned++;
1.46      albertel  489: 	    &log($fh,"Unlinking $fname<br>");
1.43      albertel  490: 	    unlink("$fname");
                    491: 	} else {
                    492: 	    $active++;
                    493: 	}
1.46      albertel  494:     }
1.48      albertel  495:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46      albertel  496:     &log($fh,"<h3>$active open session(s)</h3>");
                    497: }
1.43      albertel  498: 
1.74      albertel  499: # ----------------------------------------------------------- clean out sockets
                    500: sub clean_sockets {
                    501:     my ($fh)=@_;
                    502:     my $cleaned=0;
                    503:     opendir(SOCKETS,$perlvar{'lonSockDir'});
                    504:     while (my $fname=readdir(SOCKETS)) {
                    505: 	next if (-d $fname 
1.80      www       506: 		 || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
1.74      albertel  507: 	$cleaned++;
                    508: 	&log($fh,"Unlinking $fname<br />");
                    509: 	unlink("/home/httpd/sockets/$fname");
                    510:     }
                    511:     &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
                    512: }
                    513: 
1.11      www       514: 
1.1       albertel  515: # ----------------------------------------------------------------------- httpd
1.46      albertel  516: sub check_httpd_logs {
                    517:     my ($fh)=@_;
1.94      raeburn   518:     if (open(PIPE,"./lchttpdlogs|")) {
1.93      raeburn   519:         while (my $line=<PIPE>) {
                    520:             &log($fh,$line);
                    521:             if ($line=~/\[error\]/) { $notices++; }
                    522:         }
                    523:         close(PIPE);
1.46      albertel  524:     }
1.43      albertel  525:     &errout($fh);
1.46      albertel  526: }
1.1       albertel  527: 
                    528: # ---------------------------------------------------------------------- lonnet
                    529: 
1.48      albertel  530: sub rotate_lonnet_logs {
1.46      albertel  531:     my ($fh)=@_;
1.48      albertel  532:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.43      albertel  533:     print "checking logs\n";
                    534:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
                    535: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.46      albertel  536: 	while (my $line=<DFH>) { 
1.48      albertel  537: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  538: 	}
1.43      albertel  539: 	close (DFH);
                    540:     }
1.46      albertel  541:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.43      albertel  542:     
                    543:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    544: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.46      albertel  545: 	while (my $line=<DFH>) { 
1.48      albertel  546: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  547: 	}
1.43      albertel  548: 	close (DFH);
1.46      albertel  549:     } else { &log($fh,"No perm log\n") }
1.43      albertel  550: 
1.46      albertel  551:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73      albertel  552:     &rotate_logfile($fname,$fh,'lonnet log');
1.1       albertel  553: 
1.46      albertel  554:     &log($fh,"</pre>");
1.43      albertel  555:     &errout($fh);
1.46      albertel  556: }
                    557: 
1.73      albertel  558: sub rotate_other_logs {
                    559:     my ($fh) = @_;
1.83      raeburn   560:     my %logs = (
                    561:                   autoenroll          => 'Auto Enroll log',
                    562:                   autocreate          => 'Create Course log',
                    563:                   searchcat           => 'Search Cataloguing log',
                    564:                   autoupdate          => 'Auto Update log',
                    565:                   refreshcourseids_db => 'Refresh CourseIDs db log',
                    566:                );
                    567:     foreach my $item (keys(%logs)) {
                    568:         my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
                    569:         &rotate_logfile($fname,$fh,$logs{$item});
                    570:     }
1.73      albertel  571: }
                    572: 
1.43      albertel  573: # ----------------------------------------------------------------- Connections
1.46      albertel  574: sub test_connections {
1.72      albertel  575:     my ($fh)=@_;
1.48      albertel  576:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.43      albertel  577:     print "testing connections\n";
1.48      albertel  578:     &log($fh,"<table border='2'>");
1.49      albertel  579:     my ($good,$bad)=(0,0);
1.72      albertel  580:     my %hostname = &Apache::lonnet::all_hostnames();
                    581:     foreach my $tryserver (sort(keys(%hostname))) {
1.43      albertel  582: 	print(".");
1.46      albertel  583: 	my $result;
1.72      albertel  584: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43      albertel  585: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
                    586: 	    $result="<b>ok</b>";
1.49      albertel  587: 	    $good++;
1.43      albertel  588: 	} else {
                    589: 	    $result=$answer;
                    590: 	    $warnings++;
1.49      albertel  591: 	    if ($answer eq 'con_lost') {
                    592: 		$bad++;
                    593: 		$warnings++;
1.50      albertel  594: 	    } else {
                    595: 		$good++; #self connection
1.49      albertel  596: 	    }
1.43      albertel  597: 	}
                    598: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46      albertel  599: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1       albertel  600:     }
1.46      albertel  601:     &log($fh,"</table>");
1.49      albertel  602:     print "\n$good good, $bad bad connections\n";
1.43      albertel  603:     &errout($fh);
1.46      albertel  604: }
                    605: 
                    606: 
1.1       albertel  607: # ------------------------------------------------------------ Delayed messages
1.46      albertel  608: sub check_delayed_msg {
1.72      albertel  609:     my ($fh)=@_;
1.48      albertel  610:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.43      albertel  611:     print "checking buffers\n";
1.46      albertel  612:     
                    613:     &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1       albertel  614: 
1.46      albertel  615:     my $unsend=0;
1.1       albertel  616: 
1.46      albertel  617:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
                    618:     while (my $line=<$dfh>) {
                    619: 	my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
                    620: 	if ($sdf eq 'F') { 
                    621: 	    my $local=localtime($time);
                    622: 	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
                    623: 	    $warnings++;
1.43      albertel  624: 	}
1.46      albertel  625: 	if ($sdf eq 'S') { $unsend--; }
                    626: 	if ($sdf eq 'D') { $unsend++; }
1.1       albertel  627:     }
1.46      albertel  628: 
1.48      albertel  629:     &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.95      raeburn   630:     if ($unsend > 0) {
                    631:         $warnings=$warnings+5*$unsend;
                    632:     }
1.1       albertel  633: 
1.43      albertel  634:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48      albertel  635:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68      www       636: # list directory with delayed messages and remember offline servers
                    637:     my %servers=();
1.43      albertel  638:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68      www       639:     while (my $line=<DFH>) {
                    640:         my ($server)=($line=~/\.(\w+)$/);
                    641:         if ($server) { $servers{$server}=1; }
1.48      albertel  642: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  643:     }
1.48      albertel  644:     &log($fh,"</pre>\n");
1.43      albertel  645:     close (DFH);
1.95      raeburn   646:     my %hostname = &Apache::lonnet::all_hostnames();
                    647:     my $numhosts = scalar(keys(%hostname));
1.68      www       648: # pong to all servers that have delayed messages
                    649: # this will trigger a reverse connection, which should flush the buffers
1.95      raeburn   650:     foreach my $tryserver (sort(keys(%servers))) {
                    651:         if ($hostname{$tryserver} || !$numhosts) {
                    652:             my $answer;
                    653:             eval {
                    654:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    655:                 alarm(20);
                    656:                 $answer = &Apache::lonnet::reply("pong",$tryserver);
                    657:                 alarm(0);
                    658:             };
                    659:             if ($@ && $@ =~ m/TIMEOUT/) {
                    660:                 &log($fh,"Attempted pong to $tryserver timed out<br />");
                    661:                 print "time out while contacting: $tryserver for pong\n";
                    662:             } else {
                    663:                 &log($fh,"Pong to $tryserver: $answer<br />");
                    664:             }
1.91      raeburn   665:         } else {
1.95      raeburn   666:             &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
1.91      raeburn   667:         }
1.68      www       668:     }
1.46      albertel  669: }
1.1       albertel  670: 
1.46      albertel  671: sub finish_logging {
                    672:     my ($fh)=@_;
1.48      albertel  673:     &log($fh,"<a name='errcount' />\n");
1.43      albertel  674:     $totalcount=$notices+4*$warnings+100*$errors;
                    675:     &errout($fh);
1.46      albertel  676:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
                    677:     my $now=time;
                    678:     my $date=localtime($now);
1.48      albertel  679:     &log($fh,"<hr />$date ($now)</body></html>\n");
1.43      albertel  680:     print "lon-status webpage updated\n";
                    681:     $fh->close();
1.46      albertel  682: 
                    683:     if ($errors) { $simplestatus{'errors'}=$errors; }
                    684:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
                    685:     if ($notices) { $simplestatus{'notices'}=$notices; }
                    686:     $simplestatus{'time'}=time;
1.1       albertel  687: }
                    688: 
1.46      albertel  689: sub log_simplestatus {
1.73      albertel  690:     rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46      albertel  691:     
1.43      albertel  692:     my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
                    693:     foreach (keys %simplestatus) {
                    694: 	print $sfh $_.'='.$simplestatus{$_}.'&';
                    695:     }
                    696:     print $sfh "\n";
                    697:     $sfh->close();
1.41      www       698: }
1.46      albertel  699: 
1.84      raeburn   700: sub write_loncaparevs {
1.90      raeburn   701:     print "Retrieving LON-CAPA version information\n";
1.84      raeburn   702:     if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                    703:         my %hostname = &Apache::lonnet::all_hostnames();
                    704:         foreach my $id (sort(keys(%hostname))) {
                    705:             if ($id ne '') {
1.90      raeburn   706:                 my $loncaparev;
                    707:                 eval {
                    708:                     local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    709:                     alarm(10);
                    710:                     $loncaparev =
                    711:                         &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
                    712:                     alarm(0);
                    713:                 };
                    714:                 if ($@ && $@ =~ m/TIMEOUT/) {
                    715:                     print "time out while contacting lonHost: $id for version\n";   
                    716:                 }
1.86      raeburn   717:                 if ($loncaparev =~ /^[\w.\-]+$/) {
1.84      raeburn   718:                     print $fh $id.':'.$loncaparev."\n";
                    719:                 }
                    720:             }
                    721:         }
                    722:         close($fh);
                    723:     }
                    724:     return;
                    725: }
                    726: 
1.85      raeburn   727: sub write_serverhomeIDs {
1.90      raeburn   728:     print "Retrieving LON-CAPA lonHostID information\n";
1.85      raeburn   729:     if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                    730:         my %name_to_host = &Apache::lonnet::all_names();
                    731:         foreach my $name (sort(keys(%name_to_host))) {
                    732:             if ($name ne '') {
                    733:                 if (ref($name_to_host{$name}) eq 'ARRAY') {
1.90      raeburn   734:                     my $serverhomeID;
                    735:                     eval {
                    736:                         local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    737:                         alarm(10);
                    738:                         $serverhomeID = 
                    739:                             &Apache::lonnet::get_server_homeID($name,1,'loncron');
                    740:                         alarm(0);
                    741:                     };
                    742:                     if ($@ && $@ =~ m/TIMEOUT/) {
                    743:                         print "Time out while contacting server: $name\n"; 
                    744:                     }
1.85      raeburn   745:                     if ($serverhomeID ne '') {
                    746:                         print $fh $name.':'.$serverhomeID."\n";
                    747:                     } else {
                    748:                         print $fh $name.':'.$name_to_host{$name}->[0]."\n";
                    749:                     }
                    750:                 }
                    751:             }
                    752:         }
                    753:         close($fh);
                    754:     }
                    755:     return;
                    756: }
                    757: 
1.46      albertel  758: sub send_mail {
1.43      albertel  759:     print "sending mail\n";
1.79      raeburn   760:     my $defdom = $perlvar{'lonDefDomain'};
                    761:     my $origmail = $perlvar{'lonAdmEMail'};
1.78      raeburn   762:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
                    763:                                    'lonstatusmail',$defdom,$origmail);
1.54      www       764:     if ($totalcount>2500) {
1.43      albertel  765: 	$emailto.=",$perlvar{'lonSysEMail'}";
                    766:     }
1.46      albertel  767:     my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
1.52      albertel  768: 
1.58      albertel  769:     my $result=system("metasend -b -S 4000000 -t $emailto -s '$subj' -f $statusdir/index.html -m text/html >& /dev/null");
1.52      albertel  770:     if ($result != 0) {
                    771: 	$result=system("mail -s '$subj' $emailto < $statusdir/index.html");
                    772:     }
1.1       albertel  773: }
1.46      albertel  774: 
1.49      albertel  775: sub usage {
                    776:     print(<<USAGE);
                    777: loncron - housekeeping program that checks up on various parts of Lon-CAPA
                    778: 
                    779: Options:
1.71      albertel  780:    --help     Display 
1.49      albertel  781:    --noemail  Do not send the status email
                    782:    --justcheckconnections  Only check the current status of the lonc/d
                    783:                                 connections, do not send emails do not
                    784:                                 check if the daemons are running, do not
                    785:                                 generate lon-status
                    786:    --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
                    787:                                 running, do not send emails do not
                    788:                                 check the lonc/d connections, do not
                    789:                                 generate lon-status
1.59      albertel  790:    --justreload            Only tell the daemons to reload the config files,
                    791: 				do not send emails do not
                    792:                                 check if the daemons are running, do not
                    793:                                 generate lon-status
1.49      albertel  794:                            
                    795: USAGE
                    796: }
                    797: 
1.46      albertel  798: # ================================================================ Main Program
                    799: sub main () {
1.71      albertel  800:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.59      albertel  801: 	$justreload);
1.49      albertel  802:     &GetOptions("help"                 => \$help,
                    803: 		"justcheckdaemons"     => \$justcheckdaemons,
                    804: 		"noemail"              => \$noemail,
1.59      albertel  805: 		"justcheckconnections" => \$justcheckconnections,
                    806: 		"justreload"           => \$justreload
1.49      albertel  807: 		);
                    808:     if ($help) { &usage(); return; }
1.46      albertel  809: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
                    810:     my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
                    811:     %perlvar=%{$perlvarref};
                    812:     undef $perlvarref;
                    813:     delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
                    814:     delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.95.10.1! raeburn   815:     delete $perlvar{'reCAPTCHA_PRIVATE'}; # remove since sensitive and not needed 
        !           816:     delete $perlvar{'reCAPTCHA_PUBLIC'}; # remove since sensitive and not needed
1.75      albertel  817:     chdir($perlvar{'lonDaemons'});
1.46      albertel  818: # --------------------------------------- Make sure that LON-CAPA is configured
                    819: # I only test for one thing here (lonHostID).  This is just a safeguard.
                    820:     if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
                    821: 	print("Unconfigured machine.\n");
                    822: 	my $emailto=$perlvar{'lonSysEMail'};
                    823: 	my $hostname=`/bin/hostname`;
                    824: 	chop $hostname;
                    825: 	$hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
                    826: 	my $subj="LON: Unconfigured machine $hostname";
                    827: 	system("echo 'Unconfigured machine $hostname.' |\
                    828:  mailto $emailto -s '$subj' > /dev/null");
                    829: 	exit 1;
                    830:     }
                    831: 
                    832: # ----------------------------- Make sure this process is running from user=www
                    833:     my $wwwid=getpwnam('www');
                    834:     if ($wwwid!=$<) {
                    835: 	print("User ID mismatch.  This program must be run as user 'www'\n");
                    836: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                    837: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
                    838: 	system("echo 'User ID mismatch.  loncron must be run as user www.' |\
                    839:  mailto $emailto -s '$subj' > /dev/null");
                    840: 	exit 1;
                    841:     }
                    842: 
1.72      albertel  843: # -------------------------------------------- Force reload of host information
                    844:     &Apache::lonnet::load_hosts_tab(1);
                    845:     &Apache::lonnet::load_domain_tab(1);
                    846:     &Apache::lonnet::get_iphost(1);
1.46      albertel  847: 
1.81      raeburn   848: # ----------------------------------------- Force firewall update for lond port  
                    849: 
                    850:     if ((!$justcheckdaemons) && (!$justreload)) {
                    851:         my $now = time;
                    852:         my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
                    853:                       $now.$$.int(rand(10000));
                    854:         if (open(my $fh,">$tmpfile")) {
                    855:             my %iphosts = &Apache::lonnet::get_iphost();
                    856:             foreach my $key (keys(%iphosts)) {
                    857:                 print $fh "$key\n";
                    858:             }
                    859:             close($fh);
1.89      raeburn   860:             if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
                    861:                 my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
                    862:                 system("$execpath $tmpfile");
                    863:                 unlink('/tmp/lock_lciptables');  # Remove the lock file. 
                    864:             }
1.88      raeburn   865:             unlink($tmpfile);
1.81      raeburn   866:         }
                    867:     }
                    868: 
1.46      albertel  869: # ---------------------------------------------------------------- Start report
                    870: 
                    871:     $errors=0;
                    872:     $warnings=0;
                    873:     $notices=0;
                    874: 
                    875: 	
1.49      albertel  876:     my $fh;
1.59      albertel  877:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72      albertel  878: 	$fh=&start_logging();
1.49      albertel  879: 
                    880: 	&log_machine_info($fh);
                    881: 	&clean_tmp($fh);
                    882: 	&clean_lonIDs($fh);
                    883: 	&check_httpd_logs($fh);
                    884: 	&rotate_lonnet_logs($fh);
1.73      albertel  885: 	&rotate_other_logs($fh);
1.49      albertel  886:     }
1.59      albertel  887:     if (!$justcheckconnections && !$justreload) {
1.76      albertel  888: 	&checkon_daemon($fh,'lonmemcached',40000);
1.49      albertel  889: 	&checkon_daemon($fh,'lonsql',200000);
1.63      albertel  890: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
                    891: 	    &checkon_daemon($fh,'lond',40000,'USR2');
                    892: 	}
1.71      albertel  893: 	&checkon_daemon($fh,'lonc',40000,'USR1');
1.70      raeburn   894:         &checkon_daemon($fh,'lonmaxima',40000);
1.80      www       895:         &checkon_daemon($fh,'lonr',40000);
1.49      albertel  896:     }
1.59      albertel  897:     if ($justreload) {
                    898: 	&checkon_daemon($fh,'lond',40000,'USR2');
1.71      albertel  899: 	&checkon_daemon($fh,'lonc',40000,'USR2');
1.59      albertel  900:     }
1.63      albertel  901:     if ($justcheckconnections) {
1.72      albertel  902: 	&test_connections($fh);
1.49      albertel  903:     }
1.59      albertel  904:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72      albertel  905: 	&check_delayed_msg($fh);
1.49      albertel  906: 	&finish_logging($fh);
                    907: 	&log_simplestatus();
1.87      raeburn   908:         &write_loncaparevs();
                    909:         &write_serverhomeIDs();
1.49      albertel  910: 	
                    911: 	if ($totalcount>200 && !$noemail) { &send_mail(); }
                    912:     }
1.46      albertel  913: }
                    914: 
                    915: &main();
1.1       albertel  916: 1;
                    917: 
                    918: 
                    919: 
                    920: 
                    921: 
                    922: 
                    923: 
                    924: 

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