File:  [LON-CAPA] / loncom / loncron
Revision 1.72: download - view: text, annotated - select for diffs
Wed Apr 11 23:08:43 2007 UTC (17 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- updated to use lonnet.pm for host information and reply communication

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

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