Annotation of loncom/loncron, revision 1.50

1.1       albertel    1: #!/usr/bin/perl
                      2: 
1.47      albertel    3: # Housekeeping program, started by cron, loncontrol and loncron.pl
                      4: #
1.50    ! albertel    5: # $Id: loncron,v 1.49 2004/05/11 21:08:55 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 {
1.50    ! albertel   80:     my ($fh,$daemon,$pidfile,$args) = @_;
1.44      albertel   81:     my $progname=$daemon;
1.50    ! albertel   82:     if ($daemon eq 'lonc' && $args eq 'new') {
1.44      albertel   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 {
1.50    ! albertel  106:     my ($fh,$daemon,$maxsize,$sendusr1,$args)=@_;
1.42      albertel  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: 	
1.50    ! albertel  155: 	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
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 " ";
1.50    ! albertel  164: 	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
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++;
1.50    ! albertel  468: 	    } else {
        !           469: 		$good++; #self connection
1.49      albertel  470: 	    }
1.43      albertel  471: 	}
                    472: 	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46      albertel  473: 	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1       albertel  474:     }
1.46      albertel  475:     &log($fh,"</table>");
1.49      albertel  476:     print "\n$good good, $bad bad connections\n";
1.43      albertel  477:     &errout($fh);
1.46      albertel  478: }
                    479: 
                    480: 
1.1       albertel  481: # ------------------------------------------------------------ Delayed messages
1.46      albertel  482: sub check_delayed_msg {
                    483:     my ($fh)=@_;
1.48      albertel  484:     &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.43      albertel  485:     print "checking buffers\n";
1.46      albertel  486:     
                    487:     &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1       albertel  488: 
1.46      albertel  489:     my $unsend=0;
1.1       albertel  490: 
1.46      albertel  491:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
                    492:     while (my $line=<$dfh>) {
                    493: 	my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
                    494: 	if ($sdf eq 'F') { 
                    495: 	    my $local=localtime($time);
                    496: 	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
                    497: 	    $warnings++;
1.43      albertel  498: 	}
1.46      albertel  499: 	if ($sdf eq 'S') { $unsend--; }
                    500: 	if ($sdf eq 'D') { $unsend++; }
1.1       albertel  501:     }
1.46      albertel  502: 
1.48      albertel  503:     &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.43      albertel  504:     $warnings=$warnings+5*$unsend;
1.1       albertel  505: 
1.43      albertel  506:     if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48      albertel  507:     &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.1       albertel  508: 
1.43      albertel  509:     open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.46      albertel  510:     while (my $line=<DFH>) { 
1.48      albertel  511: 	&log($fh,&encode_entities($line,'<>&"'));
1.46      albertel  512:     }
1.48      albertel  513:     &log($fh,"</pre>\n");
1.43      albertel  514:     close (DFH);
1.46      albertel  515: }
1.1       albertel  516: 
1.46      albertel  517: sub finish_logging {
                    518:     my ($fh)=@_;
1.48      albertel  519:     &log($fh,"<a name='errcount' />\n");
1.43      albertel  520:     $totalcount=$notices+4*$warnings+100*$errors;
                    521:     &errout($fh);
1.46      albertel  522:     &log($fh,"<h1>Total Error Count: $totalcount</h1>");
                    523:     my $now=time;
                    524:     my $date=localtime($now);
1.48      albertel  525:     &log($fh,"<hr />$date ($now)</body></html>\n");
1.43      albertel  526:     print "lon-status webpage updated\n";
                    527:     $fh->close();
1.46      albertel  528: 
                    529:     if ($errors) { $simplestatus{'errors'}=$errors; }
                    530:     if ($warnings) { $simplestatus{'warnings'}=$warnings; }
                    531:     if ($notices) { $simplestatus{'notices'}=$notices; }
                    532:     $simplestatus{'time'}=time;
1.1       albertel  533: }
                    534: 
1.46      albertel  535: sub log_simplestatus {
                    536:     rename ("$statusdir/newstatus.html","$statusdir/index.html");
                    537:     
1.43      albertel  538:     my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
                    539:     foreach (keys %simplestatus) {
                    540: 	print $sfh $_.'='.$simplestatus{$_}.'&';
                    541:     }
                    542:     print $sfh "\n";
                    543:     $sfh->close();
1.41      www       544: }
1.46      albertel  545: 
                    546: sub send_mail {
1.43      albertel  547:     print "sending mail\n";
1.46      albertel  548:     my $emailto="$perlvar{'lonAdmEMail'}";
1.43      albertel  549:     if ($totalcount>1000) {
                    550: 	$emailto.=",$perlvar{'lonSysEMail'}";
                    551:     }
1.46      albertel  552:     my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
1.43      albertel  553:     system("metasend -b -t $emailto -s '$subj' -f $statusdir/index.html -m text/html");
1.1       albertel  554: }
1.46      albertel  555: 
1.49      albertel  556: sub usage {
                    557:     print(<<USAGE);
                    558: loncron - housekeeping program that checks up on various parts of Lon-CAPA
                    559: 
                    560: Options:
                    561:    --help     Display help
                    562:    --oldlonc  When starting the lonc daemon use 'lonc' not 'loncnew'
                    563:    --noemail  Do not send the status email
                    564:    --justcheckconnections  Only check the current status of the lonc/d
                    565:                                 connections, do not send emails do not
                    566:                                 check if the daemons are running, do not
                    567:                                 generate lon-status
                    568:    --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
                    569:                                 running, do not send emails do not
                    570:                                 check the lonc/d connections, do not
                    571:                                 generate lon-status
                    572:                            
                    573: USAGE
                    574: }
                    575: 
1.46      albertel  576: # ================================================================ Main Program
                    577: sub main () {
1.49      albertel  578:     my ($oldlonc,$help,$justcheckdaemons,$noemail,$justcheckconnections);
                    579:     &GetOptions("help"                 => \$help,
                    580: 		"oldlonc"              => \$oldlonc,
                    581: 		"justcheckdaemons"     => \$justcheckdaemons,
                    582: 		"noemail"              => \$noemail,
                    583: 		"justcheckconnections" => \$justcheckconnections
                    584: 		);
                    585:     if ($help) { &usage(); return; }
1.46      albertel  586: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
                    587:     my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
                    588:     %perlvar=%{$perlvarref};
                    589:     undef $perlvarref;
                    590:     delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
                    591:     delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
                    592: 
                    593: # --------------------------------------- Make sure that LON-CAPA is configured
                    594: # I only test for one thing here (lonHostID).  This is just a safeguard.
                    595:     if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
                    596: 	print("Unconfigured machine.\n");
                    597: 	my $emailto=$perlvar{'lonSysEMail'};
                    598: 	my $hostname=`/bin/hostname`;
                    599: 	chop $hostname;
                    600: 	$hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
                    601: 	my $subj="LON: Unconfigured machine $hostname";
                    602: 	system("echo 'Unconfigured machine $hostname.' |\
                    603:  mailto $emailto -s '$subj' > /dev/null");
                    604: 	exit 1;
                    605:     }
                    606: 
                    607: # ----------------------------- Make sure this process is running from user=www
                    608:     my $wwwid=getpwnam('www');
                    609:     if ($wwwid!=$<) {
                    610: 	print("User ID mismatch.  This program must be run as user 'www'\n");
                    611: 	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                    612: 	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
                    613: 	system("echo 'User ID mismatch.  loncron must be run as user www.' |\
                    614:  mailto $emailto -s '$subj' > /dev/null");
                    615: 	exit 1;
                    616:     }
                    617: 
                    618: # ------------------------------------------------------------- Read hosts file
                    619:     my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");
                    620:     
                    621:     my (%hostname,%hostdom,%hostrole,%spareid);
                    622:     while (my $configline=<$config>) {
1.49      albertel  623: 	next if ($configline =~ /^(\#|\s*\$)/);
1.46      albertel  624: 	my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
                    625: 	if ($id && $domain && $role && $name && $ip) {
                    626: 	    $hostname{$id}=$name;
                    627: 	    $hostdom{$id}=$domain;
                    628: 	    $hostrole{$id}=$role;
                    629: 	}
                    630:     }
                    631:     undef $config;
                    632: 
                    633: # ------------------------------------------------------ Read spare server file
                    634:     $config=IO::File->new("$perlvar{'lonTabDir'}/spare.tab");
                    635:     
                    636:     while (my $configline=<$config>) {
                    637: 	chomp($configline);
                    638: 	if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
                    639: 	    $spareid{$configline}=1;
                    640: 	}
                    641:     }
                    642:     undef $config;
                    643: 
                    644: # ---------------------------------------------------------------- Start report
                    645: 
                    646:     $errors=0;
                    647:     $warnings=0;
                    648:     $notices=0;
                    649: 
                    650: 	
1.49      albertel  651:     my $fh;
                    652:     if (!$justcheckdaemons && !$justcheckconnections) {
                    653: 	$fh=&start_logging(\%hostdom,\%hostrole,\%hostname,\%spareid);
                    654: 
                    655: 	&log_machine_info($fh);
                    656: 	&clean_tmp($fh);
                    657: 	&clean_lonIDs($fh);
                    658: 	&check_httpd_logs($fh);
                    659: 	&rotate_lonnet_logs($fh);
                    660:     }
                    661:     if (!$justcheckconnections) {
                    662: 	&checkon_daemon($fh,'lonsql',200000);
                    663: 	&checkon_daemon($fh,'lond',40000,1);
1.50    ! albertel  664: 	my $args='new';
        !           665: 	if ($oldlonc) { $args = ''; }
        !           666: 	&checkon_daemon($fh,'lonc',40000,1,$args);
1.49      albertel  667: 	&checkon_daemon($fh,'lonhttpd',40000);
                    668:     }
                    669:     if (!$justcheckdaemons) {
                    670: 	&test_connections($fh,\%hostname);
                    671:     }
                    672:     if (!$justcheckdaemons && !$justcheckconnections) {
                    673: 	&check_delayed_msg($fh);
                    674: 	&finish_logging($fh);
                    675: 	&log_simplestatus();
                    676: 	
                    677: 	if ($totalcount>200 && !$noemail) { &send_mail(); }
                    678:     }
1.46      albertel  679: }
                    680: 
                    681: &main();
1.1       albertel  682: 1;
                    683: 
                    684: 
                    685: 
                    686: 
                    687: 
                    688: 
                    689: 
                    690: 

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