--- loncom/loncron 2003/09/10 19:13:09 1.42
+++ loncom/loncron 2018/08/08 11:49:48 1.108
@@ -1,590 +1,1292 @@
#!/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.108 2018/08/08 11:49:48 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 LONCAPA::Checksumming;
+use LONCAPA;
+use Apache::lonnet;
+use Apache::loncommon;
use IO::File;
use IO::Socket;
+use HTML::Entities;
+use Getopt::Long;
+use GDBM_File;
+use Storable qw(thaw);
+#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 $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 {
+ my $fh=shift;
+ if ($fh) { print $fh @_ }
+}
+
sub errout {
my $fh=shift;
- print $fh (<
+ Rotating $description ... ";
+ &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 ($dev,$ino,$mode,$nlink, - $uid,$gid,$rdev,$size, - $atime,$mtime,$ctime, - $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 "
"; - rename("$fname.2","$fname.3"); - rename("$fname.1","$fname.2"); - rename("$fname","$fname.1"); - } + my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log"; + &rotate_logfile($fname,$fh,'logs'); &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; -} +# --------------------------------------------------------------------- Machine +sub log_machine_info { + my ($fh)=@_; + &log($fh,'
"); -# ------------------------------------------------------------- Read hosts file -{ - my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab"); - - 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; - } - } -} -# ---------------------------------------------------------------- Start report + &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="); -$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,"
"); + &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"')); + &log($fh,""); -$now=time; -$date=localtime($now); + &errout($fh); +} -{ -my $fh=IO::File->new(">$statusdir/newstatus.html"); -my %simplestatus=(); +sub start_logging { + my $fh=IO::File->new(">$statusdir/newstatus.html"); + my %simplestatus=(); + my $now=time; + my $date=localtime($now); + -print $fh (<
$varname | $perlvar{$varname} |
$id | $hostdom{$id} | $hostrole{$id} | "; - print $fh "$hostname{$id} | $hostip{$id} |
$varname | ". + &encode_entities($perlvar{$varname},'<>&"')." |
$id | ".&Apache::lonnet::host_domain($id). + " | ".$role. + " | ".&Apache::lonnet::hostname($id)." |
Cleaned up ".$cleaned." stale session token(s).
"); + &log($fh,"Cleaned up ".$cleaned." stale webDAV session token(s).
"); + &log($fh,"Cleaned up ".$cleaned." stale sockets.
"); +} -print $fh "$loadavg"; -@parts=split(/\s+/,$loadavg); -if ($parts[1]>4.0) { - $errors++; -} elsif ($parts[1]>2.0) { - $warnings++; -} elsif ($parts[1]>1.0) { - $notices++; -} - -print $fh ""; - -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++; } - -&errout($fh); +# ---------------------------------------------------------------------- lonnet -# --------------------------------------------------------------- clean out tmp -print $fh ') { - print $fh "$line"; - $psproc++; +# ----------------------------------------------------------------------- httpd +sub check_httpd_logs { + my ($fh)=@_; + if (open(PIPE,"./lchttpdlogs|")) { + while (my $line= ) { + &log($fh,$line); + if ($line=~/\[error\]/) { $notices++; } + } + close(PIPE); + } + &errout($fh); } -close (PSH); -print $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 "Cleaned up ".$cleaned." files (".$old." old checkout tokens)."; + if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") { + open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|"); + while (my $line="); + &errout($fh); +} -print $fh ') { + &log($fh,&encode_entities($line,'<>&"')); + } + close (DFH); + } else { &log($fh,"No perm log\n") } -# ------------------------------------------------------------ clean out lonIDs -print $fh ' Session Tokens
'; -$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
"; - unlink("$fname"); - } else { - $active++; - } - -} -print $fh "Cleaned up ".$cleaned." stale session token(s)."; -print $fh "
$active open session(s)
"; + my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log"; + &rotate_logfile($fname,$fh,'lonnet log'); -# ----------------------------------------------------------------------- httpd + &log($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}); + } +} -open (DFH,"tail -n25 /etc/httpd/logs/access_log|"); -while ($line=) { print $fh "$line" }; -close (DFH); +# ----------------------------------------------------------------- Connections +sub test_connections { + my ($fh)=@_; + &log($fh,' Connections
'); + print "Testing connections.\n"; + &log($fh,""); + my ($good,$bad)=(0,0); + my %hostname = &Apache::lonnet::all_hostnames(); + foreach my $tryserver (sort(keys(%hostname))) { + print("."); + my $result; + my $answer=&Apache::lonnet::reply("ping",$tryserver); + if ($answer eq "$tryserver:$perlvar{'lonHostID'}") { + $result="ok"; + $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,"
"); + print "\n$good good, $bad bad connections\n"; + &errout($fh); +} -print $fh "\n"); + } + &log($fh," $tryserver $result
"; -open (DFH,"tail -n25 /etc/httpd/logs/error_log|"); -while ($line="; -&errout($fh); +# ------------------------------------------------------------ Delayed messages +sub check_delayed_msg { + my ($fh)=@_; + &log($fh,') { - print $fh "$line"; - if ($line=~/\[error\]/) { $notices++; } -}; -close (DFH); -print $fh "
Total unsend messages: $unsend
\n"); + if ($unsend > 0) { + $warnings=$warnings+5*$unsend; + } -&checkon_daemon($fh,'lond',40000,1); + 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 (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) { + my ($server)=($line=~/\.(\w+)$/); + if ($server) { $servers{$server}=1; } + &log($fh,&encode_entities($line,'<>&"')); + } + &log($fh,"
'; -print "checking logs\n"; -if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){ -open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|"); -while ($line=) { - print $fh "$line"; -}; -close (DFH); -} -print $fh "
"; - -if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") { - open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|"); -while ($line="; -&errout($fh); -# ----------------------------------------------------------------- Connections +sub write_connection_config { + my ($isprimary,$domconf,$url,%connectssl,%changes); + my $primaryLibServer = &Apache::lonnet::domain($perlvar{'lonDefDomain'},'primary'); + if ($primaryLibServer eq $perlvar{'lonHostID'}) { + $isprimary = 1; + } elsif ($primaryLibServer ne '') { + my $protocol = $Apache::lonnet::protocol{$primaryLibServer}; + my $hostname = &Apache::lonnet::hostname($primaryLibServer); + unless ($protocol eq 'https') { + $protocol = 'http'; + } + $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl'; + } + my $domconf = &get_domain_config($perlvar{'lonDefDomain'},$primaryLibServer,$isprimary, + $url); + if (ref($domconf) eq 'HASH') { + if (ref($domconf->{'ssl'}) eq 'HASH') { + foreach my $connect ('connto','connfrom') { + if (ref($domconf->{'ssl'}->{$connect}) eq 'HASH') { + my ($sslreq,$sslnoreq,$currsetting); + my %contypes; + foreach my $type ('dom','intdom','other') { + $connectssl{$connect.'_'.$type} = $domconf->{'ssl'}->{$connect}->{$type}; + } + } + } + } + if (keys(%connectssl)) { + my %currconf; + if (open(my $fh,'<',"$perlvar{'lonTabDir'}/connectionrules.tab")) { + while (my $line = <$fh>) { + chomp($line); + my ($name,$value) = split(/=/,$line); + if ($value =~ /^(?:no|yes|req)$/) { + if ($name =~ /^conn(to|from)_(dom|intdom|other)$/) { + $currconf{$name} = $value; + } + } + } + close($fh); + } + if (open(my $fh,'>',"$perlvar{'lonTabDir'}/connectionrules.tab")) { + my $count = 0; + foreach my $key (sort(keys(%connectssl))) { + print $fh "$key=$connectssl{$key}\n"; + if (exists($currconf{$key})) { + unless ($currconf{$key} eq $connectssl{$key}) { + $changes{$key} = 1; + } + } else { + $changes{$key} = 1; + } + $count ++; + } + close($fh); + print "Completed writing SSL options for lonc/lond for $count items.\n"; + } + } else { + print "Writing of SSL options skipped - no connection rules in domain configuration.\n"; + } + } else { + print "Retrieval of SSL options for lonc/lond skipped - no configuration data available for domain.\n"; + } + return %changes; +} -print $fh ') { - 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 ... "; - rename("$fname.2","$fname.3"); - rename("$fname.1","$fname.2"); - rename("$fname","$fname.1"); +sub clean_nosslverify { + my ($fh) = @_; + my %unlinked; + if (-d "$perlvar{'lonSockDir'}/nosslverify") { + if (opendir(my $dh,"$perlvar{'lonSockDir'}/nosslverify")) { + while (my $fname=readdir($dh)) { + next if ($fname =~ /^\.+$/); + if (unlink("/home/httpd/sockets/nosslverify/$fname")) { + &log($fh,"Unlinking $fname
"); + $unlinked{$fname} = 1; + } + } + closedir($dh); + } + } + &log($fh,"Removed ".scalar(keys(%unlinked))." nosslverify clients
"); + return %unlinked; +} +sub clean_lonc_childpids { + my $childpiddir = "$perlvar{'lonDocRoot'}/lon-status/loncchld"; + if (-d $childpiddir) { + if (opendir(my $dh,$childpiddir)) { + while (my $fname=readdir($dh)) { + next if ($fname =~ /^\.+$/); + unlink("$childpiddir/$fname"); + } + closedir($dh); + } + } } -print $fh "
$tryserver | $result |
\n"; -$warnings=$warnings+5*$unsend; - -if ($unsend) { $simplestatus{'unsend'}=$unsend; } -print $fh "