Annotation of loncom/loncron, revision 1.49

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

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