Annotation of loncom/loncron, revision 1.103.2.7

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.7! raeburn     5: # $Id: loncron,v 1.103.2.6 2020/01/11 22:39:55 raeburn Exp $
1.47      albertel    6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
1.1       albertel   28: #
1.24      www        29: 
                     30: $|=1;
1.48      albertel   31: use strict;
1.1       albertel   32: 
1.26      harris41   33: use lib '/home/httpd/lib/perl/';
                     34: use LONCAPA::Configuration;
1.96      raeburn    35: use LONCAPA::Checksumming;
1.89      raeburn    36: use LONCAPA;
1.72      albertel   37: use Apache::lonnet;
1.79      raeburn    38: use Apache::loncommon;
1.26      harris41   39: 
1.1       albertel   40: use IO::File;
                     41: use IO::Socket;
1.48      albertel   42: use HTML::Entities;
1.49      albertel   43: use Getopt::Long;
1.103.2.3  raeburn    44: use File::Copy;
1.103.2.7! raeburn    45: use Sys::Hostname::FQDN();
        !            46: 
1.46      albertel   47: #globals
                     48: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
                     49: 
                     50: my $statusdir="/home/httpd/html/lon-status";
                     51: 
1.1       albertel   52: 
                     53: # --------------------------------------------------------- Output error status
                     54: 
1.46      albertel   55: sub log {
                     56:     my $fh=shift;
                     57:     if ($fh) {	print $fh @_  }
                     58: }
                     59: 
1.1       albertel   60: sub errout {
                     61:    my $fh=shift;
1.46      albertel   62:    &log($fh,(<<ENDERROUT));
1.48      albertel   63:      <table border="2" bgcolor="#CCCCCC">
1.1       albertel   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>
1.48      albertel   67:      </table><p><a href="#top">Top</a></p>
1.1       albertel   68: ENDERROUT
                     69: }
                     70: 
1.73      albertel   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: 
1.42      albertel   82: sub start_daemon {
1.50      albertel   83:     my ($fh,$daemon,$pidfile,$args) = @_;
1.44      albertel   84:     my $progname=$daemon;
1.71      albertel   85:     if ($daemon eq 'lonc') {
1.44      albertel   86: 	$progname='loncnew'; 
                     87:     }
1.51      albertel   88:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
1.73      albertel   89:     &rotate_logfile($error_fname,$fh,'error logs');
1.74      albertel   90:     if ($daemon eq 'lonc') {
                     91: 	&clean_sockets($fh);
                     92:     }
1.51      albertel   93:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60      albertel   94:     sleep 1;
1.42      albertel   95:     if (-e $pidfile) {
1.48      albertel   96: 	&log($fh,"<p>Seems like it started ...</p>");
1.42      albertel   97: 	my $lfh=IO::File->new("$pidfile");
                     98: 	my $daemonpid=<$lfh>;
                     99: 	chomp($daemonpid);
1.62      albertel  100: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42      albertel  101: 	    return 1;
                    102: 	} else {
                    103: 	    return 0;
                    104: 	}
                    105:     }
1.48      albertel  106:     &log($fh,"<p>Seems like that did not work!</p>");
1.42      albertel  107:     $errors++;
                    108:     return 0;
                    109: }
                    110: 
                    111: sub checkon_daemon {
1.59      albertel  112:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42      albertel  113: 
1.63      albertel  114:     my $result;
1.48      albertel  115:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57      albertel  116:     printf("%-15s ",$daemon);
1.103.2.2  raeburn   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>");
1.42      albertel  130:     }
1.103.2.2  raeburn   131:    
1.42      albertel  132:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
                    133:     
                    134:     my $restartflag=1;
1.46      albertel  135:     my $daemonpid;
1.42      albertel  136:     if (-e $pidfile) {
                    137: 	my $lfh=IO::File->new("$pidfile");
1.46      albertel  138: 	$daemonpid=<$lfh>;
1.42      albertel  139: 	chomp($daemonpid);
1.62      albertel  140: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46      albertel  141: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59      albertel  142: 	    if ($send) { &log($fh,", sending $send"); }
1.46      albertel  143: 	    &log($fh,"</h3>");
1.59      albertel  144: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
                    145: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42      albertel  146: 	    $restartflag=0;
1.59      albertel  147: 	    if ($send eq 'USR2') {
1.63      albertel  148: 		$result = 'reloaded';
1.59      albertel  149: 		print "reloaded\n";
                    150: 	    } else {
1.63      albertel  151: 		$result = 'running';
1.59      albertel  152: 		print "running\n";
                    153: 	    }
1.42      albertel  154: 	} else {
                    155: 	    $errors++;
1.46      albertel  156: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42      albertel  157: 	    $restartflag=1;
1.46      albertel  158: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42      albertel  159: 	}
                    160:     }
                    161:     if ($restartflag==1) {
                    162: 	$simplestatus{$daemon}='off';
                    163: 	$errors++;
1.57      albertel  164: 	my $kadaemon=$daemon;
                    165: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.101     raeburn   166: 	&log($fh,'<br /><font color="red">Killall '.$daemon.': '.
1.57      albertel  167: 	    `killall $kadaemon 2>&1`.' - ');
1.60      albertel  168: 	sleep 1;
1.46      albertel  169: 	&log($fh,unlink($pidfile).' - '.
1.57      albertel  170: 	    `killall -9 $kadaemon 2>&1`.
1.101     raeburn   171: 	    '</font><br />');
1.46      albertel  172: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
1.103.2.2  raeburn   173: 
1.50      albertel  174: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  175: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  176: 	    $simplestatus{$daemon}='restarted';
1.63      albertel  177: 	    $result = 'started';
1.42      albertel  178: 	    print "started\n";
                    179: 	} else {
                    180: 	    $errors++;
1.46      albertel  181: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  182: 	    &log($fh,"<p>Give it one more try ...</p>");
1.42      albertel  183: 	    print " ";
1.50      albertel  184: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  185: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  186: 		$simplestatus{$daemon}='restarted';
1.63      albertel  187: 		$result = 'started';
1.42      albertel  188: 		print "started\n";
                    189: 	    } else {
1.63      albertel  190: 		$result = 'failed';
1.42      albertel  191: 		print " failed\n";
                    192: 		$simplestatus{$daemon}='failed';
                    193: 		$errors++; $errors++;
1.46      albertel  194: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  195: 		&log($fh,"<p>Unable to start $daemon</p>");
1.42      albertel  196: 	    }
                    197: 	}
1.103.2.2  raeburn   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:         }
1.42      albertel  212:     }
                    213:     
1.46      albertel  214:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.73      albertel  215:     &rotate_logfile($fname,$fh,'logs');
1.42      albertel  216: 
                    217:     &errout($fh);
1.63      albertel  218:     return $result;
1.42      albertel  219: }
1.1       albertel  220: 
1.46      albertel  221: # --------------------------------------------------------------------- Machine
                    222: sub log_machine_info {
                    223:     my ($fh)=@_;
1.48      albertel  224:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46      albertel  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:     }
1.13      harris41  241: 
1.46      albertel  242:     &log($fh,"<h3>df</h3>");
                    243:     &log($fh,"<pre>");
1.14      harris41  244: 
1.46      albertel  245:     open (DFH,"df|");
                    246:     while (my $line=<DFH>) { 
1.48      albertel  247: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  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++;
1.31      albertel  258: 	}
1.46      albertel  259: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1       albertel  260:     }
1.46      albertel  261:     close (DFH);
                    262:     &log($fh,"</pre>");
1.1       albertel  263: 
                    264: 
1.46      albertel  265:     &log($fh,"<h3>ps</h3>");
                    266:     &log($fh,"<pre>");
                    267:     my $psproc=0;
1.1       albertel  268: 
1.53      albertel  269:     open (PSH,"ps aux --cols 140 |");
1.46      albertel  270:     while (my $line=<PSH>) { 
1.48      albertel  271: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  272: 	$psproc++;
                    273:     }
                    274:     close (PSH);
                    275:     &log($fh,"</pre>");
1.1       albertel  276: 
1.46      albertel  277:     if ($psproc>200) { $notices++; }
                    278:     if ($psproc>250) { $notices++; }
1.1       albertel  279: 
1.61      albertel  280:     &log($fh,"<h3>distprobe</h3>");
                    281:     &log($fh,"<pre>");
1.98      raeburn   282:     &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
1.61      albertel  283:     &log($fh,"</pre>");
                    284: 
1.46      albertel  285:     &errout($fh);
                    286: }
1.1       albertel  287: 
1.46      albertel  288: sub start_logging {
1.43      albertel  289:     my $fh=IO::File->new(">$statusdir/newstatus.html");
                    290:     my %simplestatus=();
1.46      albertel  291:     my $now=time;
                    292:     my $date=localtime($now);
1.43      albertel  293:     
1.46      albertel  294: 
                    295:     &log($fh,(<<ENDHEADERS));
1.101     raeburn   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">
1.1       albertel  298: <head>
                    299: <title>LON Status Report $perlvar{'lonHostID'}</title>
1.101     raeburn   300: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1.1       albertel  301: </head>
1.3       www       302: <body bgcolor="#AAAAAA">
1.48      albertel  303: <a name="top" />
1.1       albertel  304: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
                    305: <h2>$date ($now)</h2>
                    306: <ol>
1.48      albertel  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>
1.102     raeburn   311: <li><a href="#webdav">WebDAV Session Tokens</a></li>
1.48      albertel  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>
1.1       albertel  320: </ol>
1.48      albertel  321: <hr />
                    322: <a name="configuration" />
1.1       albertel  323: <h2>Configuration</h2>
                    324: <h3>PerlVars</h3>
1.48      albertel  325: <table border="2">
1.1       albertel  326: ENDHEADERS
                    327: 
1.46      albertel  328:     foreach my $varname (sort(keys(%perlvar))) {
1.48      albertel  329: 	&log($fh,"<tr><td>$varname</td><td>".
                    330: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43      albertel  331:     }
1.48      albertel  332:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72      albertel  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');
1.46      albertel  337: 	&log($fh,
1.72      albertel  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:     }
1.101     raeburn   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");
1.43      albertel  355:     }
1.46      albertel  356:     return $fh;
                    357: }
1.11      www       358: 
                    359: # --------------------------------------------------------------- clean out tmp
1.46      albertel  360: sub clean_tmp {
                    361:     my ($fh)=@_;
1.48      albertel  362:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.82      raeburn   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);
1.43      albertel  482:     }
1.82      raeburn   483:     return ($cleaned,$old,$removed);
1.46      albertel  484: }
1.11      www       485: 
                    486: # ------------------------------------------------------------ clean out lonIDs
1.46      albertel  487: sub clean_lonIDs {
                    488:     my ($fh)=@_;
1.48      albertel  489:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46      albertel  490:     my $cleaned=0;
                    491:     my $active=0;
                    492:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.103.2.7! raeburn   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:         }
1.46      albertel  525:     }
1.48      albertel  526:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46      albertel  527:     &log($fh,"<h3>$active open session(s)</h3>");
                    528: }
1.43      albertel  529: 
1.103.2.5  raeburn   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: 
1.102     raeburn   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: 
1.74      albertel  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 
1.80      www       591: 		 || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
1.74      albertel  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: 
1.11      www       599: 
1.1       albertel  600: # ----------------------------------------------------------------------- httpd
1.46      albertel  601: sub check_httpd_logs {
                    602:     my ($fh)=@_;
1.94      raeburn   603:     if (open(PIPE,"./lchttpdlogs|")) {
1.93      raeburn   604:         while (my $line=<PIPE>) {
                    605:             &log($fh,$line);
                    606:             if ($line=~/\[error\]/) { $notices++; }
                    607:         }
                    608:         close(PIPE);
1.46      albertel  609:     }
1.43      albertel  610:     &errout($fh);
1.46      albertel  611: }
1.1       albertel  612: 
                    613: # ---------------------------------------------------------------------- lonnet
                    614: 
1.48      albertel  615: sub rotate_lonnet_logs {
1.46      albertel  616:     my ($fh)=@_;
1.48      albertel  617:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.100     bisitz    618:     print "Checking logs.\n";
1.43      albertel  619:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
                    620: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.46      albertel  621: 	while (my $line=<DFH>) { 
1.48      albertel  622: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  623: 	}
1.43      albertel  624: 	close (DFH);
                    625:     }
1.46      albertel  626:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.43      albertel  627:     
                    628:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    629: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.46      albertel  630: 	while (my $line=<DFH>) { 
1.48      albertel  631: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  632: 	}
1.43      albertel  633: 	close (DFH);
1.46      albertel  634:     } else { &log($fh,"No perm log\n") }
1.43      albertel  635: 
1.46      albertel  636:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73      albertel  637:     &rotate_logfile($fname,$fh,'lonnet log');
1.1       albertel  638: 
1.46      albertel  639:     &log($fh,"</pre>");
1.43      albertel  640:     &errout($fh);
1.46      albertel  641: }
                    642: 
1.73      albertel  643: sub rotate_other_logs {
                    644:     my ($fh) = @_;
1.83      raeburn   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:     }
1.73      albertel  656: }
                    657: 
1.43      albertel  658: # ----------------------------------------------------------------- Connections
1.46      albertel  659: sub test_connections {
1.72      albertel  660:     my ($fh)=@_;
1.48      albertel  661:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.100     bisitz    662:     print "Testing connections.\n";
1.48      albertel  663:     &log($fh,"<table border='2'>");
1.49      albertel  664:     my ($good,$bad)=(0,0);
1.72      albertel  665:     my %hostname = &Apache::lonnet::all_hostnames();
                    666:     foreach my $tryserver (sort(keys(%hostname))) {
1.43      albertel  667: 	print(".");
1.46      albertel  668: 	my $result;
1.72      albertel  669: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43      albertel  670: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
                    671: 	    $result="<b>ok</b>";
1.49      albertel  672: 	    $good++;
1.43      albertel  673: 	} else {
                    674: 	    $result=$answer;
                    675: 	    $warnings++;
1.49      albertel  676: 	    if ($answer eq 'con_lost') {
                    677: 		$bad++;
                    678: 		$warnings++;
1.50      albertel  679: 	    } else {
                    680: 		$good++; #self connection
1.49      albertel  681: 	    }
1.43      albertel  682: 	}
                    683: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46      albertel  684: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1       albertel  685:     }
1.46      albertel  686:     &log($fh,"</table>");
1.49      albertel  687:     print "\n$good good, $bad bad connections\n";
1.43      albertel  688:     &errout($fh);
1.46      albertel  689: }
                    690: 
                    691: 
1.1       albertel  692: # ------------------------------------------------------------ Delayed messages
1.46      albertel  693: sub check_delayed_msg {
1.72      albertel  694:     my ($fh)=@_;
1.48      albertel  695:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.100     bisitz    696:     print "Checking buffers.\n";
1.46      albertel  697:     
                    698:     &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1       albertel  699: 
1.46      albertel  700:     my $unsend=0;
1.1       albertel  701: 
1.103.2.1  raeburn   702:     my %hostname = &Apache::lonnet::all_hostnames();
                    703:     my $numhosts = scalar(keys(%hostname));
                    704: 
1.46      albertel  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);
1.103.2.1  raeburn   708:         if ($numhosts) {
                    709:             next unless ($hostname{$dserv});
                    710:         }
1.46      albertel  711: 	if ($sdf eq 'F') { 
                    712: 	    my $local=localtime($time);
1.101     raeburn   713: 	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
1.46      albertel  714: 	    $warnings++;
1.43      albertel  715: 	}
1.46      albertel  716: 	if ($sdf eq 'S') { $unsend--; }
                    717: 	if ($sdf eq 'D') { $unsend++; }
1.1       albertel  718:     }
1.46      albertel  719: 
1.48      albertel  720:     &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.95      raeburn   721:     if ($unsend > 0) {
                    722:         $warnings=$warnings+5*$unsend;
                    723:     }
1.1       albertel  724: 
1.43      albertel  725:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48      albertel  726:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68      www       727: # list directory with delayed messages and remember offline servers
                    728:     my %servers=();
1.43      albertel  729:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68      www       730:     while (my $line=<DFH>) {
                    731:         my ($server)=($line=~/\.(\w+)$/);
                    732:         if ($server) { $servers{$server}=1; }
1.48      albertel  733: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  734:     }
1.48      albertel  735:     &log($fh,"</pre>\n");
1.43      albertel  736:     close (DFH);
1.68      www       737: # pong to all servers that have delayed messages
                    738: # this will trigger a reverse connection, which should flush the buffers
1.95      raeburn   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 />");
1.100     bisitz    750:                 print "Time out while contacting: $tryserver for pong.\n";
1.95      raeburn   751:             } else {
                    752:                 &log($fh,"Pong to $tryserver: $answer<br />");
                    753:             }
1.91      raeburn   754:         } else {
1.95      raeburn   755:             &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
1.91      raeburn   756:         }
1.68      www       757:     }
1.46      albertel  758: }
1.1       albertel  759: 
1.46      albertel  760: sub finish_logging {
                    761:     my ($fh)=@_;
1.48      albertel  762:     &log($fh,"<a name='errcount' />\n");
1.43      albertel  763:     $totalcount=$notices+4*$warnings+100*$errors;
                    764:     &errout($fh);
1.46      albertel  765:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
                    766:     my $now=time;
                    767:     my $date=localtime($now);
1.48      albertel  768:     &log($fh,"<hr />$date ($now)</body></html>\n");
1.100     bisitz    769:     print "lon-status webpage updated.\n";
1.43      albertel  770:     $fh->close();
1.46      albertel  771: 
                    772:     if ($errors) { $simplestatus{'errors'}=$errors; }
                    773:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
                    774:     if ($notices) { $simplestatus{'notices'}=$notices; }
                    775:     $simplestatus{'time'}=time;
1.1       albertel  776: }
                    777: 
1.46      albertel  778: sub log_simplestatus {
1.73      albertel  779:     rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46      albertel  780:     
1.43      albertel  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();
1.41      www       787: }
1.46      albertel  788: 
1.84      raeburn   789: sub write_loncaparevs {
1.100     bisitz    790:     print "Retrieving LON-CAPA version information.\n";
1.99      raeburn   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/) {
1.100     bisitz    804:                 print "Time out while contacting lonHost: $id for version.\n";   
1.99      raeburn   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 {
1.100     bisitz    822:     print "Retrieving LON-CAPA lonHostID information.\n";
1.99      raeburn   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;
1.90      raeburn   829:                 eval {
                    830:                     local $SIG{ ALRM } = sub { die "TIMEOUT" };
                    831:                     alarm(10);
1.99      raeburn   832:                     $serverhomeID = 
                    833:                         &Apache::lonnet::get_server_homeID($name,1,'loncron');
1.90      raeburn   834:                     alarm(0);
                    835:                 };
                    836:                 if ($@ && $@ =~ m/TIMEOUT/) {
1.99      raeburn   837:                     print "Time out while contacting server: $name\n"; 
1.90      raeburn   838:                 }
1.99      raeburn   839:                 if ($serverhomeID ne '') {
                    840:                     $output .= $name.':'.$serverhomeID."\n";
                    841:                 } else {
                    842:                     $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1.84      raeburn   843:                 }
                    844:             }
                    845:         }
                    846:     }
1.99      raeburn   847:     if ($output) {
                    848:         if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                    849:             print $fh $output;
                    850:             close($fh);
                    851:             &Apache::lonnet::load_serverhomeIDs();
1.85      raeburn   852:         }
                    853:     }
                    854:     return;
                    855: }
                    856: 
1.96      raeburn   857: sub write_checksums {
1.98      raeburn   858:     my $distro = &LONCAPA::distro();
1.96      raeburn   859:     if ($distro) {
                    860:         print "Retrieving file version and checksumming.\n";
1.97      raeburn   861:         my $numchksums = 0;
1.96      raeburn   862:         my ($chksumsref,$versionsref) =
1.97      raeburn   863:             &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
                    864:                                                   $perlvar{'lonLib'},
                    865:                                                   $perlvar{'lonIncludes'},
                    866:                                                   $perlvar{'lonTabDir'});
1.96      raeburn   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:     }
1.97      raeburn   874:     return;
1.96      raeburn   875: }
                    876: 
1.103.2.3  raeburn   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}) {
1.103.2.6  raeburn   917:                 $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1.103.2.3  raeburn   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".
1.103.2.6  raeburn   930:                          "| Hostname | Previous IP | New IP |\n".
                    931:                          " --------------------------------- \n";
1.103.2.3  raeburn   932:                foreach my $hostname (sort(keys(%ipchange))) {
1.103.2.6  raeburn   933:                     print $fh "| $hostname | $ipchange{$hostname} |\n";
1.103.2.3  raeburn   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".
1.103.2.6  raeburn   948:                               "| Hostname | Previous IP | New IP |\n".
                    949:                               " --------------------------------- \n";
1.103.2.3  raeburn   950:                 foreach my $hostname (sort(keys(%ipchange))) {
1.103.2.6  raeburn   951:                     $chgmail .= "| $hostname | $ipchange{$hostname} |\n";
1.103.2.3  raeburn   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: 
1.46      albertel  965: sub send_mail {
1.79      raeburn   966:     my $defdom = $perlvar{'lonDefDomain'};
                    967:     my $origmail = $perlvar{'lonAdmEMail'};
1.78      raeburn   968:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
                    969:                                    'lonstatusmail',$defdom,$origmail);
1.54      www       970:     if ($totalcount>2500) {
1.43      albertel  971: 	$emailto.=",$perlvar{'lonSysEMail'}";
                    972:     }
1.101     raeburn   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";
1.52      albertel 1001:     }
1.1       albertel 1002: }
1.46      albertel 1003: 
1.49      albertel 1004: sub usage {
                   1005:     print(<<USAGE);
1.100     bisitz   1006: loncron - housekeeping program that checks up on various parts of LON-CAPA
1.49      albertel 1007: 
                   1008: Options:
1.71      albertel 1009:    --help     Display 
1.49      albertel 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
1.59      albertel 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
1.103.2.4  raeburn  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
1.49      albertel 1027: USAGE
                   1028: }
                   1029: 
1.46      albertel 1030: # ================================================================ Main Program
                   1031: sub main () {
1.71      albertel 1032:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.103.2.4  raeburn  1033: 	$justreload,$justiptables);
1.49      albertel 1034:     &GetOptions("help"                 => \$help,
                   1035: 		"justcheckdaemons"     => \$justcheckdaemons,
                   1036: 		"noemail"              => \$noemail,
1.59      albertel 1037: 		"justcheckconnections" => \$justcheckconnections,
1.103.2.4  raeburn  1038: 		"justreload"           => \$justreload,
                   1039:                 "justiptables"         => \$justiptables
1.49      albertel 1040: 		);
                   1041:     if ($help) { &usage(); return; }
1.46      albertel 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
1.75      albertel 1048:     chdir($perlvar{'lonDaemons'});
1.46      albertel 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'};
1.103.2.7! raeburn  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
1.46      albertel 1058: 	my $subj="LON: Unconfigured machine $hostname";
1.103.2.2  raeburn  1059: 	system("echo 'Unconfigured machine $hostname.' |".
                   1060:                " mail -s '$subj' $emailto > /dev/null");
1.46      albertel 1061: 	exit 1;
                   1062:     }
                   1063: 
                   1064: # ----------------------------- Make sure this process is running from user=www
                   1065:     my $wwwid=getpwnam('www');
                   1066:     if ($wwwid!=$<) {
1.100     bisitz   1067: 	print("User ID mismatch. This program must be run as user 'www'.\n");
1.46      albertel 1068: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                   1069: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.103.2.2  raeburn  1070: 	system("echo 'User ID mismatch. loncron must be run as user www.' |".
                   1071:                " mail -s '$subj' $emailto > /dev/null");
1.46      albertel 1072: 	exit 1;
                   1073:     }
                   1074: 
1.72      albertel 1075: # -------------------------------------------- Force reload of host information
1.103     raeburn  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:     }
1.103.2.4  raeburn  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:     }
1.46      albertel 1095: 
1.81      raeburn  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);
1.89      raeburn  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:             }
1.88      raeburn  1113:             unlink($tmpfile);
1.81      raeburn  1114:         }
                   1115:     }
                   1116: 
1.46      albertel 1117: # ---------------------------------------------------------------- Start report
                   1118: 
                   1119:     $errors=0;
                   1120:     $warnings=0;
                   1121:     $notices=0;
                   1122: 
                   1123: 	
1.49      albertel 1124:     my $fh;
1.103.2.4  raeburn  1125:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.72      albertel 1126: 	$fh=&start_logging();
1.49      albertel 1127: 
                   1128: 	&log_machine_info($fh);
                   1129: 	&clean_tmp($fh);
                   1130: 	&clean_lonIDs($fh);
1.103.2.5  raeburn  1131:         &clean_balanceIDs($fh);
1.102     raeburn  1132:         &clean_webDAV_sessionIDs($fh);
1.49      albertel 1133: 	&check_httpd_logs($fh);
                   1134: 	&rotate_lonnet_logs($fh);
1.73      albertel 1135: 	&rotate_other_logs($fh);
1.49      albertel 1136:     }
1.103.2.4  raeburn  1137:     if (!$justcheckconnections && !$justreload && !$justiptables) {
1.76      albertel 1138: 	&checkon_daemon($fh,'lonmemcached',40000);
1.49      albertel 1139: 	&checkon_daemon($fh,'lonsql',200000);
1.63      albertel 1140: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
                   1141: 	    &checkon_daemon($fh,'lond',40000,'USR2');
                   1142: 	}
1.71      albertel 1143: 	&checkon_daemon($fh,'lonc',40000,'USR1');
1.70      raeburn  1144:         &checkon_daemon($fh,'lonmaxima',40000);
1.80      www      1145:         &checkon_daemon($fh,'lonr',40000);
1.49      albertel 1146:     }
1.59      albertel 1147:     if ($justreload) {
                   1148: 	&checkon_daemon($fh,'lond',40000,'USR2');
1.71      albertel 1149: 	&checkon_daemon($fh,'lonc',40000,'USR2');
1.59      albertel 1150:     }
1.63      albertel 1151:     if ($justcheckconnections) {
1.72      albertel 1152: 	&test_connections($fh);
1.49      albertel 1153:     }
1.103.2.4  raeburn  1154:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.72      albertel 1155: 	&check_delayed_msg($fh);
1.49      albertel 1156: 	&finish_logging($fh);
                   1157: 	&log_simplestatus();
1.87      raeburn  1158:         &write_loncaparevs();
                   1159:         &write_serverhomeIDs();
1.97      raeburn  1160: 	&write_checksums();
1.103.2.3  raeburn  1161:         &write_hostips();
1.49      albertel 1162: 	if ($totalcount>200 && !$noemail) { &send_mail(); }
                   1163:     }
1.46      albertel 1164: }
                   1165: 
                   1166: &main();
1.1       albertel 1167: 1;
                   1168: 

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