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