File:  [LON-CAPA] / loncom / loncron
Revision 1.103.2.7: download - view: text, annotated - select for diffs
Wed May 6 14:40:08 2020 UTC (3 years, 11 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.103: preferred, unified
- For 2.11
  Backport 1.122 1.123

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

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