--- loncom/loncron 2003/09/10 19:13:09 1.42
+++ loncom/loncron 2007/02/02 12:59:13 1.70
@@ -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.70 2007/02/02 12:59:13 raeburn 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 (<
+ Rotating error logs ... ";
+ &log($fh," Seems like it started ... ";
+ &log($fh," Seems like that did not work! ');
+ printf("%-15s ",$daemon);
if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
- while ($line=
+ &log($fh,(<
Notices $notices Warnings $warnings
- Errors $errors '.$daemon.'
Log
';
- printf("%-10s ",$daemon);
+ my $result;
+ &log($fh,'
";
+ &log($fh,"'.$daemon.'
Log
"; + &log($fh,"
Give it one more try ...
"); print " "; - if (&start_daemon($fh,$daemon,$pidfile)) { - print $fh ""; + &log($fh,"
Unable to start $daemon
"); } } if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){ - print $fh ""; + &log($fh,""); } } - $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,531 @@ sub checkon_daemon { $blksize,$blocks)=stat($fname); if ($size>$maxsize) { - print $fh "Rotating logs ..."); open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|"); - while ($line="; + &log($fh,") { - print $fh "$line"; + while (my $line= ) { + &log($fh,"$line"); if ($line=~/WARNING/) { $notices++; } if ($line=~/CRITICAL/) { $notices++; } }; close (DFH); - print $fh "
"; + &log($fh,"
Rotating logs ...
"); 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,'"); - 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="); -# ------------------------------------------------------ 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,") { + &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,"
"); + my $psproc=0; + + open (PSH,"ps aux --cols 140 |"); + while (my $line="); -# ---------------------------------------------------------------- Start report - -$statusdir="/home/httpd/html/lon-status"; + if ($psproc>200) { $notices++; } + if ($psproc>250) { $notices++; } -$errors=0; -$warnings=0; -$notices=0; + &log($fh,") { + &log($fh,&encode_entities($line,'<>&"')); + $psproc++; } -} + close (PSH); + &log($fh,"
"); + open(DSH,"$perlvar{'lonDaemons'}/distprobe |"); + while (my $line="); -$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 (<) { + &log($fh,&encode_entities($line,'<>&"')); + $psproc++; + } + close(DSH); + &log($fh,"
$varname | $perlvar{$varname} |
$id | $hostdom{$id} | $hostrole{$id} | "; - print $fh "$hostname{$id} | $hostip{$id} |
$id | ".$hostdom->{$id}. + " | ".$hostrole->{$id}. + " | ".$hostname->{$id}." |
"; - -open (DFH,"df|"); -while ($line="; - - -print $fh ") { - 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 "
"; -$psproc=0; - -open (PSH,"ps -aux|"); -while ($line="; -if ($psproc>200) { $notices++; } -if ($psproc>250) { $notices++; } +# ------------------------------------------------------------ clean out lonIDs +sub clean_lonIDs { + my ($fh)=@_; + &log($fh,') { - print $fh "$line"; - $psproc++; +# --------------------------------------------------------------- clean out tmp +sub clean_tmp { + my ($fh)=@_; + &log($fh,' Temporary Files
'); + 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=; + 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 "
Cleaned up ".$cleaned." stale session token(s).
"); + &log($fh,"'); + open (DFH,"tail -n25 /etc/httpd/logs/access_log|"); + while (my $line=) { &log($fh,&encode_entities($line,'<>&"')) }; + close (DFH); + + &log($fh,"
"); + + open (DFH,"tail -n25 /etc/httpd/logs/error_log|"); + while (my $line="); + &errout($fh); } -print $fh "Cleaned up ".$cleaned." files (".$old." old checkout tokens)."; -# ------------------------------------------------------------ clean out lonIDs -print $fh ') { + &log($fh,"$line"); + if ($line=~/\[error\]/) { $notices++; } + } + close (DFH); + &log($fh,"
'); + print "checking logs\n"; + if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){ + open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|"); + while (my $line=) { + &log($fh,&encode_entities($line,'<>&"')); + } + close (DFH); + } + &log($fh,"
"); -} -print $fh ""; -&errout($fh); +# ----------------------------------------------------------------- Connections +sub test_connections { + my ($fh,$hostname)=@_; + &log($fh,'Cleaned up ".$cleaned." stale session token(s)."; -print $fh "
$active open session(s)
"; + if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") { + open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|"); + while (my $line=) { + &log($fh,&encode_entities($line,'<>&"')); + } + close (DFH); + } else { &log($fh,"No perm log\n") } -# ----------------------------------------------------------------------- httpd + my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log"; -print $fh ' httpd
Access Log
'; + 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=) { print $fh "$line" }; -close (DFH); + if ($size>40000) { + &log($fh," Rotating logs ...
"); + rename("$fname.2","$fname.3"); + rename("$fname.1","$fname.2"); + rename("$fname","$fname.1"); + } -print $fh "Error Log
"; + &log($fh,""); + &errout($fh); +} -open (DFH,"tail -n25 /etc/httpd/logs/error_log|"); -while ($line=) { - print $fh "$line"; - if ($line=~/\[error\]/) { $notices++; } -}; -close (DFH); -print $fh "
$tryserver | $result |
Total unsend messages: $unsend
\n"); + $warnings=$warnings+5*$unsend; -# ------------------------------------------------------------------------ lonc + if ($unsend) { $simplestatus{'unsend'}=$unsend; } + &log($fh,""); +# list directory with delayed messages and remember offline servers + my %servers=(); + open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|"); + while (my $line=\n"); + close (DFH); +# pong to all servers that have delayed messages +# this will trigger a reverse connection, which should flush the buffers + foreach my $tryserver (keys %servers) { + my $answer=&reply("pong",$tryserver,$hostname); + &log($fh,"Pong to $tryserver: $answer) { + my ($server)=($line=~/\.(\w+)$/); + if ($server) { $servers{$server}=1; } + &log($fh,&encode_entities($line,'<>&"')); + } + &log($fh,"