--- loncom/loncron 2004/01/09 17:43:40 1.45
+++ loncom/loncron 2006/01/27 21:30:53 1.64
@@ -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.64 2006/01/27 21:30:53 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,129 +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, @@ -168,157 +215,29 @@ 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); -} -# ================================================================ 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; + return $result; } -# ----------------------------- 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; -} - -# ------------------------------------------------------------- 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-"); - } - } - } -} - -# ------------------------------------------------------ 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 - -$statusdir="/home/httpd/html/lon-status"; - -$errors=0; -$warnings=0; -$notices=0; - -$now=time; -$date=localtime($now); - -{ - my $fh=IO::File->new(">$statusdir/newstatus.html"); - my %simplestatus=(); - - print $fh (<$varname | $perlvar{$varname} |
$id | $hostdom{$id} | $hostrole{$id} | "; - print $fh "$hostname{$id} | $hostip{$id} |
"; + &log($fh,""); - print $fh "df
"); + &log($fh,""); open (DFH,"df|"); - while ($line="; + &log($fh,") { - print $fh "$line"; + while (my $line= ) { + &log($fh,&encode_entities($line,'<>&"')); @parts=split(/\s+/,$line); - $usage=$parts[4]; + my $usage=$parts[4]; $usage=~s/\W//g; if ($usage>90) { $warnings++; @@ -347,39 +266,110 @@ ENDHEADERS if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; } } close (DFH); - print $fh "
"; - $psproc=0; + &log($fh,""); if ($psproc>200) { $notices++; } if ($psproc>250) { $notices++; } + &log($fh,"ps
"); + &log($fh,""); + my $psproc=0; - open (PSH,"ps -aux --cols 140 |"); - while ($line="; + &log($fh,") { - print $fh "$line"; + open (PSH,"ps aux --cols 140 |"); + while (my $line= ) { + &log($fh,&encode_entities($line,'<>&"')); $psproc++; } close (PSH); - print $fh "
"); + open(DSH,"$perlvar{'lonDaemons'}/distprobe |"); + while (my $line="); + &errout($fh); +} + +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); + + + &log($fh,(<) { + &log($fh,&encode_entities($line,'<>&"')); + $psproc++; + } + close(DSH); + &log($fh,"
$varname | ". + &encode_entities($perlvar{$varname},'<>&"')." |
$id | ".$hostdom->{$id}. + " | ".$hostrole->{$id}. + " | ".$hostname->{$id}." |
Cleaned up ".$cleaned." stale session token(s)."; - print $fh "
Cleaned up ".$cleaned." stale session token(s).
"); + &log($fh,"'; +# ----------------------------------------------------------------------- httpd +sub check_httpd_logs { + my ($fh)=@_; + &log($fh,'"); &errout($fh); - - -# ---------------------------------------------------------------------- lonsql - - &checkon_daemon($fh,'lonsql',200000); - -# ------------------------------------------------------------------------ lond - - &checkon_daemon($fh,'lond',40000,1); - -# ------------------------------------------------------------------------ lonc - - &checkon_daemon($fh,'lonc',40000,1); - -# -------------------------------------------------------------------- lonhttpd - - &checkon_daemon($fh,'lonhttpd',40000); +} # ---------------------------------------------------------------------- lonnet - print $fh 'httpd
Access Log
'); open (DFH,"tail -n25 /etc/httpd/logs/access_log|"); - while ($line=) { print $fh "$line" }; + while (my $line= ) { &log($fh,&encode_entities($line,'<>&"')) }; close (DFH); - - print $fh " Error Log
"; - + + &log($fh,"Error Log
"); + open (DFH,"tail -n25 /etc/httpd/logs/error_log|"); - while ($line="; + &log($fh,") { - print $fh "$line"; + while (my $line= ) { + &log($fh,"$line"); if ($line=~/\[error\]/) { $notices++; } - }; + } close (DFH); - print $fh "
'; +sub rotate_lonnet_logs { + my ($fh)=@_; + &log($fh,'"); &errout($fh); -# ----------------------------------------------------------------- Connections +} - print $fh 'lonnet
Temp Log
'); 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"; - }; + while (my $line= ) { + &log($fh,&encode_entities($line,'<>&"')); + } close (DFH); } - print $fh " Perm Log
"; + &log($fh,"Perm Log
"); if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") { open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|"); - while ($line="; + &log($fh,") { - print $fh "$line"; - }; + while (my $line= ) { + &log($fh,&encode_entities($line,'<>&"')); + } close (DFH); - } else { print $fh "No perm log\n" } + } else { &log($fh,"No perm log\n") } - $fname="$perlvar{'lonDaemons'}/logs/lonnet.log"; + my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log"; my ($dev,$ino,$mode,$nlink, $uid,$gid,$rdev,$size, @@ -486,87 +466,106 @@ ENDHEADERS $blksize,$blocks)=stat($fname); if ($size>40000) { - print $fh "Rotating logs ... "; + &log($fh,"
Rotating logs ...
"); rename("$fname.2","$fname.3"); rename("$fname.1","$fname.2"); rename("$fname","$fname.1"); } - print $fh "
$tryserver | $result |
$tryserver | $result |
\n"; + + &log($fh,"
Total unsend messages: $unsend
\n"); $warnings=$warnings+5*$unsend; if ($unsend) { $simplestatus{'unsend'}=$unsend; } - print $fh ""); open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|"); - while ($line=\n"); close (DFH); +} -# ------------------------------------------------------------------------- End - print $fh "\n"; +sub finish_logging { + my ($fh)=@_; + &log($fh,"\n"); $totalcount=$notices+4*$warnings+100*$errors; &errout($fh); - print $fh ") { - print $fh "$line
"; - }; + while (my $line=) { + &log($fh,&encode_entities($line,'<>&"')); + } + &log($fh,"