--- loncom/loncron 2005/02/09 20:41:29 1.56 +++ 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.56 2005/02/09 20:41:29 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; @@ -111,10 +106,11 @@ sub start_daemon { } sub checkon_daemon { - my ($fh,$daemon,$maxsize,$sendusr1,$args)=@_; + my ($fh,$daemon,$maxsize,$send,$args)=@_; + my $result; &log($fh,'');
- printf("%-10s ",$daemon);
+ printf("%-15s ",$daemon);
if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
while (my $line= Rotating logs ... Cleaned up ".$cleaned." stale sockets. Rotating logs ...$daemon at pid $daemonpid responding");
- if ($sendusr1) { &log($fh,", sending USR1"); }
+ if ($send) { &log($fh,", sending $send"); }
&log($fh,"
");
- if ($sendusr1) { kill USR1 => $daemonpid; }
+ 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++;
&log($fh,"$daemon at pid $daemonpid not responding
");
@@ -152,17 +155,20 @@ sub checkon_daemon {
if ($restartflag==1) {
$simplestatus{$daemon}='off';
$errors++;
+ my $kadaemon=$daemon;
+ if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
&log($fh,'
Killall '.$daemon.': '.
- `killall $daemon 2>&1`.' - ');
- sleep 2;
+ `killall $kadaemon 2>&1`.' - ');
+ sleep 1;
&log($fh,unlink($pidfile).' - '.
- `killall -9 $daemon 2>&1`.
+ `killall -9 $kadaemon 2>&1`.
'
');
&log($fh,"$daemon not running, trying to start
");
if (&start_daemon($fh,$daemon,$pidfile,$args)) {
&log($fh,"$daemon at pid $daemonpid responding
");
$simplestatus{$daemon}='restarted';
+ $result = 'started';
print "started\n";
} else {
$errors++;
@@ -172,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++;
@@ -196,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,"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;
@@ -300,7 +302,6 @@ sub start_logging {
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,
- "
\n");
- }
- &log($fh,"$id ".$hostdom->{$id}.
- " ".$hostrole->{$id}.
- " ".$hostname->{$id}." Spare Hosts
");
- foreach my $id (sort(keys(%{$spareid}))) {
- &log($fh,"
\n");
+ &log($fh,"\n");
return $fh;
}
@@ -336,33 +344,127 @@ ENDHEADERS
sub clean_tmp {
my ($fh)=@_;
&log($fh,' \n");
+ }
+ &log($fh,"$id ".&Apache::lonnet::host_domain($id).
+ " ".$role.
+ " ".&Apache::lonnet::hostname($id)." Spare Hosts
");
+ foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
+ &log($fh,"
");
+ foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
+ &log($fh,"
\nTemporary 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=
');
+ }
+ }
+ }
+}
+
+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=$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,"httpd
Access Log
');
-
- open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
- while (my $line=
Error Log
");
-
- open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
- while (my $line=
");
&errout($fh);
}
@@ -417,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=
");
&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,"