File:  [LON-CAPA] / loncom / loncron
Revision 1.58: download - view: text, annotated - select for diffs
Wed Apr 13 18:56:07 2005 UTC (19 years ago) by albertel
Branches: MAIN
CVS tags: version_1_99_1_tmcc, version_1_99_0_tmcc, HEAD
- specify a larger than default size for metamail

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

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