1: #!/usr/bin/perl
2:
3: # Housekeeping program, started by cron, loncontrol and loncron.pl
4: #
5: # $Id: loncron,v 1.103.2.9 2021/01/30 22:25:53 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 Apache::lonnet;
38: use Apache::loncommon;
39:
40: use LWP::UserAgent();
41: use HTTP::Request();
42: use IO::File;
43: use IO::Socket;
44: use HTML::Entities;
45: use Getopt::Long;
46: use File::ReadBackwards;
47: use File::Copy;
48: use Sys::Hostname::FQDN();
49:
50: #globals
51: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
52:
53: my $statusdir="/home/httpd/html/lon-status";
54:
55:
56: # --------------------------------------------------------- Output error status
57:
58: sub log {
59: my $fh=shift;
60: if ($fh) { print $fh @_ }
61: }
62:
63: sub errout {
64: my $fh=shift;
65: &log($fh,(<<ENDERROUT));
66: <table border="2" bgcolor="#CCCCCC">
67: <tr><td>Notices</td><td>$notices</td></tr>
68: <tr><td>Warnings</td><td>$warnings</td></tr>
69: <tr><td>Errors</td><td>$errors</td></tr>
70: </table><p><a href="#top">Top</a></p>
71: ENDERROUT
72: }
73:
74: sub rotate_logfile {
75: my ($file,$fh,$description) = @_;
76: my $size=(stat($file))[7];
77: if ($size>40000) {
78: &log($fh,"<p>Rotating $description ...</p>");
79: rename("$file.2","$file.3");
80: rename("$file.1","$file.2");
81: rename("$file","$file.1");
82: }
83: }
84:
85: sub start_daemon {
86: my ($fh,$daemon,$pidfile,$args) = @_;
87: my $progname=$daemon;
88: if ($daemon eq 'lonc') {
89: $progname='loncnew';
90: }
91: my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
92: &rotate_logfile($error_fname,$fh,'error logs');
93: if ($daemon eq 'lonc') {
94: &clean_sockets($fh);
95: }
96: system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
97: sleep 1;
98: if (-e $pidfile) {
99: &log($fh,"<p>Seems like it started ...</p>");
100: my $lfh=IO::File->new("$pidfile");
101: my $daemonpid=<$lfh>;
102: chomp($daemonpid);
103: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
104: return 1;
105: } else {
106: return 0;
107: }
108: }
109: &log($fh,"<p>Seems like that did not work!</p>");
110: $errors++;
111: return 0;
112: }
113:
114: sub checkon_daemon {
115: my ($fh,$daemon,$maxsize,$send,$args)=@_;
116:
117: my $result;
118: &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
119: printf("%-15s ",$daemon);
120: if ($fh) {
121: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
122: if (open(DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|")) {
123: while (my $line=<DFH>) {
124: &log($fh,"$line");
125: if ($line=~/INFO/) { $notices++; }
126: if ($line=~/WARNING/) { $notices++; }
127: if ($line=~/CRITICAL/) { $warnings++; }
128: }
129: close (DFH);
130: }
131: }
132: &log($fh,"</tt></p>");
133: }
134:
135: my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
136:
137: my $restartflag=1;
138: my $daemonpid;
139: if (-e $pidfile) {
140: my $lfh=IO::File->new("$pidfile");
141: $daemonpid=<$lfh>;
142: chomp($daemonpid);
143: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
144: &log($fh,"<h3>$daemon at pid $daemonpid responding");
145: if ($send) { &log($fh,", sending $send"); }
146: &log($fh,"</h3>");
147: if ($send eq 'USR1') { kill USR1 => $daemonpid; }
148: if ($send eq 'USR2') { kill USR2 => $daemonpid; }
149: $restartflag=0;
150: if ($send eq 'USR2') {
151: $result = 'reloaded';
152: print "reloaded\n";
153: } else {
154: $result = 'running';
155: print "running\n";
156: }
157: } else {
158: $errors++;
159: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
160: $restartflag=1;
161: &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
162: }
163: }
164: if ($restartflag==1) {
165: $simplestatus{$daemon}='off';
166: $errors++;
167: my $kadaemon=$daemon;
168: if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
169: &log($fh,'<br /><font color="red">Killall '.$daemon.': '.
170: `killall $kadaemon 2>&1`.' - ');
171: sleep 1;
172: &log($fh,unlink($pidfile).' - '.
173: `killall -9 $kadaemon 2>&1`.
174: '</font><br />');
175: &log($fh,"<h3>$daemon not running, trying to start</h3>");
176:
177: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
178: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
179: $simplestatus{$daemon}='restarted';
180: $result = 'started';
181: print "started\n";
182: } else {
183: $errors++;
184: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
185: &log($fh,"<p>Give it one more try ...</p>");
186: print " ";
187: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
188: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
189: $simplestatus{$daemon}='restarted';
190: $result = 'started';
191: print "started\n";
192: } else {
193: $result = 'failed';
194: print " failed\n";
195: $simplestatus{$daemon}='failed';
196: $errors++; $errors++;
197: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
198: &log($fh,"<p>Unable to start $daemon</p>");
199: }
200: }
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: }
214: }
215: }
216:
217: my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
218: &rotate_logfile($fname,$fh,'logs');
219:
220: &errout($fh);
221: return $result;
222: }
223:
224: # --------------------------------------------------------------------- Machine
225: sub log_machine_info {
226: my ($fh)=@_;
227: &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
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: }
244:
245: &log($fh,"<h3>df</h3>");
246: &log($fh,"<pre>");
247:
248: open (DFH,"df|");
249: while (my $line=<DFH>) {
250: &log($fh,&encode_entities($line,'<>&"'));
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++;
261: }
262: if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
263: }
264: close (DFH);
265: &log($fh,"</pre>");
266:
267:
268: &log($fh,"<h3>ps</h3>");
269: &log($fh,"<pre>");
270: my $psproc=0;
271:
272: open (PSH,"ps aux --cols 140 |");
273: while (my $line=<PSH>) {
274: &log($fh,&encode_entities($line,'<>&"'));
275: $psproc++;
276: }
277: close (PSH);
278: &log($fh,"</pre>");
279:
280: if ($psproc>200) { $notices++; }
281: if ($psproc>250) { $notices++; }
282:
283: &log($fh,"<h3>distprobe</h3>");
284: &log($fh,"<pre>");
285: &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
286: &log($fh,"</pre>");
287:
288: &errout($fh);
289: }
290:
291: sub start_logging {
292: my $fh=IO::File->new(">$statusdir/newstatus.html");
293: my %simplestatus=();
294: my $now=time;
295: my $date=localtime($now);
296:
297:
298: &log($fh,(<<ENDHEADERS));
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">
301: <head>
302: <title>LON Status Report $perlvar{'lonHostID'}</title>
303: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
304: </head>
305: <body bgcolor="#AAAAAA">
306: <a name="top" />
307: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
308: <h2>$date ($now)</h2>
309: <ol>
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>
314: <li><a href="#webdav">WebDAV Session Tokens</a></li>
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>
323: </ol>
324: <hr />
325: <a name="configuration" />
326: <h2>Configuration</h2>
327: <h3>PerlVars</h3>
328: <table border="2">
329: ENDHEADERS
330:
331: foreach my $varname (sort(keys(%perlvar))) {
332: &log($fh,"<tr><td>$varname</td><td>".
333: &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
334: }
335: &log($fh,"</table><h3>Hosts</h3><table border='2'>");
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');
340: &log($fh,
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: }
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");
358: }
359: return $fh;
360: }
361:
362: # --------------------------------------------------------------- clean out tmp
363: sub clean_tmp {
364: my ($fh)=@_;
365: &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
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);
485: }
486: return ($cleaned,$old,$removed);
487: }
488:
489: # ------------------------------------------------------------ clean out lonIDs
490: sub clean_lonIDs {
491: my ($fh)=@_;
492: &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
493: my $cleaned=0;
494: my $active=0;
495: while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
496: my $now=time;
497: if (-l $fname) {
498: my $linkfname = readlink($fname);
499: if (-f $linkfname) {
500: if ($linkfname =~ m{^$perlvar{'lonIDsDir'}/[^/]+\.id$}) {
501: my @data = stat($linkfname);
502: my $mtime = $data[9];
503: my $since=$now-$mtime;
504: if ($since>$perlvar{'lonExpire'}) {
505: if (unlink($linkfname)) {
506: $cleaned++;
507: &log($fh,"Unlinking $linkfname<br />");
508: unlink($fname);
509: }
510: }
511: }
512: } else {
513: unlink($fname);
514: }
515: } elsif (-f $fname) {
516: my @data = stat($fname);
517: my $mtime = $data[9];
518: my $since=$now-$mtime;
519: if ($since>$perlvar{'lonExpire'}) {
520: if (unlink($fname)) {
521: $cleaned++;
522: &log($fh,"Unlinking $fname<br />");
523: }
524: } else {
525: $active++;
526: }
527: }
528: }
529: &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
530: &log($fh,"<h3>$active open session(s)</h3>");
531: }
532:
533: # -------------------------------------------------------- clean out balanceIDs
534:
535: sub clean_balanceIDs {
536: my ($fh)=@_;
537: &log($fh,'<hr /><a name="balcookies" /><h2>Session Tokens</h2>');
538: my $cleaned=0;
539: my $active=0;
540: if (-d $perlvar{'lonBalanceDir'}) {
541: while (my $fname=<$perlvar{'lonBalanceDir'}/*.id>) {
542: my ($dev,$ino,$mode,$nlink,
543: $uid,$gid,$rdev,$size,
544: $atime,$mtime,$ctime,
545: $blksize,$blocks)=stat($fname);
546: my $now=time;
547: my $since=$now-$mtime;
548: if ($since>$perlvar{'lonExpire'}) {
549: $cleaned++;
550: &log($fh,"Unlinking $fname<br />");
551: unlink("$fname");
552: } else {
553: $active++;
554: }
555: }
556: }
557: &log($fh,"<p>Cleaned up ".$cleaned." stale balancer files</p>");
558: &log($fh,"<h3>$active unexpired balancer files</h3>");
559: }
560:
561: # ------------------------------------------------ clean out webDAV Session IDs
562: sub clean_webDAV_sessionIDs {
563: my ($fh)=@_;
564: if ($perlvar{'lonRole'} eq 'library') {
565: &log($fh,'<hr /><a name="webdav" /><h2>WebDAV Session Tokens</h2>');
566: my $cleaned=0;
567: my $active=0;
568: my $now = time;
569: if (-d $perlvar{'lonDAVsessDir'}) {
570: while (my $fname=<$perlvar{'lonDAVsessDir'}/*>) {
571: my @stats = stat($fname);
572: my $since=$now-$stats[9];
573: if ($since>$perlvar{'lonExpire'}) {
574: $cleaned++;
575: &log($fh,"Unlinking $fname<br />");
576: unlink("$fname");
577: } else {
578: $active++;
579: }
580: }
581: &log($fh,"<p>Cleaned up ".$cleaned." stale webDAV session token(s).</p>");
582: &log($fh,"<h3>$active open webDAV session(s)</h3>");
583: }
584: }
585: }
586:
587: # ----------------------------------------------------------- clean out sockets
588: sub clean_sockets {
589: my ($fh)=@_;
590: my $cleaned=0;
591: opendir(SOCKETS,$perlvar{'lonSockDir'});
592: while (my $fname=readdir(SOCKETS)) {
593: next if (-d $fname
594: || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
595: $cleaned++;
596: &log($fh,"Unlinking $fname<br />");
597: unlink("/home/httpd/sockets/$fname");
598: }
599: &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
600: }
601:
602:
603: # ----------------------------------------------------------------------- httpd
604: sub check_httpd_logs {
605: my ($fh)=@_;
606: if (open(PIPE,"./lchttpdlogs|")) {
607: while (my $line=<PIPE>) {
608: &log($fh,$line);
609: if ($line=~/\[error\]/) { $notices++; }
610: }
611: close(PIPE);
612: }
613: &errout($fh);
614: }
615:
616: # ---------------------------------------------------------------------- lonnet
617:
618: sub rotate_lonnet_logs {
619: my ($fh)=@_;
620: &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
621: print "Checking logs.\n";
622: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
623: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
624: while (my $line=<DFH>) {
625: &log($fh,&encode_entities($line,'<>&"'));
626: }
627: close (DFH);
628: }
629: &log($fh,"</pre><h3>Perm Log</h3><pre>");
630:
631: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
632: open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
633: while (my $line=<DFH>) {
634: &log($fh,&encode_entities($line,'<>&"'));
635: }
636: close (DFH);
637: } else { &log($fh,"No perm log\n") }
638:
639: my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
640: &rotate_logfile($fname,$fh,'lonnet log');
641:
642: &log($fh,"</pre>");
643: &errout($fh);
644: }
645:
646: sub rotate_other_logs {
647: my ($fh) = @_;
648: my %logs = (
649: autoenroll => 'Auto Enroll log',
650: autocreate => 'Create Course log',
651: searchcat => 'Search Cataloguing log',
652: autoupdate => 'Auto Update log',
653: refreshcourseids_db => 'Refresh CourseIDs db log',
654: );
655: foreach my $item (keys(%logs)) {
656: my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
657: &rotate_logfile($fname,$fh,$logs{$item});
658: }
659: }
660:
661: # ----------------------------------------------------------------- Connections
662: sub test_connections {
663: my ($fh)=@_;
664: &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
665: print "Testing connections.\n";
666: &log($fh,"<table border='2'>");
667: my ($good,$bad)=(0,0);
668: my %hostname = &Apache::lonnet::all_hostnames();
669: foreach my $tryserver (sort(keys(%hostname))) {
670: print(".");
671: my $result;
672: my $answer=&Apache::lonnet::reply("ping",$tryserver);
673: if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
674: $result="<b>ok</b>";
675: $good++;
676: } else {
677: $result=$answer;
678: $warnings++;
679: if ($answer eq 'con_lost') {
680: $bad++;
681: $warnings++;
682: } else {
683: $good++; #self connection
684: }
685: }
686: if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
687: &log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
688: }
689: &log($fh,"</table>");
690: print "\n$good good, $bad bad connections\n";
691: &errout($fh);
692: }
693:
694:
695: # ------------------------------------------------------------ Delayed messages
696: sub check_delayed_msg {
697: my ($fh,$weightsref,$exclusionsref)=@_;
698: &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
699: print "Checking buffers.\n";
700:
701: &log($fh,'<h3>Scanning Permanent Log</h3>');
702:
703: my $unsend=0;
704: my $ignored=0;
705:
706: my %hostname = &Apache::lonnet::all_hostnames();
707: my $numhosts = scalar(keys(%hostname));
708: my $checkbackwards = 0;
709: my $checkfrom = 0;
710: my $checkexcluded = 0;
711: my (%bymachine,%weights,%exclusions,%serverhomes);
712: if (ref($weightsref) eq 'HASH') {
713: %weights = %{$weightsref};
714: }
715: if (ref($exclusionsref) eq 'HASH') {
716: %exclusions = %{$exclusionsref};
717: if (keys(%exclusions)) {
718: $checkexcluded = 1;
719: %serverhomes = &read_serverhomeIDs();
720: }
721: }
722:
723: #
724: # For LON-CAPA 1.2.0 to 2.1.3 (release dates: 8/31/2004 and 3/31/2006) any
725: # entry logged in lonnet.perm.log for completion of a delayed (critical)
726: # transaction lacked the hostID for the remote node to which the command
727: # to be completed was sent.
728: #
729: # Because of this, exclusion of items in lonnet.perm.log for nodes which are
730: # no longer part of the cluster from adding to the overall "unsend" count
731: # needs additional effort besides the changes made in loncron rev. 1.105.
732: #
733: # For "S" (completion) events logging in LON-CAPA 1.2.0 through 2.1.3 included
734: # "LondTransaction=HASH(hexadecimal)->getClient() :$cmd, where the hexadecimal
735: # is a memory location, and $cmd is the command sent to the remote node.
736: #
737: # Starting with 2.2.0 (released 8/21/2006) logging for "S" (completion) events
738: # had sethost:$host_id:$cmd after LondTransaction=HASH(hexadecimal)->getClient()
739: #
740: # Starting with 2.4.1 (released 6/13/2007) logging for "S" replaced echoing the
741: # getClient() call with the result of the Transaction->getClient() call itself
742: # undef for completion of delivery of a delayed message.
743: #
744: # The net effect of these changes is that lonnet.perm.log is now accessed three
745: # times: (a) oldest record is checked, if earlier than release date for 2.5.0
746: # then (b) file is read backwards, with timestamp recorded for most recent
747: # instance of logged "S" event for "update" command without "sethost:$host_id:"
748: # then (c) file is read forward with records ignored which predate the timestamp
749: # recorded in (b), if one was found.
750: #
751: # In (c), when calculating the unsend total, i.e., the difference between delayed
752: # transactions ("D") and sent transactions ("S"), transactions are ignored if the
753: # target node is no longer in the cluster, and also (for "update" commands), if
754: # the target node is in the list of nodes excluded from the count, in the domain
755: # configuration for this machine's default domain. The idea here is to remove
756: # delayed "update" commands for nodes for which inbound access to port 5663,
757: # is blocked, but are still part of the LON-CAPA network, (i.e., they can still
758: # replicate content from other nodes).
759: #
760:
761: my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
762: if (defined($dfh)) {
763: while (my $line=<$dfh>) {
764: my ($time,$sdf,$rest)=split(/:/,$line,3);
765: if ($time < 1541185772) {
766: $checkbackwards = 1;
767: }
768: last;
769: }
770: undef $dfh;
771: }
772:
773: if ($checkbackwards) {
774: if (tie *BW, 'File::ReadBackwards', "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
775: while(my $line=<BW>) {
776: if ($line =~ /\QLondTransaction=HASH\E[^:]+:update:/) {
777: ($checkfrom) = split(/:/,$line,2);
778: last;
779: }
780: }
781: close(BW);
782: }
783: }
784: $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log","r");
785: if (defined($dfh)) {
786: while (my $line=<$dfh>) {
787: my ($time,$sdf,$rest)=split(/:/,$line,3);
788: next unless (($sdf eq 'F') || ($sdf eq 'S') || ($sdf eq 'D'));
789: next if (($checkfrom) && ($time <= $checkfrom));
790: my ($dserv,$dcmd);
791: if ($sdf eq 'S') {
792: my ($serva,$cmda,$servb,$cmdb) = split(/:/,$rest);
793: if ($cmda eq 'sethost') {
794: chomp($cmdb);
795: $dcmd = $cmdb;
796: } else {
797: $dcmd = $cmda;
798: }
799: if (($serva =~ /^LondTransaction/) || ($serva eq '')) {
800: unless (($servb eq '') || ($servb =~ m{^/})) {
801: $dserv = $servb;
802: }
803: } else {
804: $dserv = $serva;
805: }
806: } else {
807: ($dserv,$dcmd) = split(/:/,$rest);
808: }
809: if ($sdf eq 'F') {
810: my $local=localtime($time);
811: &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
812: $warnings++;
813: }
814: next if ((($dserv eq '') || ($dcmd eq '')) && ($sdf ne 'F'));
815: if ($sdf eq 'S') {
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: } elsif ($sdf eq 'D') {
835: if ($dcmd eq 'update') {
836: if ($hostname{$dserv}) {
837: if ($exclusions{$serverhomes{$hostname{$dserv}}}) {
838: $ignored ++;
839: } else {
840: $unsend ++;
841: }
842: }
843: if (exists($bymachine{$dserv})) {
844: $bymachine{$dserv} ++;
845: } else {
846: $bymachine{$dserv} = 1;
847: }
848: } else {
849: if ($hostname{$dserv}) {
850: $unsend ++;
851: }
852: }
853: }
854: }
855: undef $dfh;
856: my $nodest = 0;
857: my $retired = 0;
858: my %active;
859: if (keys(%bymachine)) {
860: unless ($checkexcluded) {
861: %serverhomes = &read_serverhomeIDs();
862: }
863: foreach my $key (keys(%bymachine)) {
864: if ($bymachine{$key} > 0) {
865: if ($hostname{$key}) {
866: $active{$serverhomes{$hostname{$key}}} += $bymachine{$key};
867: } else {
868: $retired ++;
869: $nodest += $bymachine{$key};
870: }
871: }
872: }
873: }
874: if (keys(%active)) {
875: &log($fh,"<p>Unsend messages by node, active (undegraded) nodes in cluster</p>\n");
876: foreach my $key (sort(keys(%active))) {
877: &log($fh,&encode_entities("$key => $active{$key}",'<>&"')."\n");
878: }
879: }
880: &log($fh,"<p>Total unsend messages: <b>$unsend</b> for ".scalar(keys(%active))." active (undegraded) nodes in cluster.</p>\n");
881: if (keys(%exclusions) > 0) {
882: &log($fh,"<p>Total incomplete updates <b>$ignored</b> for ".scalar(keys(%exclusions))." degraded nodes in cluster.</p>\n");
883: }
884: if ($retired) {
885: &log($fh,"<p>Total unsent <b>$nodest</b> for $retired nodes no longer in cluster.</p>\n");
886: }
887: if ($unsend > 0) {
888: $warnings=$warnings+$weights{'U'}*$unsend;
889: }
890: }
891:
892: if ($unsend) { $simplestatus{'unsend'}=$unsend; }
893: &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
894: # list directory with delayed messages and remember offline servers
895: my %servers=();
896: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
897: while (my $line=<DFH>) {
898: my ($server)=($line=~/\.(\w+)$/);
899: if ($server) { $servers{$server}=1; }
900: &log($fh,&encode_entities($line,'<>&"'));
901: }
902: &log($fh,"</pre>\n");
903: close (DFH);
904: # pong to all servers that have delayed messages
905: # this will trigger a reverse connection, which should flush the buffers
906: foreach my $tryserver (sort(keys(%servers))) {
907: if ($hostname{$tryserver} || !$numhosts) {
908: my $answer;
909: eval {
910: local $SIG{ ALRM } = sub { die "TIMEOUT" };
911: alarm(20);
912: $answer = &Apache::lonnet::reply("pong",$tryserver);
913: alarm(0);
914: };
915: if ($@ && $@ =~ m/TIMEOUT/) {
916: &log($fh,"Attempted pong to $tryserver timed out<br />");
917: print "Time out while contacting: $tryserver for pong.\n";
918: } else {
919: &log($fh,"Pong to $tryserver: $answer<br />");
920: }
921: } else {
922: &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
923: }
924: }
925: }
926:
927: sub finish_logging {
928: my ($fh,$weightsref)=@_;
929: my %weights;
930: if (ref($weightsref) eq 'HASH') {
931: %weights = %{$weightsref};
932: }
933: &log($fh,"<a name='errcount' />\n");
934: $totalcount=($weights{'N'}*$notices)+($weights{'W'}*$warnings)+($weights{'E'}*$errors);
935: &errout($fh);
936: &log($fh,"<h1>Total Error Count: $totalcount</h1>");
937: my $now=time;
938: my $date=localtime($now);
939: &log($fh,"<hr />$date ($now)</body></html>\n");
940: print "lon-status webpage updated.\n";
941: $fh->close();
942:
943: if ($errors) { $simplestatus{'errors'}=$errors; }
944: if ($warnings) { $simplestatus{'warnings'}=$warnings; }
945: if ($notices) { $simplestatus{'notices'}=$notices; }
946: $simplestatus{'time'}=time;
947: }
948:
949: sub log_simplestatus {
950: rename("$statusdir/newstatus.html","$statusdir/index.html");
951:
952: my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
953: foreach (keys %simplestatus) {
954: print $sfh $_.'='.$simplestatus{$_}.'&';
955: }
956: print $sfh "\n";
957: $sfh->close();
958: }
959:
960: sub write_loncaparevs {
961: print "Retrieving LON-CAPA version information.\n";
962: my %hostname = &Apache::lonnet::all_hostnames();
963: my $output;
964: foreach my $id (sort(keys(%hostname))) {
965: if ($id ne '') {
966: my $loncaparev;
967: eval {
968: local $SIG{ ALRM } = sub { die "TIMEOUT" };
969: alarm(10);
970: $loncaparev =
971: &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
972: alarm(0);
973: };
974: if ($@ && $@ =~ m/TIMEOUT/) {
975: print "Time out while contacting lonHost: $id for version.\n";
976: }
977: if ($loncaparev =~ /^[\w.\-]+$/) {
978: $output .= $id.':'.$loncaparev."\n";
979: }
980: }
981: }
982: if ($output) {
983: if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
984: print $fh $output;
985: close($fh);
986: &Apache::lonnet::load_loncaparevs();
987: }
988: }
989: return;
990: }
991:
992: sub write_serverhomeIDs {
993: print "Retrieving LON-CAPA lonHostID information.\n";
994: my %name_to_host = &Apache::lonnet::all_names();
995: my $output;
996: foreach my $name (sort(keys(%name_to_host))) {
997: if ($name ne '') {
998: if (ref($name_to_host{$name}) eq 'ARRAY') {
999: my $serverhomeID;
1000: eval {
1001: local $SIG{ ALRM } = sub { die "TIMEOUT" };
1002: alarm(10);
1003: $serverhomeID =
1004: &Apache::lonnet::get_server_homeID($name,1,'loncron');
1005: alarm(0);
1006: };
1007: if ($@ && $@ =~ m/TIMEOUT/) {
1008: print "Time out while contacting server: $name\n";
1009: }
1010: if ($serverhomeID ne '') {
1011: $output .= $name.':'.$serverhomeID."\n";
1012: } else {
1013: $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1014: }
1015: }
1016: }
1017: }
1018: if ($output) {
1019: if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1020: print $fh $output;
1021: close($fh);
1022: &Apache::lonnet::load_serverhomeIDs();
1023: }
1024: }
1025: return;
1026: }
1027:
1028: sub write_checksums {
1029: my $distro = &LONCAPA::distro();
1030: if ($distro) {
1031: print "Retrieving file version and checksumming.\n";
1032: my $numchksums = 0;
1033: my ($chksumsref,$versionsref) =
1034: &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
1035: $perlvar{'lonLib'},
1036: $perlvar{'lonIncludes'},
1037: $perlvar{'lonTabDir'});
1038: if (ref($chksumsref) eq 'HASH') {
1039: $numchksums = scalar(keys(%{$chksumsref}));
1040: }
1041: print "File version retrieved and checksumming completed for $numchksums files.\n";
1042: } else {
1043: print "File version retrieval and checksumming skipped - could not determine Linux distro.\n";
1044: }
1045: return;
1046: }
1047:
1048: sub write_hostips {
1049: my $lontabdir = $perlvar{'lonTabDir'};
1050: my $defdom = $perlvar{'lonDefDomain'};
1051: my $lonhost = $perlvar{'lonHostID'};
1052: my $newfile = "$lontabdir/currhostips.tab";
1053: my $oldfile = "$lontabdir/prevhostips.tab";
1054: my (%prevhosts,%currhosts,%ipchange);
1055: if ((-e $newfile) && (-s $newfile)) {
1056: move($newfile,$oldfile);
1057: chmod(0644,$oldfile);
1058: if (open(my $fh,'<',$oldfile)) {
1059: while (my $line=<$fh>) {
1060: chomp($line);
1061: if ($line =~ /^([^:]+):([\d.]+)$/) {
1062: $prevhosts{$1} = $2;
1063: }
1064: }
1065: close($fh);
1066: }
1067: }
1068: my ($ip_info,$cached) =
1069: &Apache::lonnet::is_cached_new('iphost','iphost');
1070: if (!$cached) {
1071: &Apache::lonnet::get_iphost();
1072: ($ip_info,$cached) =
1073: &Apache::lonnet::is_cached_new('iphost','iphost');
1074: }
1075: if (ref($ip_info) eq 'ARRAY') {
1076: %currhosts = %{$ip_info->[1]};
1077: if (open(my $fh,'>',$newfile)) {
1078: foreach my $key (keys(%currhosts)) {
1079: print $fh "$key:$currhosts{$key}\n";
1080: }
1081: close($fh);
1082: chmod(0644,$newfile);
1083: }
1084: }
1085: if (keys(%prevhosts) && keys(%currhosts)) {
1086: foreach my $key (keys(%prevhosts)) {
1087: unless ($currhosts{$key} eq $prevhosts{$key}) {
1088: $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1089: }
1090: }
1091: foreach my $key (keys(%currhosts)) {
1092: unless ($currhosts{$key} eq $prevhosts{$key}) {
1093: $ipchange{$key} = $prevhosts{$key}.' | '.$currhosts{$key};
1094: }
1095: }
1096: }
1097: if (&Apache::lonnet::domain($defdom,'primary') eq $lonhost) {
1098: if (keys(%ipchange)) {
1099: if (open(my $fh,'>>',$perlvar{'lonDaemons'}.'/logs/hostip.log')) {
1100: print $fh "********************\n".localtime(time).' Changes --'."\n".
1101: "| Hostname | Previous IP | New IP |\n".
1102: " --------------------------------- \n";
1103: foreach my $hostname (sort(keys(%ipchange))) {
1104: print $fh "| $hostname | $ipchange{$hostname} |\n";
1105: }
1106: print $fh "\n*******************\n\n";
1107: close($fh);
1108: }
1109: my $emailto = &Apache::loncommon::build_recipient_list(undef,
1110: 'hostipmail',$defdom);
1111: if ($emailto) {
1112: my $subject = "LON-CAPA Hostname to IP change ($perlvar{'lonHostID'})";
1113: my $chgmail = "To: $emailto\n".
1114: "Subject: $subject\n".
1115: "Content-type: text/plain\; charset=UTF-8\n".
1116: "MIME-Version: 1.0\n\n".
1117: "Host/IP changes\n".
1118: " \n".
1119: "| Hostname | Previous IP | New IP |\n".
1120: " --------------------------------- \n";
1121: foreach my $hostname (sort(keys(%ipchange))) {
1122: $chgmail .= "| $hostname | $ipchange{$hostname} |\n";
1123: }
1124: $chgmail .= "\n\n";
1125: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
1126: print $mailh $chgmail;
1127: close($mailh);
1128: print "Sending mail notification of hostname/IP changes.\n";
1129: }
1130: }
1131: }
1132: }
1133: return;
1134: }
1135:
1136: sub get_domain_config {
1137: my ($dom,$primlibserv,$isprimary,$url,%confhash);
1138: $dom = $perlvar{'lonDefDomain'};
1139: $primlibserv = &Apache::lonnet::domain($dom,'primary');
1140: if ($primlibserv eq $perlvar{'lonHostID'}) {
1141: $isprimary = 1;
1142: } elsif ($primlibserv ne '') {
1143: my $protocol = $Apache::lonnet::protocol{$primlibserv};
1144: my $hostname = &Apache::lonnet::hostname($primlibserv);
1145: unless ($protocol eq 'https') {
1146: $protocol = 'http';
1147: }
1148: $url = $protocol.'://'.$hostname.'/cgi-bin/listdomconfig.pl?primary='.$primlibserv.'&format=raw';
1149: }
1150: if ($isprimary) {
1151: my $lonusersdir = $perlvar{'lonUsersDir'};
1152: my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
1153: if (-e $fname) {
1154: my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
1155: if (ref($dbref) eq 'HASH') {
1156: foreach my $key (sort(keys(%{$dbref}))) {
1157: my $value = $dbref->{$key};
1158: if ($value =~ s/^__FROZEN__//) {
1159: $value = thaw(&LONCAPA::unescape($value));
1160: } else {
1161: $value = &LONCAPA::unescape($value);
1162: }
1163: $confhash{$key} = $value;
1164: }
1165: &LONCAPA::locking_hash_untie($dbref);
1166: }
1167: }
1168: } else {
1169: my $ua=new LWP::UserAgent;
1170: $ua->timeout(5);
1171: my $request=new HTTP::Request('GET',$url);
1172: my $response=$ua->request($request);
1173: unless ($response->is_error()) {
1174: my $content = $response->content;
1175: if ($content) {
1176: my @pairs=split(/\&/,$content);
1177: foreach my $item (@pairs) {
1178: my ($key,$value)=split(/=/,$item,2);
1179: my $what = &LONCAPA::unescape($key);
1180: if ($value =~ s/^__FROZEN__//) {
1181: $value = thaw(&LONCAPA::unescape($value));
1182: } else {
1183: $value = &LONCAPA::unescape($value);
1184: }
1185: $confhash{$what}=$value;
1186: }
1187: }
1188: }
1189: }
1190: return \%confhash;
1191: }
1192:
1193: sub get_permcount_settings {
1194: my ($domconf) = @_;
1195: my ($defaults,$names) = &Apache::loncommon::lon_status_items();
1196: my (%weights,$threshold,$sysmail,$reportstatus,%exclusions);
1197: foreach my $type ('E','W','N','U') {
1198: $weights{$type} = $defaults->{$type};
1199: }
1200: $threshold = $defaults->{'threshold'};
1201: $sysmail = $defaults->{'sysmail'};
1202: $reportstatus = 1;
1203: if (ref($domconf) eq 'HASH') {
1204: if (ref($domconf->{'contacts'}) eq 'HASH') {
1205: if ($domconf->{'contacts'}{'reportstatus'} == 0) {
1206: $reportstatus = 0;
1207: }
1208: if (ref($domconf->{'contacts'}{'lonstatus'}) eq 'HASH') {
1209: if (ref($domconf->{'contacts'}{'lonstatus'}{weights}) eq 'HASH') {
1210: foreach my $type ('E','W','N','U') {
1211: if (exists($domconf->{'contacts'}{'lonstatus'}{weights}{$type})) {
1212: $weights{$type} = $domconf->{'contacts'}{'lonstatus'}{weights}{$type};
1213: }
1214: }
1215: }
1216: if (ref($domconf->{'contacts'}{'lonstatus'}{'excluded'}) eq 'ARRAY') {
1217: my @excluded = @{$domconf->{'contacts'}{'lonstatus'}{'excluded'}};
1218: if (@excluded) {
1219: map { $exclusions{$_} = 1; } @excluded;
1220: }
1221: }
1222: if (exists($domconf->{'contacts'}{'lonstatus'}{'threshold'})) {
1223: $threshold = $domconf->{'contacts'}{'lonstatus'}{'threshold'};
1224: }
1225: if (exists($domconf->{'contacts'}{'lonstatus'}{'sysmail'})) {
1226: $sysmail = $domconf->{'contacts'}{'lonstatus'}{'sysmail'};
1227: }
1228: }
1229: }
1230: }
1231: return ($threshold,$sysmail,$reportstatus,\%weights,\%exclusions);
1232: }
1233:
1234: sub read_serverhomeIDs {
1235: my %server;
1236: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
1237: if (open(my $fh,'<',"$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
1238: while (<$fh>) {
1239: my($host,$id) = split(/:/);
1240: chomp($id);
1241: $server{$host} = $id;
1242: }
1243: close($fh);
1244: }
1245: }
1246: return %server;
1247: }
1248:
1249: sub send_mail {
1250: my ($sysmail,$reportstatus) = @_;
1251: my $defdom = $perlvar{'lonDefDomain'};
1252: my $origmail = $perlvar{'lonAdmEMail'};
1253: my $emailto = &Apache::loncommon::build_recipient_list(undef,
1254: 'lonstatusmail',$defdom,$origmail);
1255: if (($totalcount>$sysmail) && ($reportstatus)) {
1256: $emailto.=",$perlvar{'lonSysEMail'}";
1257: }
1258: my $from;
1259: my $hostname=`/bin/hostname`;
1260: chop($hostname);
1261: $hostname=~s/[^\w\.]//g;
1262: if ($hostname) {
1263: $from = 'www@'.$hostname;
1264: }
1265: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
1266: my $loncronmail = "To: $emailto\n".
1267: "From: $from\n".
1268: "Subject: ".$subj."\n".
1269: "Content-type: text/html\; charset=UTF-8\n".
1270: "MIME-Version: 1.0\n\n";
1271: if (open(my $fh,"<$statusdir/index.html")) {
1272: while (<$fh>) {
1273: $loncronmail .= $_;
1274: }
1275: close($fh);
1276: } else {
1277: $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
1278: }
1279: $loncronmail .= "\n\n";
1280: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
1281: print $mailh $loncronmail;
1282: close($mailh);
1283: print "Sending mail.\n";
1284: } else {
1285: print "Sending mail failed.\n";
1286: }
1287: }
1288:
1289: sub usage {
1290: print(<<USAGE);
1291: loncron - housekeeping program that checks up on various parts of LON-CAPA
1292:
1293: Options:
1294: --help Display
1295: --noemail Do not send the status email
1296: --justcheckconnections Only check the current status of the lonc/d
1297: connections, do not send emails do not
1298: check if the daemons are running, do not
1299: generate lon-status
1300: --justcheckdaemons Only check that all of the Lon-CAPA daemons are
1301: running, do not send emails do not
1302: check the lonc/d connections, do not
1303: generate lon-status
1304: --justreload Only tell the daemons to reload the config files,
1305: do not send emails do not
1306: check if the daemons are running, do not
1307: generate lon-status
1308: --justiptables Only update the dynamic iptables rules for the
1309: lond port; do not send emails, do not
1310: check if the daemons are running, do not
1311: generate lon-status
1312: USAGE
1313: }
1314:
1315: # ================================================================ Main Program
1316: sub main () {
1317: my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1318: $justreload,$justiptables);
1319: &GetOptions("help" => \$help,
1320: "justcheckdaemons" => \$justcheckdaemons,
1321: "noemail" => \$noemail,
1322: "justcheckconnections" => \$justcheckconnections,
1323: "justreload" => \$justreload,
1324: "justiptables" => \$justiptables
1325: );
1326: if ($help) { &usage(); return; }
1327: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
1328: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1329: %perlvar=%{$perlvarref};
1330: undef $perlvarref;
1331: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
1332: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1333: chdir($perlvar{'lonDaemons'});
1334: # --------------------------------------- Make sure that LON-CAPA is configured
1335: # I only test for one thing here (lonHostID). This is just a safeguard.
1336: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
1337: print("Unconfigured machine.\n");
1338: my $emailto=$perlvar{'lonSysEMail'};
1339: my $hostname = Sys::Hostname::FQDN::fqdn();
1340: $hostname=~s/\.+/./g;
1341: $hostname=~s/\-+/-/g;
1342: $hostname=~s/[^\w\.-]//g; # make sure is safe to pass through shell
1343: my $subj="LON: Unconfigured machine $hostname";
1344: system("echo 'Unconfigured machine $hostname.' |".
1345: " mail -s '$subj' $emailto > /dev/null");
1346: exit 1;
1347: }
1348:
1349: # ----------------------------- Make sure this process is running from user=www
1350: my $wwwid=getpwnam('www');
1351: if ($wwwid!=$<) {
1352: print("User ID mismatch. This program must be run as user 'www'.\n");
1353: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
1354: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1355: system("echo 'User ID mismatch. loncron must be run as user www.' |".
1356: " mail -s '$subj' $emailto > /dev/null");
1357: exit 1;
1358: }
1359:
1360: # -------------------------------------------- Force reload of host information
1361: my $nomemcache;
1362: if ($justcheckdaemons) {
1363: $nomemcache=1;
1364: my $memcachepidfile="$perlvar{'lonDaemons'}/logs/memcached.pid";
1365: my $memcachepid;
1366: if (-e $memcachepidfile) {
1367: my $memfh=IO::File->new($memcachepidfile);
1368: $memcachepid=<$memfh>;
1369: chomp($memcachepid);
1370: if ($memcachepid =~ /^\d+$/ && kill 0 => $memcachepid) {
1371: undef($nomemcache);
1372: }
1373: }
1374: }
1375: if (!$justiptables) {
1376: &Apache::lonnet::load_hosts_tab(1,$nomemcache);
1377: &Apache::lonnet::load_domain_tab(1,$nomemcache);
1378: &Apache::lonnet::get_iphost(1,$nomemcache);
1379: }
1380:
1381: # ----------------------------------------- Force firewall update for lond port
1382:
1383: if ((!$justcheckdaemons) && (!$justreload)) {
1384: my $now = time;
1385: my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
1386: $now.$$.int(rand(10000));
1387: if (open(my $fh,">$tmpfile")) {
1388: my %iphosts = &Apache::lonnet::get_iphost();
1389: foreach my $key (keys(%iphosts)) {
1390: print $fh "$key\n";
1391: }
1392: close($fh);
1393: if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
1394: my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
1395: system("$execpath $tmpfile");
1396: unlink('/tmp/lock_lciptables'); # Remove the lock file.
1397: }
1398: unlink($tmpfile);
1399: }
1400: }
1401:
1402: # ---------------------------------------------------------------- Start report
1403:
1404: $errors=0;
1405: $warnings=0;
1406: $notices=0;
1407:
1408:
1409: my $fh;
1410: if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1411: $fh=&start_logging();
1412:
1413: &log_machine_info($fh);
1414: &clean_tmp($fh);
1415: &clean_lonIDs($fh);
1416: &clean_balanceIDs($fh);
1417: &clean_webDAV_sessionIDs($fh);
1418: &check_httpd_logs($fh);
1419: &rotate_lonnet_logs($fh);
1420: &rotate_other_logs($fh);
1421: }
1422: if (!$justcheckconnections && !$justreload && !$justiptables) {
1423: &checkon_daemon($fh,'lonmemcached',40000);
1424: &checkon_daemon($fh,'lonsql',200000);
1425: if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
1426: &checkon_daemon($fh,'lond',40000,'USR2');
1427: }
1428: &checkon_daemon($fh,'lonc',40000,'USR1');
1429: &checkon_daemon($fh,'lonmaxima',40000);
1430: &checkon_daemon($fh,'lonr',40000);
1431: }
1432: if ($justreload) {
1433: &checkon_daemon($fh,'lond',40000,'USR2');
1434: &checkon_daemon($fh,'lonc',40000,'USR2');
1435: }
1436: if ($justcheckconnections) {
1437: &test_connections($fh);
1438: }
1439: if (!$justcheckdaemons && !$justcheckconnections && !$justreload && !$justiptables) {
1440: my $domconf = &get_domain_config();
1441: my ($threshold,$sysmail,$reportstatus,$weightsref,$exclusionsref) =
1442: &get_permcount_settings($domconf);
1443: &check_delayed_msg($fh,$weightsref,$exclusionsref);
1444: &finish_logging($fh,$weightsref);
1445: &log_simplestatus();
1446: &write_loncaparevs();
1447: &write_serverhomeIDs();
1448: &write_checksums();
1449: &write_hostips();
1450: if ($totalcount>$threshold && !$noemail) { &send_mail($sysmail,$reportstatus); }
1451: }
1452: }
1453:
1454: &main();
1455: 1;
1456:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>