File:  [LON-CAPA] / loncom / loncron
Revision 1.73: download - view: text, annotated - select for diffs
Fri Apr 13 18:26:22 2007 UTC (17 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_2_3_99_0, HEAD
- bug#5199 rotate more log files

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

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