--- loncom/loncron	2003/09/10 19:13:09	1.42
+++ loncom/loncron	2006/03/07 20:01:26	1.67
@@ -1,35 +1,57 @@
 #!/usr/bin/perl
 
-# The LearningOnline Network
-# Housekeeping program, started by cron
+# Housekeeping program, started by cron, loncontrol and loncron.pl
 #
-# (TCP networking package
-# 6/1/99,6/2,6/10,6/11,6/12,6/14,6/26,6/28,6/29,6/30,
-# 7/1,7/2,7/9,7/10,7/12 Gerd Kortemeyer)
+# $Id: loncron,v 1.67 2006/03/07 20:01:26 albertel Exp $
+#
+# Copyright Michigan State University Board of Trustees
+#
+# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
+#
+# LON-CAPA is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# LON-CAPA is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with LON-CAPA; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# /home/httpd/html/adm/gpl.txt
+#
+# http://www.lon-capa.org/
 #
-# 7/14,7/15,7/19,7/21,7/22,11/18,
-# 2/8 Gerd Kortemeyer
-# 12/23 Gerd Kortemeyer
-# YEAR=2001
-# 09/04,09/06,11/26 Gerd Kortemeyer
 
 $|=1;
+use strict;
 
 use lib '/home/httpd/lib/perl/';
 use LONCAPA::Configuration;
 
 use IO::File;
 use IO::Socket;
+use HTML::Entities;
+use Getopt::Long;
+#globals
+use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
+
+my $statusdir="/home/httpd/html/lon-status";
+
 
 # -------------------------------------------------- Non-critical communication
 sub reply {
-    my ($cmd,$server)=@_;
-    my $peerfile="$perlvar{'lonSockDir'}/$server";
+    my ($cmd,$server,$hostname)=@_;
+    my $peerfile="$perlvar{'lonSockDir'}/".$hostname->{$server};
     my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                                      Type    => SOCK_STREAM,
                                      Timeout => 10)
        or return "con_lost";
-    print $client "$cmd\n";
+    print $client "sethost:$server:$cmd\n";
     my $answer=<$client>;
     chomp($answer);
     if (!$answer) { $answer="con_lost"; }
@@ -38,124 +60,154 @@ sub reply {
 
 # --------------------------------------------------------- Output error status
 
+sub log {
+    my $fh=shift;
+    if ($fh) {	print $fh @_  }
+}
+
 sub errout {
    my $fh=shift;
-   print $fh (<<ENDERROUT);
-     <p><table border=2 bgcolor="#CCCCCC">
+   &log($fh,(<<ENDERROUT));
+     <table border="2" bgcolor="#CCCCCC">
      <tr><td>Notices</td><td>$notices</td></tr>
      <tr><td>Warnings</td><td>$warnings</td></tr>
      <tr><td>Errors</td><td>$errors</td></tr>
-     </table><p><a href="#top">Top</a><p>
+     </table><p><a href="#top">Top</a></p>
 ENDERROUT
 }
 
 sub start_daemon {
-    my ($fh,$daemon,$pidfile) = @_;
-    system("$perlvar{'lonDaemons'}/$daemon 2>>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
-    sleep 2;
+    my ($fh,$daemon,$pidfile,$args) = @_;
+    my $progname=$daemon;
+    if ($daemon eq 'lonc' && $args eq 'new') {
+	$progname='loncnew'; 
+	print "new ";
+    }
+    my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
+    my $size=(stat($error_fname))[7];
+    if ($size>40000) {
+	&log($fh,"<p>Rotating error logs ...</p>");
+	rename("$error_fname.2","$error_fname.3");
+	rename("$error_fname.1","$error_fname.2");
+	rename("$error_fname","$error_fname.1");
+    }
+    system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
+    sleep 1;
     if (-e $pidfile) {
-	print $fh "Seems like it started ...<p>";
+	&log($fh,"<p>Seems like it started ...</p>");
 	my $lfh=IO::File->new("$pidfile");
 	my $daemonpid=<$lfh>;
 	chomp($daemonpid);
-	sleep 2;
-	if (kill 0 => $daemonpid) {
+	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
 	    return 1;
 	} else {
 	    return 0;
 	}
     }
-    print $fh "Seems like that did not work!<p>";
+    &log($fh,"<p>Seems like that did not work!</p>");
     $errors++;
     return 0;
 }
 
 sub checkon_daemon {
-    my ($fh,$daemon,$maxsize,$sendusr1)=@_;
+    my ($fh,$daemon,$maxsize,$send,$args)=@_;
 
-    print $fh '<hr><a name="'.$daemon.'"><h2>'.$daemon.'</h2><h3>Log</h3><pre>';
-    printf("%-10s ",$daemon);
+    my $result;
+    &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
+    printf("%-15s ",$daemon);
     if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
 	open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
-	while ($line=<DFH>) { 
-	    print $fh "$line";
+	while (my $line=<DFH>) { 
+	    &log($fh,"$line");
 	    if ($line=~/INFO/) { $notices++; }
 	    if ($line=~/WARNING/) { $notices++; }
 	    if ($line=~/CRITICAL/) { $warnings++; }
 	};
 	close (DFH);
     }
-    print $fh "</pre>";
+    &log($fh,"</tt></p>");
     
     my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
     
     my $restartflag=1;
-    
+    my $daemonpid;
     if (-e $pidfile) {
 	my $lfh=IO::File->new("$pidfile");
-	my $daemonpid=<$lfh>;
+	$daemonpid=<$lfh>;
 	chomp($daemonpid);
-	if (kill 0 => $daemonpid) {
-	    print $fh "<h3>$daemon at pid $daemonpid responding";
-	    if ($sendusr1) { print $fh ", sending USR1"; }
-	    print $fh "</h3>";
-	    if ($sendusr1) { kill USR1 => $daemonpid; }
+	if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
+	    &log($fh,"<h3>$daemon at pid $daemonpid responding");
+	    if ($send) { &log($fh,", sending $send"); }
+	    &log($fh,"</h3>");
+	    if ($send eq 'USR1') { kill USR1 => $daemonpid; }
+	    if ($send eq 'USR2') { kill USR2 => $daemonpid; }
 	    $restartflag=0;
-	    print "running\n";
+	    if ($send eq 'USR2') {
+		$result = 'reloaded';
+		print "reloaded\n";
+	    } else {
+		$result = 'running';
+		print "running\n";
+	    }
 	} else {
 	    $errors++;
-	    print $fh "<h3>$daemon at pid $daemonpid not responding</h3>";
+	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
 	    $restartflag=1;
-	    print $fh "<h3>Decided to clean up stale .pid file and restart $daemon</h3>";
+	    &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
 	}
     }
     if ($restartflag==1) {
 	$simplestatus{$daemon}='off';
 	$errors++;
-	print $fh '<br><font color="red">Killall '.$daemon.': '.
-	    `killall $daemon 2>&1`.' - ';
-	sleep 2;
-	print $fh unlink($pidfile).' - '.
-	    `killall -9 $daemon 2>&1`.
-	    '</font><br>';
-	print $fh "<h3>$daemon not running, trying to start</h3>";
+	my $kadaemon=$daemon;
+	if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
+	&log($fh,'<br><font color="red">Killall '.$daemon.': '.
+	    `killall $kadaemon 2>&1`.' - ');
+	sleep 1;
+	&log($fh,unlink($pidfile).' - '.
+	    `killall -9 $kadaemon 2>&1`.
+	    '</font><br>');
+	&log($fh,"<h3>$daemon not running, trying to start</h3>");
 	
-	if (&start_daemon($fh,$daemon,$pidfile)) {
-	    print $fh "<h3>$daemon at pid $daemonpid responding</h3>";
+	if (&start_daemon($fh,$daemon,$pidfile,$args)) {
+	    &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
 	    $simplestatus{$daemon}='restarted';
+	    $result = 'started';
 	    print "started\n";
 	} else {
 	    $errors++;
-	    print $fh "<h3>$daemon at pid $daemonpid not responding</h3>";
-	    print $fh "Give it one more try ...<p>";
+	    &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
+	    &log($fh,"<p>Give it one more try ...</p>");
 	    print " ";
-	    if (&start_daemon($fh,$daemon,$pidfile)) {
-		print $fh "<h3>$daemon at pid $daemonpid responding</h3>";
+	    if (&start_daemon($fh,$daemon,$pidfile,$args)) {
+		&log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
 		$simplestatus{$daemon}='restarted';
+		$result = 'started';
 		print "started\n";
 	    } else {
+		$result = 'failed';
 		print " failed\n";
 		$simplestatus{$daemon}='failed';
 		$errors++; $errors++;
-		print $fh "<h3>$daemon at pid $daemonpid not responding</h3>";
-		print $fh "Unable to start $daemon<p>";
+		&log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
+		&log($fh,"<p>Unable to start $daemon</p>");
 	    }
 	}
 
 	if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
-	    print $fh "<p><pre>";
+	    &log($fh,"<p><pre>");
 	    open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
-	    while ($line=<DFH>) { 
-		print $fh "$line";
+	    while (my $line=<DFH>) { 
+		&log($fh,"$line");
 		if ($line=~/WARNING/) { $notices++; }
 		if ($line=~/CRITICAL/) { $notices++; }
 	    };
 	    close (DFH);
-	    print $fh "</pre>";
+	    &log($fh,"</pre></p>");
 	}
     }
     
-    $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
+    my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
     
     my ($dev,$ino,$mode,$nlink,
 	$uid,$gid,$rdev,$size,
@@ -163,422 +215,521 @@ sub checkon_daemon {
 	$blksize,$blocks)=stat($fname);
     
     if ($size>$maxsize) {
-	print $fh "Rotating logs ...<p>";
+	&log($fh,"<p>Rotating logs ...</p>");
 	rename("$fname.2","$fname.3");
 	rename("$fname.1","$fname.2");
 	rename("$fname","$fname.1");
     }
 
     &errout($fh);
+    return $result;
 }
-# ================================================================ Main Program
 
-# --------------------------------- Read loncapa_apache.conf and loncapa.conf
-my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
-%perlvar=%{$perlvarref};
-undef $perlvarref;
-delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
-delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
-
-# --------------------------------------- Make sure that LON-CAPA is configured
-# I only test for one thing here (lonHostID).  This is just a safeguard.
-if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
-   print("Unconfigured machine.\n");
-   $emailto=$perlvar{'lonSysEMail'};
-   $hostname=`/bin/hostname`;
-   chop $hostname;
-   $hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
-   $subj="LON: Unconfigured machine $hostname";
-   system("echo 'Unconfigured machine $hostname.' |\
- mailto $emailto -s '$subj' > /dev/null");
-    exit 1;
-}
-
-# ----------------------------- Make sure this process is running from user=www
-my $wwwid=getpwnam('www');
-if ($wwwid!=$<) {
-   print("User ID mismatch.  This program must be run as user 'www'\n");
-   $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
-   $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
-   system("echo 'User ID mismatch.  loncron must be run as user www.' |\
- mailto $emailto -s '$subj' > /dev/null");
-   exit 1;
-}
+# --------------------------------------------------------------------- Machine
+sub log_machine_info {
+    my ($fh)=@_;
+    &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
+    &log($fh,"<h3>loadavg</h3>");
+	
+    open (LOADAVGH,"/proc/loadavg");
+    my $loadavg=<LOADAVGH>;
+    close (LOADAVGH);
+    
+    &log($fh,"<tt>$loadavg</tt>");
+    
+    my @parts=split(/\s+/,$loadavg);
+    if ($parts[1]>4.0) {
+	$errors++;
+    } elsif ($parts[1]>2.0) {
+	$warnings++;
+    } elsif ($parts[1]>1.0) {
+	$notices++;
+    }
 
-# ------------------------------------------------------------- Read hosts file
-{
-    my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");
+    &log($fh,"<h3>df</h3>");
+    &log($fh,"<pre>");
 
-    while (my $configline=<$config>) {
-	my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
-	if ($id && $domain && $role && $name && $ip) {
-	    $hostname{$id}=$name;
-	    $hostdom{$id}=$domain;
-	    $hostip{$id}=$ip;
-	    $hostrole{$id}=$role;
-	    if ($domdescr) { $domaindescription{$domain}=$domdescr; }
-	    if (($role eq 'library') && ($id ne $perlvar{'lonHostID'})) {
-		$libserv{$id}=$name;
-	    }
-	} else {
-	    if ($configline) {
-#		&logthis("Skipping hosts.tab line -$configline-");
-	    }
+    open (DFH,"df|");
+    while (my $line=<DFH>) { 
+	&log($fh,&encode_entities($line,'<>&"')); 
+	@parts=split(/\s+/,$line);
+	my $usage=$parts[4];
+	$usage=~s/\W//g;
+	if ($usage>90) { 
+	    $warnings++;
+	    $notices++; 
+	} elsif ($usage>80) {
+	    $warnings++;
+	} elsif ($usage>60) {
+	    $notices++;
 	}
+	if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
     }
-}
+    close (DFH);
+    &log($fh,"</pre>");
 
-# ------------------------------------------------------ Read spare server file
-{
-    my $config=IO::File->new("$perlvar{'lonTabDir'}/spare.tab");
 
-    while (my $configline=<$config>) {
-       chomp($configline);
-       if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
-          $spareid{$configline}=1;
-       }
+    &log($fh,"<h3>ps</h3>");
+    &log($fh,"<pre>");
+    my $psproc=0;
+
+    open (PSH,"ps aux --cols 140 |");
+    while (my $line=<PSH>) { 
+	&log($fh,&encode_entities($line,'<>&"')); 
+	$psproc++;
     }
-}
+    close (PSH);
+    &log($fh,"</pre>");
 
-# ---------------------------------------------------------------- Start report
-
-$statusdir="/home/httpd/html/lon-status";
+    if ($psproc>200) { $notices++; }
+    if ($psproc>250) { $notices++; }
 
-$errors=0;
-$warnings=0;
-$notices=0;
+    &log($fh,"<h3>distprobe</h3>");
+    &log($fh,"<pre>");
+    open(DSH,"$perlvar{'lonDaemons'}/distprobe |");
+    while (my $line=<DSH>) { 
+	&log($fh,&encode_entities($line,'<>&"')); 
+	$psproc++;
+    }
+    close(DSH);
+    &log($fh,"</pre>");
 
-$now=time;
-$date=localtime($now);
+    &errout($fh);
+}
 
-{
-my $fh=IO::File->new(">$statusdir/newstatus.html");
-my %simplestatus=();
+sub start_logging {
+    my ($hostdom,$hostrole,$hostname,$spareid)=@_;
+    my $fh=IO::File->new(">$statusdir/newstatus.html");
+    my %simplestatus=();
+    my $now=time;
+    my $date=localtime($now);
+    
 
-print $fh (<<ENDHEADERS);
+    &log($fh,(<<ENDHEADERS));
 <html>
 <head>
 <title>LON Status Report $perlvar{'lonHostID'}</title>
 </head>
 <body bgcolor="#AAAAAA">
-<a name="top">
+<a name="top" />
 <h1>LON Status Report $perlvar{'lonHostID'}</h1>
 <h2>$date ($now)</h2>
 <ol>
-<li><a href="#configuration">Configuration</a>
-<li><a href="#machine">Machine Information</a>
-<li><a href="#tmp">Temporary Files</a>
-<li><a href="#tokens">Session Tokens</a>
-<li><a href="#httpd">httpd</a>
-<li><a href="#lonsql">lonsql</a>
-<li><a href="#lond">lond</a>
-<li><a href="#lonc">lonc</a>
-<li><a href="#lonhttpd">lonhttpd</a>
-<li><a href="#lonnet">lonnet</a>
-<li><a href="#connections">Connections</a>
-<li><a href="#delayed">Delayed Messages</a>
-<li><a href="#errcount">Error Count</a>
+<li><a href="#configuration">Configuration</a></li>
+<li><a href="#machine">Machine Information</a></li>
+<li><a href="#tmp">Temporary Files</a></li>
+<li><a href="#tokens">Session Tokens</a></li>
+<li><a href="#httpd">httpd</a></li>
+<li><a href="#lonsql">lonsql</a></li>
+<li><a href="#lond">lond</a></li>
+<li><a href="#lonc">lonc</a></li>
+<li><a href="#lonhttpd">lonhttpd</a></li>
+<li><a href="#lonnet">lonnet</a></li>
+<li><a href="#connections">Connections</a></li>
+<li><a href="#delayed">Delayed Messages</a></li>
+<li><a href="#errcount">Error Count</a></li>
 </ol>
-<hr>
-<a name="configuration">
+<hr />
+<a name="configuration" />
 <h2>Configuration</h2>
 <h3>PerlVars</h3>
-<table border=2>
+<table border="2">
 ENDHEADERS
 
-foreach $varname (sort(keys(%perlvar))) {
-    print $fh "<tr><td>$varname</td><td>$perlvar{$varname}</td></tr>\n";
-}
-print $fh "</table><h3>Hosts</h3><table border=2>";
-foreach $id (sort(keys(%hostname))) {
-    print $fh 
-	"<tr><td>$id</td><td>$hostdom{$id}</td><td>$hostrole{$id}</td>";
-    print $fh "<td>$hostname{$id}</td><td>$hostip{$id}</td></tr>\n";
-}
-print $fh "</table><h3>Spare Hosts</h3><ol>";
-foreach $id (sort(keys(%spareid))) {
-    print $fh "<li>$id\n";
+    foreach my $varname (sort(keys(%perlvar))) {
+	&log($fh,"<tr><td>$varname</td><td>".
+	     &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
+    }
+    &log($fh,"</table><h3>Hosts</h3><table border='2'>");
+    foreach my $id (sort(keys(%{$hostname}))) {
+	&log($fh,
+	    "<tr><td>$id</td><td>".$hostdom->{$id}.
+	    "</td><td>".$hostrole->{$id}.
+	    "</td><td>".$hostname->{$id}."</td></tr>\n");
+    }
+    &log($fh,"</table><h3>Spare Hosts</h3><ol>");
+    foreach my $id (sort(keys(%{$spareid}))) {
+	&log($fh,"<li>$id\n</li>");
+    }
+    &log($fh,"</ol>\n");
+    return $fh;
 }
 
-print $fh "</ol>\n";
-
-# --------------------------------------------------------------------- Machine
-
-print $fh '<hr><a name="machine"><h2>Machine Information</h2>';
-print $fh "<h3>loadavg</h3>";
-
-open (LOADAVGH,"/proc/loadavg");
-$loadavg=<LOADAVGH>;
-close (LOADAVGH);
-
-print $fh "<tt>$loadavg</tt>";
-
-@parts=split(/\s+/,$loadavg);
-if ($parts[1]>4.0) {
-    $errors++;
-} elsif ($parts[1]>2.0) {
-    $warnings++;
-} elsif ($parts[1]>1.0) {
-    $notices++;
-}
-
-print $fh "<h3>df</h3>";
-print $fh "<pre>";
-
-open (DFH,"df|");
-while ($line=<DFH>) { 
-   print $fh "$line"; 
-   @parts=split(/\s+/,$line);
-   $usage=$parts[4];
-   $usage=~s/\W//g;
-   if ($usage>90) { 
-      $warnings++;
-      $notices++; 
-   } elsif ($usage>80) {
-      $warnings++;
-   } elsif ($usage>60) {
-      $notices++;
-   }
-   if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
-}
-close (DFH);
-print $fh "</pre>";
-
-
-print $fh "<h3>ps</h3>";
-print $fh "<pre>";
-$psproc=0;
-
-open (PSH,"ps -aux|");
-while ($line=<PSH>) { 
-   print $fh "$line"; 
-   $psproc++;
+# --------------------------------------------------------------- clean out tmp
+sub clean_tmp {
+    my ($fh)=@_;
+    &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
+    my $cleaned=0;
+    my $old=0;
+    while (my $fname=<$perlvar{'lonDaemons'}/tmp/*>) {
+	my ($dev,$ino,$mode,$nlink,
+	    $uid,$gid,$rdev,$size,
+	    $atime,$mtime,$ctime,
+	    $blksize,$blocks)=stat($fname);
+	my $now=time;
+	my $since=$now-$mtime;
+	if ($since>$perlvar{'lonExpire'}) {
+	    my $line='';
+	    if (open(PROBE,$fname)) {
+		$line=<PROBE>;
+		close(PROBE);
+	    }
+	    unless ($line=~/^CHECKOUTTOKEN\&/) {
+		$cleaned++;
+		unlink("$fname");
+	    } else {
+		if ($since>365*$perlvar{'lonExpire'}) {
+		    $cleaned++;
+		    unlink("$fname");
+		} else { $old++; }
+	    }
+	}
+    }
+    &log($fh,"Cleaned up ".$cleaned." files (".$old." old checkout tokens).");
 }
-close (PSH);
-print $fh "</pre>";
 
-if ($psproc>200) { $notices++; }
-if ($psproc>250) { $notices++; }
+# ------------------------------------------------------------ clean out lonIDs
+sub clean_lonIDs {
+    my ($fh)=@_;
+    &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
+    my $cleaned=0;
+    my $active=0;
+    while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
+	my ($dev,$ino,$mode,$nlink,
+	    $uid,$gid,$rdev,$size,
+	    $atime,$mtime,$ctime,
+	    $blksize,$blocks)=stat($fname);
+	my $now=time;
+	my $since=$now-$mtime;
+	if ($since>$perlvar{'lonExpire'}) {
+	    $cleaned++;
+	    &log($fh,"Unlinking $fname<br>");
+	    unlink("$fname");
+	} else {
+	    $active++;
+	}
+    }
+    &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
+    &log($fh,"<h3>$active open session(s)</h3>");
+}
 
-&errout($fh);
 
-# --------------------------------------------------------------- clean out tmp
-print $fh '<hr><a name="tmp"><h2>Temporary Files</h2>';
-$cleaned=0;
-$old=0;
-while ($fname=<$perlvar{'lonDaemons'}/tmp/*>) {
-                          my ($dev,$ino,$mode,$nlink,
-                              $uid,$gid,$rdev,$size,
-                              $atime,$mtime,$ctime,
-                              $blksize,$blocks)=stat($fname);
-                          $now=time;
-                          $since=$now-$mtime;
-                          if ($since>$perlvar{'lonExpire'}) {
-                              $line='';
-                              if (open(PROBE,$fname)) {
-				  $line=<PROBE>;
-                                  close(PROBE);
-			      }
-			      unless ($line=~/^CHECKOUTTOKEN\&/) {
-                                 $cleaned++;
-                                 unlink("$fname");
-			      } else {
-				  if ($since>365*$perlvar{'lonExpire'}) {
-                                     $cleaned++;
-                                     unlink("$fname");
-				 } else { $old++; }
-                              }
-                          }
+# ----------------------------------------------------------------------- httpd
+sub check_httpd_logs {
+    my ($fh)=@_;
+    &log($fh,'<hr /><a name="httpd" /><h2>httpd</h2><h3>Access Log</h3><pre>');
     
+    open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
+    while (my $line=<DFH>) { &log($fh,&encode_entities($line,'<>&"')) };
+    close (DFH);
+	
+    &log($fh,"</pre><h3>Error Log</h3><pre>");
+	
+    open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
+    while (my $line=<DFH>) { 
+	&log($fh,"$line");
+	if ($line=~/\[error\]/) { $notices++; } 
+    }
+    close (DFH);
+    &log($fh,"</pre>");
+    &errout($fh);
 }
-print $fh "Cleaned up ".$cleaned." files (".$old." old checkout tokens).";
 
-# ------------------------------------------------------------ clean out lonIDs
-print $fh '<hr><a name="tokens"><h2>Session Tokens</h2>';
-$cleaned=0;
-$active=0;
-while ($fname=<$perlvar{'lonIDsDir'}/*>) {
-                          my ($dev,$ino,$mode,$nlink,
-                              $uid,$gid,$rdev,$size,
-                              $atime,$mtime,$ctime,
-                              $blksize,$blocks)=stat($fname);
-                          $now=time;
-                          $since=$now-$mtime;
-                          if ($since>$perlvar{'lonExpire'}) {
-                              $cleaned++;
-                              print $fh "Unlinking $fname<br>";
-                              unlink("$fname");
-                          } else {
-                              $active++;
-                          }
+# ---------------------------------------------------------------------- lonnet
+
+sub rotate_lonnet_logs {
+    my ($fh)=@_;
+    &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
+    print "checking logs\n";
+    if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
+	open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
+	while (my $line=<DFH>) { 
+	    &log($fh,&encode_entities($line,'<>&"'));
+	}
+	close (DFH);
+    }
+    &log($fh,"</pre><h3>Perm Log</h3><pre>");
     
-}
-print $fh "<p>Cleaned up ".$cleaned." stale session token(s).";
-print $fh "<h3>$active open session(s)</h3>";
+    if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
+	open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
+	while (my $line=<DFH>) { 
+	    &log($fh,&encode_entities($line,'<>&"'));
+	}
+	close (DFH);
+    } else { &log($fh,"No perm log\n") }
 
-# ----------------------------------------------------------------------- httpd
+    my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
 
-print $fh '<hr><a name="httpd"><h2>httpd</h2><h3>Access Log</h3><pre>';
+    my ($dev,$ino,$mode,$nlink,
+	$uid,$gid,$rdev,$size,
+	$atime,$mtime,$ctime,
+	$blksize,$blocks)=stat($fname);
 
-open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
-while ($line=<DFH>) { print $fh "$line" };
-close (DFH);
+    if ($size>40000) {
+	&log($fh,"<p>Rotating logs ...</p>");
+	rename("$fname.2","$fname.3");
+	rename("$fname.1","$fname.2");
+	rename("$fname","$fname.1");
+    }
 
-print $fh "</pre><h3>Error Log</h3><pre>";
+    &log($fh,"</pre>");
+    &errout($fh);
+}
 
-open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
-while ($line=<DFH>) { 
-   print $fh "$line";
-   if ($line=~/\[error\]/) { $notices++; } 
-};
-close (DFH);
-print $fh "</pre>";
-&errout($fh);
+# ----------------------------------------------------------------- Connections
+sub test_connections {
+    my ($fh,$hostname)=@_;
+    &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
+    print "testing connections\n";
+    &log($fh,"<table border='2'>");
+    my ($good,$bad)=(0,0);
+    foreach my $tryserver (sort(keys(%{$hostname}))) {
+	print(".");
+	my $result;
+	my $answer=reply("ping",$tryserver,$hostname);
+	if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
+	    $result="<b>ok</b>";
+	    $good++;
+	} else {
+	    $result=$answer;
+	    $warnings++;
+	    if ($answer eq 'con_lost') {
+		$bad++;
+		$warnings++;
+	    } else {
+		$good++; #self connection
+	    }
+	}
+	if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
+	&log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
+    }
+    &log($fh,"</table>");
+    print "\n$good good, $bad bad connections\n";
+    &errout($fh);
+}
 
 
-# ---------------------------------------------------------------------- lonsql
+# ------------------------------------------------------------ Delayed messages
+sub check_delayed_msg {
+    my ($fh)=@_;
+    &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
+    print "checking buffers\n";
+    
+    &log($fh,'<h3>Scanning Permanent Log</h3>');
 
-&checkon_daemon($fh,'lonsql',200000);
+    my $unsend=0;
 
-# ------------------------------------------------------------------------ lond
+    my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
+    while (my $line=<$dfh>) {
+	my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
+	if ($sdf eq 'F') { 
+	    my $local=localtime($time);
+	    &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
+	    $warnings++;
+	}
+	if ($sdf eq 'S') { $unsend--; }
+	if ($sdf eq 'D') { $unsend++; }
+    }
 
-&checkon_daemon($fh,'lond',40000,1);
+    &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
+    $warnings=$warnings+5*$unsend;
 
-# ------------------------------------------------------------------------ lonc
+    if ($unsend) { $simplestatus{'unsend'}=$unsend; }
+    &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
 
-&checkon_daemon($fh,'lonc',40000,1);
+    open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
+    while (my $line=<DFH>) { 
+	&log($fh,&encode_entities($line,'<>&"'));
+    }
+    &log($fh,"</pre>\n");
+    close (DFH);
+}
 
-# -------------------------------------------------------------------- lonhttpd
+sub finish_logging {
+    my ($fh)=@_;
+    &log($fh,"<a name='errcount' />\n");
+    $totalcount=$notices+4*$warnings+100*$errors;
+    &errout($fh);
+    &log($fh,"<h1>Total Error Count: $totalcount</h1>");
+    my $now=time;
+    my $date=localtime($now);
+    &log($fh,"<hr />$date ($now)</body></html>\n");
+    print "lon-status webpage updated\n";
+    $fh->close();
+
+    if ($errors) { $simplestatus{'errors'}=$errors; }
+    if ($warnings) { $simplestatus{'warnings'}=$warnings; }
+    if ($notices) { $simplestatus{'notices'}=$notices; }
+    $simplestatus{'time'}=time;
+}
 
-&checkon_daemon($fh,'lonhttpd',40000);
+sub log_simplestatus {
+    rename ("$statusdir/newstatus.html","$statusdir/index.html");
+    
+    my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
+    foreach (keys %simplestatus) {
+	print $sfh $_.'='.$simplestatus{$_}.'&';
+    }
+    print $sfh "\n";
+    $sfh->close();
+}
 
-# ---------------------------------------------------------------------- lonnet
+sub send_mail {
+    print "sending mail\n";
+    my $emailto="$perlvar{'lonAdmEMail'}";
+    if ($totalcount>2500) {
+	$emailto.=",$perlvar{'lonSysEMail'}";
+    }
+    my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
 
-print $fh '<hr><a name="lonnet"><h2>lonnet</h2><h3>Temp Log</h3><pre>';
-print "checking logs\n";
-if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
-open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
-while ($line=<DFH>) { 
-    print $fh "$line";
-};
-close (DFH);
-}
-print $fh "</pre><h3>Perm Log</h3><pre>";
-
-if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
-    open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
-while ($line=<DFH>) { 
-   print $fh "$line";
-};
-close (DFH);
-} else { print $fh "No perm log\n" }
-
-$fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
-
-                          my ($dev,$ino,$mode,$nlink,
-                              $uid,$gid,$rdev,$size,
-                              $atime,$mtime,$ctime,
-                              $blksize,$blocks)=stat($fname);
-
-if ($size>40000) {
-    print $fh "Rotating logs ...<p>";
-    rename("$fname.2","$fname.3");
-    rename("$fname.1","$fname.2");
-    rename("$fname","$fname.1");
+    my $result=system("metasend -b -S 4000000 -t $emailto -s '$subj' -f $statusdir/index.html -m text/html >& /dev/null");
+    if ($result != 0) {
+	$result=system("mail -s '$subj' $emailto < $statusdir/index.html");
+    }
 }
 
-print $fh "</pre>";
-&errout($fh);
-# ----------------------------------------------------------------- Connections
+sub usage {
+    print(<<USAGE);
+loncron - housekeeping program that checks up on various parts of Lon-CAPA
+
+Options:
+   --help     Display help
+   --oldlonc  When starting the lonc daemon use 'lonc' not 'loncnew'
+   --noemail  Do not send the status email
+   --justcheckconnections  Only check the current status of the lonc/d
+                                connections, do not send emails do not
+                                check if the daemons are running, do not
+                                generate lon-status
+   --justcheckdaemons      Only check that all of the Lon-CAPA daemons are
+                                running, do not send emails do not
+                                check the lonc/d connections, do not
+                                generate lon-status
+   --justreload            Only tell the daemons to reload the config files,
+				do not send emails do not
+                                check if the daemons are running, do not
+                                generate lon-status
+                           
+USAGE
+}
 
-print $fh '<hr><a name="connections"><h2>Connections</h2>';
-print "testing connections\n";
-print $fh "<table border=2>";
-foreach $tryserver (sort(keys(%hostname))) {
-    print(".");
-    $answer=reply("pong",$tryserver);
-    if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
-	$result="<b>ok</b>";
-    } else {
-        $result=$answer;
-        $warnings++;
-        if ($answer eq 'con_lost') { $warnings++; }
+# ================================================================ Main Program
+sub main () {
+    my ($oldlonc,$help,$justcheckdaemons,$noemail,$justcheckconnections,
+	$justreload);
+    &GetOptions("help"                 => \$help,
+		"oldlonc"              => \$oldlonc,
+		"justcheckdaemons"     => \$justcheckdaemons,
+		"noemail"              => \$noemail,
+		"justcheckconnections" => \$justcheckconnections,
+		"justreload"           => \$justreload
+		);
+    if ($help) { &usage(); return; }
+# --------------------------------- Read loncapa_apache.conf and loncapa.conf
+    my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
+    %perlvar=%{$perlvarref};
+    undef $perlvarref;
+    delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
+    delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
+
+# --------------------------------------- Make sure that LON-CAPA is configured
+# I only test for one thing here (lonHostID).  This is just a safeguard.
+    if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
+	print("Unconfigured machine.\n");
+	my $emailto=$perlvar{'lonSysEMail'};
+	my $hostname=`/bin/hostname`;
+	chop $hostname;
+	$hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
+	my $subj="LON: Unconfigured machine $hostname";
+	system("echo 'Unconfigured machine $hostname.' |\
+ mailto $emailto -s '$subj' > /dev/null");
+	exit 1;
     }
-    if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
-    print $fh "<tr><td>$tryserver</td><td>$result</td></tr>\n";
 
-}
-print $fh "</table>";
+# ----------------------------- Make sure this process is running from user=www
+    my $wwwid=getpwnam('www');
+    if ($wwwid!=$<) {
+	print("User ID mismatch.  This program must be run as user 'www'\n");
+	my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
+	my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
+	system("echo 'User ID mismatch.  loncron must be run as user www.' |\
+ mailto $emailto -s '$subj' > /dev/null");
+	exit 1;
+    }
 
-&errout($fh);
-# ------------------------------------------------------------ Delayed messages
+# ------------------------------------------------------------- Read hosts file
+    my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");
+    
+    my (%hostname,%hostdom,%hostrole,%spareid);
+    while (my $configline=<$config>) {
+	next if ($configline =~ /^(\#|\s*\$)/);
+	my ($id,$domain,$role,$name)=split(/:/,$configline);
+	if ($id && $domain && $role && $name) {
+	    $name=~s/\s//g;
+	    $hostname{$id}=$name;
+	    $hostdom{$id}=$domain;
+	    $hostrole{$id}=$role;
+	}
+    }
+    undef $config;
 
-print $fh '<hr><a name="delayed"><h2>Delayed Messages</h2>';
-print "checking buffers\n";
+# ------------------------------------------------------ Read spare server file
+    $config=IO::File->new("$perlvar{'lonTabDir'}/spare.tab");
+    
+    while (my $configline=<$config>) {
+	chomp($configline);
+	if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
+	    $spareid{$configline}=1;
+	}
+    }
+    undef $config;
 
-print $fh '<h3>Scanning Permanent Log</h3>';
+# ---------------------------------------------------------------- Start report
 
-$unsend=0;
-{
-    my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
-    while ($line=<$dfh>) {
-	($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
-        if ($sdf eq 'F') { 
-	    $local=localtime($time);
-            print $fh "<b>Failed: $time, $dserv, $dcmd</b><br>";
-            $warnings++;
-        }
-        if ($sdf eq 'S') { $unsend--; }
-        if ($sdf eq 'D') { $unsend++; }
-    }
-}
-print $fh "Total unsend messages: <b>$unsend</b><p>\n";
-$warnings=$warnings+5*$unsend;
-
-if ($unsend) { $simplestatus{'unsend'}=$unsend; }
-print $fh "<h3>Outgoing Buffer</h3>";
-
-open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
-while ($line=<DFH>) { 
-    print $fh "$line<br>";
-};
-close (DFH);
-
-# ------------------------------------------------------------------------- End
-print $fh "<a name=errcount>\n";
-$totalcount=$notices+4*$warnings+100*$errors;
-&errout($fh);
-print $fh "<h1>Total Error Count: $totalcount</h1>";
-$now=time;
-$date=localtime($now);
-print $fh "<hr>$date ($now)</body></html>\n";
-print "lon-status webpage updated\n";
-$fh->close();
-}
-if ($errors) { $simplestatus{'errors'}=$errors; }
-if ($warnings) { $simplestatus{'warnings'}=$warnings; }
-if ($notices) { $simplestatus{'notices'}=$notices; }
-$simplestatus{'time'}=time;
-
-rename ("$statusdir/newstatus.html","$statusdir/index.html");
-{
-my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
-foreach (keys %simplestatus) {
-    print $sfh $_.'='.$simplestatus{$_}.'&';
-}
-print $sfh "\n";
-$sfh->close();
-}
-if ($totalcount>200) {
-   print "sending mail\n";
-   $emailto="$perlvar{'lonAdmEMail'}";
-   if ($totalcount>1000) {
-      $emailto.=",$perlvar{'lonSysEMail'}";
-   }
-   $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
-   system(
- "metasend -b -t $emailto -s '$subj' -f $statusdir/index.html -m text/html");
+    $errors=0;
+    $warnings=0;
+    $notices=0;
+
+	
+    my $fh;
+    if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
+	$fh=&start_logging(\%hostdom,\%hostrole,\%hostname,\%spareid);
+
+	&log_machine_info($fh);
+	&clean_tmp($fh);
+	&clean_lonIDs($fh);
+	&check_httpd_logs($fh);
+	&rotate_lonnet_logs($fh);
+    }
+    if (!$justcheckconnections && !$justreload) {
+	&checkon_daemon($fh,'lonsql',200000);
+	if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
+	    &checkon_daemon($fh,'lond',40000,'USR2');
+	}
+	my $args='new';
+	if ($oldlonc) { $args = ''; }
+	&checkon_daemon($fh,'lonc',40000,'USR1',$args);
+	&checkon_daemon($fh,'lonhttpd',40000);
+	&checkon_daemon($fh,'lonmemcached',40000);
+    }
+    if ($justreload) {
+	&checkon_daemon($fh,'lond',40000,'USR2');
+	my $args='new';
+	if ($oldlonc) { $args = ''; }
+	&checkon_daemon($fh,'lonc',40000,'USR2',$args);
+    }
+    if ($justcheckconnections) {
+	&test_connections($fh,\%hostname);
+    }
+    if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
+	&check_delayed_msg($fh);
+	&finish_logging($fh);
+	&log_simplestatus();
+	
+	if ($totalcount>200 && !$noemail) { &send_mail(); }
+    }
 }
+
+&main();
 1;