File:  [LON-CAPA] / loncom / loncron
Revision 1.67: download - view: text, annotated - select for diffs
Tue Mar 7 20:01:26 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- revert to USR1 to lonc

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

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