File:  [LON-CAPA] / loncom / loncron
Revision 1.53: download - view: text, annotated - select for diffs
Wed Jun 9 13:30:41 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_0, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
- no hyphen should be passed to ps

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

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