Annotation of loncom/loncron, revision 1.72

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

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