Annotation of loncom/loncron, revision 1.135
1.1 albertel 1: #!/usr/bin/perl
2:
1.47 albertel 3: # Housekeeping program, started by cron, loncontrol and loncron.pl
4: #
1.135 ! raeburn 5: # $Id: loncron,v 1.134 2024/10/24 19:48:51 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) {
1.135 ! raeburn 1033: if (open(my $fh,'>',"$perlvar{'lonTabDir'}/loncaparevs.tab")) {
1.99 raeburn 1034: print $fh $output;
1035: close($fh);
1036: &Apache::lonnet::load_loncaparevs();
1.135 ! raeburn 1037: } else {
! 1038: print "Could not write to $perlvar{'lonTabDir'}/loncaparevs.tab\n";
1.99 raeburn 1039: }
1040: }
1041: return;
1042: }
1043:
1044: sub write_serverhomeIDs {
1.100 bisitz 1045: print "Retrieving LON-CAPA lonHostID information.\n";
1.99 raeburn 1046: my %name_to_host = &Apache::lonnet::all_names();
1047: my $output;
1048: foreach my $name (sort(keys(%name_to_host))) {
1049: if ($name ne '') {
1050: if (ref($name_to_host{$name}) eq 'ARRAY') {
1051: my $serverhomeID;
1.90 raeburn 1052: eval {
1053: local $SIG{ ALRM } = sub { die "TIMEOUT" };
1054: alarm(10);
1.125 raeburn 1055: $serverhomeID =
1.99 raeburn 1056: &Apache::lonnet::get_server_homeID($name,1,'loncron');
1.90 raeburn 1057: alarm(0);
1058: };
1059: if ($@ && $@ =~ m/TIMEOUT/) {
1.99 raeburn 1060: print "Time out while contacting server: $name\n";
1.90 raeburn 1061: }
1.99 raeburn 1062: if ($serverhomeID ne '') {
1063: $output .= $name.':'.$serverhomeID."\n";
1064: } else {
1065: $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1.84 raeburn 1066: }
1067: }
1068: }
1069: }
1.99 raeburn 1070: if ($output) {
1.135 ! raeburn 1071: if (open(my $fh,'>',"$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1.99 raeburn 1072: print $fh $output;
1073: close($fh);
1074: &Apache::lonnet::load_serverhomeIDs();
1.135 ! raeburn 1075: } else {
! 1076: print "Could not write to $perlvar{'lonTabDir'}/serverhomeIDs.tab\n";
1.85 raeburn 1077: }
1078: }
1079: return;
1080: }
1081:
1.96 raeburn 1082: sub write_checksums {
1.98 raeburn 1083: my $distro = &LONCAPA::distro();
1.96 raeburn 1084: if ($distro) {
1085: print "Retrieving file version and checksumming.\n";
1.97 raeburn 1086: my $numchksums = 0;
1.96 raeburn 1087: my ($chksumsref,$versionsref) =
1.97 raeburn 1088: &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
1089: $perlvar{'lonLib'},
1090: $perlvar{'lonIncludes'},
1091: $perlvar{'lonTabDir'});
1.96 raeburn 1092: if (ref($chksumsref) eq 'HASH') {
1093: $numchksums = scalar(keys(%{$chksumsref}));
1094: }
1095: print "File version retrieved and checksumming completed for $numchksums files.\n";
1096: } else {
1097: print "File version retrieval and checksumming skipped - could not determine Linux distro.\n";
1098: }
1.97 raeburn 1099: return;
1.96 raeburn 1100: }
1101:
1.117 raeburn 1102: sub write_hostips {
1103: my $lontabdir = $perlvar{'lonTabDir'};
1104: my $defdom = $perlvar{'lonDefDomain'};
1105: my $lonhost = $perlvar{'lonHostID'};
1106: my $newfile = "$lontabdir/currhostips.tab";
1107: my $oldfile = "$lontabdir/prevhostips.tab";
1108: my (%prevhosts,%currhosts,%ipchange);
1109: if ((-e $newfile) && (-s $newfile)) {
1110: move($newfile,$oldfile);
1111: chmod(0644,$oldfile);
1112: if (open(my $fh,'<',$oldfile)) {
1113: while (my $line=<$fh>) {
1114: chomp($line);
1115: if ($line =~ /^([^:]+):([\d.]+)$/) {
1116: $prevhosts{$1} = $2;
1117: }
1118: }
1119: close($fh);
1120: }
1121: }
1122: my ($ip_info,$cached) =
1123: &Apache::lonnet::is_cached_new('iphost','iphost');
1124: if (!$cached) {
1125: &Apache::lonnet::get_iphost();
1126: ($ip_info,$cached) =
1127: &Apache::lonnet::is_cached_new('iphost','iphost');
1128: }
1129: if (ref($ip_info) eq 'ARRAY') {
1130: %currhosts = %{$ip_info->[1]};
1131: if (open(my $fh,'>',$newfile)) {
1132: foreach my $key (keys(%currhosts)) {
1133: print $fh "$key:$currhosts{$key}\n";
1134: }
1135: close($fh);
1136: chmod(0644,$newfile);
1.135 ! raeburn 1137: } else {
! 1138: print "Could not write to $lontabdir/currhostips.tab\n";
1.117 raeburn 1139: }
1140: }
1141: if (keys(%prevhosts) && keys(%currhosts)) {
1142: foreach my $key (keys(%prevhosts)) {
1143: unless ($currhosts{$key} eq $prevhosts{$key}) {
1.121 raeburn 1144: $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1.117 raeburn 1145: }
1146: }
1147: foreach my $key (keys(%currhosts)) {
1148: unless ($currhosts{$key} eq $prevhosts{$key}) {
1.121 raeburn 1149: $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1.117 raeburn 1150: }
1151: }
1152: }
1153: if (&Apache::lonnet::domain($defdom,'primary') eq $lonhost) {
1154: if (keys(%ipchange)) {
1155: if (open(my $fh,'>>',$perlvar{'lonDaemons'}.'/logs/hostip.log')) {
1156: print $fh "********************\n".localtime(time).' Changes --'."\n".
1.121 raeburn 1157: "| Hostname | Previous IP | New IP |\n".
1158: " --------------------------------- \n";
1.117 raeburn 1159: foreach my $hostname (sort(keys(%ipchange))) {
1.121 raeburn 1160: print $fh "| $hostname | $ipchange{$hostname} |\n";
1.117 raeburn 1161: }
1162: print $fh "\n*******************\n\n";
1163: close($fh);
1.135 ! raeburn 1164: } else {
! 1165: print "Could not write to $perlvar{'lonDaemons'}/logs/hostip.log\n";
1.117 raeburn 1166: }
1167: my $emailto = &Apache::loncommon::build_recipient_list(undef,
1168: 'hostipmail',$defdom);
1169: if ($emailto) {
1170: my $subject = "LON-CAPA Hostname to IP change ($perlvar{'lonHostID'})";
1171: my $chgmail = "To: $emailto\n".
1172: "Subject: $subject\n".
1173: "Content-type: text/plain\; charset=UTF-8\n".
1174: "MIME-Version: 1.0\n\n".
1175: "Host/IP changes\n".
1176: " \n".
1.121 raeburn 1177: "| Hostname | Previous IP | New IP |\n".
1178: " --------------------------------- \n";
1.117 raeburn 1179: foreach my $hostname (sort(keys(%ipchange))) {
1.121 raeburn 1180: $chgmail .= "| $hostname | $ipchange{$hostname} |\n";
1.117 raeburn 1181: }
1182: $chgmail .= "\n\n";
1183: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
1184: print $mailh $chgmail;
1185: close($mailh);
1186: print "Sending mail notification of hostname/IP changes.\n";
1187: }
1188: }
1189: }
1190: }
1191: return;
1192: }
1193:
1.107 raeburn 1194: sub clean_nosslverify {
1195: my ($fh) = @_;
1.125 raeburn 1196: my %unlinked;
1.107 raeburn 1197: if (-d "$perlvar{'lonSockDir'}/nosslverify") {
1198: if (opendir(my $dh,"$perlvar{'lonSockDir'}/nosslverify")) {
1199: while (my $fname=readdir($dh)) {
1200: next if ($fname =~ /^\.+$/);
1201: if (unlink("/home/httpd/sockets/nosslverify/$fname")) {
1202: &log($fh,"Unlinking $fname<br />");
1203: $unlinked{$fname} = 1;
1204: }
1205: }
1206: closedir($dh);
1207: }
1208: }
1209: &log($fh,"<p>Removed ".scalar(keys(%unlinked))." nosslverify clients</p>");
1210: return %unlinked;
1211: }
1212: sub clean_lonc_childpids {
1213: my $childpiddir = "$perlvar{'lonDocRoot'}/lon-status/loncchld";
1214: if (-d $childpiddir) {
1215: if (opendir(my $dh,$childpiddir)) {
1216: while (my $fname=readdir($dh)) {
1217: next if ($fname =~ /^\.+$/);
1218: unlink("$childpiddir/$fname");
1219: }
1220: closedir($dh);
1221: }
1222: }
1223: }
1224:
1.104 raeburn 1225: sub write_connection_config {
1.113 raeburn 1226: my ($domconf,%connectssl,%changes);
1227: $domconf = &get_domain_config();
1.104 raeburn 1228: if (ref($domconf) eq 'HASH') {
1229: if (ref($domconf->{'ssl'}) eq 'HASH') {
1230: foreach my $connect ('connto','connfrom') {
1231: if (ref($domconf->{'ssl'}->{$connect}) eq 'HASH') {
1232: my ($sslreq,$sslnoreq,$currsetting);
1233: my %contypes;
1234: foreach my $type ('dom','intdom','other') {
1235: $connectssl{$connect.'_'.$type} = $domconf->{'ssl'}->{$connect}->{$type};
1236: }
1237: }
1238: }
1239: }
1240: if (keys(%connectssl)) {
1.107 raeburn 1241: my %currconf;
1242: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
1243: while (my $line = <$fh>) {
1244: chomp($line);
1245: my ($name,$value) = split(/=/,$line);
1246: if ($value =~ /^(?:no|yes|req)$/) {
1247: if ($name =~ /^conn(to|from)_(dom|intdom|other)$/) {
1248: $currconf{$name} = $value;
1249: }
1250: }
1251: }
1252: close($fh);
1253: }
1254: if (open(my $fh,'>',"$perlvar{'lonTabDir'}/connectionrules.tab")) {
1.104 raeburn 1255: my $count = 0;
1256: foreach my $key (sort(keys(%connectssl))) {
1257: print $fh "$key=$connectssl{$key}\n";
1.107 raeburn 1258: if (exists($currconf{$key})) {
1259: unless ($currconf{$key} eq $connectssl{$key}) {
1260: $changes{$key} = 1;
1261: }
1262: } else {
1263: $changes{$key} = 1;
1264: }
1.104 raeburn 1265: $count ++;
1266: }
1267: close($fh);
1268: print "Completed writing SSL options for lonc/lond for $count items.\n";
1.135 ! raeburn 1269: } else {
! 1270: print "Could not write to $perlvar{'lonTabDir'}/connectionrules.tab\n";
1.104 raeburn 1271: }
1272: } else {
1273: print "Writing of SSL options skipped - no connection rules in domain configuration.\n";
1274: }
1275: } else {
1276: print "Retrieval of SSL options for lonc/lond skipped - no configuration data available for domain.\n";
1277: }
1.107 raeburn 1278: return %changes;
1.104 raeburn 1279: }
1280:
1281: sub get_domain_config {
1.113 raeburn 1282: my ($dom,$primlibserv,$isprimary,$url,%confhash);
1283: $dom = $perlvar{'lonDefDomain'};
1284: $primlibserv = &Apache::lonnet::domain($dom,'primary');
1285: if ($primlibserv eq $perlvar{'lonHostID'}) {
1286: $isprimary = 1;
1287: } elsif ($primlibserv ne '') {
1288: my $protocol = $Apache::lonnet::protocol{$primlibserv};
1289: my $hostname = &Apache::lonnet::hostname($primlibserv);
1290: unless ($protocol eq 'https') {
1291: $protocol = 'http';
1292: }
1.116 raeburn 1293: $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl?primary='.$primlibserv.'&format=raw';
1.113 raeburn 1294: }
1.104 raeburn 1295: if ($isprimary) {
1296: my $lonusersdir = $perlvar{'lonUsersDir'};
1297: my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
1298: if (-e $fname) {
1299: my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
1300: if (ref($dbref) eq 'HASH') {
1301: foreach my $key (sort(keys(%{$dbref}))) {
1302: my $value = $dbref->{$key};
1303: if ($value =~ s/^__FROZEN__//) {
1304: $value = thaw(&LONCAPA::unescape($value));
1305: } else {
1306: $value = &LONCAPA::unescape($value);
1307: }
1308: $confhash{$key} = $value;
1309: }
1310: &LONCAPA::locking_hash_untie($dbref);
1311: }
1312: }
1313: } else {
1.116 raeburn 1314: my $request=new HTTP::Request('GET',$url);
1315: my $response=&LONCAPA::LWPReq::makerequest($primlibserv,$request,'',\%perlvar,5);
1316: unless ($response->is_error()) {
1317: my $content = $response->content;
1318: if ($content) {
1319: my @pairs=split(/\&/,$content);
1.104 raeburn 1320: foreach my $item (@pairs) {
1321: my ($key,$value)=split(/=/,$item,2);
1322: my $what = &LONCAPA::unescape($key);
1323: if ($value =~ s/^__FROZEN__//) {
1324: $value = thaw(&LONCAPA::unescape($value));
1325: } else {
1326: $value = &LONCAPA::unescape($value);
1327: }
1328: $confhash{$what}=$value;
1329: }
1330: }
1331: }
1332: }
1333: return \%confhash;
1334: }
1335:
1336: sub write_hosttypes {
1337: my %intdom = &Apache::lonnet::all_host_intdom();
1338: my %hostdom = &Apache::lonnet::all_host_domain();
1339: my $dom = $hostdom{$perlvar{'lonHostID'}};
1340: my $internetdom = $intdom{$perlvar{'lonHostID'}};
1.107 raeburn 1341: my %changes;
1.104 raeburn 1342: if (($dom ne '') && ($internetdom ne '')) {
1343: if (keys(%hostdom)) {
1.107 raeburn 1344: my %currhosttypes;
1345: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
1346: while (my $line = <$fh>) {
1347: chomp($line);
1348: my ($name,$value) = split(/:/,$line);
1349: if (($name ne '') && ($value =~ /^(dom|intdom|other)$/)) {
1350: $currhosttypes{$name} = $value;
1351: }
1352: }
1353: close($fh);
1354: }
1355: if (open(my $fh,'>',"$perlvar{'lonTabDir'}/hosttypes.tab")) {
1.104 raeburn 1356: my $count = 0;
1357: foreach my $lonid (sort(keys(%hostdom))) {
1358: my $type = 'other';
1359: if ($hostdom{$lonid} eq $dom) {
1.125 raeburn 1360: $type = 'dom';
1.104 raeburn 1361: } elsif ($intdom{$lonid} eq $internetdom) {
1362: $type = 'intdom';
1363: }
1364: print $fh "$lonid:$type\n";
1.107 raeburn 1365: if (exists($currhosttypes{$lonid})) {
1366: if ($type ne $currhosttypes{$lonid}) {
1367: $changes{$lonid} = 1;
1368: }
1369: } else {
1370: $changes{$lonid} = 1;
1371: }
1.104 raeburn 1372: $count ++;
1373: }
1374: close($fh);
1375: print "Completed writing host type data for $count hosts.\n";
1.135 ! raeburn 1376: } else {
! 1377: print "Could not write to $perlvar{'lonTabDir'}/hosttypes.tab\n";
1.104 raeburn 1378: }
1379: } else {
1380: print "Writing of host types skipped - no hosts found.\n";
1381: }
1382: } else {
1383: print "Writing of host types skipped - could not determine this host's LON-CAPA domain or 'internet' domain.\n";
1384: }
1.107 raeburn 1385: return %changes;
1.104 raeburn 1386: }
1387:
1.106 raeburn 1388: sub update_revocation_list {
1.107 raeburn 1389: my ($result,$changed) = &Apache::lonnet::fetch_crl_pemfile();
1390: if ($result eq 'ok') {
1.106 raeburn 1391: print "Certificate Revocation List (from CA) updated.\n";
1392: } else {
1393: print "Certificate Revocation List from (CA) not updated.\n";
1394: }
1.107 raeburn 1395: return $changed;
1396: }
1397:
1398: sub reset_nosslverify_pids {
1.108 raeburn 1399: my ($fh,%sslrem) = @_;
1.107 raeburn 1400: &checkon_daemon($fh,'lond',40000,'USR2');
1401: my $loncpidfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1402: my $loncppid;
1403: if ((-e $loncpidfile) && (open(my $pfh,'<',$loncpidfile))) {
1404: $loncppid=<$pfh>;
1405: chomp($loncppid);
1406: close($pfh);
1407: if ($loncppid =~ /^\d+$/) {
1408: my %pids_by_host;
1409: my $docdir = $perlvar{'lonDocRoot'};
1410: if (-d "$docdir/lon-status/loncchld") {
1411: if (opendir(my $dh,"$docdir/lon-status/loncchld")) {
1412: while (my $file = readdir($dh)) {
1413: next if ($file =~ /^\./);
1414: if (open(my $fh,'<',"$docdir/lon-status/loncchld/$file")) {
1415: my $record = <$fh>;
1416: chomp($record);
1417: close($fh);
1418: my ($remotehost,$authmode) = split(/:/,$record);
1419: $pids_by_host{$remotehost}{$authmode}{$file} = 1;
1420: }
1421: }
1422: closedir($dh);
1423: if (keys(%pids_by_host)) {
1424: foreach my $host (keys(%pids_by_host)) {
1425: if ($sslrem{$host}) {
1426: if (ref($pids_by_host{$host}) eq 'HASH') {
1427: if (ref($pids_by_host{$host}{'insecure'}) eq 'HASH') {
1.109 raeburn 1428: if (keys(%{$pids_by_host{$host}{'insecure'}})) {
1429: foreach my $pid (keys(%{$pids_by_host{$host}{'insecure'}})) {
1.107 raeburn 1430: if (open(PIPE,"ps -o ppid= -p $pid |")) {
1431: my $ppid = <PIPE>;
1432: chomp($ppid);
1433: close(PIPE);
1434: $ppid =~ s/(^\s+|\s+$)//g;
1435: if (($ppid == $loncppid) && (kill 0 => $pid)) {
1436: kill QUIT => $pid;
1437: }
1438: }
1439: }
1440: }
1441: }
1442: }
1443: }
1444: }
1445: }
1446: }
1447: }
1448: }
1449: }
1450: return;
1.106 raeburn 1451: }
1452:
1.114 raeburn 1453: sub get_permcount_settings {
1454: my ($domconf) = @_;
1455: my ($defaults,$names) = &Apache::loncommon::lon_status_items();
1456: my (%weights,$threshold,$sysmail,$reportstatus,%exclusions);
1457: foreach my $type ('E','W','N','U') {
1458: $weights{$type} = $defaults->{$type};
1459: }
1460: $threshold = $defaults->{'threshold'};
1461: $sysmail = $defaults->{'sysmail'};
1462: $reportstatus = 1;
1463: if (ref($domconf) eq 'HASH') {
1464: if (ref($domconf->{'contacts'}) eq 'HASH') {
1465: if ($domconf->{'contacts'}{'reportstatus'} == 0) {
1466: $reportstatus = 0;
1467: }
1468: if (ref($domconf->{'contacts'}{'lonstatus'}) eq 'HASH') {
1469: if (ref($domconf->{'contacts'}{'lonstatus'}{weights}) eq 'HASH') {
1470: foreach my $type ('E','W','N','U') {
1471: if (exists($domconf->{'contacts'}{'lonstatus'}{weights}{$type})) {
1472: $weights{$type} = $domconf->{'contacts'}{'lonstatus'}{weights}{$type};
1473: }
1474: }
1475: }
1476: if (ref($domconf->{'contacts'}{'lonstatus'}{'excluded'}) eq 'ARRAY') {
1477: my @excluded = @{$domconf->{'contacts'}{'lonstatus'}{'excluded'}};
1478: if (@excluded) {
1479: map { $exclusions{$_} = 1; } @excluded;
1480: }
1481: }
1482: if (exists($domconf->{'contacts'}{'lonstatus'}{'threshold'})) {
1483: $threshold = $domconf->{'contacts'}{'lonstatus'}{'threshold'};
1484: }
1485: if (exists($domconf->{'contacts'}{'lonstatus'}{'sysmail'})) {
1486: $sysmail = $domconf->{'contacts'}{'lonstatus'}{'sysmail'};
1487: }
1488: }
1489: }
1490: }
1491: return ($threshold,$sysmail,$reportstatus,\%weights,\%exclusions);
1492: }
1493:
1494: sub read_serverhomeIDs {
1495: my %server;
1496: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1497: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1498: while (<$fh>) {
1499: my($host,$id) = split(/:/);
1500: chomp($id);
1501: $server{$host} = $id;
1502: }
1503: close($fh);
1504: }
1505: }
1506: return %server;
1507: }
1508:
1.130 raeburn 1509: sub check_bash_settings {
1510: my $distro = &LONCAPA::distro();
1511: my ($check_bracketed_paste,$bracketed_warning);
1512: if ($distro =~ /^debian(\d+)$/) {
1513: if ($1 >= 12) {
1514: $check_bracketed_paste = 1;
1515: }
1516: } elsif ($distro =~ /^ubuntu(\d+)$/) {
1517: if ($1 >= 22) {
1518: $check_bracketed_paste = 1;
1519: }
1520: } elsif ($distro =~ /^(?:redhat|oracle|alma|rocky|centos-stream)(\d+)$/) {
1521: if ($1 >= 9) {
1522: $check_bracketed_paste = 1;
1523: }
1524: } elsif ($distro =~ /^fedora(\d+)/) {
1525: if ($1 >= 34) {
1526: $check_bracketed_paste = 1;
1527: }
1528: }
1529: if ($check_bracketed_paste) {
1530: if (open(PIPE,"bind -V 2>&1 | grep enable-bracketed-paste |")) {
1531: my $info = <PIPE>;
1532: chomp($info);
1533: my ($bracketed) = ($info =~ /^\Qenable-bracketed-paste\E\s+is\s+set\s+to\s+\W(on|off)\W$/);
1534: close(PIPE);
1535: if ($bracketed eq 'on') {
1536: $bracketed_warning = 1;
1537: }
1538: } else {
1539: print "Unable to check if bracketed paste is set to off for www user's shell\n";
1540: }
1541: }
1542: return ($bracketed_warning,$check_bracketed_paste);
1543: }
1544:
1545: sub set_bracketed_paste_off {
1546: my $bash_www_cnf = '/home/www/.inputrc';
1547: my $result;
1548: if (!-e $bash_www_cnf) {
1549: system("touch $bash_www_cnf");
1550: if (open(my $cfh,'>',$bash_www_cnf)) {
1.132 raeburn 1551: print $cfh <<'END';
1552: $if R
1553: set enable-bracketed-paste off
1554: $endif
1555:
1556: $if maxima
1557: set enable-bracketed-paste off
1558: $endif
1559: END
1.130 raeburn 1560: close($cfh);
1.132 raeburn 1561: $result = "Updated $bash_www_cnf so enable-bracketed-paste is off for R bash shell";
1.130 raeburn 1562: } else {
1563: $result = "Could not open $bash_www_cnf to add 'set enable-bracketed-paste to off'";
1564: }
1565: my $wwwuid = getpwnam('www');
1566: my $wwwgid = getgrnam('www');
1567: if ($wwwuid!=$<) {
1568: chown($wwwuid,$wwwgid,$bash_www_cnf);
1569: }
1570: } else {
1.132 raeburn 1571: my (%bracketed_paste_on,%bracketed_paste_off,@preserve,$condition);
1572: $condition = '';
1.130 raeburn 1573: if (open(my $cfh,'<',$bash_www_cnf)) {
1574: while (my $line=<$cfh>) {
1575: chomp($line);
1.132 raeburn 1576: if ($line =~ /^\$if\s+(\w+)\s*$/) {
1577: if ($1 eq 'R') {
1578: $condition = 'r';
1579: } elsif ($1 eq 'maxima') {
1580: $condition = 'maxima';
1581: } else {
1582: $condition = 'other';
1583: }
1584: } elsif ($line =~ /^\$endif\s*$/) {
1585: $condition = '';
1586: }
1.130 raeburn 1587: if ($line =~ /^\s*set\s+enable\-bracketed\-paste\s+(off|on)\s*$/) {
1588: if ($1 eq 'off') {
1.132 raeburn 1589: if ($condition ne '') {
1590: $bracketed_paste_off{$condition} = 1;
1591: } else {
1592: $bracketed_paste_off{all} = 1;
1593: }
1594: push(@preserve,$line);
1.130 raeburn 1595: } else {
1.132 raeburn 1596: if ($condition ne '') {
1597: $bracketed_paste_on{$condition} = 1;
1598: if (($condition eq 'r') || ($condition eq 'maxima')) {
1599: push(@preserve,' set enable-bracketed-paste off');
1600: } else {
1601: push(@preserve,$line);
1602: }
1603: } else {
1604: $bracketed_paste_on{all} = 1;
1605: push(@preserve,$line);
1606: }
1.130 raeburn 1607: }
1608: } else {
1609: push(@preserve,$line);
1610: }
1611: }
1612: close($cfh);
1.132 raeburn 1613: } else {
1614: $result = "Could not open $bash_www_cnf to check if a value is included for 'enable-bracketed-paste'.";
1615: }
1616: if (($bracketed_paste_on{r} || $bracketed_paste_on{maxima}) ||
1617: (!exists($bracketed_paste_off{r}) && !exists($bracketed_paste_on{r}) &&
1618: !exists($bracketed_paste_off{maxima}) && !exists($bracketed_paste_on{maxima}))) {
1619: if (open(my $cfh,'>',$bash_www_cnf)) {
1620: if (@preserve) {
1621: foreach my $entry (@preserve) {
1622: print $cfh "$entry\n";
1623: }
1624: if (!exists($bracketed_paste_off{r}) && !exists($bracketed_paste_on{r})) {
1625: print $cfh <<'END';
1626: $if R
1627: set enable-bracketed-paste off
1628: $endif
1629: END
1630: }
1631: if (!exists($bracketed_paste_off{r}) && !exists($bracketed_paste_on{r})) {
1632: print $cfh <<'END';
1633: $if maxima
1634: set enable-bracketed-paste off
1635: $endif
1636: END
1.130 raeburn 1637: }
1638: } else {
1.132 raeburn 1639: print $cfh <<'END';
1640: $if R
1641: set enable-bracketed-paste off
1642: $endif
1643:
1644: $if maxima
1645: set enable-bracketed-paste off
1646: $endif
1647: END
1.130 raeburn 1648: }
1.132 raeburn 1649: close($cfh);
1650: $result = "Updated $bash_www_cnf";
1.130 raeburn 1651: } else {
1.132 raeburn 1652: $result = "Could not open $bash_www_cnf to add 'set enable-bracketed-paste to off'";
1.130 raeburn 1653: }
1654: } else {
1.132 raeburn 1655: $result = "No action needed; $bash_www_cnf already includes 'set enable-bracketed-paste to off'";
1.130 raeburn 1656: }
1657: }
1658: return $result;
1659: }
1660:
1.46 albertel 1661: sub send_mail {
1.113 raeburn 1662: my ($sysmail,$reportstatus) = @_;
1.79 raeburn 1663: my $defdom = $perlvar{'lonDefDomain'};
1664: my $origmail = $perlvar{'lonAdmEMail'};
1.78 raeburn 1665: my $emailto = &Apache::loncommon::build_recipient_list(undef,
1666: 'lonstatusmail',$defdom,$origmail);
1.113 raeburn 1667: if (($totalcount>$sysmail) && ($reportstatus)) {
1.43 albertel 1668: $emailto.=",$perlvar{'lonSysEMail'}";
1669: }
1.101 raeburn 1670: my $from;
1671: my $hostname=`/bin/hostname`;
1672: chop($hostname);
1673: $hostname=~s/[^\w\.]//g;
1674: if ($hostname) {
1675: $from = 'www@'.$hostname;
1676: }
1677: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
1678: my $loncronmail = "To: $emailto\n".
1679: "From: $from\n".
1680: "Subject: ".$subj."\n".
1681: "Content-type: text/html\; charset=UTF-8\n".
1682: "MIME-Version: 1.0\n\n";
1.135 ! raeburn 1683: if (open(my $fh,'<',$statusdir/index.html")) {
1.101 raeburn 1684: while (<$fh>) {
1685: $loncronmail .= $_;
1686: }
1687: close($fh);
1688: } else {
1689: $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
1690: }
1691: $loncronmail .= "\n\n";
1692: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
1693: print $mailh $loncronmail;
1694: close($mailh);
1695: print "Sending mail.\n";
1696: } else {
1697: print "Sending mail failed.\n";
1.52 albertel 1698: }
1.1 albertel 1699: }
1.46 albertel 1700:
1.49 albertel 1701: sub usage {
1702: print(<<USAGE);
1.100 bisitz 1703: loncron - housekeeping program that checks up on various parts of LON-CAPA
1.49 albertel 1704:
1705: Options:
1.71 albertel 1706: --help Display
1.49 albertel 1707: --noemail Do not send the status email
1708: --justcheckconnections Only check the current status of the lonc/d
1709: connections, do not send emails do not
1710: check if the daemons are running, do not
1711: generate lon-status
1712: --justcheckdaemons Only check that all of the Lon-CAPA daemons are
1713: running, do not send emails do not
1714: check the lonc/d connections, do not
1715: generate lon-status
1.59 albertel 1716: --justreload Only tell the daemons to reload the config files,
1717: do not send emails do not
1718: check if the daemons are running, do not
1719: generate lon-status
1.118 raeburn 1720: --justiptables Only update the dynamic iptables rules for the
1721: lond port; do not send emails, do not
1722: check if the daemons are running, do not
1723: generate lon-status
1.49 albertel 1724: USAGE
1725: }
1726:
1.46 albertel 1727: # ================================================================ Main Program
1728: sub main () {
1.71 albertel 1729: my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.118 raeburn 1730: $justreload,$justiptables);
1.49 albertel 1731: &GetOptions("help" => \$help,
1732: "justcheckdaemons" => \$justcheckdaemons,
1733: "noemail" => \$noemail,
1.59 albertel 1734: "justcheckconnections" => \$justcheckconnections,
1.118 raeburn 1735: "justreload" => \$justreload,
1736: "justiptables" => \$justiptables
1.49 albertel 1737: );
1738: if ($help) { &usage(); return; }
1.46 albertel 1739: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
1740: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1741: %perlvar=%{$perlvarref};
1742: undef $perlvarref;
1743: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
1744: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.75 albertel 1745: chdir($perlvar{'lonDaemons'});
1.46 albertel 1746: # --------------------------------------- Make sure that LON-CAPA is configured
1747: # I only test for one thing here (lonHostID). This is just a safeguard.
1748: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
1749: print("Unconfigured machine.\n");
1750: my $emailto=$perlvar{'lonSysEMail'};
1.123 raeburn 1751: my $hostname = Sys::Hostname::FQDN::fqdn();
1752: $hostname=~s/\.+/./g;
1753: $hostname=~s/\-+/-/g;
1754: $hostname=~s/[^\w\.-]//g; # make sure is safe to pass through shell
1.46 albertel 1755: my $subj="LON: Unconfigured machine $hostname";
1.112 raeburn 1756: system("echo 'Unconfigured machine $hostname.' |".
1757: " mail -s '$subj' $emailto > /dev/null");
1.46 albertel 1758: exit 1;
1759: }
1760:
1761: # ----------------------------- Make sure this process is running from user=www
1762: my $wwwid=getpwnam('www');
1763: if ($wwwid!=$<) {
1.100 bisitz 1764: print("User ID mismatch. This program must be run as user 'www'.\n");
1.129 raeburn 1765: my $emailto="$perlvar{'lonAdmEMail'} $perlvar{'lonSysEMail'}";
1.46 albertel 1766: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.112 raeburn 1767: system("echo 'User ID mismatch. loncron must be run as user www.' |".
1768: " mail -s '$subj' $emailto > /dev/null");
1.46 albertel 1769: exit 1;
1770: }
1771:
1.72 albertel 1772: # -------------------------------------------- Force reload of host information
1.103 raeburn 1773: my $nomemcache;
1774: if ($justcheckdaemons) {
1775: $nomemcache=1;
1776: my $memcachepidfile="$perlvar{'lonDaemons'}/logs/memcached.pid";
1777: my $memcachepid;
1778: if (-e $memcachepidfile) {
1779: my $memfh=IO::File->new($memcachepidfile);
1780: $memcachepid=<$memfh>;
1781: chomp($memcachepid);
1782: if ($memcachepid =~ /^\d+$/ && kill 0 => $memcachepid) {
1783: undef($nomemcache);
1784: }
1785: }
1786: }
1.118 raeburn 1787: if (!$justiptables) {
1788: &Apache::lonnet::load_hosts_tab(1,$nomemcache);
1789: &Apache::lonnet::load_domain_tab(1,$nomemcache);
1790: &Apache::lonnet::get_iphost(1,$nomemcache);
1791: }
1.46 albertel 1792:
1.125 raeburn 1793: # ----------------------------------------- Force firewall update for lond port
1.81 raeburn 1794:
1795: if ((!$justcheckdaemons) && (!$justreload)) {
1796: my $now = time;
1797: my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
1798: $now.$$.int(rand(10000));
1.135 ! raeburn 1799: if (open(my $fh,'>',"$tmpfile")) {
1.81 raeburn 1800: my %iphosts = &Apache::lonnet::get_iphost();
1801: foreach my $key (keys(%iphosts)) {
1802: print $fh "$key\n";
1803: }
1804: close($fh);
1.89 raeburn 1805: if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
1806: my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
1807: system("$execpath $tmpfile");
1.125 raeburn 1808: unlink('/tmp/lock_lciptables'); # Remove the lock file.
1.89 raeburn 1809: }
1.88 raeburn 1810: unlink($tmpfile);
1.81 raeburn 1811: }
1812: }
1813:
1.46 albertel 1814: # ---------------------------------------------------------------- Start report
1815:
1816: $errors=0;
1817: $warnings=0;
1818: $notices=0;
1819:
1.125 raeburn 1820:
1.49 albertel 1821: my $fh;
1.118 raeburn 1822: if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.72 albertel 1823: $fh=&start_logging();
1.49 albertel 1824:
1825: &log_machine_info($fh);
1826: &clean_tmp($fh);
1827: &clean_lonIDs($fh);
1.115 raeburn 1828: &clean_balanceIDs($fh);
1.102 raeburn 1829: &clean_webDAV_sessionIDs($fh);
1.119 raeburn 1830: &clean_ltiIDs($fh);
1.49 albertel 1831: &check_httpd_logs($fh);
1832: &rotate_lonnet_logs($fh);
1.73 albertel 1833: &rotate_other_logs($fh);
1.49 albertel 1834: }
1.118 raeburn 1835: if (!$justcheckconnections && !$justreload && !$justiptables) {
1.76 albertel 1836: &checkon_daemon($fh,'lonmemcached',40000);
1.49 albertel 1837: &checkon_daemon($fh,'lonsql',200000);
1.63 albertel 1838: if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
1839: &checkon_daemon($fh,'lond',40000,'USR2');
1840: }
1.71 albertel 1841: &checkon_daemon($fh,'lonc',40000,'USR1');
1.70 raeburn 1842: &checkon_daemon($fh,'lonmaxima',40000);
1.80 www 1843: &checkon_daemon($fh,'lonr',40000);
1.49 albertel 1844: }
1.59 albertel 1845: if ($justreload) {
1.107 raeburn 1846: &clean_nosslverify($fh);
1.104 raeburn 1847: &write_connection_config();
1848: &write_hosttypes();
1.107 raeburn 1849: &update_revocation_list();
1.59 albertel 1850: &checkon_daemon($fh,'lond',40000,'USR2');
1.71 albertel 1851: &checkon_daemon($fh,'lonc',40000,'USR2');
1.59 albertel 1852: }
1.63 albertel 1853: if ($justcheckconnections) {
1.72 albertel 1854: &test_connections($fh);
1.49 albertel 1855: }
1.118 raeburn 1856: if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1.130 raeburn 1857: my ($bracketed_warning,$check_bracketed_paste) = &check_bash_settings();
1858: if ($check_bracketed_paste) {
1.131 raeburn 1859: &log($fh,'<hr /><a name="bashconf" /><h2>bash readline config</h2><h3>Bracketed Paste</h3>'.
1.132 raeburn 1860: '<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>');
1861: my $bash_www_cnf = '/home/www/.inputrc';
1862: my $non_empty_conffile;
1863: unless ($bracketed_warning) {
1864: if (-e $bash_www_cnf) {
1865: my $filesize = (stat($bash_www_cnf))[7];
1866: if ($filesize > 0) {
1867: $non_empty_conffile = 1;
1868: }
1869: }
1870: }
1871: if (($bracketed_warning) || ($non_empty_conffile)) {
1.130 raeburn 1872: my $bash_update = &set_bracketed_paste_off();
1873: if ($bash_update) {
1874: &log($fh,'<p>'.$bash_update.'</p>'."\n");
1875: }
1876: } else {
1877: &log($fh,'<p>No action needed; /home/www/.inputrc already set.</p>'."\n");
1878: }
1879: } else {
1880: &log($fh,'<hr /><a name="bashconf" /><h2>bash readline config</h2><h3>Bracketed Paste</h3>'.
1881: '<p>No action needed for distros using pre-8.1 bash readline library</p>'."\n");
1882: }
1.114 raeburn 1883: my $domconf = &get_domain_config();
1884: my ($threshold,$sysmail,$reportstatus,$weightsref,$exclusionsref) =
1885: &get_permcount_settings($domconf);
1886: &check_delayed_msg($fh,$weightsref,$exclusionsref);
1.87 raeburn 1887: &write_loncaparevs();
1888: &write_serverhomeIDs();
1.97 raeburn 1889: &write_checksums();
1.117 raeburn 1890: &write_hostips();
1.107 raeburn 1891: my %sslrem = &clean_nosslverify($fh);
1892: my %conchgs = &write_connection_config();
1893: my %hosttypechgs = &write_hosttypes();
1894: my $hadcrlchg = &update_revocation_list();
1.108 raeburn 1895: if ((keys(%conchgs) > 0) || (keys(%hosttypechgs) > 0) ||
1.107 raeburn 1896: $hadcrlchg || (keys(%sslrem) > 0)) {
1897: &checkon_daemon($fh,'lond',40000,'USR2');
1.108 raeburn 1898: &reset_nosslverify_pids($fh,%sslrem);
1.107 raeburn 1899: }
1.114 raeburn 1900: &finish_logging($fh,$weightsref);
1901: &log_simplestatus();
1902: if ($totalcount>$threshold && !$noemail) { &send_mail($sysmail,$reportstatus); }
1.49 albertel 1903: }
1.46 albertel 1904: }
1905:
1906: &main();
1.1 albertel 1907: 1;
1908:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>