File:  [LON-CAPA] / loncom / loncron
Revision 1.59: download - view: text, annotated - select for diffs
Thu May 26 20:25:00 2005 UTC (18 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- 'reload' command now sends USR2 against lonc/d causng them to reread the hosts.tab/doain.tab files

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

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