Annotation of loncom/loncron, revision 1.134
1.1 albertel 1: #!/usr/bin/perl
2:
1.47 albertel 3: # Housekeeping program, started by cron, loncontrol and loncron.pl
4: #
1.134 ! raeburn 5: # $Id: loncron,v 1.133 2024/07/28 23:49:32 raeburn Exp $
1.47 albertel 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
1.1 albertel 28: #
1.24 www 29:
30: $|=1;
1.48 albertel 31: use strict;
1.1 albertel 32:
1.26 harris41 33: use lib '/home/httpd/lib/perl/';
34: use LONCAPA::Configuration;
1.96 raeburn 35: use LONCAPA::Checksumming;
1.89 raeburn 36: use LONCAPA;
1.116 raeburn 37: use LONCAPA::LWPReq;
1.72 albertel 38: use Apache::lonnet;
1.79 raeburn 39: use Apache::loncommon;
1.26 harris41 40:
1.1 albertel 41: use IO::File;
42: use IO::Socket;
1.48 albertel 43: use HTML::Entities;
1.49 albertel 44: use Getopt::Long;
1.128 raeburn 45: use GDBM_File qw(GDBM_READER);
1.104 raeburn 46: use Storable qw(thaw);
1.114 raeburn 47: use File::ReadBackwards;
1.117 raeburn 48: use File::Copy;
1.123 raeburn 49: use Sys::Hostname::FQDN();
50:
1.46 albertel 51: #globals
52: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
53:
54: my $statusdir="/home/httpd/html/lon-status";
55:
1.1 albertel 56:
57: # --------------------------------------------------------- Output error status
58:
1.46 albertel 59: sub log {
60: my $fh=shift;
61: if ($fh) { print $fh @_ }
62: }
63:
1.1 albertel 64: sub errout {
65: my $fh=shift;
1.46 albertel 66: &log($fh,(<<ENDERROUT));
1.48 albertel 67: <table border="2" bgcolor="#CCCCCC">
1.1 albertel 68: <tr><td>Notices</td><td>$notices</td></tr>
69: <tr><td>Warnings</td><td>$warnings</td></tr>
70: <tr><td>Errors</td><td>$errors</td></tr>
1.48 albertel 71: </table><p><a href="#top">Top</a></p>
1.1 albertel 72: ENDERROUT
73: }
74:
1.73 albertel 75: sub rotate_logfile {
76: my ($file,$fh,$description) = @_;
77: my $size=(stat($file))[7];
78: if ($size>40000) {
79: &log($fh,"<p>Rotating $description ...</p>");
80: rename("$file.2","$file.3");
81: rename("$file.1","$file.2");
82: rename("$file","$file.1");
1.125 raeburn 83: }
1.73 albertel 84: }
85:
1.42 albertel 86: sub start_daemon {
1.50 albertel 87: my ($fh,$daemon,$pidfile,$args) = @_;
1.44 albertel 88: my $progname=$daemon;
1.71 albertel 89: if ($daemon eq 'lonc') {
1.125 raeburn 90: $progname='loncnew';
1.44 albertel 91: }
1.51 albertel 92: my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
1.73 albertel 93: &rotate_logfile($error_fname,$fh,'error logs');
1.74 albertel 94: if ($daemon eq 'lonc') {
95: &clean_sockets($fh);
96: }
1.51 albertel 97: system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60 albertel 98: sleep 1;
1.42 albertel 99: if (-e $pidfile) {
1.48 albertel 100: &log($fh,"<p>Seems like it started ...</p>");
1.42 albertel 101: my $lfh=IO::File->new("$pidfile");
102: my $daemonpid=<$lfh>;
103: chomp($daemonpid);
1.62 albertel 104: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42 albertel 105: return 1;
106: } else {
107: return 0;
108: }
109: }
1.48 albertel 110: &log($fh,"<p>Seems like that did not work!</p>");
1.42 albertel 111: $errors++;
112: return 0;
113: }
114:
115: sub checkon_daemon {
1.59 albertel 116: my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42 albertel 117:
1.63 albertel 118: my $result;
1.48 albertel 119: &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57 albertel 120: printf("%-15s ",$daemon);
1.110 raeburn 121: if ($fh) {
122: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
123: if (open(DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
1.125 raeburn 124: while (my $line=<DFH>) {
1.110 raeburn 125: &log($fh,"$line");
126: if ($line=~/INFO/) { $notices++; }
127: if ($line=~/WARNING/) { $notices++; }
128: if ($line=~/CRITICAL/) { $warnings++; }
129: }
130: close (DFH);
131: }
132: }
133: &log($fh,"</tt></p>");
1.42 albertel 134: }
1.125 raeburn 135:
1.42 albertel 136: my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
1.125 raeburn 137:
1.42 albertel 138: my $restartflag=1;
1.46 albertel 139: my $daemonpid;
1.42 albertel 140: if (-e $pidfile) {
141: my $lfh=IO::File->new("$pidfile");
1.46 albertel 142: $daemonpid=<$lfh>;
1.42 albertel 143: chomp($daemonpid);
1.62 albertel 144: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46 albertel 145: &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59 albertel 146: if ($send) { &log($fh,", sending $send"); }
1.46 albertel 147: &log($fh,"</h3>");
1.59 albertel 148: if ($send eq 'USR1') { kill USR1 => $daemonpid; }
149: if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42 albertel 150: $restartflag=0;
1.59 albertel 151: if ($send eq 'USR2') {
1.63 albertel 152: $result = 'reloaded';
1.59 albertel 153: print "reloaded\n";
154: } else {
1.63 albertel 155: $result = 'running';
1.59 albertel 156: print "running\n";
157: }
1.42 albertel 158: } else {
159: $errors++;
1.46 albertel 160: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42 albertel 161: $restartflag=1;
1.46 albertel 162: &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42 albertel 163: }
164: }
165: if ($restartflag==1) {
166: $simplestatus{$daemon}='off';
167: $errors++;
1.57 albertel 168: my $kadaemon=$daemon;
169: if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.101 raeburn 170: &log($fh,'<br /><font color="red">Killall '.$daemon.': '.
1.57 albertel 171: `killall $kadaemon 2>&1`.' - ');
1.60 albertel 172: sleep 1;
1.46 albertel 173: &log($fh,unlink($pidfile).' - '.
1.57 albertel 174: `killall -9 $kadaemon 2>&1`.
1.101 raeburn 175: '</font><br />');
1.107 raeburn 176: if ($kadaemon eq 'loncnew') {
177: &clean_lonc_childpids();
178: }
1.46 albertel 179: &log($fh,"<h3>$daemon not running, trying to start</h3>");
1.110 raeburn 180:
1.50 albertel 181: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46 albertel 182: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42 albertel 183: $simplestatus{$daemon}='restarted';
1.63 albertel 184: $result = 'started';
1.42 albertel 185: print "started\n";
186: } else {
187: $errors++;
1.46 albertel 188: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48 albertel 189: &log($fh,"<p>Give it one more try ...</p>");
1.42 albertel 190: print " ";
1.50 albertel 191: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46 albertel 192: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42 albertel 193: $simplestatus{$daemon}='restarted';
1.63 albertel 194: $result = 'started';
1.42 albertel 195: print "started\n";
196: } else {
1.63 albertel 197: $result = 'failed';
1.42 albertel 198: print " failed\n";
199: $simplestatus{$daemon}='failed';
200: $errors++; $errors++;
1.46 albertel 201: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48 albertel 202: &log($fh,"<p>Unable to start $daemon</p>");
1.42 albertel 203: }
204: }
1.110 raeburn 205: if ($fh) {
206: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
207: &log($fh,"<p><pre>");
208: if (open(DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
209: while (my $line=<DFH>) {
210: &log($fh,"$line");
211: if ($line=~/WARNING/) { $notices++; }
212: if ($line=~/CRITICAL/) { $notices++; }
213: }
214: close (DFH);
215: }
216: &log($fh,"</pre></p>");
217: }
1.42 albertel 218: }
219: }
1.125 raeburn 220:
1.46 albertel 221: my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.73 albertel 222: &rotate_logfile($fname,$fh,'logs');
1.42 albertel 223:
224: &errout($fh);
1.63 albertel 225: return $result;
1.42 albertel 226: }
1.1 albertel 227:
1.46 albertel 228: # --------------------------------------------------------------------- Machine
229: sub log_machine_info {
230: my ($fh)=@_;
1.48 albertel 231: &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46 albertel 232: &log($fh,"<h3>loadavg</h3>");
1.125 raeburn 233:
1.126 raeburn 234: my $cpucount;
235: if (open(PIPE,"lscpu |grep '^CPU(s)' 2>&1 |")) {
236: my $info = <PIPE>;
237: chomp($info);
238: ($cpucount) = ($info =~ /^\QCPU(s):\E\s+(\d+)$/);
239: close(PIPE);
240: }
241: if (!$cpucount) {
242: $cpucount = 1;
243: }
244: my %loadtarget = (
245: error => 4.0*$cpucount,
246: warn => 2.0*$cpucount,
247: note => 1.0*$cpucount,
248: );
1.46 albertel 249: open (LOADAVGH,"/proc/loadavg");
250: my $loadavg=<LOADAVGH>;
251: close (LOADAVGH);
1.125 raeburn 252:
1.46 albertel 253: &log($fh,"<tt>$loadavg</tt>");
1.125 raeburn 254:
1.46 albertel 255: my @parts=split(/\s+/,$loadavg);
1.126 raeburn 256: if ($parts[1]>$loadtarget{'error'}) {
1.46 albertel 257: $errors++;
1.126 raeburn 258: } elsif ($parts[1]>$loadtarget{'warn'}) {
1.46 albertel 259: $warnings++;
1.126 raeburn 260: } elsif ($parts[1]>$loadtarget{'note'}) {
1.46 albertel 261: $notices++;
262: }
1.13 harris41 263:
1.46 albertel 264: &log($fh,"<h3>df</h3>");
265: &log($fh,"<pre>");
1.14 harris41 266:
1.46 albertel 267: open (DFH,"df|");
1.125 raeburn 268: while (my $line=<DFH>) {
269: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 270: @parts=split(/\s+/,$line);
271: my $usage=$parts[4];
272: $usage=~s/\W//g;
1.125 raeburn 273: if ($usage>90) {
1.46 albertel 274: $warnings++;
1.125 raeburn 275: $notices++;
1.46 albertel 276: } elsif ($usage>80) {
277: $warnings++;
278: } elsif ($usage>60) {
279: $notices++;
1.31 albertel 280: }
1.46 albertel 281: if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1 albertel 282: }
1.46 albertel 283: close (DFH);
284: &log($fh,"</pre>");
1.1 albertel 285:
286:
1.46 albertel 287: &log($fh,"<h3>ps</h3>");
288: &log($fh,"<pre>");
289: my $psproc=0;
1.1 albertel 290:
1.53 albertel 291: open (PSH,"ps aux --cols 140 |");
1.125 raeburn 292: while (my $line=<PSH>) {
293: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 294: $psproc++;
295: }
296: close (PSH);
297: &log($fh,"</pre>");
1.1 albertel 298:
1.46 albertel 299: if ($psproc>200) { $notices++; }
300: if ($psproc>250) { $notices++; }
1.1 albertel 301:
1.61 albertel 302: &log($fh,"<h3>distprobe</h3>");
303: &log($fh,"<pre>");
1.98 raeburn 304: &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
1.61 albertel 305: &log($fh,"</pre>");
306:
1.46 albertel 307: &errout($fh);
308: }
1.1 albertel 309:
1.46 albertel 310: sub start_logging {
1.43 albertel 311: my $fh=IO::File->new(">$statusdir/newstatus.html");
1.134 ! raeburn 312: %simplestatus=();
1.46 albertel 313: my $now=time;
314: my $date=localtime($now);
1.125 raeburn 315:
1.46 albertel 316:
317: &log($fh,(<<ENDHEADERS));
1.101 raeburn 318: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
319: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1.1 albertel 320: <head>
321: <title>LON Status Report $perlvar{'lonHostID'}</title>
1.101 raeburn 322: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1.1 albertel 323: </head>
1.3 www 324: <body bgcolor="#AAAAAA">
1.48 albertel 325: <a name="top" />
1.1 albertel 326: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
327: <h2>$date ($now)</h2>
328: <ol>
1.48 albertel 329: <li><a href="#configuration">Configuration</a></li>
330: <li><a href="#machine">Machine Information</a></li>
331: <li><a href="#tmp">Temporary Files</a></li>
332: <li><a href="#tokens">Session Tokens</a></li>
1.102 raeburn 333: <li><a href="#webdav">WebDAV Session Tokens</a></li>
1.48 albertel 334: <li><a href="#httpd">httpd</a></li>
335: <li><a href="#lonsql">lonsql</a></li>
336: <li><a href="#lond">lond</a></li>
337: <li><a href="#lonc">lonc</a></li>
338: <li><a href="#lonnet">lonnet</a></li>
339: <li><a href="#connections">Connections</a></li>
1.130 raeburn 340: <li><a href="#bashconf">bash readline config</a></li>
1.48 albertel 341: <li><a href="#delayed">Delayed Messages</a></li>
342: <li><a href="#errcount">Error Count</a></li>
1.1 albertel 343: </ol>
1.48 albertel 344: <hr />
345: <a name="configuration" />
1.1 albertel 346: <h2>Configuration</h2>
347: <h3>PerlVars</h3>
1.48 albertel 348: <table border="2">
1.1 albertel 349: ENDHEADERS
350:
1.46 albertel 351: foreach my $varname (sort(keys(%perlvar))) {
1.48 albertel 352: &log($fh,"<tr><td>$varname</td><td>".
353: &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43 albertel 354: }
1.48 albertel 355: &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72 albertel 356: my %hostname = &Apache::lonnet::all_hostnames();
357: foreach my $id (sort(keys(%hostname))) {
358: my $role = (&Apache::lonnet::is_library($id) ? 'library'
359: : 'access');
1.46 albertel 360: &log($fh,
1.72 albertel 361: "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
362: "</td><td>".$role.
363: "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
364: }
1.101 raeburn 365: &log($fh,"</table><h3>Spare Hosts</h3>");
366: if (keys(%Apache::lonnet::spareid) > 0) {
367: &log($fh,"<ul>");
368: foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
369: &log($fh,"<li>$type\n<ol>");
370: foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
371: &log($fh,"<li>$id</li>\n");
372: }
373: &log($fh,"</ol>\n</li>\n");
374: }
375: &log($fh,"</ul>\n");
376: } else {
377: &log($fh,"No spare hosts specified<br />\n");
1.43 albertel 378: }
1.46 albertel 379: return $fh;
380: }
1.11 www 381:
382: # --------------------------------------------------------------- clean out tmp
1.46 albertel 383: sub clean_tmp {
384: my ($fh)=@_;
1.48 albertel 385: &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.82 raeburn 386: my ($cleaned,$old,$removed) = (0,0,0);
387: my %errors = (
388: dir => [],
389: file => [],
390: failopen => [],
391: );
392: my %error_titles = (
393: dir => 'failed to remove empty directory:',
394: file => 'failed to unlike stale file',
395: failopen => 'failed to open file or directory'
396: );
397: ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
398: &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
399: foreach my $key (sort(keys(%errors))) {
400: if (ref($errors{$key}) eq 'ARRAY') {
401: if (@{$errors{$key}} > 0) {
402: &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
403: join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
404: }
405: }
406: }
407: }
408:
409: sub recursive_clean_tmp {
410: my ($subdir,$cleaned,$old,$removed,$errors) = @_;
411: my $base = "$perlvar{'lonDaemons'}/tmp";
412: my $path = $base;
413: next if ($subdir =~ m{\.\./});
414: next unless (ref($errors) eq 'HASH');
415: unless ($subdir eq '') {
416: $path .= '/'.$subdir;
417: }
418: if (opendir(my $dh,"$path")) {
419: while (my $file = readdir($dh)) {
420: next if ($file =~ /^\.\.?$/);
421: my $fname = "$path/$file";
422: if (-d $fname) {
423: my $innerdir;
424: if ($subdir eq '') {
425: $innerdir = $file;
426: } else {
427: $innerdir = $subdir.'/'.$file;
428: }
429: ($cleaned,$old,$removed) =
430: &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
431: my @doms = &Apache::lonnet::current_machine_domains();
1.125 raeburn 432:
1.82 raeburn 433: if (open(my $dirhandle,$fname)) {
434: unless (($innerdir eq 'helprequests') ||
435: (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
436: my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
1.125 raeburn 437: join('&&',@contents)."\n";
1.82 raeburn 438: if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
439: closedir($dirhandle);
440: if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
441: if (rmdir($fname)) {
442: $removed ++;
443: } elsif (ref($errors->{dir}) eq 'ARRAY') {
444: push(@{$errors->{dir}},$fname);
445: }
446: }
447: }
448: } else {
449: closedir($dirhandle);
450: }
451: }
452: } else {
453: my ($dev,$ino,$mode,$nlink,
454: $uid,$gid,$rdev,$size,
455: $atime,$mtime,$ctime,
456: $blksize,$blocks)=stat($fname);
457: my $now=time;
458: my $since=$now-$mtime;
459: if ($since>$perlvar{'lonExpire'}) {
460: if ($subdir eq '') {
461: my $line='';
462: if ($fname =~ /\.db$/) {
463: if (unlink($fname)) {
464: $cleaned++;
465: } elsif (ref($errors->{file}) eq 'ARRAY') {
466: push(@{$errors->{file}},$fname);
467: }
468: } elsif (open(PROBE,$fname)) {
469: my $line='';
470: $line=<PROBE>;
471: close(PROBE);
472: if ($line=~/^CHECKOUTTOKEN\&/) {
473: if ($since>365*$perlvar{'lonExpire'}) {
474: if (unlink($fname)) {
475: $cleaned++;
476: } elsif (ref($errors->{file}) eq 'ARRAY') {
477: push(@{$errors->{file}},$fname);
478: }
479: } else {
480: $old++;
481: }
482: } else {
483: if (unlink($fname)) {
484: $cleaned++;
485: } elsif (ref($errors->{file}) eq 'ARRAY') {
486: push(@{$errors->{file}},$fname);
487: }
488: }
489: } elsif (ref($errors->{failopen}) eq 'ARRAY') {
1.125 raeburn 490: push(@{$errors->{failopen}},$fname);
1.82 raeburn 491: }
492: } else {
493: if (unlink($fname)) {
494: $cleaned++;
495: } elsif (ref($errors->{file}) eq 'ARRAY') {
496: push(@{$errors->{file}},$fname);
497: }
498: }
499: }
500: }
501: }
502: closedir($dh);
503: } elsif (ref($errors->{failopen}) eq 'ARRAY') {
504: push(@{$errors->{failopen}},$path);
1.43 albertel 505: }
1.82 raeburn 506: return ($cleaned,$old,$removed);
1.46 albertel 507: }
1.11 www 508:
509: # ------------------------------------------------------------ clean out lonIDs
1.46 albertel 510: sub clean_lonIDs {
511: my ($fh)=@_;
1.48 albertel 512: &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46 albertel 513: my $cleaned=0;
514: my $active=0;
515: while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.122 raeburn 516: my $now=time;
517: if (-l $fname) {
518: my $linkfname = readlink($fname);
519: if (-f $linkfname) {
520: if ($linkfname =~ m{^$perlvar{'lonIDsDir'}/[^/]+\.id$}) {
521: my @data = stat($linkfname);
522: my $mtime = $data[9];
523: my $since=$now-$mtime;
524: if ($since>$perlvar{'lonExpire'}) {
525: if (unlink($linkfname)) {
526: $cleaned++;
527: &log($fh,"Unlinking $linkfname<br />");
528: unlink($fname);
529: }
530: }
531: }
532: } else {
533: unlink($fname);
534: }
535: } elsif (-f $fname) {
536: my @data = stat($fname);
537: my $mtime = $data[9];
538: my $since=$now-$mtime;
539: if ($since>$perlvar{'lonExpire'}) {
540: if (unlink($fname)) {
541: $cleaned++;
542: &log($fh,"Unlinking $fname<br />");
543: }
544: } else {
545: $active++;
546: }
547: }
1.46 albertel 548: }
1.48 albertel 549: &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46 albertel 550: &log($fh,"<h3>$active open session(s)</h3>");
551: }
1.43 albertel 552:
1.115 raeburn 553: # -------------------------------------------------------- clean out balanceIDs
554:
555: sub clean_balanceIDs {
556: my ($fh)=@_;
557: &log($fh,'<hr /><a name="balcookies" /><h2>Session Tokens</h2>');
558: my $cleaned=0;
559: my $active=0;
560: if (-d $perlvar{'lonBalanceDir'}) {
1.124 raeburn 561: while (my $fname=<$perlvar{'lonBalanceDir'}/*.id>) {
1.115 raeburn 562: my ($dev,$ino,$mode,$nlink,
563: $uid,$gid,$rdev,$size,
564: $atime,$mtime,$ctime,
565: $blksize,$blocks)=stat($fname);
566: my $now=time;
567: my $since=$now-$mtime;
568: if ($since>$perlvar{'lonExpire'}) {
569: $cleaned++;
570: &log($fh,"Unlinking $fname<br />");
571: unlink("$fname");
572: } else {
573: $active++;
574: }
575: }
576: }
577: &log($fh,"<p>Cleaned up ".$cleaned." stale balancer files</p>");
578: &log($fh,"<h3>$active unexpired balancer files</h3>");
579: }
580:
1.102 raeburn 581: # ------------------------------------------------ clean out webDAV Session IDs
582: sub clean_webDAV_sessionIDs {
583: my ($fh)=@_;
584: if ($perlvar{'lonRole'} eq 'library') {
585: &log($fh,'<hr /><a name="webdav" /><h2>WebDAV Session Tokens</h2>');
586: my $cleaned=0;
587: my $active=0;
588: my $now = time;
589: if (-d $perlvar{'lonDAVsessDir'}) {
590: while (my $fname=<$perlvar{'lonDAVsessDir'}/*>) {
591: my @stats = stat($fname);
592: my $since=$now-$stats[9];
593: if ($since>$perlvar{'lonExpire'}) {
594: $cleaned++;
595: &log($fh,"Unlinking $fname<br />");
596: unlink("$fname");
597: } else {
598: $active++;
599: }
600: }
601: &log($fh,"<p>Cleaned up ".$cleaned." stale webDAV session token(s).</p>");
602: &log($fh,"<h3>$active open webDAV session(s)</h3>");
603: }
604: }
605: }
606:
1.119 raeburn 607: # ------------------------------------------------------------ clean out ltiIDs
608:
609: sub clean_ltiIDs {
610: my ($fh)=@_;
611: &log($fh,'<hr /><a name="ltisessions" /><h2>LTI Session Pointers</h2>');
612: my $cleaned=0;
613: my $active=0;
614: if (-d $perlvar{'ltiIDsDir'}) {
615: while (my $fname=<$perlvar{'ltiIDsDir'}/*>) {
616: my ($dev,$ino,$mode,$nlink,
617: $uid,$gid,$rdev,$size,
618: $atime,$mtime,$ctime,
619: $blksize,$blocks)=stat($fname);
620: my $now=time;
621: my $since=$now-$mtime;
622: if ($since>$perlvar{'lonExpire'}) {
623: $cleaned++;
624: &log($fh,"Unlinking $fname<br />");
625: unlink("$fname");
626: } else {
627: $active++;
628: }
629: }
630: }
631: &log($fh,"<p>Cleaned up ".$cleaned." old LTI session pointers.</p>");
632: &log($fh,"<h3>$active unexpired LTI session pointers</h3>");
633: }
634:
1.74 albertel 635: # ----------------------------------------------------------- clean out sockets
636: sub clean_sockets {
637: my ($fh)=@_;
638: my $cleaned=0;
639: opendir(SOCKETS,$perlvar{'lonSockDir'});
640: while (my $fname=readdir(SOCKETS)) {
641: next if (-d $fname
1.80 www 642: || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
1.74 albertel 643: $cleaned++;
644: &log($fh,"Unlinking $fname<br />");
645: unlink("/home/httpd/sockets/$fname");
646: }
647: &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
648: }
649:
1.11 www 650:
1.1 albertel 651: # ----------------------------------------------------------------------- httpd
1.46 albertel 652: sub check_httpd_logs {
653: my ($fh)=@_;
1.94 raeburn 654: if (open(PIPE,"./lchttpdlogs|")) {
1.93 raeburn 655: while (my $line=<PIPE>) {
656: &log($fh,$line);
657: if ($line=~/\[error\]/) { $notices++; }
658: }
659: close(PIPE);
1.46 albertel 660: }
1.43 albertel 661: &errout($fh);
1.46 albertel 662: }
1.1 albertel 663:
664: # ---------------------------------------------------------------------- lonnet
665:
1.48 albertel 666: sub rotate_lonnet_logs {
1.46 albertel 667: my ($fh)=@_;
1.48 albertel 668: &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.100 bisitz 669: print "Checking logs.\n";
1.43 albertel 670: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
671: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.125 raeburn 672: while (my $line=<DFH>) {
1.48 albertel 673: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 674: }
1.43 albertel 675: close (DFH);
676: }
1.46 albertel 677: &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.125 raeburn 678:
1.43 albertel 679: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
680: open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.125 raeburn 681: while (my $line=<DFH>) {
1.48 albertel 682: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 683: }
1.43 albertel 684: close (DFH);
1.46 albertel 685: } else { &log($fh,"No perm log\n") }
1.43 albertel 686:
1.46 albertel 687: my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73 albertel 688: &rotate_logfile($fname,$fh,'lonnet log');
1.1 albertel 689:
1.46 albertel 690: &log($fh,"</pre>");
1.43 albertel 691: &errout($fh);
1.46 albertel 692: }
693:
1.73 albertel 694: sub rotate_other_logs {
695: my ($fh) = @_;
1.83 raeburn 696: my %logs = (
697: autoenroll => 'Auto Enroll log',
698: autocreate => 'Create Course log',
699: searchcat => 'Search Cataloguing log',
700: autoupdate => 'Auto Update log',
701: refreshcourseids_db => 'Refresh CourseIDs db log',
702: );
703: foreach my $item (keys(%logs)) {
704: my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
705: &rotate_logfile($fname,$fh,$logs{$item});
706: }
1.73 albertel 707: }
708:
1.43 albertel 709: # ----------------------------------------------------------------- Connections
1.46 albertel 710: sub test_connections {
1.72 albertel 711: my ($fh)=@_;
1.48 albertel 712: &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.100 bisitz 713: print "Testing connections.\n";
1.48 albertel 714: &log($fh,"<table border='2'>");
1.49 albertel 715: my ($good,$bad)=(0,0);
1.72 albertel 716: my %hostname = &Apache::lonnet::all_hostnames();
717: foreach my $tryserver (sort(keys(%hostname))) {
1.43 albertel 718: print(".");
1.46 albertel 719: my $result;
1.72 albertel 720: my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43 albertel 721: if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
722: $result="<b>ok</b>";
1.49 albertel 723: $good++;
1.43 albertel 724: } else {
725: $result=$answer;
726: $warnings++;
1.49 albertel 727: if ($answer eq 'con_lost') {
728: $bad++;
729: $warnings++;
1.50 albertel 730: } else {
731: $good++; #self connection
1.49 albertel 732: }
1.43 albertel 733: }
734: if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46 albertel 735: &log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1 albertel 736: }
1.46 albertel 737: &log($fh,"</table>");
1.49 albertel 738: print "\n$good good, $bad bad connections\n";
1.43 albertel 739: &errout($fh);
1.46 albertel 740: }
741:
742:
1.1 albertel 743: # ------------------------------------------------------------ Delayed messages
1.46 albertel 744: sub check_delayed_msg {
1.114 raeburn 745: my ($fh,$weightsref,$exclusionsref)=@_;
1.48 albertel 746: &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.100 bisitz 747: print "Checking buffers.\n";
1.46 albertel 748:
749: &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1 albertel 750:
1.46 albertel 751: my $unsend=0;
1.114 raeburn 752: my $ignored=0;
1.1 albertel 753:
1.105 raeburn 754: my %hostname = &Apache::lonnet::all_hostnames();
755: my $numhosts = scalar(keys(%hostname));
1.114 raeburn 756: my $checkbackwards = 0;
757: my $checkfrom = 0;
758: my $checkexcluded = 0;
759: my (%bymachine,%weights,%exclusions,%serverhomes);
760: if (ref($weightsref) eq 'HASH') {
761: %weights = %{$weightsref};
762: }
763: if (ref($exclusionsref) eq 'HASH') {
764: %exclusions = %{$exclusionsref};
765: if (keys(%exclusions)) {
766: $checkexcluded = 1;
767: %serverhomes = &read_serverhomeIDs();
768: }
769: }
1.105 raeburn 770:
1.114 raeburn 771: #
772: # For LON-CAPA 1.2.0 to 2.1.3 (release dates: 8/31/2004 and 3/31/2006) any
773: # entry logged in lonnet.perm.log for completion of a delayed (critical)
774: # transaction lacked the hostID for the remote node to which the command
775: # to be completed was sent.
776: #
777: # Because of this, exclusion of items in lonnet.perm.log for nodes which are
778: # no longer part of the cluster from adding to the overall "unsend" count
779: # needs additional effort besides the changes made in loncron rev. 1.105.
780: #
781: # For "S" (completion) events logging in LON-CAPA 1.2.0 through 2.1.3 included
782: # "LondTransaction=HASH(hexadecimal)->getClient() :$cmd, where the hexadecimal
783: # is a memory location, and $cmd is the command sent to the remote node.
784: #
785: # Starting with 2.2.0 (released 8/21/2006) logging for "S" (completion) events
786: # had sethost:$host_id:$cmd after LondTransaction=HASH(hexadecimal)->getClient()
787: #
788: # Starting with 2.4.1 (released 6/13/2007) logging for "S" replaced echoing the
789: # getClient() call with the result of the Transaction->getClient() call itself
790: # undef for completion of delivery of a delayed message.
791: #
792: # The net effect of these changes is that lonnet.perm.log is now accessed three
793: # times: (a) oldest record is checked, if earlier than release date for 2.5.0
794: # then (b) file is read backwards, with timestamp recorded for most recent
795: # instance of logged "S" event for "update" command without "sethost:$host_id:"
796: # then (c) file is read forward with records ignored which predate the timestamp
797: # recorded in (b), if one was found.
798: #
799: # In (c), when calculating the unsend total, i.e., the difference between delayed
800: # transactions ("D") and sent transactions ("S"), transactions are ignored if the
801: # target node is no longer in the cluster, and also (for "update" commands), if
802: # the target node is in the list of nodes excluded from the count, in the domain
803: # configuration for this machine's default domain. The idea here is to remove
804: # delayed "update" commands for nodes for which inbound access to port 5663,
805: # is blocked, but are still part of the LON-CAPA network, (i.e., they can still
806: # replicate content from other nodes).
807: #
808:
809: my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
810: if (defined($dfh)) {
811: while (my $line=<$dfh>) {
812: my ($time,$sdf,$rest)=split(/:/,$line,3);
813: if ($time < 1541185772) {
814: $checkbackwards = 1;
815: }
816: last;
817: }
818: undef $dfh;
819: }
820:
821: if ($checkbackwards) {
822: if (tie *BW, 'File::ReadBackwards', "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
823: while(my $line=<BW>) {
824: if ($line =~ /\QLondTransaction=HASH\E[^:]+:update:/) {
825: ($checkfrom) = split(/:/,$line,2);
826: last;
827: }
828: }
829: close(BW);
830: }
1.1 albertel 831: }
1.114 raeburn 832: $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
833: if (defined($dfh)) {
834: while (my $line=<$dfh>) {
835: my ($time,$sdf,$rest)=split(/:/,$line,3);
836: next unless (($sdf eq 'F') || ($sdf eq 'S') || ($sdf eq 'D'));
837: next if (($checkfrom) && ($time <= $checkfrom));
838: my ($dserv,$dcmd);
839: if ($sdf eq 'S') {
840: my ($serva,$cmda,$servb,$cmdb) = split(/:/,$rest);
841: if ($cmda eq 'sethost') {
842: chomp($cmdb);
843: $dcmd = $cmdb;
844: } else {
845: $dcmd = $cmda;
846: }
847: if (($serva =~ /^LondTransaction/) || ($serva eq '')) {
848: unless (($servb eq '') || ($servb =~ m{^/})) {
849: $dserv = $servb;
850: }
851: } else {
852: $dserv = $serva;
853: }
854: } else {
855: ($dserv,$dcmd) = split(/:/,$rest);
856: }
857: if ($sdf eq 'F') {
858: my $local=localtime($time);
859: &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
860: $warnings++;
861: }
862: next if ((($dserv eq '') || ($dcmd eq '')) && ($sdf ne 'F'));
863: if ($sdf eq 'S') {
864: if ($dcmd eq 'update') {
865: if ($hostname{$dserv}) {
866: if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
867: $ignored --;
868: } else {
869: $unsend --;
870: }
871: }
872: if (exists($bymachine{$dserv})) {
873: $bymachine{$dserv} --;
874: } else {
875: $bymachine{$dserv} = -1;
876: }
877: } else {
878: if ($hostname{$dserv}) {
879: $unsend --;
880: }
881: }
882: } elsif ($sdf eq 'D') {
883: if ($dcmd eq 'update') {
884: if ($hostname{$dserv}) {
885: if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
886: $ignored ++;
887: } else {
888: $unsend ++;
889: }
890: }
891: if (exists($bymachine{$dserv})) {
892: $bymachine{$dserv} ++;
893: } else {
894: $bymachine{$dserv} = 1;
895: }
896: } else {
897: if ($hostname{$dserv}) {
898: $unsend ++;
899: }
900: }
901: }
902: }
903: undef $dfh;
904: my $nodest = 0;
905: my $retired = 0;
906: my %active;
907: if (keys(%bymachine)) {
908: unless ($checkexcluded) {
909: %serverhomes = &read_serverhomeIDs();
910: }
911: foreach my $key (keys(%bymachine)) {
912: if ($bymachine{$key} > 0) {
913: if ($hostname{$key}) {
914: $active{$serverhomes{$hostname{$key}}} += $bymachine{$key};
915: } else {
916: $retired ++;
917: $nodest += $bymachine{$key};
918: }
919: }
920: }
921: }
922: if (keys(%active)) {
923: &log($fh,"<p>Unsend messages by node, active (undegraded) nodes in cluster</p>\n");
924: foreach my $key (sort(keys(%active))) {
925: &log($fh,&encode_entities("$key => $active{$key}",'<>&"')."\n");
926: }
927: }
928: &log($fh,"<p>Total unsend messages: <b>$unsend</b> for ".scalar(keys(%active))." active (undegraded) nodes in cluster.</p>\n");
929: if (keys(%exclusions) > 0) {
930: &log($fh,"<p>Total incomplete updates <b>$ignored</b> for ".scalar(keys(%exclusions))." degraded nodes in cluster.</p>\n");
931: }
932: if ($retired) {
933: &log($fh,"<p>Total unsent <b>$nodest</b> for $retired nodes no longer in cluster.</p>\n");
934: }
935: if ($unsend > 0) {
936: $warnings=$warnings+$weights{'U'}*$unsend;
937: }
1.95 raeburn 938: }
1.1 albertel 939:
1.43 albertel 940: if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48 albertel 941: &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68 www 942: # list directory with delayed messages and remember offline servers
943: my %servers=();
1.43 albertel 944: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68 www 945: while (my $line=<DFH>) {
946: my ($server)=($line=~/\.(\w+)$/);
947: if ($server) { $servers{$server}=1; }
1.48 albertel 948: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 949: }
1.48 albertel 950: &log($fh,"</pre>\n");
1.43 albertel 951: close (DFH);
1.68 www 952: # pong to all servers that have delayed messages
953: # this will trigger a reverse connection, which should flush the buffers
1.95 raeburn 954: foreach my $tryserver (sort(keys(%servers))) {
955: if ($hostname{$tryserver} || !$numhosts) {
956: my $answer;
957: eval {
958: local $SIG{ ALRM } = sub { die "TIMEOUT" };
959: alarm(20);
960: $answer = &Apache::lonnet::reply("pong",$tryserver);
961: alarm(0);
962: };
963: if ($@ && $@ =~ m/TIMEOUT/) {
964: &log($fh,"Attempted pong to $tryserver timed out<br />");
1.100 bisitz 965: print "Time out while contacting: $tryserver for pong.\n";
1.95 raeburn 966: } else {
967: &log($fh,"Pong to $tryserver: $answer<br />");
968: }
1.91 raeburn 969: } else {
1.95 raeburn 970: &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
1.91 raeburn 971: }
1.68 www 972: }
1.46 albertel 973: }
1.1 albertel 974:
1.46 albertel 975: sub finish_logging {
1.114 raeburn 976: my ($fh,$weightsref)=@_;
977: my %weights;
978: if (ref($weightsref) eq 'HASH') {
979: %weights = %{$weightsref};
980: }
1.48 albertel 981: &log($fh,"<a name='errcount' />\n");
1.113 raeburn 982: $totalcount=($weights{'N'}*$notices)+($weights{'W'}*$warnings)+($weights{'E'}*$errors);
1.43 albertel 983: &errout($fh);
1.46 albertel 984: &log($fh,"<h1>Total Error Count: $totalcount</h1>");
985: my $now=time;
986: my $date=localtime($now);
1.48 albertel 987: &log($fh,"<hr />$date ($now)</body></html>\n");
1.100 bisitz 988: print "lon-status webpage updated.\n";
1.43 albertel 989: $fh->close();
1.46 albertel 990:
991: if ($errors) { $simplestatus{'errors'}=$errors; }
992: if ($warnings) { $simplestatus{'warnings'}=$warnings; }
993: if ($notices) { $simplestatus{'notices'}=$notices; }
994: $simplestatus{'time'}=time;
1.1 albertel 995: }
996:
1.46 albertel 997: sub log_simplestatus {
1.73 albertel 998: rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46 albertel 999:
1.43 albertel 1000: my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
1.134 ! raeburn 1001: if (defined($sfh)) {
! 1002: foreach my $key (keys(%simplestatus)) {
! 1003: print $sfh $key.'='.$simplestatus{$key}.'&';
! 1004: }
! 1005: print $sfh "\n";
! 1006: $sfh->close();
1.43 albertel 1007: }
1.41 www 1008: }
1.46 albertel 1009:
1.84 raeburn 1010: sub write_loncaparevs {
1.100 bisitz 1011: print "Retrieving LON-CAPA version information.\n";
1.99 raeburn 1012: my %hostname = &Apache::lonnet::all_hostnames();
1013: my $output;
1014: foreach my $id (sort(keys(%hostname))) {
1015: if ($id ne '') {
1016: my $loncaparev;
1017: eval {
1018: local $SIG{ ALRM } = sub { die "TIMEOUT" };
1019: alarm(10);
1020: $loncaparev =
1021: &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
1022: alarm(0);
1023: };
1024: if ($@ && $@ =~ m/TIMEOUT/) {
1.100 bisitz 1025: print "Time out while contacting lonHost: $id for version.\n";
1.99 raeburn 1026: }
1027: if ($loncaparev =~ /^[\w.\-]+$/) {
1028: $output .= $id.':'.$loncaparev."\n";
1029: }
1030: }
1031: }
1032: if ($output) {
1033: if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1034: print $fh $output;
1035: close($fh);
1036: &Apache::lonnet::load_loncaparevs();
1037: }
1038: }
1039: return;
1040: }
1041:
1042: sub write_serverhomeIDs {
1.100 bisitz 1043: print "Retrieving LON-CAPA lonHostID information.\n";
1.99 raeburn 1044: my %name_to_host = &Apache::lonnet::all_names();
1045: my $output;
1046: foreach my $name (sort(keys(%name_to_host))) {
1047: if ($name ne '') {
1048: if (ref($name_to_host{$name}) eq 'ARRAY') {
1049: my $serverhomeID;
1.90 raeburn 1050: eval {
1051: local $SIG{ ALRM } = sub { die "TIMEOUT" };
1052: alarm(10);
1.125 raeburn 1053: $serverhomeID =
1.99 raeburn 1054: &Apache::lonnet::get_server_homeID($name,1,'loncron');
1.90 raeburn 1055: alarm(0);
1056: };
1057: if ($@ && $@ =~ m/TIMEOUT/) {
1.99 raeburn 1058: print "Time out while contacting server: $name\n";
1.90 raeburn 1059: }
1.99 raeburn 1060: if ($serverhomeID ne '') {
1061: $output .= $name.':'.$serverhomeID."\n";
1062: } else {
1063: $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1.84 raeburn 1064: }
1065: }
1066: }
1067: }
1.99 raeburn 1068: if ($output) {
1069: if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1070: print $fh $output;
1071: close($fh);
1072: &Apache::lonnet::load_serverhomeIDs();
1.85 raeburn 1073: }
1074: }
1075: return;
1076: }
1077:
1.96 raeburn 1078: sub write_checksums {
1.98 raeburn 1079: my $distro = &LONCAPA::distro();
1.96 raeburn 1080: if ($distro) {
1081: print "Retrieving file version and checksumming.\n";
1.97 raeburn 1082: my $numchksums = 0;
1.96 raeburn 1083: my ($chksumsref,$versionsref) =
1.97 raeburn 1084: &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
1085: $perlvar{'lonLib'},
1086: $perlvar{'lonIncludes'},
1087: $perlvar{'lonTabDir'});
1.96 raeburn 1088: if (ref($chksumsref) eq 'HASH') {
1089: $numchksums = scalar(keys(%{$chksumsref}));
1090: }
1091: print "File version retrieved and checksumming completed for $numchksums files.\n";
1092: } else {
1093: print "File version retrieval and checksumming skipped - could not determine Linux distro.\n";
1094: }
1.97 raeburn 1095: return;
1.96 raeburn 1096: }
1097:
1.117 raeburn 1098: sub write_hostips {
1099: my $lontabdir = $perlvar{'lonTabDir'};
1100: my $defdom = $perlvar{'lonDefDomain'};
1101: my $lonhost = $perlvar{'lonHostID'};
1102: my $newfile = "$lontabdir/currhostips.tab";
1103: my $oldfile = "$lontabdir/prevhostips.tab";
1104: my (%prevhosts,%currhosts,%ipchange);
1105: if ((-e $newfile) && (-s $newfile)) {
1106: move($newfile,$oldfile);
1107: chmod(0644,$oldfile);
1108: if (open(my $fh,'<',$oldfile)) {
1109: while (my $line=<$fh>) {
1110: chomp($line);
1111: if ($line =~ /^([^:]+):([\d.]+)$/) {
1112: $prevhosts{$1} = $2;
1113: }
1114: }
1115: close($fh);
1116: }
1117: }
1118: my ($ip_info,$cached) =
1119: &Apache::lonnet::is_cached_new('iphost','iphost');
1120: if (!$cached) {
1121: &Apache::lonnet::get_iphost();
1122: ($ip_info,$cached) =
1123: &Apache::lonnet::is_cached_new('iphost','iphost');
1124: }
1125: if (ref($ip_info) eq 'ARRAY') {
1126: %currhosts = %{$ip_info->[1]};
1127: if (open(my $fh,'>',$newfile)) {
1128: foreach my $key (keys(%currhosts)) {
1129: print $fh "$key:$currhosts{$key}\n";
1130: }
1131: close($fh);
1132: chmod(0644,$newfile);
1133: }
1134: }
1135: if (keys(%prevhosts) && keys(%currhosts)) {
1136: foreach my $key (keys(%prevhosts)) {
1137: unless ($currhosts{$key} eq $prevhosts{$key}) {
1.121 raeburn 1138: $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1.117 raeburn 1139: }
1140: }
1141: foreach my $key (keys(%currhosts)) {
1142: unless ($currhosts{$key} eq $prevhosts{$key}) {
1.121 raeburn 1143: $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1.117 raeburn 1144: }
1145: }
1146: }
1147: if (&Apache::lonnet::domain($defdom,'primary') eq $lonhost) {
1148: if (keys(%ipchange)) {
1149: if (open(my $fh,'>>',$perlvar{'lonDaemons'}.'/logs/hostip.log')) {
1150: print $fh "********************\n".localtime(time).' Changes --'."\n".
1.121 raeburn 1151: "| Hostname | Previous IP | New IP |\n".
1152: " --------------------------------- \n";
1.117 raeburn 1153: foreach my $hostname (sort(keys(%ipchange))) {
1.121 raeburn 1154: print $fh "| $hostname | $ipchange{$hostname} |\n";
1.117 raeburn 1155: }
1156: print $fh "\n*******************\n\n";
1157: close($fh);
1158: }
1159: my $emailto = &Apache::loncommon::build_recipient_list(undef,
1160: 'hostipmail',$defdom);
1161: if ($emailto) {
1162: my $subject = "LON-CAPA Hostname to IP change ($perlvar{'lonHostID'})";
1163: my $chgmail = "To: $emailto\n".
1164: "Subject: $subject\n".
1165: "Content-type: text/plain\; charset=UTF-8\n".
1166: "MIME-Version: 1.0\n\n".
1167: "Host/IP changes\n".
1168: " \n".
1.121 raeburn 1169: "| Hostname | Previous IP | New IP |\n".
1170: " --------------------------------- \n";
1.117 raeburn 1171: foreach my $hostname (sort(keys(%ipchange))) {
1.121 raeburn 1172: $chgmail .= "| $hostname | $ipchange{$hostname} |\n";
1.117 raeburn 1173: }
1174: $chgmail .= "\n\n";
1175: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
1176: print $mailh $chgmail;
1177: close($mailh);
1178: print "Sending mail notification of hostname/IP changes.\n";
1179: }
1180: }
1181: }
1182: }
1183: return;
1184: }
1185:
1.107 raeburn 1186: sub clean_nosslverify {
1187: my ($fh) = @_;
1.125 raeburn 1188: my %unlinked;
1.107 raeburn 1189: if (-d "$perlvar{'lonSockDir'}/nosslverify") {
1190: if (opendir(my $dh,"$perlvar{'lonSockDir'}/nosslverify")) {
1191: while (my $fname=readdir($dh)) {
1192: next if ($fname =~ /^\.+$/);
1193: if (unlink("/home/httpd/sockets/nosslverify/$fname")) {
1194: &log($fh,"Unlinking $fname<br />");
1195: $unlinked{$fname} = 1;
1196: }
1197: }
1198: closedir($dh);
1199: }
1200: }
1201: &log($fh,"<p>Removed ".scalar(keys(%unlinked))." nosslverify clients</p>");
1202: return %unlinked;
1203: }
1204: sub clean_lonc_childpids {
1205: my $childpiddir = "$perlvar{'lonDocRoot'}/lon-status/loncchld";
1206: if (-d $childpiddir) {
1207: if (opendir(my $dh,$childpiddir)) {
1208: while (my $fname=readdir($dh)) {
1209: next if ($fname =~ /^\.+$/);
1210: unlink("$childpiddir/$fname");
1211: }
1212: closedir($dh);
1213: }
1214: }
1215: }
1216:
1.104 raeburn 1217: sub write_connection_config {
1.113 raeburn 1218: my ($domconf,%connectssl,%changes);
1219: $domconf = &get_domain_config();
1.104 raeburn 1220: if (ref($domconf) eq 'HASH') {
1221: if (ref($domconf->{'ssl'}) eq 'HASH') {
1222: foreach my $connect ('connto','connfrom') {
1223: if (ref($domconf->{'ssl'}->{$connect}) eq 'HASH') {
1224: my ($sslreq,$sslnoreq,$currsetting);
1225: my %contypes;
1226: foreach my $type ('dom','intdom','other') {
1227: $connectssl{$connect.'_'.$type} = $domconf->{'ssl'}->{$connect}->{$type};
1228: }
1229: }
1230: }
1231: }
1232: if (keys(%connectssl)) {
1.107 raeburn 1233: my %currconf;
1234: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
1235: while (my $line = <$fh>) {
1236: chomp($line);
1237: my ($name,$value) = split(/=/,$line);
1238: if ($value =~ /^(?:no|yes|req)$/) {
1239: if ($name =~ /^conn(to|from)_(dom|intdom|other)$/) {
1240: $currconf{$name} = $value;
1241: }
1242: }
1243: }
1244: close($fh);
1245: }
1246: if (open(my $fh,'>',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
1.104 raeburn 1247: my $count = 0;
1248: foreach my $key (sort(keys(%connectssl))) {
1249: print $fh "$key=$connectssl{$key}\n";
1.107 raeburn 1250: if (exists($currconf{$key})) {
1251: unless ($currconf{$key} eq $connectssl{$key}) {
1252: $changes{$key} = 1;
1253: }
1254: } else {
1255: $changes{$key} = 1;
1256: }
1.104 raeburn 1257: $count ++;
1258: }
1259: close($fh);
1260: print "Completed writing SSL options for lonc/lond for $count items.\n";
1261: }
1262: } else {
1263: print "Writing of SSL options skipped - no connection rules in domain configuration.\n";
1264: }
1265: } else {
1266: print "Retrieval of SSL options for lonc/lond skipped - no configuration data available for domain.\n";
1267: }
1.107 raeburn 1268: return %changes;
1.104 raeburn 1269: }
1270:
1271: sub get_domain_config {
1.113 raeburn 1272: my ($dom,$primlibserv,$isprimary,$url,%confhash);
1273: $dom = $perlvar{'lonDefDomain'};
1274: $primlibserv = &Apache::lonnet::domain($dom,'primary');
1275: if ($primlibserv eq $perlvar{'lonHostID'}) {
1276: $isprimary = 1;
1277: } elsif ($primlibserv ne '') {
1278: my $protocol = $Apache::lonnet::protocol{$primlibserv};
1279: my $hostname = &Apache::lonnet::hostname($primlibserv);
1280: unless ($protocol eq 'https') {
1281: $protocol = 'http';
1282: }
1.116 raeburn 1283: $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl?primary='.$primlibserv.'&format=raw';
1.113 raeburn 1284: }
1.104 raeburn 1285: if ($isprimary) {
1286: my $lonusersdir = $perlvar{'lonUsersDir'};
1287: my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
1288: if (-e $fname) {
1289: my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
1290: if (ref($dbref) eq 'HASH') {
1291: foreach my $key (sort(keys(%{$dbref}))) {
1292: my $value = $dbref->{$key};
1293: if ($value =~ s/^__FROZEN__//) {
1294: $value = thaw(&LONCAPA::unescape($value));
1295: } else {
1296: $value = &LONCAPA::unescape($value);
1297: }
1298: $confhash{$key} = $value;
1299: }
1300: &LONCAPA::locking_hash_untie($dbref);
1301: }
1302: }
1303: } else {
1.116 raeburn 1304: my $request=new HTTP::Request('GET',$url);
1305: my $response=&LONCAPA::LWPReq::makerequest($primlibserv,$request,'',\%perlvar,5);
1306: unless ($response->is_error()) {
1307: my $content = $response->content;
1308: if ($content) {
1309: my @pairs=split(/\&/,$content);
1.104 raeburn 1310: foreach my $item (@pairs) {
1311: my ($key,$value)=split(/=/,$item,2);
1312: my $what = &LONCAPA::unescape($key);
1313: if ($value =~ s/^__FROZEN__//) {
1314: $value = thaw(&LONCAPA::unescape($value));
1315: } else {
1316: $value = &LONCAPA::unescape($value);
1317: }
1318: $confhash{$what}=$value;
1319: }
1320: }
1321: }
1322: }
1323: return \%confhash;
1324: }
1325:
1326: sub write_hosttypes {
1327: my %intdom = &Apache::lonnet::all_host_intdom();
1328: my %hostdom = &Apache::lonnet::all_host_domain();
1329: my $dom = $hostdom{$perlvar{'lonHostID'}};
1330: my $internetdom = $intdom{$perlvar{'lonHostID'}};
1.107 raeburn 1331: my %changes;
1.104 raeburn 1332: if (($dom ne '') && ($internetdom ne '')) {
1333: if (keys(%hostdom)) {
1.107 raeburn 1334: my %currhosttypes;
1335: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
1336: while (my $line = <$fh>) {
1337: chomp($line);
1338: my ($name,$value) = split(/:/,$line);
1339: if (($name ne '') && ($value =~ /^(dom|intdom|other)$/)) {
1340: $currhosttypes{$name} = $value;
1341: }
1342: }
1343: close($fh);
1344: }
1345: if (open(my $fh,'>',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
1.104 raeburn 1346: my $count = 0;
1347: foreach my $lonid (sort(keys(%hostdom))) {
1348: my $type = 'other';
1349: if ($hostdom{$lonid} eq $dom) {
1.125 raeburn 1350: $type = 'dom';
1.104 raeburn 1351: } elsif ($intdom{$lonid} eq $internetdom) {
1352: $type = 'intdom';
1353: }
1354: print $fh "$lonid:$type\n";
1.107 raeburn 1355: if (exists($currhosttypes{$lonid})) {
1356: if ($type ne $currhosttypes{$lonid}) {
1357: $changes{$lonid} = 1;
1358: }
1359: } else {
1360: $changes{$lonid} = 1;
1361: }
1.104 raeburn 1362: $count ++;
1363: }
1364: close($fh);
1365: print "Completed writing host type data for $count hosts.\n";
1366: }
1367: } else {
1368: print "Writing of host types skipped - no hosts found.\n";
1369: }
1370: } else {
1371: print "Writing of host types skipped - could not determine this host's LON-CAPA domain or 'internet' domain.\n";
1372: }
1.107 raeburn 1373: return %changes;
1.104 raeburn 1374: }
1375:
1.106 raeburn 1376: sub update_revocation_list {
1.107 raeburn 1377: my ($result,$changed) = &Apache::lonnet::fetch_crl_pemfile();
1378: if ($result eq 'ok') {
1.106 raeburn 1379: print "Certificate Revocation List (from CA) updated.\n";
1380: } else {
1381: print "Certificate Revocation List from (CA) not updated.\n";
1382: }
1.107 raeburn 1383: return $changed;
1384: }
1385:
1386: sub reset_nosslverify_pids {
1.108 raeburn 1387: my ($fh,%sslrem) = @_;
1.107 raeburn 1388: &checkon_daemon($fh,'lond',40000,'USR2');
1389: my $loncpidfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1390: my $loncppid;
1391: if ((-e $loncpidfile) && (open(my $pfh,'<',$loncpidfile))) {
1392: $loncppid=<$pfh>;
1393: chomp($loncppid);
1394: close($pfh);
1395: if ($loncppid =~ /^\d+$/) {
1396: my %pids_by_host;
1397: my $docdir = $perlvar{'lonDocRoot'};
1398: if (-d "$docdir/lon-status/loncchld") {
1399: if (opendir(my $dh,"$docdir/lon-status/loncchld")) {
1400: while (my $file = readdir($dh)) {
1401: next if ($file =~ /^\./);
1402: if (open(my $fh,'<',"$docdir/lon-status/loncchld/$file")) {
1403: my $record = <$fh>;
1404: chomp($record);
1405: close($fh);
1406: my ($remotehost,$authmode) = split(/:/,$record);
1407: $pids_by_host{$remotehost}{$authmode}{$file} = 1;
1408: }
1409: }
1410: closedir($dh);
1411: if (keys(%pids_by_host)) {
1412: foreach my $host (keys(%pids_by_host)) {
1413: if ($sslrem{$host}) {
1414: if (ref($pids_by_host{$host}) eq 'HASH') {
1415: if (ref($pids_by_host{$host}{'insecure'}) eq 'HASH') {
1.109 raeburn 1416: if (keys(%{$pids_by_host{$host}{'insecure'}})) {
1417: foreach my $pid (keys(%{$pids_by_host{$host}{'insecure'}})) {
1.107 raeburn 1418: if (open(PIPE,"ps -o ppid= -p $pid |")) {
1419: my $ppid = <PIPE>;
1420: chomp($ppid);
1421: close(PIPE);
1422: $ppid =~ s/(^\s+|\s+$)//g;
1423: if (($ppid == $loncppid) && (kill 0 => $pid)) {
1424: kill QUIT => $pid;
1425: }
1426: }
1427: }
1428: }
1429: }
1430: }
1431: }
1432: }
1433: }
1434: }
1435: }
1436: }
1437: }
1438: return;
1.106 raeburn 1439: }
1440:
1.114 raeburn 1441: sub get_permcount_settings {
1442: my ($domconf) = @_;
1443: my ($defaults,$names) = &Apache::loncommon::lon_status_items();
1444: my (%weights,$threshold,$sysmail,$reportstatus,%exclusions);
1445: foreach my $type ('E','W','N','U') {
1446: $weights{$type} = $defaults->{$type};
1447: }
1448: $threshold = $defaults->{'threshold'};
1449: $sysmail = $defaults->{'sysmail'};
1450: $reportstatus = 1;
1451: if (ref($domconf) eq 'HASH') {
1452: if (ref($domconf->{'contacts'}) eq 'HASH') {
1453: if ($domconf->{'contacts'}{'reportstatus'} == 0) {
1454: $reportstatus = 0;
1455: }
1456: if (ref($domconf->{'contacts'}{'lonstatus'}) eq 'HASH') {
1457: if (ref($domconf->{'contacts'}{'lonstatus'}{weights}) eq 'HASH') {
1458: foreach my $type ('E','W','N','U') {
1459: if (exists($domconf->{'contacts'}{'lonstatus'}{weights}{$type})) {
1460: $weights{$type} = $domconf->{'contacts'}{'lonstatus'}{weights}{$type};
1461: }
1462: }
1463: }
1464: if (ref($domconf->{'contacts'}{'lonstatus'}{'excluded'}) eq 'ARRAY') {
1465: my @excluded = @{$domconf->{'contacts'}{'lonstatus'}{'excluded'}};
1466: if (@excluded) {
1467: map { $exclusions{$_} = 1; } @excluded;
1468: }
1469: }
1470: if (exists($domconf->{'contacts'}{'lonstatus'}{'threshold'})) {
1471: $threshold = $domconf->{'contacts'}{'lonstatus'}{'threshold'};
1472: }
1473: if (exists($domconf->{'contacts'}{'lonstatus'}{'sysmail'})) {
1474: $sysmail = $domconf->{'contacts'}{'lonstatus'}{'sysmail'};
1475: }
1476: }
1477: }
1478: }
1479: return ($threshold,$sysmail,$reportstatus,\%weights,\%exclusions);
1480: }
1481:
1482: sub read_serverhomeIDs {
1483: my %server;
1484: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1485: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1486: while (<$fh>) {
1487: my($host,$id) = split(/:/);
1488: chomp($id);
1489: $server{$host} = $id;
1490: }
1491: close($fh);
1492: }
1493: }
1494: return %server;
1495: }
1496:
1.130 raeburn 1497: sub check_bash_settings {
1498: my $distro = &LONCAPA::distro();
1499: my ($check_bracketed_paste,$bracketed_warning);
1500: if ($distro =~ /^debian(\d+)$/) {
1501: if ($1 >= 12) {
1502: $check_bracketed_paste = 1;
1503: }
1504: } elsif ($distro =~ /^ubuntu(\d+)$/) {
1505: if ($1 >= 22) {
1506: $check_bracketed_paste = 1;
1507: }
1508: } elsif ($distro =~ /^(?:redhat|oracle|alma|rocky|centos-stream)(\d+)$/) {
1509: if ($1 >= 9) {
1510: $check_bracketed_paste = 1;
1511: }
1512: } elsif ($distro =~ /^fedora(\d+)/) {
1513: if ($1 >= 34) {
1514: $check_bracketed_paste = 1;
1515: }
1516: }
1517: if ($check_bracketed_paste) {
1518: if (open(PIPE,"bind -V 2>&1 | grep enable-bracketed-paste |")) {
1519: my $info = <PIPE>;
1520: chomp($info);
1521: my ($bracketed) = ($info =~ /^\Qenable-bracketed-paste\E\s+is\s+set\s+to\s+\W(on|off)\W$/);
1522: close(PIPE);
1523: if ($bracketed eq 'on') {
1524: $bracketed_warning = 1;
1525: }
1526: } else {
1527: print "Unable to check if bracketed paste is set to off for www user's shell\n";
1528: }
1529: }
1530: return ($bracketed_warning,$check_bracketed_paste);
1531: }
1532:
1533: sub set_bracketed_paste_off {
1534: my $bash_www_cnf = '/home/www/.inputrc';
1535: my $result;
1536: if (!-e $bash_www_cnf) {
1537: system("touch $bash_www_cnf");
1538: if (open(my $cfh,'>',$bash_www_cnf)) {
1.132 raeburn 1539: print $cfh <<'END';
1540: $if R
1541: set enable-bracketed-paste off
1542: $endif
1543:
1544: $if maxima
1545: set enable-bracketed-paste off
1546: $endif
1547: END
1.130 raeburn 1548: close($cfh);
1.132 raeburn 1549: $result = "Updated $bash_www_cnf so enable-bracketed-paste is off for R bash shell";
1.130 raeburn 1550: } else {
1551: $result = "Could not open $bash_www_cnf to add 'set enable-bracketed-paste to off'";
1552: }
1553: my $wwwuid = getpwnam('www');
1554: my $wwwgid = getgrnam('www');
1555: if ($wwwuid!=$<) {
1556: chown($wwwuid,$wwwgid,$bash_www_cnf);
1557: }
1558: } else {
1.132 raeburn 1559: my (%bracketed_paste_on,%bracketed_paste_off,@preserve,$condition);
1560: $condition = '';
1.130 raeburn 1561: if (open(my $cfh,'<',$bash_www_cnf)) {
1562: while (my $line=<$cfh>) {
1563: chomp($line);
1.132 raeburn 1564: if ($line =~ /^\$if\s+(\w+)\s*$/) {
1565: if ($1 eq 'R') {
1566: $condition = 'r';
1567: } elsif ($1 eq 'maxima') {
1568: $condition = 'maxima';
1569: } else {
1570: $condition = 'other';
1571: }
1572: } elsif ($line =~ /^\$endif\s*$/) {
1573: $condition = '';
1574: }
1.130 raeburn 1575: if ($line =~ /^\s*set\s+enable\-bracketed\-paste\s+(off|on)\s*$/) {
1576: if ($1 eq 'off') {
1.132 raeburn 1577: if ($condition ne '') {
1578: $bracketed_paste_off{$condition} = 1;
1579: } else {
1580: $bracketed_paste_off{all} = 1;
1581: }
1582: push(@preserve,$line);
1.130 raeburn 1583: } else {
1.132 raeburn 1584: if ($condition ne '') {
1585: $bracketed_paste_on{$condition} = 1;
1586: if (($condition eq 'r') || ($condition eq 'maxima')) {
1587: push(@preserve,' set enable-bracketed-paste off');
1588: } else {
1589: push(@preserve,$line);
1590: }
1591: } else {
1592: $bracketed_paste_on{all} = 1;
1593: push(@preserve,$line);
1594: }
1.130 raeburn 1595: }
1596: } else {
1597: push(@preserve,$line);
1598: }
1599: }
1600: close($cfh);
1.132 raeburn 1601: } else {
1602: $result = "Could not open $bash_www_cnf to check if a value is included for 'enable-bracketed-paste'.";
1603: }
1604: if (($bracketed_paste_on{r} || $bracketed_paste_on{maxima}) ||
1605: (!exists($bracketed_paste_off{r}) && !exists($bracketed_paste_on{r}) &&
1606: !exists($bracketed_paste_off{maxima}) && !exists($bracketed_paste_on{maxima}))) {
1607: if (open(my $cfh,'>',$bash_www_cnf)) {
1608: if (@preserve) {
1609: foreach my $entry (@preserve) {
1610: print $cfh "$entry\n";
1611: }
1612: if (!exists($bracketed_paste_off{r}) && !exists($bracketed_paste_on{r})) {
1613: print $cfh <<'END';
1614: $if R
1615: set enable-bracketed-paste off
1616: $endif
1617: END
1618: }
1619: if (!exists($bracketed_paste_off{r}) && !exists($bracketed_paste_on{r})) {
1620: print $cfh <<'END';
1621: $if maxima
1622: set enable-bracketed-paste off
1623: $endif
1624: END
1.130 raeburn 1625: }
1626: } else {
1.132 raeburn 1627: print $cfh <<'END';
1628: $if R
1629: set enable-bracketed-paste off
1630: $endif
1631:
1632: $if maxima
1633: set enable-bracketed-paste off
1634: $endif
1635: END
1.130 raeburn 1636: }
1.132 raeburn 1637: close($cfh);
1638: $result = "Updated $bash_www_cnf";
1.130 raeburn 1639: } else {
1.132 raeburn 1640: $result = "Could not open $bash_www_cnf to add 'set enable-bracketed-paste to off'";
1.130 raeburn 1641: }
1642: } else {
1.132 raeburn 1643: $result = "No action needed; $bash_www_cnf already includes 'set enable-bracketed-paste to off'";
1.130 raeburn 1644: }
1645: }
1646: return $result;
1647: }
1648:
1.46 albertel 1649: sub send_mail {
1.113 raeburn 1650: my ($sysmail,$reportstatus) = @_;
1.79 raeburn 1651: my $defdom = $perlvar{'lonDefDomain'};
1652: my $origmail = $perlvar{'lonAdmEMail'};
1.78 raeburn 1653: my $emailto = &Apache::loncommon::build_recipient_list(undef,
1654: 'lonstatusmail',$defdom,$origmail);
1.113 raeburn 1655: if (($totalcount>$sysmail) && ($reportstatus)) {
1.43 albertel 1656: $emailto.=",$perlvar{'lonSysEMail'}";
1657: }
1.101 raeburn 1658: my $from;
1659: my $hostname=`/bin/hostname`;
1660: chop($hostname);
1661: $hostname=~s/[^\w\.]//g;
1662: if ($hostname) {
1663: $from = 'www@'.$hostname;
1664: }
1665: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
1666: my $loncronmail = "To: $emailto\n".
1667: "From: $from\n".
1668: "Subject: ".$subj."\n".
1669: "Content-type: text/html\; charset=UTF-8\n".
1670: "MIME-Version: 1.0\n\n";
1671: if (open(my $fh,"<$statusdir/index.html")) {
1672: while (<$fh>) {
1673: $loncronmail .= $_;
1674: }
1675: close($fh);
1676: } else {
1677: $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
1678: }
1679: $loncronmail .= "\n\n";
1680: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
1681: print $mailh $loncronmail;
1682: close($mailh);
1683: print "Sending mail.\n";
1684: } else {
1685: print "Sending mail failed.\n";
1.52 albertel 1686: }
1.1 albertel 1687: }
1.46 albertel 1688:
1.49 albertel 1689: sub usage {
1690: print(<<USAGE);
1.100 bisitz 1691: loncron - housekeeping program that checks up on various parts of LON-CAPA
1.49 albertel 1692:
1693: Options:
1.71 albertel 1694: --help Display
1.49 albertel 1695: --noemail Do not send the status email
1696: --justcheckconnections Only check the current status of the lonc/d
1697: connections, do not send emails do not
1698: check if the daemons are running, do not
1699: generate lon-status
1700: --justcheckdaemons Only check that all of the Lon-CAPA daemons are
1701: running, do not send emails do not
1702: check the lonc/d connections, do not
1703: generate lon-status
1.59 albertel 1704: --justreload Only tell the daemons to reload the config files,
1705: do not send emails do not
1706: check if the daemons are running, do not
1707: generate lon-status
1.118 raeburn 1708: --justiptables Only update the dynamic iptables rules for the
1709: lond port; do not send emails, do not
1710: check if the daemons are running, do not
1711: generate lon-status
1.49 albertel 1712: USAGE
1713: }
1714:
1.46 albertel 1715: # ================================================================ Main Program
1716: sub main () {
1.71 albertel 1717: my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.118 raeburn 1718: $justreload,$justiptables);
1.49 albertel 1719: &GetOptions("help" => \$help,
1720: "justcheckdaemons" => \$justcheckdaemons,
1721: "noemail" => \$noemail,
1.59 albertel 1722: "justcheckconnections" => \$justcheckconnections,
1.118 raeburn 1723: "justreload" => \$justreload,
1724: "justiptables" => \$justiptables
1.49 albertel 1725: );
1726: if ($help) { &usage(); return; }
1.46 albertel 1727: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
1728: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1729: %perlvar=%{$perlvarref};
1730: undef $perlvarref;
1731: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
1732: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.75 albertel 1733: chdir($perlvar{'lonDaemons'});
1.46 albertel 1734: # --------------------------------------- Make sure that LON-CAPA is configured
1735: # I only test for one thing here (lonHostID). This is just a safeguard.
1736: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
1737: print("Unconfigured machine.\n");
1738: my $emailto=$perlvar{'lonSysEMail'};
1.123 raeburn 1739: my $hostname = Sys::Hostname::FQDN::fqdn();
1740: $hostname=~s/\.+/./g;
1741: $hostname=~s/\-+/-/g;
1742: $hostname=~s/[^\w\.-]//g; # make sure is safe to pass through shell
1.46 albertel 1743: my $subj="LON: Unconfigured machine $hostname";
1.112 raeburn 1744: system("echo 'Unconfigured machine $hostname.' |".
1745: " mail -s '$subj' $emailto > /dev/null");
1.46 albertel 1746: exit 1;
1747: }
1748:
1749: # ----------------------------- Make sure this process is running from user=www
1750: my $wwwid=getpwnam('www');
1751: if ($wwwid!=$<) {
1.100 bisitz 1752: print("User ID mismatch. This program must be run as user 'www'.\n");
1.129 raeburn 1753: my $emailto="$perlvar{'lonAdmEMail'} $perlvar{'lonSysEMail'}";
1.46 albertel 1754: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.112 raeburn 1755: system("echo 'User ID mismatch. loncron must be run as user www.' |".
1756: " mail -s '$subj' $emailto > /dev/null");
1.46 albertel 1757: exit 1;
1758: }
1759:
1.72 albertel 1760: # -------------------------------------------- Force reload of host information
1.103 raeburn 1761: my $nomemcache;
1762: if ($justcheckdaemons) {
1763: $nomemcache=1;
1764: my $memcachepidfile="$perlvar{'lonDaemons'}/logs/memcached.pid";
1765: my $memcachepid;
1766: if (-e $memcachepidfile) {
1767: my $memfh=IO::File->new($memcachepidfile);
1768: $memcachepid=<$memfh>;
1769: chomp($memcachepid);
1770: if ($memcachepid =~ /^\d+$/ && kill 0 => $memcachepid) {
1771: undef($nomemcache);
1772: }
1773: }
1774: }
1.118 raeburn 1775: if (!$justiptables) {
1776: &Apache::lonnet::load_hosts_tab(1,$nomemcache);
1777: &Apache::lonnet::load_domain_tab(1,$nomemcache);
1778: &Apache::lonnet::get_iphost(1,$nomemcache);
1779: }
1.46 albertel 1780:
1.125 raeburn 1781: # ----------------------------------------- Force firewall update for lond port
1.81 raeburn 1782:
1783: if ((!$justcheckdaemons) && (!$justreload)) {
1784: my $now = time;
1785: my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
1786: $now.$$.int(rand(10000));
1787: if (open(my $fh,">$tmpfile")) {
1788: my %iphosts = &Apache::lonnet::get_iphost();
1789: foreach my $key (keys(%iphosts)) {
1790: print $fh "$key\n";
1791: }
1792: close($fh);
1.89 raeburn 1793: if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
1794: my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
1795: system("$execpath $tmpfile");
1.125 raeburn 1796: unlink('/tmp/lock_lciptables'); # Remove the lock file.
1.89 raeburn 1797: }
1.88 raeburn 1798: unlink($tmpfile);
1.81 raeburn 1799: }
1800: }
1801:
1.46 albertel 1802: # ---------------------------------------------------------------- Start report
1803:
1804: $errors=0;
1805: $warnings=0;
1806: $notices=0;
1807:
1.125 raeburn 1808:
1.49 albertel 1809: my $fh;
1.118 raeburn 1810: if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.72 albertel 1811: $fh=&start_logging();
1.49 albertel 1812:
1813: &log_machine_info($fh);
1814: &clean_tmp($fh);
1815: &clean_lonIDs($fh);
1.115 raeburn 1816: &clean_balanceIDs($fh);
1.102 raeburn 1817: &clean_webDAV_sessionIDs($fh);
1.119 raeburn 1818: &clean_ltiIDs($fh);
1.49 albertel 1819: &check_httpd_logs($fh);
1820: &rotate_lonnet_logs($fh);
1.73 albertel 1821: &rotate_other_logs($fh);
1.49 albertel 1822: }
1.118 raeburn 1823: if (!$justcheckconnections && !$justreload && !$justiptables) {
1.76 albertel 1824: &checkon_daemon($fh,'lonmemcached',40000);
1.49 albertel 1825: &checkon_daemon($fh,'lonsql',200000);
1.63 albertel 1826: if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
1827: &checkon_daemon($fh,'lond',40000,'USR2');
1828: }
1.71 albertel 1829: &checkon_daemon($fh,'lonc',40000,'USR1');
1.70 raeburn 1830: &checkon_daemon($fh,'lonmaxima',40000);
1.80 www 1831: &checkon_daemon($fh,'lonr',40000);
1.49 albertel 1832: }
1.59 albertel 1833: if ($justreload) {
1.107 raeburn 1834: &clean_nosslverify($fh);
1.104 raeburn 1835: &write_connection_config();
1836: &write_hosttypes();
1.107 raeburn 1837: &update_revocation_list();
1.59 albertel 1838: &checkon_daemon($fh,'lond',40000,'USR2');
1.71 albertel 1839: &checkon_daemon($fh,'lonc',40000,'USR2');
1.59 albertel 1840: }
1.63 albertel 1841: if ($justcheckconnections) {
1.72 albertel 1842: &test_connections($fh);
1.49 albertel 1843: }
1.118 raeburn 1844: if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.130 raeburn 1845: my ($bracketed_warning,$check_bracketed_paste) = &check_bash_settings();
1846: if ($check_bracketed_paste) {
1.131 raeburn 1847: &log($fh,'<hr /><a name="bashconf" /><h2>bash readline config</h2><h3>Bracketed Paste</h3>'.
1.132 raeburn 1848: '<p>Distros using bash readline library 8.1 or later need bracketed paste disabled for the R bash shell for the www user so R commands sent to lonr daemon will be processed.</p>');
1849: my $bash_www_cnf = '/home/www/.inputrc';
1850: my $non_empty_conffile;
1851: unless ($bracketed_warning) {
1852: if (-e $bash_www_cnf) {
1853: my $filesize = (stat($bash_www_cnf))[7];
1854: if ($filesize > 0) {
1855: $non_empty_conffile = 1;
1856: }
1857: }
1858: }
1859: if (($bracketed_warning) || ($non_empty_conffile)) {
1.130 raeburn 1860: my $bash_update = &set_bracketed_paste_off();
1861: if ($bash_update) {
1862: &log($fh,'<p>'.$bash_update.'</p>'."\n");
1863: }
1864: } else {
1865: &log($fh,'<p>No action needed; /home/www/.inputrc already set.</p>'."\n");
1866: }
1867: } else {
1868: &log($fh,'<hr /><a name="bashconf" /><h2>bash readline config</h2><h3>Bracketed Paste</h3>'.
1869: '<p>No action needed for distros using pre-8.1 bash readline library</p>'."\n");
1870: }
1.114 raeburn 1871: my $domconf = &get_domain_config();
1872: my ($threshold,$sysmail,$reportstatus,$weightsref,$exclusionsref) =
1873: &get_permcount_settings($domconf);
1874: &check_delayed_msg($fh,$weightsref,$exclusionsref);
1.87 raeburn 1875: &write_loncaparevs();
1876: &write_serverhomeIDs();
1.97 raeburn 1877: &write_checksums();
1.117 raeburn 1878: &write_hostips();
1.107 raeburn 1879: my %sslrem = &clean_nosslverify($fh);
1880: my %conchgs = &write_connection_config();
1881: my %hosttypechgs = &write_hosttypes();
1882: my $hadcrlchg = &update_revocation_list();
1.108 raeburn 1883: if ((keys(%conchgs) > 0) || (keys(%hosttypechgs) > 0) ||
1.107 raeburn 1884: $hadcrlchg || (keys(%sslrem) > 0)) {
1885: &checkon_daemon($fh,'lond',40000,'USR2');
1.108 raeburn 1886: &reset_nosslverify_pids($fh,%sslrem);
1.107 raeburn 1887: }
1.114 raeburn 1888: &finish_logging($fh,$weightsref);
1889: &log_simplestatus();
1890: if ($totalcount>$threshold && !$noemail) { &send_mail($sysmail,$reportstatus); }
1.49 albertel 1891: }
1.46 albertel 1892: }
1893:
1894: &main();
1.1 albertel 1895: 1;
1896:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>