--- loncom/loncron 2005/05/26 20:25:00 1.59
+++ loncom/loncron 2014/03/17 14:47:38 1.100
@@ -2,7 +2,7 @@
# Housekeeping program, started by cron, loncontrol and loncron.pl
#
-# $Id: loncron,v 1.59 2005/05/26 20:25:00 albertel Exp $
+# $Id: loncron,v 1.100 2014/03/17 14:47:38 bisitz Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -32,6 +32,10 @@ use strict;
use lib '/home/httpd/lib/perl/';
use LONCAPA::Configuration;
+use LONCAPA::Checksumming;
+use LONCAPA;
+use Apache::lonnet;
+use Apache::loncommon;
use IO::File;
use IO::Socket;
@@ -43,21 +47,6 @@ use vars qw (%perlvar %simplestatus $err
my $statusdir="/home/httpd/html/lon-status";
-# -------------------------------------------------- Non-critical communication
-sub reply {
- my ($cmd,$server)=@_;
- my $peerfile="$perlvar{'lonSockDir'}/$server";
- my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
- Type => SOCK_STREAM,
- Timeout => 10)
- or return "con_lost";
- print $client "$cmd\n";
- my $answer=<$client>;
- chomp($answer);
- if (!$answer) { $answer="con_lost"; }
- return $answer;
-}
-
# --------------------------------------------------------- Output error status
sub log {
@@ -76,30 +65,36 @@ sub errout {
ENDERROUT
}
+sub rotate_logfile {
+ my ($file,$fh,$description) = @_;
+ my $size=(stat($file))[7];
+ if ($size>40000) {
+ &log($fh,"
Rotating $description ...
");
+ rename("$file.2","$file.3");
+ rename("$file.1","$file.2");
+ rename("$file","$file.1");
+ }
+}
+
sub start_daemon {
my ($fh,$daemon,$pidfile,$args) = @_;
my $progname=$daemon;
- if ($daemon eq 'lonc' && $args eq 'new') {
+ if ($daemon eq 'lonc') {
$progname='loncnew';
- print "new ";
}
my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
- my $size=(stat($error_fname))[7];
- if ($size>40000) {
- &log($fh,"Rotating error logs ...
");
- rename("$error_fname.2","$error_fname.3");
- rename("$error_fname.1","$error_fname.2");
- rename("$error_fname","$error_fname.1");
+ &rotate_logfile($error_fname,$fh,'error logs');
+ if ($daemon eq 'lonc') {
+ &clean_sockets($fh);
}
system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
- sleep 2;
+ sleep 1;
if (-e $pidfile) {
&log($fh,"Seems like it started ...
");
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;
@@ -113,6 +108,7 @@ sub start_daemon {
sub checkon_daemon {
my ($fh,$daemon,$maxsize,$send,$args)=@_;
+ my $result;
&log($fh,'
'.$daemon.'
Log
');
printf("%-15s ",$daemon);
if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
@@ -135,7 +131,7 @@ sub checkon_daemon {
my $lfh=IO::File->new("$pidfile");
$daemonpid=<$lfh>;
chomp($daemonpid);
- if (kill 0 => $daemonpid) {
+ if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
&log($fh,"$daemon at pid $daemonpid responding");
if ($send) { &log($fh,", sending $send"); }
&log($fh,"
");
@@ -143,8 +139,10 @@ sub checkon_daemon {
if ($send eq 'USR2') { kill USR2 => $daemonpid; }
$restartflag=0;
if ($send eq 'USR2') {
+ $result = 'reloaded';
print "reloaded\n";
} else {
+ $result = 'running';
print "running\n";
}
} else {
@@ -161,7 +159,7 @@ sub checkon_daemon {
if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
&log($fh,'
Killall '.$daemon.': '.
`killall $kadaemon 2>&1`.' - ');
- sleep 2;
+ sleep 1;
&log($fh,unlink($pidfile).' - '.
`killall -9 $kadaemon 2>&1`.
'
');
@@ -170,6 +168,7 @@ sub checkon_daemon {
if (&start_daemon($fh,$daemon,$pidfile,$args)) {
&log($fh,"$daemon at pid $daemonpid responding
");
$simplestatus{$daemon}='restarted';
+ $result = 'started';
print "started\n";
} else {
$errors++;
@@ -179,8 +178,10 @@ sub checkon_daemon {
if (&start_daemon($fh,$daemon,$pidfile,$args)) {
&log($fh,"$daemon at pid $daemonpid responding
");
$simplestatus{$daemon}='restarted';
+ $result = 'started';
print "started\n";
} else {
+ $result = 'failed';
print " failed\n";
$simplestatus{$daemon}='failed';
$errors++; $errors++;
@@ -203,20 +204,10 @@ sub checkon_daemon {
}
my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
-
- my ($dev,$ino,$mode,$nlink,
- $uid,$gid,$rdev,$size,
- $atime,$mtime,$ctime,
- $blksize,$blocks)=stat($fname);
-
- if ($size>$maxsize) {
- &log($fh,"
Rotating logs ...
");
- rename("$fname.2","$fname.3");
- rename("$fname.1","$fname.2");
- rename("$fname","$fname.1");
- }
+ &rotate_logfile($fname,$fh,'logs');
&errout($fh);
+ return $result;
}
# --------------------------------------------------------------------- Machine
@@ -278,11 +269,15 @@ sub log_machine_info {
if ($psproc>200) { $notices++; }
if ($psproc>250) { $notices++; }
+ &log($fh,"distprobe
");
+ &log($fh,"");
+ &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
+ &log($fh,"
");
+
&errout($fh);
}
sub start_logging {
- my ($hostdom,$hostrole,$hostname,$spareid)=@_;
my $fh=IO::File->new(">$statusdir/newstatus.html");
my %simplestatus=();
my $now=time;
@@ -307,7 +302,6 @@ sub start_logging {
lonsql
lond
lonc
-lonhttpd
lonnet
Connections
Delayed Messages
@@ -325,17 +319,24 @@ ENDHEADERS
&encode_entities($perlvar{$varname},'<>&"')."\n");
}
&log($fh,"Hosts
");
- foreach my $id (sort(keys(%{$hostname}))) {
+ my %hostname = &Apache::lonnet::all_hostnames();
+ foreach my $id (sort(keys(%hostname))) {
+ my $role = (&Apache::lonnet::is_library($id) ? 'library'
+ : 'access');
&log($fh,
- "$id | ".$hostdom->{$id}.
- " | ".$hostrole->{$id}.
- " | ".$hostname->{$id}." |
\n");
- }
- &log($fh,"
Spare Hosts
");
- foreach my $id (sort(keys(%{$spareid}))) {
- &log($fh,"- $id\n
");
+ "$id | ".&Apache::lonnet::host_domain($id).
+ " | ".$role.
+ " | ".&Apache::lonnet::hostname($id)." |
\n");
+ }
+ &log($fh,"Spare Hosts
");
+ foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
+ &log($fh,"- $type\n
");
+ foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
+ &log($fh,"- $id
\n");
+ }
+ &log($fh,"
\n \n");
}
- &log($fh,"
\n");
+ &log($fh,"\n");
return $fh;
}
@@ -343,33 +344,127 @@ ENDHEADERS
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++; }
- }
- }
+ my ($cleaned,$old,$removed) = (0,0,0);
+ my %errors = (
+ dir => [],
+ file => [],
+ failopen => [],
+ );
+ my %error_titles = (
+ dir => 'failed to remove empty directory:',
+ file => 'failed to unlike stale file',
+ failopen => 'failed to open file or directory'
+ );
+ ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
+ &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
+ foreach my $key (sort(keys(%errors))) {
+ if (ref($errors{$key}) eq 'ARRAY') {
+ if (@{$errors{$key}} > 0) {
+ &log($fh,"Error during cleanup ($error_titles{$key}):- ".
+ join('
- ',@{$errors{$key}}).'
');
+ }
+ }
+ }
+}
+
+sub recursive_clean_tmp {
+ my ($subdir,$cleaned,$old,$removed,$errors) = @_;
+ my $base = "$perlvar{'lonDaemons'}/tmp";
+ my $path = $base;
+ next if ($subdir =~ m{\.\./});
+ next unless (ref($errors) eq 'HASH');
+ unless ($subdir eq '') {
+ $path .= '/'.$subdir;
+ }
+ if (opendir(my $dh,"$path")) {
+ while (my $file = readdir($dh)) {
+ next if ($file =~ /^\.\.?$/);
+ my $fname = "$path/$file";
+ if (-d $fname) {
+ my $innerdir;
+ if ($subdir eq '') {
+ $innerdir = $file;
+ } else {
+ $innerdir = $subdir.'/'.$file;
+ }
+ ($cleaned,$old,$removed) =
+ &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
+ my @doms = &Apache::lonnet::current_machine_domains();
+
+ if (open(my $dirhandle,$fname)) {
+ unless (($innerdir eq 'helprequests') ||
+ (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
+ my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
+ join('&&',@contents)."\n";
+ if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
+ closedir($dirhandle);
+ if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
+ if (rmdir($fname)) {
+ $removed ++;
+ } elsif (ref($errors->{dir}) eq 'ARRAY') {
+ push(@{$errors->{dir}},$fname);
+ }
+ }
+ }
+ } else {
+ closedir($dirhandle);
+ }
+ }
+ } else {
+ 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'}) {
+ if ($subdir eq '') {
+ my $line='';
+ if ($fname =~ /\.db$/) {
+ if (unlink($fname)) {
+ $cleaned++;
+ } elsif (ref($errors->{file}) eq 'ARRAY') {
+ push(@{$errors->{file}},$fname);
+ }
+ } elsif (open(PROBE,$fname)) {
+ my $line='';
+ $line=;
+ close(PROBE);
+ if ($line=~/^CHECKOUTTOKEN\&/) {
+ if ($since>365*$perlvar{'lonExpire'}) {
+ if (unlink($fname)) {
+ $cleaned++;
+ } elsif (ref($errors->{file}) eq 'ARRAY') {
+ push(@{$errors->{file}},$fname);
+ }
+ } else {
+ $old++;
+ }
+ } else {
+ if (unlink($fname)) {
+ $cleaned++;
+ } elsif (ref($errors->{file}) eq 'ARRAY') {
+ push(@{$errors->{file}},$fname);
+ }
+ }
+ } elsif (ref($errors->{failopen}) eq 'ARRAY') {
+ push(@{$errors->{failopen}},$fname);
+ }
+ } else {
+ if (unlink($fname)) {
+ $cleaned++;
+ } elsif (ref($errors->{file}) eq 'ARRAY') {
+ push(@{$errors->{file}},$fname);
+ }
+ }
+ }
+ }
+ }
+ closedir($dh);
+ } elsif (ref($errors->{failopen}) eq 'ARRAY') {
+ push(@{$errors->{failopen}},$path);
}
- &log($fh,"Cleaned up ".$cleaned." files (".$old." old checkout tokens).");
+ return ($cleaned,$old,$removed);
}
# ------------------------------------------------------------ clean out lonIDs
@@ -397,25 +492,32 @@ sub clean_lonIDs {
&log($fh,"$active open session(s)
");
}
+# ----------------------------------------------------------- clean out sockets
+sub clean_sockets {
+ my ($fh)=@_;
+ my $cleaned=0;
+ opendir(SOCKETS,$perlvar{'lonSockDir'});
+ while (my $fname=readdir(SOCKETS)) {
+ next if (-d $fname
+ || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
+ $cleaned++;
+ &log($fh,"Unlinking $fname
");
+ unlink("/home/httpd/sockets/$fname");
+ }
+ &log($fh,"Cleaned up ".$cleaned." stale sockets.
");
+}
+
# ----------------------------------------------------------------------- httpd
sub check_httpd_logs {
my ($fh)=@_;
- &log($fh,'
httpd
Access Log
');
-
- open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
- while (my $line=) { &log($fh,&encode_entities($line,'<>&"')) };
- close (DFH);
-
- &log($fh,"
Error Log
");
-
- open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
- while (my $line=) {
- &log($fh,"$line");
- if ($line=~/\[error\]/) { $notices++; }
+ if (open(PIPE,"./lchttpdlogs|")) {
+ while (my $line=) {
+ &log($fh,$line);
+ if ($line=~/\[error\]/) { $notices++; }
+ }
+ close(PIPE);
}
- close (DFH);
- &log($fh,"
");
&errout($fh);
}
@@ -424,7 +526,7 @@ sub check_httpd_logs {
sub rotate_lonnet_logs {
my ($fh)=@_;
&log($fh,'
lonnet
Temp Log
');
- print "checking logs\n";
+ print "Checking logs.\n";
if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
while (my $line=) {
@@ -443,34 +545,39 @@ sub rotate_lonnet_logs {
} else { &log($fh,"No perm log\n") }
my $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) {
- &log($fh,"Rotating logs ...
");
- rename("$fname.2","$fname.3");
- rename("$fname.1","$fname.2");
- rename("$fname","$fname.1");
- }
+ &rotate_logfile($fname,$fh,'lonnet log');
&log($fh,"
");
&errout($fh);
}
+sub rotate_other_logs {
+ my ($fh) = @_;
+ my %logs = (
+ autoenroll => 'Auto Enroll log',
+ autocreate => 'Create Course log',
+ searchcat => 'Search Cataloguing log',
+ autoupdate => 'Auto Update log',
+ refreshcourseids_db => 'Refresh CourseIDs db log',
+ );
+ foreach my $item (keys(%logs)) {
+ my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
+ &rotate_logfile($fname,$fh,$logs{$item});
+ }
+}
+
# ----------------------------------------------------------------- Connections
sub test_connections {
- my ($fh,$hostname)=@_;
+ my ($fh)=@_;
&log($fh,'
Connections
');
- print "testing connections\n";
+ print "Testing connections.\n";
&log($fh,"");
my ($good,$bad)=(0,0);
- foreach my $tryserver (sort(keys(%{$hostname}))) {
+ my %hostname = &Apache::lonnet::all_hostnames();
+ foreach my $tryserver (sort(keys(%hostname))) {
print(".");
my $result;
- my $answer=reply("ping",$tryserver);
+ my $answer=&Apache::lonnet::reply("ping",$tryserver);
if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
$result="ok";
$good++;
@@ -497,7 +604,7 @@ sub test_connections {
sub check_delayed_msg {
my ($fh)=@_;
&log($fh,'
Delayed Messages
');
- print "checking buffers\n";
+ print "Checking buffers.\n";
&log($fh,'Scanning Permanent Log
');
@@ -516,17 +623,45 @@ sub check_delayed_msg {
}
&log($fh,"Total unsend messages: $unsend
\n");
- $warnings=$warnings+5*$unsend;
+ if ($unsend > 0) {
+ $warnings=$warnings+5*$unsend;
+ }
if ($unsend) { $simplestatus{'unsend'}=$unsend; }
&log($fh,"Outgoing Buffer
\n");
-
+# list directory with delayed messages and remember offline servers
+ my %servers=();
open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
- while (my $line=) {
+ while (my $line=) {
+ my ($server)=($line=~/\.(\w+)$/);
+ if ($server) { $servers{$server}=1; }
&log($fh,&encode_entities($line,'<>&"'));
}
&log($fh,"
\n");
close (DFH);
+ my %hostname = &Apache::lonnet::all_hostnames();
+ my $numhosts = scalar(keys(%hostname));
+# pong to all servers that have delayed messages
+# this will trigger a reverse connection, which should flush the buffers
+ foreach my $tryserver (sort(keys(%servers))) {
+ if ($hostname{$tryserver} || !$numhosts) {
+ my $answer;
+ eval {
+ local $SIG{ ALRM } = sub { die "TIMEOUT" };
+ alarm(20);
+ $answer = &Apache::lonnet::reply("pong",$tryserver);
+ alarm(0);
+ };
+ if ($@ && $@ =~ m/TIMEOUT/) {
+ &log($fh,"Attempted pong to $tryserver timed out
");
+ print "Time out while contacting: $tryserver for pong.\n";
+ } else {
+ &log($fh,"Pong to $tryserver: $answer
");
+ }
+ } else {
+ &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.
");
+ }
+ }
}
sub finish_logging {
@@ -538,7 +673,7 @@ sub finish_logging {
my $now=time;
my $date=localtime($now);
&log($fh,"
$date ($now)