Annotation of loncom/loncron, revision 1.78

1.1       albertel    1: #!/usr/bin/perl
                      2: 
1.47      albertel    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
                      4: #
1.78    ! raeburn     5: # $Id: loncron,v 1.77 2008/11/04 21:06:21 www 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.72      albertel   35: use Apache::lonnet;
1.26      harris41   36: 
1.1       albertel   37: use IO::File;
                     38: use IO::Socket;
1.48      albertel   39: use HTML::Entities;
1.49      albertel   40: use Getopt::Long;
1.46      albertel   41: #globals
                     42: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
                     43: 
                     44: my $statusdir="/home/httpd/html/lon-status";
                     45: 
1.1       albertel   46: 
                     47: # --------------------------------------------------------- Output error status
                     48: 
1.46      albertel   49: sub log {
                     50:     my $fh=shift;
                     51:     if ($fh) {	print $fh @_  }
                     52: }
                     53: 
1.1       albertel   54: sub errout {
                     55:    my $fh=shift;
1.46      albertel   56:    &log($fh,(<<ENDERROUT));
1.48      albertel   57:      <table border="2" bgcolor="#CCCCCC">
1.1       albertel   58:      <tr><td>Notices</td><td>$notices</td></tr>
                     59:      <tr><td>Warnings</td><td>$warnings</td></tr>
                     60:      <tr><td>Errors</td><td>$errors</td></tr>
1.48      albertel   61:      </table><p><a href="#top">Top</a></p>
1.1       albertel   62: ENDERROUT
                     63: }
                     64: 
1.73      albertel   65: sub rotate_logfile {
                     66:     my ($file,$fh,$description) = @_;
                     67:     my $size=(stat($file))[7];
                     68:     if ($size>40000) {
                     69: 	&log($fh,"<p>Rotating $description ...</p>");
                     70: 	rename("$file.2","$file.3");
                     71: 	rename("$file.1","$file.2");
                     72: 	rename("$file","$file.1");
                     73:     } 
                     74: }
                     75: 
1.42      albertel   76: sub start_daemon {
1.50      albertel   77:     my ($fh,$daemon,$pidfile,$args) = @_;
1.44      albertel   78:     my $progname=$daemon;
1.71      albertel   79:     if ($daemon eq 'lonc') {
1.44      albertel   80: 	$progname='loncnew'; 
                     81:     }
1.51      albertel   82:     my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
1.73      albertel   83:     &rotate_logfile($error_fname,$fh,'error logs');
1.74      albertel   84:     if ($daemon eq 'lonc') {
                     85: 	&clean_sockets($fh);
                     86:     }
1.51      albertel   87:     system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60      albertel   88:     sleep 1;
1.42      albertel   89:     if (-e $pidfile) {
1.48      albertel   90: 	&log($fh,"<p>Seems like it started ...</p>");
1.42      albertel   91: 	my $lfh=IO::File->new("$pidfile");
                     92: 	my $daemonpid=<$lfh>;
                     93: 	chomp($daemonpid);
1.62      albertel   94: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42      albertel   95: 	    return 1;
                     96: 	} else {
                     97: 	    return 0;
                     98: 	}
                     99:     }
1.48      albertel  100:     &log($fh,"<p>Seems like that did not work!</p>");
1.42      albertel  101:     $errors++;
                    102:     return 0;
                    103: }
                    104: 
                    105: sub checkon_daemon {
1.59      albertel  106:     my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42      albertel  107: 
1.63      albertel  108:     my $result;
1.48      albertel  109:     &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57      albertel  110:     printf("%-15s ",$daemon);
1.42      albertel  111:     if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
                    112: 	open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46      albertel  113: 	while (my $line=<DFH>) { 
                    114: 	    &log($fh,"$line");
1.42      albertel  115: 	    if ($line=~/INFO/) { $notices++; }
                    116: 	    if ($line=~/WARNING/) { $notices++; }
                    117: 	    if ($line=~/CRITICAL/) { $warnings++; }
                    118: 	};
                    119: 	close (DFH);
                    120:     }
1.48      albertel  121:     &log($fh,"</tt></p>");
1.42      albertel  122:     
                    123:     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
                    124:     
                    125:     my $restartflag=1;
1.46      albertel  126:     my $daemonpid;
1.42      albertel  127:     if (-e $pidfile) {
                    128: 	my $lfh=IO::File->new("$pidfile");
1.46      albertel  129: 	$daemonpid=<$lfh>;
1.42      albertel  130: 	chomp($daemonpid);
1.62      albertel  131: 	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46      albertel  132: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59      albertel  133: 	    if ($send) { &log($fh,", sending $send"); }
1.46      albertel  134: 	    &log($fh,"</h3>");
1.59      albertel  135: 	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
                    136: 	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42      albertel  137: 	    $restartflag=0;
1.59      albertel  138: 	    if ($send eq 'USR2') {
1.63      albertel  139: 		$result = 'reloaded';
1.59      albertel  140: 		print "reloaded\n";
                    141: 	    } else {
1.63      albertel  142: 		$result = 'running';
1.59      albertel  143: 		print "running\n";
                    144: 	    }
1.42      albertel  145: 	} else {
                    146: 	    $errors++;
1.46      albertel  147: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42      albertel  148: 	    $restartflag=1;
1.46      albertel  149: 	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42      albertel  150: 	}
                    151:     }
                    152:     if ($restartflag==1) {
                    153: 	$simplestatus{$daemon}='off';
                    154: 	$errors++;
1.57      albertel  155: 	my $kadaemon=$daemon;
                    156: 	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.46      albertel  157: 	&log($fh,'<br><font color="red">Killall '.$daemon.': '.
1.57      albertel  158: 	    `killall $kadaemon 2>&1`.' - ');
1.60      albertel  159: 	sleep 1;
1.46      albertel  160: 	&log($fh,unlink($pidfile).' - '.
1.57      albertel  161: 	    `killall -9 $kadaemon 2>&1`.
1.46      albertel  162: 	    '</font><br>');
                    163: 	&log($fh,"<h3>$daemon not running, trying to start</h3>");
1.42      albertel  164: 	
1.50      albertel  165: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  166: 	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  167: 	    $simplestatus{$daemon}='restarted';
1.63      albertel  168: 	    $result = 'started';
1.42      albertel  169: 	    print "started\n";
                    170: 	} else {
                    171: 	    $errors++;
1.46      albertel  172: 	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  173: 	    &log($fh,"<p>Give it one more try ...</p>");
1.42      albertel  174: 	    print " ";
1.50      albertel  175: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46      albertel  176: 		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42      albertel  177: 		$simplestatus{$daemon}='restarted';
1.63      albertel  178: 		$result = 'started';
1.42      albertel  179: 		print "started\n";
                    180: 	    } else {
1.63      albertel  181: 		$result = 'failed';
1.42      albertel  182: 		print " failed\n";
                    183: 		$simplestatus{$daemon}='failed';
                    184: 		$errors++; $errors++;
1.46      albertel  185: 		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48      albertel  186: 		&log($fh,"<p>Unable to start $daemon</p>");
1.42      albertel  187: 	    }
                    188: 	}
                    189: 
                    190: 	if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
1.46      albertel  191: 	    &log($fh,"<p><pre>");
1.42      albertel  192: 	    open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46      albertel  193: 	    while (my $line=<DFH>) { 
                    194: 		&log($fh,"$line");
1.42      albertel  195: 		if ($line=~/WARNING/) { $notices++; }
                    196: 		if ($line=~/CRITICAL/) { $notices++; }
                    197: 	    };
                    198: 	    close (DFH);
1.48      albertel  199: 	    &log($fh,"</pre></p>");
1.42      albertel  200: 	}
                    201:     }
                    202:     
1.46      albertel  203:     my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.73      albertel  204:     &rotate_logfile($fname,$fh,'logs');
1.42      albertel  205: 
                    206:     &errout($fh);
1.63      albertel  207:     return $result;
1.42      albertel  208: }
1.1       albertel  209: 
1.46      albertel  210: # --------------------------------------------------------------------- Machine
                    211: sub log_machine_info {
                    212:     my ($fh)=@_;
1.48      albertel  213:     &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46      albertel  214:     &log($fh,"<h3>loadavg</h3>");
                    215: 	
                    216:     open (LOADAVGH,"/proc/loadavg");
                    217:     my $loadavg=<LOADAVGH>;
                    218:     close (LOADAVGH);
                    219:     
                    220:     &log($fh,"<tt>$loadavg</tt>");
                    221:     
                    222:     my @parts=split(/\s+/,$loadavg);
                    223:     if ($parts[1]>4.0) {
                    224: 	$errors++;
                    225:     } elsif ($parts[1]>2.0) {
                    226: 	$warnings++;
                    227:     } elsif ($parts[1]>1.0) {
                    228: 	$notices++;
                    229:     }
1.13      harris41  230: 
1.46      albertel  231:     &log($fh,"<h3>df</h3>");
                    232:     &log($fh,"<pre>");
1.14      harris41  233: 
1.46      albertel  234:     open (DFH,"df|");
                    235:     while (my $line=<DFH>) { 
1.48      albertel  236: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  237: 	@parts=split(/\s+/,$line);
                    238: 	my $usage=$parts[4];
                    239: 	$usage=~s/\W//g;
                    240: 	if ($usage>90) { 
                    241: 	    $warnings++;
                    242: 	    $notices++; 
                    243: 	} elsif ($usage>80) {
                    244: 	    $warnings++;
                    245: 	} elsif ($usage>60) {
                    246: 	    $notices++;
1.31      albertel  247: 	}
1.46      albertel  248: 	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1       albertel  249:     }
1.46      albertel  250:     close (DFH);
                    251:     &log($fh,"</pre>");
1.1       albertel  252: 
                    253: 
1.46      albertel  254:     &log($fh,"<h3>ps</h3>");
                    255:     &log($fh,"<pre>");
                    256:     my $psproc=0;
1.1       albertel  257: 
1.53      albertel  258:     open (PSH,"ps aux --cols 140 |");
1.46      albertel  259:     while (my $line=<PSH>) { 
1.48      albertel  260: 	&log($fh,&encode_entities($line,'<>&"')); 
1.46      albertel  261: 	$psproc++;
                    262:     }
                    263:     close (PSH);
                    264:     &log($fh,"</pre>");
1.1       albertel  265: 
1.46      albertel  266:     if ($psproc>200) { $notices++; }
                    267:     if ($psproc>250) { $notices++; }
1.1       albertel  268: 
1.61      albertel  269:     &log($fh,"<h3>distprobe</h3>");
                    270:     &log($fh,"<pre>");
                    271:     open(DSH,"$perlvar{'lonDaemons'}/distprobe |");
                    272:     while (my $line=<DSH>) { 
                    273: 	&log($fh,&encode_entities($line,'<>&"')); 
                    274: 	$psproc++;
                    275:     }
                    276:     close(DSH);
                    277:     &log($fh,"</pre>");
                    278: 
1.46      albertel  279:     &errout($fh);
                    280: }
1.1       albertel  281: 
1.46      albertel  282: sub start_logging {
1.43      albertel  283:     my $fh=IO::File->new(">$statusdir/newstatus.html");
                    284:     my %simplestatus=();
1.46      albertel  285:     my $now=time;
                    286:     my $date=localtime($now);
1.43      albertel  287:     
1.46      albertel  288: 
                    289:     &log($fh,(<<ENDHEADERS));
1.1       albertel  290: <html>
                    291: <head>
                    292: <title>LON Status Report $perlvar{'lonHostID'}</title>
                    293: </head>
1.3       www       294: <body bgcolor="#AAAAAA">
1.48      albertel  295: <a name="top" />
1.1       albertel  296: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
                    297: <h2>$date ($now)</h2>
                    298: <ol>
1.48      albertel  299: <li><a href="#configuration">Configuration</a></li>
                    300: <li><a href="#machine">Machine Information</a></li>
                    301: <li><a href="#tmp">Temporary Files</a></li>
                    302: <li><a href="#tokens">Session Tokens</a></li>
                    303: <li><a href="#httpd">httpd</a></li>
                    304: <li><a href="#lonsql">lonsql</a></li>
                    305: <li><a href="#lond">lond</a></li>
                    306: <li><a href="#lonc">lonc</a></li>
                    307: <li><a href="#lonnet">lonnet</a></li>
                    308: <li><a href="#connections">Connections</a></li>
                    309: <li><a href="#delayed">Delayed Messages</a></li>
                    310: <li><a href="#errcount">Error Count</a></li>
1.1       albertel  311: </ol>
1.48      albertel  312: <hr />
                    313: <a name="configuration" />
1.1       albertel  314: <h2>Configuration</h2>
                    315: <h3>PerlVars</h3>
1.48      albertel  316: <table border="2">
1.1       albertel  317: ENDHEADERS
                    318: 
1.46      albertel  319:     foreach my $varname (sort(keys(%perlvar))) {
1.48      albertel  320: 	&log($fh,"<tr><td>$varname</td><td>".
                    321: 	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43      albertel  322:     }
1.48      albertel  323:     &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72      albertel  324:     my %hostname = &Apache::lonnet::all_hostnames();
                    325:     foreach my $id (sort(keys(%hostname))) {
                    326: 	my $role = (&Apache::lonnet::is_library($id) ? 'library'
                    327: 		                                     : 'access');
1.46      albertel  328: 	&log($fh,
1.72      albertel  329: 	    "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
                    330: 	    "</td><td>".$role.
                    331: 	    "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
                    332:     }
                    333:     &log($fh,"</table><h3>Spare Hosts</h3><ul>");
                    334:     foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
                    335: 	&log($fh,"<li>$type\n<ol>");
                    336: 	foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
                    337: 	    &log($fh,"<li>$id</li>\n");
                    338: 	}
                    339: 	&log($fh,"</ol>\n</li>\n");
1.43      albertel  340:     }
1.72      albertel  341:     &log($fh,"</ul>\n");
1.46      albertel  342:     return $fh;
                    343: }
1.11      www       344: 
                    345: # --------------------------------------------------------------- clean out tmp
1.46      albertel  346: sub clean_tmp {
                    347:     my ($fh)=@_;
1.48      albertel  348:     &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.46      albertel  349:     my $cleaned=0;
                    350:     my $old=0;
                    351:     while (my $fname=<$perlvar{'lonDaemons'}/tmp/*>) {
1.43      albertel  352: 	my ($dev,$ino,$mode,$nlink,
                    353: 	    $uid,$gid,$rdev,$size,
                    354: 	    $atime,$mtime,$ctime,
                    355: 	    $blksize,$blocks)=stat($fname);
1.46      albertel  356: 	my $now=time;
                    357: 	my $since=$now-$mtime;
1.43      albertel  358: 	if ($since>$perlvar{'lonExpire'}) {
1.46      albertel  359: 	    my $line='';
1.43      albertel  360: 	    if (open(PROBE,$fname)) {
                    361: 		$line=<PROBE>;
                    362: 		close(PROBE);
                    363: 	    }
                    364: 	    unless ($line=~/^CHECKOUTTOKEN\&/) {
                    365: 		$cleaned++;
                    366: 		unlink("$fname");
                    367: 	    } else {
                    368: 		if ($since>365*$perlvar{'lonExpire'}) {
                    369: 		    $cleaned++;
                    370: 		    unlink("$fname");
                    371: 		} else { $old++; }
                    372: 	    }
                    373: 	}
                    374:     }
1.46      albertel  375:     &log($fh,"Cleaned up ".$cleaned." files (".$old." old checkout tokens).");
                    376: }
1.11      www       377: 
                    378: # ------------------------------------------------------------ clean out lonIDs
1.46      albertel  379: sub clean_lonIDs {
                    380:     my ($fh)=@_;
1.48      albertel  381:     &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46      albertel  382:     my $cleaned=0;
                    383:     my $active=0;
                    384:     while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.43      albertel  385: 	my ($dev,$ino,$mode,$nlink,
                    386: 	    $uid,$gid,$rdev,$size,
                    387: 	    $atime,$mtime,$ctime,
                    388: 	    $blksize,$blocks)=stat($fname);
1.46      albertel  389: 	my $now=time;
                    390: 	my $since=$now-$mtime;
1.43      albertel  391: 	if ($since>$perlvar{'lonExpire'}) {
                    392: 	    $cleaned++;
1.46      albertel  393: 	    &log($fh,"Unlinking $fname<br>");
1.43      albertel  394: 	    unlink("$fname");
                    395: 	} else {
                    396: 	    $active++;
                    397: 	}
1.46      albertel  398:     }
1.48      albertel  399:     &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46      albertel  400:     &log($fh,"<h3>$active open session(s)</h3>");
                    401: }
1.43      albertel  402: 
1.74      albertel  403: # ----------------------------------------------------------- clean out sockets
                    404: sub clean_sockets {
                    405:     my ($fh)=@_;
                    406:     my $cleaned=0;
                    407:     opendir(SOCKETS,$perlvar{'lonSockDir'});
                    408:     while (my $fname=readdir(SOCKETS)) {
                    409: 	next if (-d $fname 
                    410: 		 || $fname=~/(mysqlsock|maximasock|\Q$perlvar{'lonSockDir'}\E)/);
                    411: 	$cleaned++;
                    412: 	&log($fh,"Unlinking $fname<br />");
                    413: 	unlink("/home/httpd/sockets/$fname");
                    414:     }
                    415:     &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
                    416: }
                    417: 
1.11      www       418: 
1.1       albertel  419: # ----------------------------------------------------------------------- httpd
1.46      albertel  420: sub check_httpd_logs {
                    421:     my ($fh)=@_;
1.48      albertel  422:     &log($fh,'<hr /><a name="httpd" /><h2>httpd</h2><h3>Access Log</h3><pre>');
1.43      albertel  423:     
                    424:     open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
1.48      albertel  425:     while (my $line=<DFH>) { &log($fh,&encode_entities($line,'<>&"')) };
1.43      albertel  426:     close (DFH);
1.46      albertel  427: 	
                    428:     &log($fh,"</pre><h3>Error Log</h3><pre>");
                    429: 	
1.43      albertel  430:     open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
1.46      albertel  431:     while (my $line=<DFH>) { 
                    432: 	&log($fh,"$line");
1.43      albertel  433: 	if ($line=~/\[error\]/) { $notices++; } 
1.46      albertel  434:     }
1.43      albertel  435:     close (DFH);
1.46      albertel  436:     &log($fh,"</pre>");
1.43      albertel  437:     &errout($fh);
1.46      albertel  438: }
1.1       albertel  439: 
                    440: # ---------------------------------------------------------------------- lonnet
                    441: 
1.48      albertel  442: sub rotate_lonnet_logs {
1.46      albertel  443:     my ($fh)=@_;
1.48      albertel  444:     &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.43      albertel  445:     print "checking logs\n";
                    446:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
                    447: 	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.46      albertel  448: 	while (my $line=<DFH>) { 
1.48      albertel  449: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  450: 	}
1.43      albertel  451: 	close (DFH);
                    452:     }
1.46      albertel  453:     &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.43      albertel  454:     
                    455:     if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    456: 	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.46      albertel  457: 	while (my $line=<DFH>) { 
1.48      albertel  458: 	    &log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  459: 	}
1.43      albertel  460: 	close (DFH);
1.46      albertel  461:     } else { &log($fh,"No perm log\n") }
1.43      albertel  462: 
1.46      albertel  463:     my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73      albertel  464:     &rotate_logfile($fname,$fh,'lonnet log');
1.1       albertel  465: 
1.46      albertel  466:     &log($fh,"</pre>");
1.43      albertel  467:     &errout($fh);
1.46      albertel  468: }
                    469: 
1.73      albertel  470: sub rotate_other_logs {
                    471:     my ($fh) = @_;
                    472:     my $fname="$perlvar{'lonDaemons'}/logs/autoenroll.log";
                    473:     &rotate_logfile($fname,$fh,'Auto Enroll log');
                    474:     $fname="$perlvar{'lonDaemons'}/logs/autocreate.log";
                    475:     &rotate_logfile($fname,$fh,'Create Course log');
                    476:     $fname="$perlvar{'lonDaemons'}/logs/searchcat.log";
                    477:     &rotate_logfile($fname,$fh,'Search Cataloguing log');
                    478: }
                    479: 
1.43      albertel  480: # ----------------------------------------------------------------- Connections
1.46      albertel  481: sub test_connections {
1.72      albertel  482:     my ($fh)=@_;
1.48      albertel  483:     &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.43      albertel  484:     print "testing connections\n";
1.48      albertel  485:     &log($fh,"<table border='2'>");
1.49      albertel  486:     my ($good,$bad)=(0,0);
1.72      albertel  487:     my %hostname = &Apache::lonnet::all_hostnames();
                    488:     foreach my $tryserver (sort(keys(%hostname))) {
1.43      albertel  489: 	print(".");
1.46      albertel  490: 	my $result;
1.72      albertel  491: 	my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43      albertel  492: 	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
                    493: 	    $result="<b>ok</b>";
1.49      albertel  494: 	    $good++;
1.43      albertel  495: 	} else {
                    496: 	    $result=$answer;
                    497: 	    $warnings++;
1.49      albertel  498: 	    if ($answer eq 'con_lost') {
                    499: 		$bad++;
                    500: 		$warnings++;
1.50      albertel  501: 	    } else {
                    502: 		$good++; #self connection
1.49      albertel  503: 	    }
1.43      albertel  504: 	}
                    505: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46      albertel  506: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1       albertel  507:     }
1.46      albertel  508:     &log($fh,"</table>");
1.49      albertel  509:     print "\n$good good, $bad bad connections\n";
1.43      albertel  510:     &errout($fh);
1.46      albertel  511: }
                    512: 
                    513: 
1.1       albertel  514: # ------------------------------------------------------------ Delayed messages
1.46      albertel  515: sub check_delayed_msg {
1.72      albertel  516:     my ($fh)=@_;
1.48      albertel  517:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.43      albertel  518:     print "checking buffers\n";
1.46      albertel  519:     
                    520:     &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1       albertel  521: 
1.46      albertel  522:     my $unsend=0;
1.1       albertel  523: 
1.46      albertel  524:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
                    525:     while (my $line=<$dfh>) {
                    526: 	my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
                    527: 	if ($sdf eq 'F') { 
                    528: 	    my $local=localtime($time);
                    529: 	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
                    530: 	    $warnings++;
1.43      albertel  531: 	}
1.46      albertel  532: 	if ($sdf eq 'S') { $unsend--; }
                    533: 	if ($sdf eq 'D') { $unsend++; }
1.1       albertel  534:     }
1.46      albertel  535: 
1.48      albertel  536:     &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.43      albertel  537:     $warnings=$warnings+5*$unsend;
1.1       albertel  538: 
1.43      albertel  539:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48      albertel  540:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68      www       541: # list directory with delayed messages and remember offline servers
                    542:     my %servers=();
1.43      albertel  543:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68      www       544:     while (my $line=<DFH>) {
                    545:         my ($server)=($line=~/\.(\w+)$/);
                    546:         if ($server) { $servers{$server}=1; }
1.48      albertel  547: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  548:     }
1.48      albertel  549:     &log($fh,"</pre>\n");
1.43      albertel  550:     close (DFH);
1.68      www       551: # pong to all servers that have delayed messages
                    552: # this will trigger a reverse connection, which should flush the buffers
                    553:     foreach my $tryserver (keys %servers) {
1.72      albertel  554: 	my $answer=&Apache::lonnet::reply("pong",$tryserver);
1.69      www       555: 	&log($fh,"Pong to $tryserver: $answer<br />");
1.68      www       556:     }
1.46      albertel  557: }
1.1       albertel  558: 
1.46      albertel  559: sub finish_logging {
                    560:     my ($fh)=@_;
1.48      albertel  561:     &log($fh,"<a name='errcount' />\n");
1.43      albertel  562:     $totalcount=$notices+4*$warnings+100*$errors;
                    563:     &errout($fh);
1.46      albertel  564:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
                    565:     my $now=time;
                    566:     my $date=localtime($now);
1.48      albertel  567:     &log($fh,"<hr />$date ($now)</body></html>\n");
1.43      albertel  568:     print "lon-status webpage updated\n";
                    569:     $fh->close();
1.46      albertel  570: 
                    571:     if ($errors) { $simplestatus{'errors'}=$errors; }
                    572:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
                    573:     if ($notices) { $simplestatus{'notices'}=$notices; }
                    574:     $simplestatus{'time'}=time;
1.1       albertel  575: }
                    576: 
1.46      albertel  577: sub log_simplestatus {
1.73      albertel  578:     rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46      albertel  579:     
1.43      albertel  580:     my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
                    581:     foreach (keys %simplestatus) {
                    582: 	print $sfh $_.'='.$simplestatus{$_}.'&';
                    583:     }
                    584:     print $sfh "\n";
                    585:     $sfh->close();
1.41      www       586: }
1.46      albertel  587: 
                    588: sub send_mail {
1.43      albertel  589:     print "sending mail\n";
1.78    ! raeburn   590:     my $defdom = $perlvar->{'lonDefDomain'};
        !           591:     my $origmail = $perlvar->{'lonAdmEMail'};
        !           592:     my $emailto = &Apache::loncommon::build_recipient_list(undef,
        !           593:                                    'lonstatusmail',$defdom,$origmail);
1.54      www       594:     if ($totalcount>2500) {
1.43      albertel  595: 	$emailto.=",$perlvar{'lonSysEMail'}";
                    596:     }
1.46      albertel  597:     my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
1.52      albertel  598: 
1.58      albertel  599:     my $result=system("metasend -b -S 4000000 -t $emailto -s '$subj' -f $statusdir/index.html -m text/html >& /dev/null");
1.52      albertel  600:     if ($result != 0) {
                    601: 	$result=system("mail -s '$subj' $emailto < $statusdir/index.html");
                    602:     }
1.1       albertel  603: }
1.46      albertel  604: 
1.49      albertel  605: sub usage {
                    606:     print(<<USAGE);
                    607: loncron - housekeeping program that checks up on various parts of Lon-CAPA
                    608: 
                    609: Options:
1.71      albertel  610:    --help     Display 
1.49      albertel  611:    --noemail  Do not send the status email
                    612:    --justcheckconnections  Only check the current status of the lonc/d
                    613:                                 connections, do not send emails do not
                    614:                                 check if the daemons are running, do not
                    615:                                 generate lon-status
                    616:    --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
                    617:                                 running, do not send emails do not
                    618:                                 check the lonc/d connections, do not
                    619:                                 generate lon-status
1.59      albertel  620:    --justreload            Only tell the daemons to reload the config files,
                    621: 				do not send emails do not
                    622:                                 check if the daemons are running, do not
                    623:                                 generate lon-status
1.49      albertel  624:                            
                    625: USAGE
                    626: }
                    627: 
1.46      albertel  628: # ================================================================ Main Program
                    629: sub main () {
1.71      albertel  630:     my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.59      albertel  631: 	$justreload);
1.49      albertel  632:     &GetOptions("help"                 => \$help,
                    633: 		"justcheckdaemons"     => \$justcheckdaemons,
                    634: 		"noemail"              => \$noemail,
1.59      albertel  635: 		"justcheckconnections" => \$justcheckconnections,
                    636: 		"justreload"           => \$justreload
1.49      albertel  637: 		);
                    638:     if ($help) { &usage(); return; }
1.46      albertel  639: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
                    640:     my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
                    641:     %perlvar=%{$perlvarref};
                    642:     undef $perlvarref;
                    643:     delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
                    644:     delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.75      albertel  645:     chdir($perlvar{'lonDaemons'});
1.46      albertel  646: # --------------------------------------- Make sure that LON-CAPA is configured
                    647: # I only test for one thing here (lonHostID).  This is just a safeguard.
                    648:     if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
                    649: 	print("Unconfigured machine.\n");
                    650: 	my $emailto=$perlvar{'lonSysEMail'};
                    651: 	my $hostname=`/bin/hostname`;
                    652: 	chop $hostname;
                    653: 	$hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
                    654: 	my $subj="LON: Unconfigured machine $hostname";
                    655: 	system("echo 'Unconfigured machine $hostname.' |\
                    656:  mailto $emailto -s '$subj' > /dev/null");
                    657: 	exit 1;
                    658:     }
                    659: 
                    660: # ----------------------------- Make sure this process is running from user=www
                    661:     my $wwwid=getpwnam('www');
                    662:     if ($wwwid!=$<) {
                    663: 	print("User ID mismatch.  This program must be run as user 'www'\n");
                    664: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                    665: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
                    666: 	system("echo 'User ID mismatch.  loncron must be run as user www.' |\
                    667:  mailto $emailto -s '$subj' > /dev/null");
                    668: 	exit 1;
                    669:     }
                    670: 
1.72      albertel  671: # -------------------------------------------- Force reload of host information
                    672:     &Apache::lonnet::load_hosts_tab(1);
                    673:     &Apache::lonnet::load_domain_tab(1);
                    674:     &Apache::lonnet::get_iphost(1);
1.46      albertel  675: 
                    676: # ---------------------------------------------------------------- Start report
                    677: 
                    678:     $errors=0;
                    679:     $warnings=0;
                    680:     $notices=0;
                    681: 
                    682: 	
1.49      albertel  683:     my $fh;
1.59      albertel  684:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72      albertel  685: 	$fh=&start_logging();
1.49      albertel  686: 
                    687: 	&log_machine_info($fh);
                    688: 	&clean_tmp($fh);
                    689: 	&clean_lonIDs($fh);
                    690: 	&check_httpd_logs($fh);
                    691: 	&rotate_lonnet_logs($fh);
1.73      albertel  692: 	&rotate_other_logs($fh);
1.49      albertel  693:     }
1.59      albertel  694:     if (!$justcheckconnections && !$justreload) {
1.76      albertel  695: 	&checkon_daemon($fh,'lonmemcached',40000);
1.49      albertel  696: 	&checkon_daemon($fh,'lonsql',200000);
1.63      albertel  697: 	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
                    698: 	    &checkon_daemon($fh,'lond',40000,'USR2');
                    699: 	}
1.71      albertel  700: 	&checkon_daemon($fh,'lonc',40000,'USR1');
1.70      raeburn   701:         &checkon_daemon($fh,'lonmaxima',40000);
1.49      albertel  702:     }
1.59      albertel  703:     if ($justreload) {
                    704: 	&checkon_daemon($fh,'lond',40000,'USR2');
1.71      albertel  705: 	&checkon_daemon($fh,'lonc',40000,'USR2');
1.59      albertel  706:     }
1.63      albertel  707:     if ($justcheckconnections) {
1.72      albertel  708: 	&test_connections($fh);
1.49      albertel  709:     }
1.59      albertel  710:     if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72      albertel  711: 	&check_delayed_msg($fh);
1.49      albertel  712: 	&finish_logging($fh);
                    713: 	&log_simplestatus();
                    714: 	
                    715: 	if ($totalcount>200 && !$noemail) { &send_mail(); }
                    716:     }
1.46      albertel  717: }
                    718: 
                    719: &main();
1.1       albertel  720: 1;
                    721: 
                    722: 
                    723: 
                    724: 
                    725: 
                    726: 
                    727: 
                    728: 

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