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