Annotation of loncom/loncron, revision 1.103.2.11

1.1       albertel    1: #!/usr/bin/perl
                      2: 
1.47      albertel    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
                      4: #
1.103.2.11! raeburn     5: # $Id: loncron,v 1.103.2.10 2021/02/01 01:33:17 raeburn Exp $
1.47      albertel    6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
1.1       albertel   28: #
1.24      www        29: 
                     30: $|=1;
1.48      albertel   31: use strict;
1.1       albertel   32: 
1.26      harris41   33: use lib '/home/httpd/lib/perl/';
                     34: use LONCAPA::Configuration;
1.96      raeburn    35: use LONCAPA::Checksumming;
1.89      raeburn    36: use LONCAPA;
1.72      albertel   37: use Apache::lonnet;
1.79      raeburn    38: use Apache::loncommon;
1.26      harris41   39: 
1.103.2.9  raeburn    40: use LWP::UserAgent();
                     41: use HTTP::Request();
1.1       albertel   42: use IO::File;
                     43: use IO::Socket;
1.48      albertel   44: use HTML::Entities;
1.49      albertel   45: use Getopt::Long;
1.103.2.11! raeburn    46: use GDBM_File qw(GDBM_READER);
        !            47: use Storable qw(thaw nfreeze);
1.103.2.9  raeburn    48: use File::ReadBackwards;
1.103.2.3  raeburn    49: use File::Copy;
1.103.2.7  raeburn    50: use Sys::Hostname::FQDN();
                     51: 
1.46      albertel   52: #globals
                     53: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
                     54: 
                     55: my $statusdir="/home/httpd/html/lon-status";
                     56: 
1.1       albertel   57: 
                     58: # --------------------------------------------------------- Output error status
                     59: 
1.46      albertel   60: sub log {
                     61:     my $fh=shift;
                     62:     if ($fh) {	print $fh @_  }
                     63: }
                     64: 
1.1       albertel   65: sub errout {
                     66:    my $fh=shift;
1.46      albertel   67:    &log($fh,(<<ENDERROUT));
1.48      albertel   68:      <table border="2" bgcolor="#CCCCCC">
1.1       albertel   69:      <tr><td>Notices</td><td>$notices</td></tr>
                     70:      <tr><td>Warnings</td><td>$warnings</td></tr>
                     71:      <tr><td>Errors</td><td>$errors</td></tr>
1.48      albertel   72:      </table><p><a href="#top">Top</a></p>
1.1       albertel   73: ENDERROUT
                     74: }
                     75: 
1.73      albertel   76: sub rotate_logfile {
                     77:     my ($file,$fh,$description) = @_;
                     78:     my $size=(stat($file))[7];
                     79:     if ($size>40000) {
                     80: 	&log($fh,"<p>Rotating $description ...</p>");
                     81: 	rename("$file.2","$file.3");
                     82: 	rename("$file.1","$file.2");
                     83: 	rename("$file","$file.1");
1.103.2.9  raeburn    84:     }
1.73      albertel   85: }
                     86: 
1.42      albertel   87: sub start_daemon {
1.50      albertel   88:     my ($fh,$daemon,$pidfile,$args) = @_;
1.44      albertel   89:     my $progname=$daemon;
1.71      albertel   90:     if ($daemon eq 'lonc') {
1.103.2.9  raeburn    91: 	$progname='loncnew';
1.44      albertel   92:     }
1.51      albertel   93:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
1.73      albertel   94:     &rotate_logfile($error_fname,$fh,'error logs');
1.74      albertel   95:     if ($daemon eq 'lonc') {
                     96: 	&clean_sockets($fh);
                     97:     }
1.51      albertel   98:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60      albertel   99:     sleep 1;
1.42      albertel  100:     if (-e $pidfile) {
1.48      albertel  101: 	&log($fh,"<p>Seems like it started ...</p>");
1.42      albertel  102: 	my $lfh=IO::File->new("$pidfile");
                    103: 	my $daemonpid=<$lfh>;
                    104: 	chomp($daemonpid);
1.62      albertel  105: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42      albertel  106: 	    return 1;
                    107: 	} else {
                    108: 	    return 0;
                    109: 	}
                    110:     }
1.48      albertel  111:     &log($fh,"<p>Seems like that did not work!</p>");
1.42      albertel  112:     $errors++;
                    113:     return 0;
                    114: }
                    115: 
                    116: sub checkon_daemon {
1.59      albertel  117:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42      albertel  118: 
1.63      albertel  119:     my $result;
1.48      albertel  120:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57      albertel  121:     printf("%-15s ",$daemon);
1.103.2.2  raeburn   122:     if ($fh) {
                    123:         if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
                    124: 	    if (open(DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
                    125: 	        while (my $line=<DFH>) {
                    126: 	            &log($fh,"$line");
                    127: 	            if ($line=~/INFO/) { $notices++; }
                    128: 	            if ($line=~/WARNING/) { $notices++; }
                    129: 	            if ($line=~/CRITICAL/) { $warnings++; }
                    130: 	        }
                    131: 	        close (DFH);
                    132:             }
                    133:         }
                    134:         &log($fh,"</tt></p>");
1.42      albertel  135:     }
1.103.2.9  raeburn   136: 
1.42      albertel  137:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
1.103.2.9  raeburn   138:  
1.42      albertel  139:     my $restartflag=1;
1.46      albertel  140:     my $daemonpid;
1.42      albertel  141:     if (-e $pidfile) {
                    142: 	my $lfh=IO::File->new("$pidfile");
1.46      albertel  143: 	$daemonpid=<$lfh>;
1.42      albertel  144: 	chomp($daemonpid);
1.62      albertel  145: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46      albertel  146: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59      albertel  147: 	    if ($send) { &log($fh,", sending $send"); }
1.46      albertel  148: 	    &log($fh,"</h3>");
1.59      albertel  149: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
                    150: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42      albertel  151: 	    $restartflag=0;
1.59      albertel  152: 	    if ($send eq 'USR2') {
1.63      albertel  153: 		$result = 'reloaded';
1.59      albertel  154: 		print "reloaded\n";
                    155: 	    } else {
1.63      albertel  156: 		$result = 'running';
1.59      albertel  157: 		print "running\n";
                    158: 	    }
1.42      albertel  159: 	} else {
                    160: 	    $errors++;
1.46      albertel  161: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42      albertel  162: 	    $restartflag=1;
1.46      albertel  163: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42      albertel  164: 	}
                    165:     }
                    166:     if ($restartflag==1) {
                    167: 	$simplestatus{$daemon}='off';
                    168: 	$errors++;
1.57      albertel  169: 	my $kadaemon=$daemon;
                    170: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.101     raeburn   171: 	&log($fh,'<br /><font color="red">Killall '.$daemon.': '.
1.57      albertel  172: 	    `killall $kadaemon 2>&1`.' - ');
1.60      albertel  173: 	sleep 1;
1.46      albertel  174: 	&log($fh,unlink($pidfile).' - '.
1.57      albertel  175: 	    `killall -9 $kadaemon 2>&1`.
1.101     raeburn   176: 	    '</font><br />');
1.46      albertel  177: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
1.103.2.2  raeburn   178: 
1.50      albertel  179: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  180: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  181: 	    $simplestatus{$daemon}='restarted';
1.63      albertel  182: 	    $result = 'started';
1.42      albertel  183: 	    print "started\n";
                    184: 	} else {
                    185: 	    $errors++;
1.46      albertel  186: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  187: 	    &log($fh,"<p>Give it one more try ...</p>");
1.42      albertel  188: 	    print " ";
1.50      albertel  189: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  190: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  191: 		$simplestatus{$daemon}='restarted';
1.63      albertel  192: 		$result = 'started';
1.42      albertel  193: 		print "started\n";
                    194: 	    } else {
1.63      albertel  195: 		$result = 'failed';
1.42      albertel  196: 		print " failed\n";
                    197: 		$simplestatus{$daemon}='failed';
                    198: 		$errors++; $errors++;
1.46      albertel  199: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  200: 		&log($fh,"<p>Unable to start $daemon</p>");
1.42      albertel  201: 	    }
                    202: 	}
1.103.2.2  raeburn   203:         if ($fh) {
                    204: 	    if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
                    205: 	        &log($fh,"<p><pre>");
                    206: 	        if (open(DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
                    207: 	            while (my $line=<DFH>) { 
                    208: 		        &log($fh,"$line");
                    209: 		        if ($line=~/WARNING/) { $notices++; }
                    210: 		        if ($line=~/CRITICAL/) { $notices++; }
                    211: 	            }
                    212: 	            close (DFH);
                    213:                 }
                    214: 	        &log($fh,"</pre></p>");
                    215: 	    }
                    216:         }
1.42      albertel  217:     }
1.103.2.9  raeburn   218: 
1.46      albertel  219:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.73      albertel  220:     &rotate_logfile($fname,$fh,'logs');
1.42      albertel  221: 
                    222:     &errout($fh);
1.63      albertel  223:     return $result;
1.42      albertel  224: }
1.1       albertel  225: 
1.46      albertel  226: # --------------------------------------------------------------------- Machine
                    227: sub log_machine_info {
                    228:     my ($fh)=@_;
1.48      albertel  229:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46      albertel  230:     &log($fh,"<h3>loadavg</h3>");
1.103.2.9  raeburn   231: 
1.103.2.10  raeburn   232:     my $cpucount;
                    233:     if (open(PIPE,"lscpu |grep '^CPU(s)' 2>&1 |")) {
                    234:         my $info = <PIPE>;
                    235:         chomp($info);
                    236:         ($cpucount) = ($info =~ /^\QCPU(s):\E\s+(\d+)$/);
                    237:         close(PIPE);
                    238:     }
                    239:     if (!$cpucount) {
                    240:         $cpucount = 1;
                    241:     }
                    242:     my %loadtarget = (
                    243:                         error => 4.0*$cpucount,
                    244:                         warn  => 2.0*$cpucount,
                    245:                         note  => 1.0*$cpucount,
                    246:                      );
1.46      albertel  247:     open (LOADAVGH,"/proc/loadavg");
                    248:     my $loadavg=<LOADAVGH>;
                    249:     close (LOADAVGH);
1.103.2.9  raeburn   250: 
1.46      albertel  251:     &log($fh,"<tt>$loadavg</tt>");
1.103.2.9  raeburn   252: 
1.46      albertel  253:     my @parts=split(/\s+/,$loadavg);
1.103.2.10  raeburn   254:     if ($parts[1]>$loadtarget{'error'}) {
1.46      albertel  255: 	$errors++;
1.103.2.10  raeburn   256:     } elsif ($parts[1]>$loadtarget{'warn'}) {
1.46      albertel  257: 	$warnings++;
1.103.2.10  raeburn   258:     } elsif ($parts[1]>$loadtarget{'note'}) {
1.46      albertel  259: 	$notices++;
                    260:     }
1.13      harris41  261: 
1.46      albertel  262:     &log($fh,"<h3>df</h3>");
                    263:     &log($fh,"<pre>");
1.14      harris41  264: 
1.46      albertel  265:     open (DFH,"df|");
1.103.2.9  raeburn   266:     while (my $line=<DFH>) {
                    267: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  268: 	@parts=split(/\s+/,$line);
                    269: 	my $usage=$parts[4];
                    270: 	$usage=~s/\W//g;
1.103.2.9  raeburn   271: 	if ($usage>90) {
1.46      albertel  272: 	    $warnings++;
1.103.2.9  raeburn   273: 	    $notices++;
1.46      albertel  274: 	} elsif ($usage>80) {
                    275: 	    $warnings++;
                    276: 	} elsif ($usage>60) {
                    277: 	    $notices++;
1.31      albertel  278: 	}
1.46      albertel  279: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1       albertel  280:     }
1.46      albertel  281:     close (DFH);
                    282:     &log($fh,"</pre>");
1.1       albertel  283: 
                    284: 
1.46      albertel  285:     &log($fh,"<h3>ps</h3>");
                    286:     &log($fh,"<pre>");
                    287:     my $psproc=0;
1.1       albertel  288: 
1.53      albertel  289:     open (PSH,"ps aux --cols 140 |");
1.103.2.9  raeburn   290:     while (my $line=<PSH>) {
                    291: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  292: 	$psproc++;
                    293:     }
                    294:     close (PSH);
                    295:     &log($fh,"</pre>");
1.1       albertel  296: 
1.46      albertel  297:     if ($psproc>200) { $notices++; }
                    298:     if ($psproc>250) { $notices++; }
1.1       albertel  299: 
1.61      albertel  300:     &log($fh,"<h3>distprobe</h3>");
                    301:     &log($fh,"<pre>");
1.98      raeburn   302:     &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
1.61      albertel  303:     &log($fh,"</pre>");
                    304: 
1.46      albertel  305:     &errout($fh);
                    306: }
1.1       albertel  307: 
1.46      albertel  308: sub start_logging {
1.43      albertel  309:     my $fh=IO::File->new(">$statusdir/newstatus.html");
                    310:     my %simplestatus=();
1.46      albertel  311:     my $now=time;
                    312:     my $date=localtime($now);
1.103.2.9  raeburn   313: 
1.46      albertel  314: 
                    315:     &log($fh,(<<ENDHEADERS));
1.101     raeburn   316: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    317: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1.1       albertel  318: <head>
                    319: <title>LON Status Report $perlvar{'lonHostID'}</title>
1.101     raeburn   320: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1.1       albertel  321: </head>
1.3       www       322: <body bgcolor="#AAAAAA">
1.48      albertel  323: <a name="top" />
1.1       albertel  324: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
                    325: <h2>$date ($now)</h2>
                    326: <ol>
1.48      albertel  327: <li><a href="#configuration">Configuration</a></li>
                    328: <li><a href="#machine">Machine Information</a></li>
                    329: <li><a href="#tmp">Temporary Files</a></li>
                    330: <li><a href="#tokens">Session Tokens</a></li>
1.102     raeburn   331: <li><a href="#webdav">WebDAV Session Tokens</a></li>
1.48      albertel  332: <li><a href="#httpd">httpd</a></li>
                    333: <li><a href="#lonsql">lonsql</a></li>
                    334: <li><a href="#lond">lond</a></li>
                    335: <li><a href="#lonc">lonc</a></li>
                    336: <li><a href="#lonnet">lonnet</a></li>
                    337: <li><a href="#connections">Connections</a></li>
                    338: <li><a href="#delayed">Delayed Messages</a></li>
                    339: <li><a href="#errcount">Error Count</a></li>
1.1       albertel  340: </ol>
1.48      albertel  341: <hr />
                    342: <a name="configuration" />
1.1       albertel  343: <h2>Configuration</h2>
                    344: <h3>PerlVars</h3>
1.48      albertel  345: <table border="2">
1.1       albertel  346: ENDHEADERS
                    347: 
1.46      albertel  348:     foreach my $varname (sort(keys(%perlvar))) {
1.48      albertel  349: 	&log($fh,"<tr><td>$varname</td><td>".
                    350: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43      albertel  351:     }
1.48      albertel  352:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72      albertel  353:     my %hostname = &Apache::lonnet::all_hostnames();
                    354:     foreach my $id (sort(keys(%hostname))) {
                    355: 	my $role = (&Apache::lonnet::is_library($id) ? 'library'
                    356: 		                                     : 'access');
1.46      albertel  357: 	&log($fh,
1.72      albertel  358: 	    "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
                    359: 	    "</td><td>".$role.
                    360: 	    "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
                    361:     }
1.101     raeburn   362:     &log($fh,"</table><h3>Spare Hosts</h3>");
                    363:     if (keys(%Apache::lonnet::spareid) > 0) {
                    364:         &log($fh,"<ul>");
                    365:         foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
                    366: 	    &log($fh,"<li>$type\n<ol>");
                    367: 	    foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
                    368: 	        &log($fh,"<li>$id</li>\n");
                    369: 	    }
                    370: 	    &log($fh,"</ol>\n</li>\n");
                    371:         }
                    372:         &log($fh,"</ul>\n");
                    373:     } else {
                    374:         &log($fh,"No spare hosts specified<br />\n");
1.43      albertel  375:     }
1.46      albertel  376:     return $fh;
                    377: }
1.11      www       378: 
                    379: # --------------------------------------------------------------- clean out tmp
1.46      albertel  380: sub clean_tmp {
                    381:     my ($fh)=@_;
1.48      albertel  382:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.82      raeburn   383:     my ($cleaned,$old,$removed) = (0,0,0);
                    384:     my %errors = (
                    385:                      dir       => [],
                    386:                      file      => [],
                    387:                      failopen  => [],
                    388:                  );
                    389:     my %error_titles = (
                    390:                          dir       => 'failed to remove empty directory:',
                    391:                          file      => 'failed to unlike stale file',
                    392:                          failopen  => 'failed to open file or directory'
                    393:                        );
                    394:     ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
                    395:     &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
                    396:     foreach my $key (sort(keys(%errors))) {
                    397:         if (ref($errors{$key}) eq 'ARRAY') {
                    398:             if (@{$errors{$key}} > 0) {
                    399:                 &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
                    400:                      join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
                    401:             }
                    402:         }
                    403:     }
                    404: }
                    405: 
                    406: sub recursive_clean_tmp {
                    407:     my ($subdir,$cleaned,$old,$removed,$errors) = @_;
                    408:     my $base = "$perlvar{'lonDaemons'}/tmp";
                    409:     my $path = $base;
                    410:     next if ($subdir =~ m{\.\./});
                    411:     next unless (ref($errors) eq 'HASH');
                    412:     unless ($subdir eq '') {
                    413:         $path .= '/'.$subdir;
                    414:     }
                    415:     if (opendir(my $dh,"$path")) {
                    416:         while (my $file = readdir($dh)) {
                    417:             next if ($file =~ /^\.\.?$/);
                    418:             my $fname = "$path/$file";
                    419:             if (-d $fname) {
                    420:                 my $innerdir;
                    421:                 if ($subdir eq '') {
                    422:                     $innerdir = $file;
                    423:                 } else {
                    424:                     $innerdir = $subdir.'/'.$file;
                    425:                 }
                    426:                 ($cleaned,$old,$removed) = 
                    427:                      &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
                    428:                 my @doms = &Apache::lonnet::current_machine_domains();
1.103.2.9  raeburn   429: 
1.82      raeburn   430:                 if (open(my $dirhandle,$fname)) {
                    431:                     unless (($innerdir eq 'helprequests') ||
                    432:                             (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
                    433:                         my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
1.103.2.9  raeburn   434:                                       join('&&',@contents)."\n";
1.82      raeburn   435:                         if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
                    436:                             closedir($dirhandle);
                    437:                             if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
                    438:                                 if (rmdir($fname)) {
                    439:                                     $removed ++;
                    440:                                 } elsif (ref($errors->{dir}) eq 'ARRAY') {
                    441:                                     push(@{$errors->{dir}},$fname);
                    442:                                 }
                    443:                             }
                    444:                         }
                    445:                     } else {
                    446:                         closedir($dirhandle);
                    447:                     }
                    448:                 }
                    449:             } else {
                    450:                 my ($dev,$ino,$mode,$nlink,
                    451:                     $uid,$gid,$rdev,$size,
                    452:                     $atime,$mtime,$ctime,
                    453:                     $blksize,$blocks)=stat($fname);
                    454:                 my $now=time;
                    455:                 my $since=$now-$mtime;
                    456:                 if ($since>$perlvar{'lonExpire'}) {
                    457:                     if ($subdir eq '') {
                    458:                         my $line='';
                    459:                         if ($fname =~ /\.db$/) {
                    460:                             if (unlink($fname)) {
                    461:                                 $cleaned++;
                    462:                             } elsif (ref($errors->{file}) eq 'ARRAY') {
                    463:                                 push(@{$errors->{file}},$fname);
                    464:                             }
                    465:                         } elsif (open(PROBE,$fname)) {
                    466:                             my $line='';
                    467:                             $line=<PROBE>;
                    468:                             close(PROBE);
                    469:                             if ($line=~/^CHECKOUTTOKEN\&/) {
                    470:                                 if ($since>365*$perlvar{'lonExpire'}) {
                    471:                                     if (unlink($fname)) {
                    472:                                         $cleaned++; 
                    473:                                     } elsif (ref($errors->{file}) eq 'ARRAY') {
                    474:                                         push(@{$errors->{file}},$fname);
                    475:                                     }
                    476:                                 } else {
                    477:                                     $old++;
                    478:                                 }
                    479:                             } else {
                    480:                                 if (unlink($fname)) {
                    481:                                     $cleaned++;
                    482:                                 } elsif (ref($errors->{file}) eq 'ARRAY') {
                    483:                                     push(@{$errors->{file}},$fname);
                    484:                                 }
                    485:                             }
                    486:                         } elsif (ref($errors->{failopen}) eq 'ARRAY') {
1.103.2.9  raeburn   487:                             push(@{$errors->{failopen}},$fname);
1.82      raeburn   488:                         }
                    489:                     } else {
                    490:                         if (unlink($fname)) {
                    491:                             $cleaned++;
                    492:                         } elsif (ref($errors->{file}) eq 'ARRAY') {
                    493:                             push(@{$errors->{file}},$fname);
                    494:                         }
                    495:                     }
                    496:                 }
                    497:             }
                    498:         }
                    499:         closedir($dh);
                    500:     } elsif (ref($errors->{failopen}) eq 'ARRAY') {
                    501:         push(@{$errors->{failopen}},$path);
1.43      albertel  502:     }
1.82      raeburn   503:     return ($cleaned,$old,$removed);
1.46      albertel  504: }
1.11      www       505: 
                    506: # ------------------------------------------------------------ clean out lonIDs
1.46      albertel  507: sub clean_lonIDs {
                    508:     my ($fh)=@_;
1.48      albertel  509:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46      albertel  510:     my $cleaned=0;
                    511:     my $active=0;
                    512:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.103.2.7  raeburn   513:         my $now=time;
                    514:         if (-l $fname) {
                    515:             my $linkfname = readlink($fname);
                    516:             if (-f $linkfname) {
                    517:                 if ($linkfname =~ m{^$perlvar{'lonIDsDir'}/[^/]+\.id$}) {
                    518:                     my @data = stat($linkfname);
                    519:                     my $mtime = $data[9];
                    520:                     my $since=$now-$mtime;
                    521:                     if ($since>$perlvar{'lonExpire'}) {
                    522:                         if (unlink($linkfname)) {
                    523:                             $cleaned++;
                    524:                             &log($fh,"Unlinking $linkfname<br />");
                    525:                             unlink($fname);
                    526:                         }
                    527:                     }
                    528:                 }
                    529:             } else {
                    530:                unlink($fname);
                    531:             }
                    532:         } elsif (-f $fname) {
                    533:             my @data = stat($fname);
                    534:             my $mtime = $data[9];
                    535:             my $since=$now-$mtime;
                    536:             if ($since>$perlvar{'lonExpire'}) {
                    537:                 if (unlink($fname)) {
                    538:                     $cleaned++;
                    539:                     &log($fh,"Unlinking $fname<br />");
                    540:                 }
                    541:             } else {
                    542:                 $active++;
                    543:             }
                    544:         }
1.46      albertel  545:     }
1.48      albertel  546:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46      albertel  547:     &log($fh,"<h3>$active open session(s)</h3>");
                    548: }
1.43      albertel  549: 
1.103.2.5  raeburn   550: # -------------------------------------------------------- clean out balanceIDs
                    551: 
                    552: sub clean_balanceIDs {
                    553:     my ($fh)=@_;
                    554:     &log($fh,'<hr /><a name="balcookies" /><h2>Session Tokens</h2>');
                    555:     my $cleaned=0;
                    556:     my $active=0;
                    557:     if (-d $perlvar{'lonBalanceDir'}) {
1.103.2.8  raeburn   558:         while (my $fname=<$perlvar{'lonBalanceDir'}/*.id>) {
1.103.2.5  raeburn   559:             my ($dev,$ino,$mode,$nlink,
                    560:                 $uid,$gid,$rdev,$size,
                    561:                 $atime,$mtime,$ctime,
                    562:                 $blksize,$blocks)=stat($fname);
                    563:             my $now=time;
                    564:             my $since=$now-$mtime;
                    565:             if ($since>$perlvar{'lonExpire'}) {
                    566:                 $cleaned++;
                    567:                 &log($fh,"Unlinking $fname<br />");
                    568:                 unlink("$fname");
                    569:             } else {
                    570:                 $active++;
                    571:             }
                    572:         }
                    573:     }
                    574:     &log($fh,"<p>Cleaned up ".$cleaned." stale balancer files</p>");
                    575:     &log($fh,"<h3>$active unexpired balancer files</h3>");
                    576: }
                    577: 
1.102     raeburn   578: # ------------------------------------------------ clean out webDAV Session IDs
                    579: sub clean_webDAV_sessionIDs {
                    580:     my ($fh)=@_;
                    581:     if ($perlvar{'lonRole'} eq 'library') {
                    582:         &log($fh,'<hr /><a name="webdav" /><h2>WebDAV Session Tokens</h2>');
                    583:         my $cleaned=0;
                    584:         my $active=0;
                    585:         my $now = time;
                    586:         if (-d $perlvar{'lonDAVsessDir'}) {
                    587:             while (my $fname=<$perlvar{'lonDAVsessDir'}/*>) {
                    588:                 my @stats = stat($fname);
                    589:                 my $since=$now-$stats[9];
                    590:                 if ($since>$perlvar{'lonExpire'}) {
                    591:                     $cleaned++;
                    592:                     &log($fh,"Unlinking $fname<br />");
                    593:                     unlink("$fname");
                    594:                 } else {
                    595:                     $active++;
                    596:                 }
                    597:             }
                    598:             &log($fh,"<p>Cleaned up ".$cleaned." stale webDAV session token(s).</p>");
                    599:             &log($fh,"<h3>$active open webDAV session(s)</h3>");
                    600:         }
                    601:     }
                    602: }
                    603: 
1.74      albertel  604: # ----------------------------------------------------------- clean out sockets
                    605: sub clean_sockets {
                    606:     my ($fh)=@_;
                    607:     my $cleaned=0;
                    608:     opendir(SOCKETS,$perlvar{'lonSockDir'});
                    609:     while (my $fname=readdir(SOCKETS)) {
                    610: 	next if (-d $fname 
1.80      www       611: 		 || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
1.74      albertel  612: 	$cleaned++;
                    613: 	&log($fh,"Unlinking $fname<br />");
                    614: 	unlink("/home/httpd/sockets/$fname");
                    615:     }
                    616:     &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
                    617: }
                    618: 
1.11      www       619: 
1.1       albertel  620: # ----------------------------------------------------------------------- httpd
1.46      albertel  621: sub check_httpd_logs {
                    622:     my ($fh)=@_;
1.94      raeburn   623:     if (open(PIPE,"./lchttpdlogs|")) {
1.93      raeburn   624:         while (my $line=<PIPE>) {
                    625:             &log($fh,$line);
                    626:             if ($line=~/\[error\]/) { $notices++; }
                    627:         }
                    628:         close(PIPE);
1.46      albertel  629:     }
1.43      albertel  630:     &errout($fh);
1.46      albertel  631: }
1.1       albertel  632: 
                    633: # ---------------------------------------------------------------------- lonnet
                    634: 
1.48      albertel  635: sub rotate_lonnet_logs {
1.46      albertel  636:     my ($fh)=@_;
1.48      albertel  637:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.100     bisitz    638:     print "Checking logs.\n";
1.43      albertel  639:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
                    640: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.103.2.9  raeburn   641: 	while (my $line=<DFH>) {
1.48      albertel  642: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  643: 	}
1.43      albertel  644: 	close (DFH);
                    645:     }
1.46      albertel  646:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.103.2.9  raeburn   647: 
1.43      albertel  648:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    649: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.103.2.9  raeburn   650: 	while (my $line=<DFH>) {
1.48      albertel  651: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  652: 	}
1.43      albertel  653: 	close (DFH);
1.46      albertel  654:     } else { &log($fh,"No perm log\n") }
1.43      albertel  655: 
1.46      albertel  656:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73      albertel  657:     &rotate_logfile($fname,$fh,'lonnet log');
1.1       albertel  658: 
1.46      albertel  659:     &log($fh,"</pre>");
1.43      albertel  660:     &errout($fh);
1.46      albertel  661: }
                    662: 
1.73      albertel  663: sub rotate_other_logs {
                    664:     my ($fh) = @_;
1.83      raeburn   665:     my %logs = (
                    666:                   autoenroll          => 'Auto Enroll log',
                    667:                   autocreate          => 'Create Course log',
                    668:                   searchcat           => 'Search Cataloguing log',
                    669:                   autoupdate          => 'Auto Update log',
                    670:                   refreshcourseids_db => 'Refresh CourseIDs db log',
                    671:                );
                    672:     foreach my $item (keys(%logs)) {
                    673:         my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
                    674:         &rotate_logfile($fname,$fh,$logs{$item});
                    675:     }
1.73      albertel  676: }
                    677: 
1.43      albertel  678: # ----------------------------------------------------------------- Connections
1.46      albertel  679: sub test_connections {
1.72      albertel  680:     my ($fh)=@_;
1.48      albertel  681:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.100     bisitz    682:     print "Testing connections.\n";
1.48      albertel  683:     &log($fh,"<table border='2'>");
1.49      albertel  684:     my ($good,$bad)=(0,0);
1.72      albertel  685:     my %hostname = &Apache::lonnet::all_hostnames();
                    686:     foreach my $tryserver (sort(keys(%hostname))) {
1.43      albertel  687: 	print(".");
1.46      albertel  688: 	my $result;
1.72      albertel  689: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43      albertel  690: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
                    691: 	    $result="<b>ok</b>";
1.49      albertel  692: 	    $good++;
1.43      albertel  693: 	} else {
                    694: 	    $result=$answer;
                    695: 	    $warnings++;
1.49      albertel  696: 	    if ($answer eq 'con_lost') {
                    697: 		$bad++;
                    698: 		$warnings++;
1.50      albertel  699: 	    } else {
                    700: 		$good++; #self connection
1.49      albertel  701: 	    }
1.43      albertel  702: 	}
                    703: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46      albertel  704: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1       albertel  705:     }
1.46      albertel  706:     &log($fh,"</table>");
1.49      albertel  707:     print "\n$good good, $bad bad connections\n";
1.43      albertel  708:     &errout($fh);
1.46      albertel  709: }
                    710: 
                    711: 
1.1       albertel  712: # ------------------------------------------------------------ Delayed messages
1.46      albertel  713: sub check_delayed_msg {
1.103.2.9  raeburn   714:     my ($fh,$weightsref,$exclusionsref)=@_;
1.48      albertel  715:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.100     bisitz    716:     print "Checking buffers.\n";
1.46      albertel  717:     
                    718:     &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1       albertel  719: 
1.46      albertel  720:     my $unsend=0;
1.103.2.9  raeburn   721:     my $ignored=0;
1.1       albertel  722: 
1.103.2.1  raeburn   723:     my %hostname = &Apache::lonnet::all_hostnames();
                    724:     my $numhosts = scalar(keys(%hostname));
1.103.2.9  raeburn   725:     my $checkbackwards = 0;
                    726:     my $checkfrom = 0;
                    727:     my $checkexcluded = 0;
                    728:     my (%bymachine,%weights,%exclusions,%serverhomes);
                    729:     if (ref($weightsref) eq 'HASH') {
                    730:         %weights = %{$weightsref};
                    731:     }
                    732:     if (ref($exclusionsref) eq 'HASH') {
                    733:         %exclusions = %{$exclusionsref};
                    734:         if (keys(%exclusions)) {
                    735:             $checkexcluded = 1;
                    736:             %serverhomes = &read_serverhomeIDs();
                    737:         }
                    738:     }
1.103.2.1  raeburn   739: 
1.103.2.9  raeburn   740: #
                    741: # For LON-CAPA 1.2.0 to 2.1.3 (release dates: 8/31/2004 and 3/31/2006) any
                    742: # entry logged in lonnet.perm.log for completion of a delayed (critical)
                    743: # transaction lacked the hostID for the remote node to which the command
                    744: # to be completed was sent.
                    745: #
                    746: # Because of this, exclusion of items in lonnet.perm.log for nodes which are
                    747: # no longer part of the cluster from adding to the overall "unsend" count
                    748: # needs additional effort besides the changes made in loncron rev. 1.105.
                    749: #
                    750: # For "S" (completion) events logging in LON-CAPA 1.2.0 through 2.1.3 included
                    751: # "LondTransaction=HASH(hexadecimal)->getClient() :$cmd, where the hexadecimal
                    752: # is a memory location, and $cmd is the command sent to the remote node.
                    753: #
                    754: # Starting with 2.2.0 (released 8/21/2006) logging for "S" (completion) events
                    755: # had sethost:$host_id:$cmd after LondTransaction=HASH(hexadecimal)->getClient()
                    756: #
                    757: # Starting with 2.4.1 (released 6/13/2007) logging for "S" replaced echoing the
                    758: # getClient() call with the result of the Transaction->getClient() call itself
                    759: # undef for completion of delivery of a delayed message.
                    760: #
                    761: # The net effect of these changes is that lonnet.perm.log is now accessed three
                    762: # times: (a) oldest record is checked, if earlier than release date for 2.5.0
                    763: # then (b) file is read backwards, with timestamp recorded for most recent
                    764: # instance of logged "S" event for "update" command without "sethost:$host_id:"
                    765: # then (c) file is read forward with records ignored which predate the timestamp
                    766: # recorded in (b), if one was found.
                    767: #
                    768: # In (c), when calculating the unsend total, i.e., the difference between delayed
                    769: # transactions ("D") and sent transactions ("S"), transactions are ignored if the
                    770: # target node is no longer in the cluster, and also (for "update" commands), if
                    771: # the target node is in the list of nodes excluded from the count, in the domain
                    772: # configuration for this machine's default domain.  The idea here is to remove
                    773: # delayed "update" commands for nodes for which inbound access to port 5663,
                    774: # is blocked, but are still part of the LON-CAPA network, (i.e., they can still
                    775: # replicate content from other nodes).
                    776: #
                    777: 
                    778:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
                    779:     if (defined($dfh)) {
                    780:         while (my $line=<$dfh>) {
                    781: 	    my ($time,$sdf,$rest)=split(/:/,$line,3);
                    782:             if ($time < 1541185772) {
                    783:                 $checkbackwards = 1;
                    784:             }
                    785:             last;
                    786:         }
                    787:         undef $dfh;
1.1       albertel  788:     }
1.46      albertel  789: 
1.103.2.9  raeburn   790:     if ($checkbackwards) {
                    791:         if (tie *BW, 'File::ReadBackwards', "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    792:             while(my $line=<BW>) {
                    793:                 if ($line =~ /\QLondTransaction=HASH\E[^:]+:update:/) {
                    794:                     ($checkfrom) = split(/:/,$line,2);
                    795:                     last;
                    796:                 }
                    797:             }
                    798:             close(BW);
                    799:         }
                    800:     }
                    801:     $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
                    802:     if (defined($dfh)) {
                    803:         while (my $line=<$dfh>) {
                    804:             my ($time,$sdf,$rest)=split(/:/,$line,3);
                    805:             next unless (($sdf eq 'F') || ($sdf eq 'S') || ($sdf eq 'D'));
                    806:             next if (($checkfrom) && ($time <= $checkfrom));
                    807:             my ($dserv,$dcmd);
                    808:             if ($sdf eq 'S') {
                    809:                 my ($serva,$cmda,$servb,$cmdb) = split(/:/,$rest);
                    810:                 if ($cmda eq 'sethost') {
                    811:                     chomp($cmdb);
                    812:                     $dcmd = $cmdb;
                    813:                 } else {
                    814:                     $dcmd = $cmda;
                    815:                 }
                    816:                 if (($serva =~ /^LondTransaction/) || ($serva eq '')) {
                    817:                     unless (($servb eq '') || ($servb =~ m{^/})) {
                    818:                         $dserv = $servb;
                    819:                     }
                    820:                 } else {
                    821:                     $dserv = $serva;
                    822:                 }
                    823:             } else {
                    824:                 ($dserv,$dcmd) = split(/:/,$rest);
                    825:             }
                    826:             if ($sdf eq 'F') {
                    827:                 my $local=localtime($time);
                    828:                 &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
                    829:                 $warnings++;
                    830:             }
                    831:             next if ((($dserv eq '') || ($dcmd eq '')) && ($sdf ne 'F'));
                    832:             if ($sdf eq 'S') {
                    833:                 if ($dcmd eq 'update') {
                    834:                     if ($hostname{$dserv}) {
                    835:                         if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
                    836:                             $ignored --;
                    837:                         } else {
                    838:                             $unsend --;
                    839:                         }
                    840:                     }
                    841:                     if (exists($bymachine{$dserv})) {
                    842:                         $bymachine{$dserv} --;
                    843:                     } else {
                    844:                         $bymachine{$dserv} = -1;
                    845:                     }
                    846:                 } else {
                    847:                     if ($hostname{$dserv}) {
                    848:                         $unsend --;
                    849:                     }
                    850:                 }
                    851:             } elsif ($sdf eq 'D') {
                    852:                 if ($dcmd eq 'update') {
                    853:                     if ($hostname{$dserv}) {
                    854:                         if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
                    855:                             $ignored ++;
                    856:                         } else {
                    857:                             $unsend ++;
                    858:                         }
                    859:                     }
                    860:                     if (exists($bymachine{$dserv})) {
                    861:                         $bymachine{$dserv} ++;
                    862:                     } else {
                    863:                         $bymachine{$dserv} = 1;
                    864:                     }
                    865:                 } else {
                    866:                     if ($hostname{$dserv}) {
                    867:                         $unsend ++;
                    868:                     }
                    869:                 }
                    870:             }
                    871:         }
                    872:         undef $dfh;
                    873:         my $nodest = 0;
                    874:         my $retired = 0;
                    875:         my %active;
                    876:         if (keys(%bymachine)) {
                    877:             unless ($checkexcluded) {
                    878:                 %serverhomes = &read_serverhomeIDs();
                    879:             }
                    880:             foreach my $key (keys(%bymachine)) {
                    881:                 if ($bymachine{$key} > 0) {
                    882:                     if ($hostname{$key}) {
                    883:                         $active{$serverhomes{$hostname{$key}}} += $bymachine{$key};
                    884:                     } else {
                    885:                         $retired ++;
                    886:                         $nodest += $bymachine{$key};
                    887:                     }
                    888:                 }
                    889:             }
                    890:         }
                    891:         if (keys(%active)) {
                    892:             &log($fh,"<p>Unsend messages by node, active (undegraded) nodes in cluster</p>\n");
                    893:             foreach my $key (sort(keys(%active))) {
                    894:                 &log($fh,&encode_entities("$key => $active{$key}",'<>&"')."\n");
                    895:             }
                    896:         }
                    897:         &log($fh,"<p>Total unsend messages: <b>$unsend</b> for ".scalar(keys(%active))." active (undegraded) nodes in cluster.</p>\n");
                    898:         if (keys(%exclusions) > 0) {
                    899:             &log($fh,"<p>Total incomplete updates <b>$ignored</b> for ".scalar(keys(%exclusions))." degraded nodes in cluster.</p>\n");
                    900:         }
                    901:         if ($retired) {
                    902:             &log($fh,"<p>Total unsent <b>$nodest</b> for $retired nodes no longer in cluster.</p>\n");
                    903:         }
                    904:         if ($unsend > 0) {
                    905:             $warnings=$warnings+$weights{'U'}*$unsend;
                    906:         }
1.95      raeburn   907:     }
1.1       albertel  908: 
1.43      albertel  909:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48      albertel  910:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68      www       911: # list directory with delayed messages and remember offline servers
                    912:     my %servers=();
1.43      albertel  913:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68      www       914:     while (my $line=<DFH>) {
                    915:         my ($server)=($line=~/\.(\w+)$/);
                    916:         if ($server) { $servers{$server}=1; }
1.48      albertel  917: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  918:     }
1.48      albertel  919:     &log($fh,"</pre>\n");
1.43      albertel  920:     close (DFH);
1.68      www       921: # pong to all servers that have delayed messages
                    922: # this will trigger a reverse connection, which should flush the buffers
1.95      raeburn   923:     foreach my $tryserver (sort(keys(%servers))) {
                    924:         if ($hostname{$tryserver} || !$numhosts) {
                    925:             my $answer;
                    926:             eval {
                    927:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    928:                 alarm(20);
                    929:                 $answer = &Apache::lonnet::reply("pong",$tryserver);
                    930:                 alarm(0);
                    931:             };
                    932:             if ($@ && $@ =~ m/TIMEOUT/) {
                    933:                 &log($fh,"Attempted pong to $tryserver timed out<br />");
1.100     bisitz    934:                 print "Time out while contacting: $tryserver for pong.\n";
1.95      raeburn   935:             } else {
                    936:                 &log($fh,"Pong to $tryserver: $answer<br />");
                    937:             }
1.91      raeburn   938:         } else {
1.95      raeburn   939:             &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
1.91      raeburn   940:         }
1.68      www       941:     }
1.46      albertel  942: }
1.1       albertel  943: 
1.46      albertel  944: sub finish_logging {
1.103.2.9  raeburn   945:     my ($fh,$weightsref)=@_;
                    946:     my %weights;
                    947:     if (ref($weightsref) eq 'HASH') {
                    948:         %weights = %{$weightsref};
                    949:     }
1.48      albertel  950:     &log($fh,"<a name='errcount' />\n");
1.103.2.9  raeburn   951:     $totalcount=($weights{'N'}*$notices)+($weights{'W'}*$warnings)+($weights{'E'}*$errors);
1.43      albertel  952:     &errout($fh);
1.46      albertel  953:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
                    954:     my $now=time;
                    955:     my $date=localtime($now);
1.48      albertel  956:     &log($fh,"<hr />$date ($now)</body></html>\n");
1.100     bisitz    957:     print "lon-status webpage updated.\n";
1.43      albertel  958:     $fh->close();
1.46      albertel  959: 
                    960:     if ($errors) { $simplestatus{'errors'}=$errors; }
                    961:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
                    962:     if ($notices) { $simplestatus{'notices'}=$notices; }
                    963:     $simplestatus{'time'}=time;
1.1       albertel  964: }
                    965: 
1.46      albertel  966: sub log_simplestatus {
1.73      albertel  967:     rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46      albertel  968:     
1.43      albertel  969:     my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
                    970:     foreach (keys %simplestatus) {
                    971: 	print $sfh $_.'='.$simplestatus{$_}.'&';
                    972:     }
                    973:     print $sfh "\n";
                    974:     $sfh->close();
1.41      www       975: }
1.46      albertel  976: 
1.84      raeburn   977: sub write_loncaparevs {
1.100     bisitz    978:     print "Retrieving LON-CAPA version information.\n";
1.99      raeburn   979:     my %hostname = &Apache::lonnet::all_hostnames();
                    980:     my $output;
                    981:     foreach my $id (sort(keys(%hostname))) {
                    982:         if ($id ne '') {
                    983:             my $loncaparev;
                    984:             eval {
                    985:                 local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    986:                 alarm(10);
                    987:                 $loncaparev =
                    988:                     &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
                    989:                 alarm(0);
                    990:             };
                    991:             if ($@ && $@ =~ m/TIMEOUT/) {
1.100     bisitz    992:                 print "Time out while contacting lonHost: $id for version.\n";   
1.99      raeburn   993:             }
                    994:             if ($loncaparev =~ /^[\w.\-]+$/) {
                    995:                 $output .= $id.':'.$loncaparev."\n";
                    996:             }
                    997:         }
                    998:     }
                    999:     if ($output) {
                   1000:         if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                   1001:             print $fh $output;
                   1002:             close($fh);
                   1003:             &Apache::lonnet::load_loncaparevs();
                   1004:         }
                   1005:     }
                   1006:     return;
                   1007: }
                   1008: 
                   1009: sub write_serverhomeIDs {
1.100     bisitz   1010:     print "Retrieving LON-CAPA lonHostID information.\n";
1.99      raeburn  1011:     my %name_to_host = &Apache::lonnet::all_names();
                   1012:     my $output;
                   1013:     foreach my $name (sort(keys(%name_to_host))) {
                   1014:         if ($name ne '') {
                   1015:             if (ref($name_to_host{$name}) eq 'ARRAY') {
                   1016:                 my $serverhomeID;
1.90      raeburn  1017:                 eval {
                   1018:                     local $SIG{ ALRM } = sub { die "TIMEOUT" };
                   1019:                     alarm(10);
1.103.2.9  raeburn  1020:                     $serverhomeID =
1.99      raeburn  1021:                         &Apache::lonnet::get_server_homeID($name,1,'loncron');
1.90      raeburn  1022:                     alarm(0);
                   1023:                 };
                   1024:                 if ($@ && $@ =~ m/TIMEOUT/) {
1.99      raeburn  1025:                     print "Time out while contacting server: $name\n"; 
1.90      raeburn  1026:                 }
1.99      raeburn  1027:                 if ($serverhomeID ne '') {
                   1028:                     $output .= $name.':'.$serverhomeID."\n";
                   1029:                 } else {
                   1030:                     $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1.84      raeburn  1031:                 }
                   1032:             }
                   1033:         }
                   1034:     }
1.99      raeburn  1035:     if ($output) {
                   1036:         if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   1037:             print $fh $output;
                   1038:             close($fh);
                   1039:             &Apache::lonnet::load_serverhomeIDs();
1.85      raeburn  1040:         }
                   1041:     }
                   1042:     return;
                   1043: }
                   1044: 
1.96      raeburn  1045: sub write_checksums {
1.98      raeburn  1046:     my $distro = &LONCAPA::distro();
1.96      raeburn  1047:     if ($distro) {
                   1048:         print "Retrieving file version and checksumming.\n";
1.97      raeburn  1049:         my $numchksums = 0;
1.96      raeburn  1050:         my ($chksumsref,$versionsref) =
1.97      raeburn  1051:             &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
                   1052:                                                   $perlvar{'lonLib'},
                   1053:                                                   $perlvar{'lonIncludes'},
                   1054:                                                   $perlvar{'lonTabDir'});
1.96      raeburn  1055:         if (ref($chksumsref) eq 'HASH') {
                   1056:             $numchksums = scalar(keys(%{$chksumsref}));
                   1057:         }
                   1058:         print "File version retrieved and checksumming completed for $numchksums files.\n";
                   1059:     } else {
                   1060:         print "File version retrieval and checksumming skipped - could not determine Linux distro.\n"; 
                   1061:     }
1.97      raeburn  1062:     return;
1.96      raeburn  1063: }
                   1064: 
1.103.2.3  raeburn  1065: sub write_hostips {
                   1066:     my $lontabdir = $perlvar{'lonTabDir'};
                   1067:     my $defdom = $perlvar{'lonDefDomain'};
                   1068:     my $lonhost = $perlvar{'lonHostID'};
                   1069:     my $newfile = "$lontabdir/currhostips.tab";
                   1070:     my $oldfile = "$lontabdir/prevhostips.tab";
                   1071:     my (%prevhosts,%currhosts,%ipchange);
                   1072:     if ((-e $newfile) && (-s $newfile)) {
                   1073:         move($newfile,$oldfile);
                   1074:         chmod(0644,$oldfile);
                   1075:         if (open(my $fh,'<',$oldfile)) {
                   1076:             while (my $line=<$fh>) {
                   1077:                 chomp($line);
                   1078:                 if ($line =~ /^([^:]+):([\d.]+)$/) {
                   1079:                     $prevhosts{$1} = $2;
                   1080:                 }
                   1081:             }
                   1082:             close($fh);
                   1083:         }
                   1084:     }
                   1085:     my ($ip_info,$cached) =
                   1086:         &Apache::lonnet::is_cached_new('iphost','iphost');
                   1087:     if (!$cached) {
                   1088:         &Apache::lonnet::get_iphost();
                   1089:         ($ip_info,$cached) =
                   1090:         &Apache::lonnet::is_cached_new('iphost','iphost');
                   1091:     }
                   1092:     if (ref($ip_info) eq 'ARRAY') {
                   1093:         %currhosts = %{$ip_info->[1]};
                   1094:         if (open(my $fh,'>',$newfile)) {
                   1095:             foreach my $key (keys(%currhosts)) {
                   1096:                 print $fh "$key:$currhosts{$key}\n";
                   1097:             }
                   1098:             close($fh);
                   1099:             chmod(0644,$newfile);
                   1100:         }
                   1101:     }
                   1102:     if (keys(%prevhosts) && keys(%currhosts)) {
                   1103:         foreach my $key (keys(%prevhosts)) {
                   1104:             unless ($currhosts{$key} eq $prevhosts{$key}) {
1.103.2.6  raeburn  1105:                 $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1.103.2.3  raeburn  1106:             }
                   1107:         }
                   1108:         foreach my $key (keys(%currhosts)) {
                   1109:             unless ($currhosts{$key} eq $prevhosts{$key}) {
                   1110:                 $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
                   1111:             }
                   1112:         }
                   1113:     }
                   1114:     if (&Apache::lonnet::domain($defdom,'primary') eq $lonhost) {
                   1115:         if (keys(%ipchange)) {
                   1116:             if (open(my $fh,'>>',$perlvar{'lonDaemons'}.'/logs/hostip.log')) {
                   1117:                print $fh "********************\n".localtime(time).' Changes --'."\n".
1.103.2.6  raeburn  1118:                          "| Hostname | Previous IP | New IP |\n".
                   1119:                          " --------------------------------- \n";
1.103.2.3  raeburn  1120:                foreach my $hostname (sort(keys(%ipchange))) {
1.103.2.6  raeburn  1121:                     print $fh "| $hostname | $ipchange{$hostname} |\n";
1.103.2.3  raeburn  1122:                 }
                   1123:                 print $fh "\n*******************\n\n";
                   1124:                 close($fh);
                   1125:             }
                   1126:             my $emailto = &Apache::loncommon::build_recipient_list(undef,
                   1127:                                    'hostipmail',$defdom);
                   1128:             if ($emailto) {
                   1129:                 my $subject = "LON-CAPA Hostname to IP change ($perlvar{'lonHostID'})";
                   1130:                 my $chgmail = "To: $emailto\n".
                   1131:                               "Subject: $subject\n".
                   1132:                               "Content-type: text/plain\; charset=UTF-8\n".
                   1133:                               "MIME-Version: 1.0\n\n".
                   1134:                               "Host/IP changes\n".
                   1135:                               " \n".
1.103.2.6  raeburn  1136:                               "| Hostname | Previous IP | New IP |\n".
                   1137:                               " --------------------------------- \n";
1.103.2.3  raeburn  1138:                 foreach my $hostname (sort(keys(%ipchange))) {
1.103.2.6  raeburn  1139:                     $chgmail .= "| $hostname | $ipchange{$hostname} |\n";
1.103.2.3  raeburn  1140:                 }
                   1141:                 $chgmail .= "\n\n";
                   1142:                 if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
                   1143:                     print $mailh $chgmail;
                   1144:                     close($mailh);
                   1145:                     print "Sending mail notification of hostname/IP changes.\n";
                   1146:                 }
                   1147:             }
                   1148:         }
                   1149:     }
                   1150:     return;
                   1151: }
                   1152: 
1.103.2.9  raeburn  1153: sub get_domain_config {
                   1154:     my ($dom,$primlibserv,$isprimary,$url,%confhash);
                   1155:     $dom = $perlvar{'lonDefDomain'};
                   1156:     $primlibserv = &Apache::lonnet::domain($dom,'primary');
                   1157:     if ($primlibserv eq $perlvar{'lonHostID'}) {
                   1158:         $isprimary = 1;
                   1159:     } elsif ($primlibserv ne '') {
                   1160:         my $protocol = $Apache::lonnet::protocol{$primlibserv};
                   1161:         my $hostname = &Apache::lonnet::hostname($primlibserv);
                   1162:         unless ($protocol eq 'https') {
                   1163:             $protocol = 'http';
                   1164:         }
                   1165:         $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl?primary='.$primlibserv.'&format=raw';
                   1166:     }
                   1167:     if ($isprimary) {
                   1168:         my $lonusersdir = $perlvar{'lonUsersDir'};
                   1169:         my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
                   1170:         if (-e $fname) {
                   1171:             my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
                   1172:             if (ref($dbref) eq 'HASH') {
                   1173:                 foreach my $key (sort(keys(%{$dbref}))) {
                   1174:                     my $value = $dbref->{$key};
                   1175:                     if ($value =~ s/^__FROZEN__//) {
                   1176:                         $value = thaw(&LONCAPA::unescape($value));
                   1177:                     } else {
                   1178:                         $value = &LONCAPA::unescape($value);
                   1179:                     }
                   1180:                     $confhash{$key} = $value;
                   1181:                 }
                   1182:                 &LONCAPA::locking_hash_untie($dbref);
                   1183:             }
                   1184:         }
                   1185:     } else {
                   1186:         my $ua=new LWP::UserAgent;
                   1187:         $ua->timeout(5);
                   1188:         my $request=new HTTP::Request('GET',$url);
                   1189:         my $response=$ua->request($request);
                   1190:         unless ($response->is_error()) {
                   1191:             my $content = $response->content;
                   1192:             if ($content) {
                   1193:                 my @pairs=split(/\&/,$content);
                   1194:                 foreach my $item (@pairs) {
                   1195:                     my ($key,$value)=split(/=/,$item,2);
                   1196:                     my $what = &LONCAPA::unescape($key);
                   1197:                     if ($value =~ s/^__FROZEN__//) {
                   1198:                         $value = thaw(&LONCAPA::unescape($value));
                   1199:                     } else {
                   1200:                         $value = &LONCAPA::unescape($value);
                   1201:                     }
                   1202:                     $confhash{$what}=$value;
                   1203:                 }
                   1204:             }
                   1205:         }
                   1206:     }
                   1207:     return \%confhash;
                   1208: }
                   1209: 
                   1210: sub get_permcount_settings {
                   1211:     my ($domconf) = @_;
                   1212:     my ($defaults,$names) = &Apache::loncommon::lon_status_items();
                   1213:     my (%weights,$threshold,$sysmail,$reportstatus,%exclusions);
                   1214:     foreach my $type ('E','W','N','U') {
                   1215:         $weights{$type} = $defaults->{$type};
                   1216:     }
                   1217:     $threshold = $defaults->{'threshold'};
                   1218:     $sysmail = $defaults->{'sysmail'};
                   1219:     $reportstatus = 1;
                   1220:     if (ref($domconf) eq 'HASH') {
                   1221:         if (ref($domconf->{'contacts'}) eq 'HASH') {
                   1222:             if ($domconf->{'contacts'}{'reportstatus'} == 0) {
                   1223:                 $reportstatus = 0;
                   1224:             }
                   1225:             if (ref($domconf->{'contacts'}{'lonstatus'}) eq 'HASH') {
                   1226:                 if (ref($domconf->{'contacts'}{'lonstatus'}{weights}) eq 'HASH') {
                   1227:                     foreach my $type ('E','W','N','U') {
                   1228:                         if (exists($domconf->{'contacts'}{'lonstatus'}{weights}{$type})) {
                   1229:                             $weights{$type} = $domconf->{'contacts'}{'lonstatus'}{weights}{$type};
                   1230:                         }
                   1231:                     }
                   1232:                 }
                   1233:                 if (ref($domconf->{'contacts'}{'lonstatus'}{'excluded'}) eq 'ARRAY') {
                   1234:                     my @excluded = @{$domconf->{'contacts'}{'lonstatus'}{'excluded'}};
                   1235:                     if (@excluded) {
                   1236:                         map { $exclusions{$_} = 1; } @excluded;
                   1237:                     }
                   1238:                 }
                   1239:                 if (exists($domconf->{'contacts'}{'lonstatus'}{'threshold'})) {
                   1240:                     $threshold = $domconf->{'contacts'}{'lonstatus'}{'threshold'};
                   1241:                 }
                   1242:                 if (exists($domconf->{'contacts'}{'lonstatus'}{'sysmail'})) {
                   1243:                     $sysmail = $domconf->{'contacts'}{'lonstatus'}{'sysmail'};
                   1244:                 }
                   1245:             }
                   1246:         }
                   1247:     }
                   1248:     return ($threshold,$sysmail,$reportstatus,\%weights,\%exclusions);
                   1249: }
                   1250: 
                   1251: sub read_serverhomeIDs {
                   1252:     my %server;
                   1253:     if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
                   1254:         if (open(my $fh,'<',"$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   1255:             while (<$fh>) {
                   1256:                 my($host,$id) = split(/:/);
                   1257:                 chomp($id);
                   1258:                 $server{$host} = $id;
                   1259:             }
                   1260:             close($fh);
                   1261:         }
                   1262:     }
                   1263:     return %server;
                   1264: }
                   1265: 
1.46      albertel 1266: sub send_mail {
1.103.2.9  raeburn  1267:     my ($sysmail,$reportstatus) = @_;
1.79      raeburn  1268:     my $defdom = $perlvar{'lonDefDomain'};
                   1269:     my $origmail = $perlvar{'lonAdmEMail'};
1.78      raeburn  1270:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
                   1271:                                    'lonstatusmail',$defdom,$origmail);
1.103.2.9  raeburn  1272:     if (($totalcount>$sysmail) && ($reportstatus)) {
1.43      albertel 1273: 	$emailto.=",$perlvar{'lonSysEMail'}";
                   1274:     }
1.101     raeburn  1275:     my $from;
                   1276:     my $hostname=`/bin/hostname`;
                   1277:     chop($hostname);
                   1278:     $hostname=~s/[^\w\.]//g;
                   1279:     if ($hostname) {
                   1280:         $from = 'www@'.$hostname;
                   1281:     }
                   1282:     my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
                   1283:     my $loncronmail = "To: $emailto\n".
                   1284:                       "From: $from\n".
                   1285:                       "Subject: ".$subj."\n".
                   1286:                       "Content-type: text/html\; charset=UTF-8\n".
                   1287:                       "MIME-Version: 1.0\n\n";
                   1288:     if (open(my $fh,"<$statusdir/index.html")) {
                   1289:         while (<$fh>) {
                   1290:             $loncronmail .= $_;
                   1291:         }
                   1292:         close($fh);
                   1293:     } else {
                   1294:         $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
                   1295:     }
                   1296:     $loncronmail .= "\n\n";
                   1297:     if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
                   1298:         print $mailh $loncronmail;
                   1299:         close($mailh);
                   1300:         print "Sending mail.\n";
                   1301:     } else {
                   1302:         print "Sending mail failed.\n";
1.52      albertel 1303:     }
1.1       albertel 1304: }
1.46      albertel 1305: 
1.49      albertel 1306: sub usage {
                   1307:     print(<<USAGE);
1.100     bisitz   1308: loncron - housekeeping program that checks up on various parts of LON-CAPA
1.49      albertel 1309: 
                   1310: Options:
1.71      albertel 1311:    --help     Display 
1.49      albertel 1312:    --noemail  Do not send the status email
                   1313:    --justcheckconnections  Only check the current status of the lonc/d
                   1314:                                 connections, do not send emails do not
                   1315:                                 check if the daemons are running, do not
                   1316:                                 generate lon-status
                   1317:    --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
                   1318:                                 running, do not send emails do not
                   1319:                                 check the lonc/d connections, do not
                   1320:                                 generate lon-status
1.59      albertel 1321:    --justreload            Only tell the daemons to reload the config files,
                   1322: 				do not send emails do not
                   1323:                                 check if the daemons are running, do not
                   1324:                                 generate lon-status
1.103.2.4  raeburn  1325:    --justiptables          Only update the dynamic iptables rules for the
                   1326:                                 lond port; do not send emails, do not
                   1327:                                 check if the daemons are running, do not
                   1328:                                 generate lon-status
1.49      albertel 1329: USAGE
                   1330: }
                   1331: 
1.46      albertel 1332: # ================================================================ Main Program
                   1333: sub main () {
1.71      albertel 1334:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.103.2.4  raeburn  1335: 	$justreload,$justiptables);
1.49      albertel 1336:     &GetOptions("help"                 => \$help,
                   1337: 		"justcheckdaemons"     => \$justcheckdaemons,
                   1338: 		"noemail"              => \$noemail,
1.59      albertel 1339: 		"justcheckconnections" => \$justcheckconnections,
1.103.2.4  raeburn  1340: 		"justreload"           => \$justreload,
                   1341:                 "justiptables"         => \$justiptables
1.49      albertel 1342: 		);
                   1343:     if ($help) { &usage(); return; }
1.46      albertel 1344: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
                   1345:     my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
                   1346:     %perlvar=%{$perlvarref};
                   1347:     undef $perlvarref;
                   1348:     delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
                   1349:     delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.75      albertel 1350:     chdir($perlvar{'lonDaemons'});
1.46      albertel 1351: # --------------------------------------- Make sure that LON-CAPA is configured
                   1352: # I only test for one thing here (lonHostID).  This is just a safeguard.
                   1353:     if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
                   1354: 	print("Unconfigured machine.\n");
                   1355: 	my $emailto=$perlvar{'lonSysEMail'};
1.103.2.7  raeburn  1356: 	my $hostname = Sys::Hostname::FQDN::fqdn();
                   1357: 	$hostname=~s/\.+/./g;
                   1358: 	$hostname=~s/\-+/-/g;
                   1359: 	$hostname=~s/[^\w\.-]//g; # make sure is safe to pass through shell
1.46      albertel 1360: 	my $subj="LON: Unconfigured machine $hostname";
1.103.2.2  raeburn  1361: 	system("echo 'Unconfigured machine $hostname.' |".
                   1362:                " mail -s '$subj' $emailto > /dev/null");
1.46      albertel 1363: 	exit 1;
                   1364:     }
                   1365: 
                   1366: # ----------------------------- Make sure this process is running from user=www
                   1367:     my $wwwid=getpwnam('www');
                   1368:     if ($wwwid!=$<) {
1.100     bisitz   1369: 	print("User ID mismatch. This program must be run as user 'www'.\n");
1.46      albertel 1370: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                   1371: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.103.2.2  raeburn  1372: 	system("echo 'User ID mismatch. loncron must be run as user www.' |".
                   1373:                " mail -s '$subj' $emailto > /dev/null");
1.46      albertel 1374: 	exit 1;
                   1375:     }
                   1376: 
1.72      albertel 1377: # -------------------------------------------- Force reload of host information
1.103     raeburn  1378:     my $nomemcache;
                   1379:     if ($justcheckdaemons) {
                   1380:         $nomemcache=1;
                   1381:         my $memcachepidfile="$perlvar{'lonDaemons'}/logs/memcached.pid";
                   1382:         my $memcachepid;
                   1383:         if (-e $memcachepidfile) {
                   1384:             my $memfh=IO::File->new($memcachepidfile);
                   1385:             $memcachepid=<$memfh>;
                   1386:             chomp($memcachepid);
                   1387:             if ($memcachepid =~ /^\d+$/ && kill 0 => $memcachepid) {
                   1388:                 undef($nomemcache);
                   1389:             }
                   1390:         }
                   1391:     }
1.103.2.4  raeburn  1392:     if (!$justiptables) {
                   1393:         &Apache::lonnet::load_hosts_tab(1,$nomemcache);
                   1394:         &Apache::lonnet::load_domain_tab(1,$nomemcache);
                   1395:         &Apache::lonnet::get_iphost(1,$nomemcache);
                   1396:     }
1.46      albertel 1397: 
1.103.2.9  raeburn  1398: # ----------------------------------------- Force firewall update for lond port
1.81      raeburn  1399: 
                   1400:     if ((!$justcheckdaemons) && (!$justreload)) {
                   1401:         my $now = time;
                   1402:         my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
                   1403:                       $now.$$.int(rand(10000));
                   1404:         if (open(my $fh,">$tmpfile")) {
                   1405:             my %iphosts = &Apache::lonnet::get_iphost();
                   1406:             foreach my $key (keys(%iphosts)) {
                   1407:                 print $fh "$key\n";
                   1408:             }
                   1409:             close($fh);
1.89      raeburn  1410:             if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
                   1411:                 my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
                   1412:                 system("$execpath $tmpfile");
1.103.2.9  raeburn  1413:                 unlink('/tmp/lock_lciptables');  # Remove the lock file.
1.89      raeburn  1414:             }
1.88      raeburn  1415:             unlink($tmpfile);
1.81      raeburn  1416:         }
                   1417:     }
                   1418: 
1.46      albertel 1419: # ---------------------------------------------------------------- Start report
                   1420: 
                   1421:     $errors=0;
                   1422:     $warnings=0;
                   1423:     $notices=0;
                   1424: 
1.103.2.9  raeburn  1425: 
1.49      albertel 1426:     my $fh;
1.103.2.4  raeburn  1427:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.72      albertel 1428: 	$fh=&start_logging();
1.49      albertel 1429: 
                   1430: 	&log_machine_info($fh);
                   1431: 	&clean_tmp($fh);
                   1432: 	&clean_lonIDs($fh);
1.103.2.5  raeburn  1433:         &clean_balanceIDs($fh);
1.102     raeburn  1434:         &clean_webDAV_sessionIDs($fh);
1.49      albertel 1435: 	&check_httpd_logs($fh);
                   1436: 	&rotate_lonnet_logs($fh);
1.73      albertel 1437: 	&rotate_other_logs($fh);
1.49      albertel 1438:     }
1.103.2.4  raeburn  1439:     if (!$justcheckconnections && !$justreload && !$justiptables) {
1.76      albertel 1440: 	&checkon_daemon($fh,'lonmemcached',40000);
1.49      albertel 1441: 	&checkon_daemon($fh,'lonsql',200000);
1.63      albertel 1442: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
                   1443: 	    &checkon_daemon($fh,'lond',40000,'USR2');
                   1444: 	}
1.71      albertel 1445: 	&checkon_daemon($fh,'lonc',40000,'USR1');
1.70      raeburn  1446:         &checkon_daemon($fh,'lonmaxima',40000);
1.80      www      1447:         &checkon_daemon($fh,'lonr',40000);
1.49      albertel 1448:     }
1.59      albertel 1449:     if ($justreload) {
                   1450: 	&checkon_daemon($fh,'lond',40000,'USR2');
1.71      albertel 1451: 	&checkon_daemon($fh,'lonc',40000,'USR2');
1.59      albertel 1452:     }
1.63      albertel 1453:     if ($justcheckconnections) {
1.72      albertel 1454: 	&test_connections($fh);
1.49      albertel 1455:     }
1.103.2.4  raeburn  1456:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.103.2.9  raeburn  1457:         my $domconf = &get_domain_config();
                   1458:         my ($threshold,$sysmail,$reportstatus,$weightsref,$exclusionsref) =
                   1459:             &get_permcount_settings($domconf);
                   1460: 	&check_delayed_msg($fh,$weightsref,$exclusionsref);
                   1461: 	&finish_logging($fh,$weightsref);
1.49      albertel 1462: 	&log_simplestatus();
1.87      raeburn  1463:         &write_loncaparevs();
                   1464:         &write_serverhomeIDs();
1.97      raeburn  1465: 	&write_checksums();
1.103.2.3  raeburn  1466:         &write_hostips();
1.103.2.9  raeburn  1467:         if ($totalcount>$threshold && !$noemail) { &send_mail($sysmail,$reportstatus); }
1.49      albertel 1468:     }
1.46      albertel 1469: }
                   1470: 
                   1471: &main();
1.1       albertel 1472: 1;
                   1473: 

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