File:  [LON-CAPA] / loncom / loncron
Revision 1.62: download - view: text, annotated - select for diffs
Thu Nov 10 19:07:01 2005 UTC (18 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- check that the value in the pid file is a number beforeing trying to kill it

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

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