Annotation of loncom/loncron, revision 1.73

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

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