File:
[LON-CAPA] /
loncom /
loncron
Revision
1.101:
download - view:
text,
annotated -
select for diffs
Tue Dec 16 18:18:44 2014 UTC (9 years, 9 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Changes to satisfy w3c xhtml validation.
- Eliminate use of metasend (provided by metamail) as currently supported
distros tyically do not have this installed.
- Include headers (Content-type, MIME-Version etc.) in e-mail of status page
contents so e-mailclients will display content as HTML.
1: #!/usr/bin/perl
2:
3: # Housekeeping program, started by cron, loncontrol and loncron.pl
4: #
5: # $Id: loncron,v 1.101 2014/12/16 18:18:44 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 IO::File;
41: use IO::Socket;
42: use HTML::Entities;
43: use Getopt::Long;
44: #globals
45: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
46:
47: my $statusdir="/home/httpd/html/lon-status";
48:
49:
50: # --------------------------------------------------------- Output error status
51:
52: sub log {
53: my $fh=shift;
54: if ($fh) { print $fh @_ }
55: }
56:
57: sub errout {
58: my $fh=shift;
59: &log($fh,(<<ENDERROUT));
60: <table border="2" bgcolor="#CCCCCC">
61: <tr><td>Notices</td><td>$notices</td></tr>
62: <tr><td>Warnings</td><td>$warnings</td></tr>
63: <tr><td>Errors</td><td>$errors</td></tr>
64: </table><p><a href="#top">Top</a></p>
65: ENDERROUT
66: }
67:
68: sub rotate_logfile {
69: my ($file,$fh,$description) = @_;
70: my $size=(stat($file))[7];
71: if ($size>40000) {
72: &log($fh,"<p>Rotating $description ...</p>");
73: rename("$file.2","$file.3");
74: rename("$file.1","$file.2");
75: rename("$file","$file.1");
76: }
77: }
78:
79: sub start_daemon {
80: my ($fh,$daemon,$pidfile,$args) = @_;
81: my $progname=$daemon;
82: if ($daemon eq 'lonc') {
83: $progname='loncnew';
84: }
85: my $error_fname="$perlvar{'lonDaemons'}/logs/${daemon}_errors";
86: &rotate_logfile($error_fname,$fh,'error logs');
87: if ($daemon eq 'lonc') {
88: &clean_sockets($fh);
89: }
90: system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
91: sleep 1;
92: if (-e $pidfile) {
93: &log($fh,"<p>Seems like it started ...</p>");
94: my $lfh=IO::File->new("$pidfile");
95: my $daemonpid=<$lfh>;
96: chomp($daemonpid);
97: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
98: return 1;
99: } else {
100: return 0;
101: }
102: }
103: &log($fh,"<p>Seems like that did not work!</p>");
104: $errors++;
105: return 0;
106: }
107:
108: sub checkon_daemon {
109: my ($fh,$daemon,$maxsize,$send,$args)=@_;
110:
111: my $result;
112: &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
113: printf("%-15s ",$daemon);
114: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
115: open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
116: while (my $line=<DFH>) {
117: &log($fh,"$line");
118: if ($line=~/INFO/) { $notices++; }
119: if ($line=~/WARNING/) { $notices++; }
120: if ($line=~/CRITICAL/) { $warnings++; }
121: };
122: close (DFH);
123: }
124: &log($fh,"</tt></p>");
125:
126: my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
127:
128: my $restartflag=1;
129: my $daemonpid;
130: if (-e $pidfile) {
131: my $lfh=IO::File->new("$pidfile");
132: $daemonpid=<$lfh>;
133: chomp($daemonpid);
134: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
135: &log($fh,"<h3>$daemon at pid $daemonpid responding");
136: if ($send) { &log($fh,", sending $send"); }
137: &log($fh,"</h3>");
138: if ($send eq 'USR1') { kill USR1 => $daemonpid; }
139: if ($send eq 'USR2') { kill USR2 => $daemonpid; }
140: $restartflag=0;
141: if ($send eq 'USR2') {
142: $result = 'reloaded';
143: print "reloaded\n";
144: } else {
145: $result = 'running';
146: print "running\n";
147: }
148: } else {
149: $errors++;
150: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
151: $restartflag=1;
152: &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
153: }
154: }
155: if ($restartflag==1) {
156: $simplestatus{$daemon}='off';
157: $errors++;
158: my $kadaemon=$daemon;
159: if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
160: &log($fh,'<br /><font color="red">Killall '.$daemon.': '.
161: `killall $kadaemon 2>&1`.' - ');
162: sleep 1;
163: &log($fh,unlink($pidfile).' - '.
164: `killall -9 $kadaemon 2>&1`.
165: '</font><br />');
166: &log($fh,"<h3>$daemon not running, trying to start</h3>");
167:
168: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
169: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
170: $simplestatus{$daemon}='restarted';
171: $result = 'started';
172: print "started\n";
173: } else {
174: $errors++;
175: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
176: &log($fh,"<p>Give it one more try ...</p>");
177: print " ";
178: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
179: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
180: $simplestatus{$daemon}='restarted';
181: $result = 'started';
182: print "started\n";
183: } else {
184: $result = 'failed';
185: print " failed\n";
186: $simplestatus{$daemon}='failed';
187: $errors++; $errors++;
188: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
189: &log($fh,"<p>Unable to start $daemon</p>");
190: }
191: }
192:
193: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
194: &log($fh,"<p><pre>");
195: open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
196: while (my $line=<DFH>) {
197: &log($fh,"$line");
198: if ($line=~/WARNING/) { $notices++; }
199: if ($line=~/CRITICAL/) { $notices++; }
200: };
201: close (DFH);
202: &log($fh,"</pre></p>");
203: }
204: }
205:
206: my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
207: &rotate_logfile($fname,$fh,'logs');
208:
209: &errout($fh);
210: return $result;
211: }
212:
213: # --------------------------------------------------------------------- Machine
214: sub log_machine_info {
215: my ($fh)=@_;
216: &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
217: &log($fh,"<h3>loadavg</h3>");
218:
219: open (LOADAVGH,"/proc/loadavg");
220: my $loadavg=<LOADAVGH>;
221: close (LOADAVGH);
222:
223: &log($fh,"<tt>$loadavg</tt>");
224:
225: my @parts=split(/\s+/,$loadavg);
226: if ($parts[1]>4.0) {
227: $errors++;
228: } elsif ($parts[1]>2.0) {
229: $warnings++;
230: } elsif ($parts[1]>1.0) {
231: $notices++;
232: }
233:
234: &log($fh,"<h3>df</h3>");
235: &log($fh,"<pre>");
236:
237: open (DFH,"df|");
238: while (my $line=<DFH>) {
239: &log($fh,&encode_entities($line,'<>&"'));
240: @parts=split(/\s+/,$line);
241: my $usage=$parts[4];
242: $usage=~s/\W//g;
243: if ($usage>90) {
244: $warnings++;
245: $notices++;
246: } elsif ($usage>80) {
247: $warnings++;
248: } elsif ($usage>60) {
249: $notices++;
250: }
251: if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
252: }
253: close (DFH);
254: &log($fh,"</pre>");
255:
256:
257: &log($fh,"<h3>ps</h3>");
258: &log($fh,"<pre>");
259: my $psproc=0;
260:
261: open (PSH,"ps aux --cols 140 |");
262: while (my $line=<PSH>) {
263: &log($fh,&encode_entities($line,'<>&"'));
264: $psproc++;
265: }
266: close (PSH);
267: &log($fh,"</pre>");
268:
269: if ($psproc>200) { $notices++; }
270: if ($psproc>250) { $notices++; }
271:
272: &log($fh,"<h3>distprobe</h3>");
273: &log($fh,"<pre>");
274: &log($fh,&encode_entities(&LONCAPA::distro(),'<>&"'));
275: &log($fh,"</pre>");
276:
277: &errout($fh);
278: }
279:
280: sub start_logging {
281: my $fh=IO::File->new(">$statusdir/newstatus.html");
282: my %simplestatus=();
283: my $now=time;
284: my $date=localtime($now);
285:
286:
287: &log($fh,(<<ENDHEADERS));
288: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
289: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
290: <head>
291: <title>LON Status Report $perlvar{'lonHostID'}</title>
292: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
293: </head>
294: <body bgcolor="#AAAAAA">
295: <a name="top" />
296: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
297: <h2>$date ($now)</h2>
298: <ol>
299: <li><a href="#configuration">Configuration</a></li>
300: <li><a href="#machine">Machine Information</a></li>
301: <li><a href="#tmp">Temporary Files</a></li>
302: <li><a href="#tokens">Session Tokens</a></li>
303: <li><a href="#httpd">httpd</a></li>
304: <li><a href="#lonsql">lonsql</a></li>
305: <li><a href="#lond">lond</a></li>
306: <li><a href="#lonc">lonc</a></li>
307: <li><a href="#lonnet">lonnet</a></li>
308: <li><a href="#connections">Connections</a></li>
309: <li><a href="#delayed">Delayed Messages</a></li>
310: <li><a href="#errcount">Error Count</a></li>
311: </ol>
312: <hr />
313: <a name="configuration" />
314: <h2>Configuration</h2>
315: <h3>PerlVars</h3>
316: <table border="2">
317: ENDHEADERS
318:
319: foreach my $varname (sort(keys(%perlvar))) {
320: &log($fh,"<tr><td>$varname</td><td>".
321: &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
322: }
323: &log($fh,"</table><h3>Hosts</h3><table border='2'>");
324: my %hostname = &Apache::lonnet::all_hostnames();
325: foreach my $id (sort(keys(%hostname))) {
326: my $role = (&Apache::lonnet::is_library($id) ? 'library'
327: : 'access');
328: &log($fh,
329: "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
330: "</td><td>".$role.
331: "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
332: }
333: &log($fh,"</table><h3>Spare Hosts</h3>");
334: if (keys(%Apache::lonnet::spareid) > 0) {
335: &log($fh,"<ul>");
336: foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
337: &log($fh,"<li>$type\n<ol>");
338: foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
339: &log($fh,"<li>$id</li>\n");
340: }
341: &log($fh,"</ol>\n</li>\n");
342: }
343: &log($fh,"</ul>\n");
344: } else {
345: &log($fh,"No spare hosts specified<br />\n");
346: }
347: return $fh;
348: }
349:
350: # --------------------------------------------------------------- clean out tmp
351: sub clean_tmp {
352: my ($fh)=@_;
353: &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
354: my ($cleaned,$old,$removed) = (0,0,0);
355: my %errors = (
356: dir => [],
357: file => [],
358: failopen => [],
359: );
360: my %error_titles = (
361: dir => 'failed to remove empty directory:',
362: file => 'failed to unlike stale file',
363: failopen => 'failed to open file or directory'
364: );
365: ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
366: &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
367: foreach my $key (sort(keys(%errors))) {
368: if (ref($errors{$key}) eq 'ARRAY') {
369: if (@{$errors{$key}} > 0) {
370: &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
371: join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
372: }
373: }
374: }
375: }
376:
377: sub recursive_clean_tmp {
378: my ($subdir,$cleaned,$old,$removed,$errors) = @_;
379: my $base = "$perlvar{'lonDaemons'}/tmp";
380: my $path = $base;
381: next if ($subdir =~ m{\.\./});
382: next unless (ref($errors) eq 'HASH');
383: unless ($subdir eq '') {
384: $path .= '/'.$subdir;
385: }
386: if (opendir(my $dh,"$path")) {
387: while (my $file = readdir($dh)) {
388: next if ($file =~ /^\.\.?$/);
389: my $fname = "$path/$file";
390: if (-d $fname) {
391: my $innerdir;
392: if ($subdir eq '') {
393: $innerdir = $file;
394: } else {
395: $innerdir = $subdir.'/'.$file;
396: }
397: ($cleaned,$old,$removed) =
398: &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
399: my @doms = &Apache::lonnet::current_machine_domains();
400:
401: if (open(my $dirhandle,$fname)) {
402: unless (($innerdir eq 'helprequests') ||
403: (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
404: my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
405: join('&&',@contents)."\n";
406: if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
407: closedir($dirhandle);
408: if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
409: if (rmdir($fname)) {
410: $removed ++;
411: } elsif (ref($errors->{dir}) eq 'ARRAY') {
412: push(@{$errors->{dir}},$fname);
413: }
414: }
415: }
416: } else {
417: closedir($dirhandle);
418: }
419: }
420: } else {
421: my ($dev,$ino,$mode,$nlink,
422: $uid,$gid,$rdev,$size,
423: $atime,$mtime,$ctime,
424: $blksize,$blocks)=stat($fname);
425: my $now=time;
426: my $since=$now-$mtime;
427: if ($since>$perlvar{'lonExpire'}) {
428: if ($subdir eq '') {
429: my $line='';
430: if ($fname =~ /\.db$/) {
431: if (unlink($fname)) {
432: $cleaned++;
433: } elsif (ref($errors->{file}) eq 'ARRAY') {
434: push(@{$errors->{file}},$fname);
435: }
436: } elsif (open(PROBE,$fname)) {
437: my $line='';
438: $line=<PROBE>;
439: close(PROBE);
440: if ($line=~/^CHECKOUTTOKEN\&/) {
441: if ($since>365*$perlvar{'lonExpire'}) {
442: if (unlink($fname)) {
443: $cleaned++;
444: } elsif (ref($errors->{file}) eq 'ARRAY') {
445: push(@{$errors->{file}},$fname);
446: }
447: } else {
448: $old++;
449: }
450: } else {
451: if (unlink($fname)) {
452: $cleaned++;
453: } elsif (ref($errors->{file}) eq 'ARRAY') {
454: push(@{$errors->{file}},$fname);
455: }
456: }
457: } elsif (ref($errors->{failopen}) eq 'ARRAY') {
458: push(@{$errors->{failopen}},$fname);
459: }
460: } else {
461: if (unlink($fname)) {
462: $cleaned++;
463: } elsif (ref($errors->{file}) eq 'ARRAY') {
464: push(@{$errors->{file}},$fname);
465: }
466: }
467: }
468: }
469: }
470: closedir($dh);
471: } elsif (ref($errors->{failopen}) eq 'ARRAY') {
472: push(@{$errors->{failopen}},$path);
473: }
474: return ($cleaned,$old,$removed);
475: }
476:
477: # ------------------------------------------------------------ clean out lonIDs
478: sub clean_lonIDs {
479: my ($fh)=@_;
480: &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
481: my $cleaned=0;
482: my $active=0;
483: while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
484: my ($dev,$ino,$mode,$nlink,
485: $uid,$gid,$rdev,$size,
486: $atime,$mtime,$ctime,
487: $blksize,$blocks)=stat($fname);
488: my $now=time;
489: my $since=$now-$mtime;
490: if ($since>$perlvar{'lonExpire'}) {
491: $cleaned++;
492: &log($fh,"Unlinking $fname<br />");
493: unlink("$fname");
494: } else {
495: $active++;
496: }
497: }
498: &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
499: &log($fh,"<h3>$active open session(s)</h3>");
500: }
501:
502: # ----------------------------------------------------------- clean out sockets
503: sub clean_sockets {
504: my ($fh)=@_;
505: my $cleaned=0;
506: opendir(SOCKETS,$perlvar{'lonSockDir'});
507: while (my $fname=readdir(SOCKETS)) {
508: next if (-d $fname
509: || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
510: $cleaned++;
511: &log($fh,"Unlinking $fname<br />");
512: unlink("/home/httpd/sockets/$fname");
513: }
514: &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
515: }
516:
517:
518: # ----------------------------------------------------------------------- httpd
519: sub check_httpd_logs {
520: my ($fh)=@_;
521: if (open(PIPE,"./lchttpdlogs|")) {
522: while (my $line=<PIPE>) {
523: &log($fh,$line);
524: if ($line=~/\[error\]/) { $notices++; }
525: }
526: close(PIPE);
527: }
528: &errout($fh);
529: }
530:
531: # ---------------------------------------------------------------------- lonnet
532:
533: sub rotate_lonnet_logs {
534: my ($fh)=@_;
535: &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
536: print "Checking logs.\n";
537: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
538: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
539: while (my $line=<DFH>) {
540: &log($fh,&encode_entities($line,'<>&"'));
541: }
542: close (DFH);
543: }
544: &log($fh,"</pre><h3>Perm Log</h3><pre>");
545:
546: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
547: open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
548: while (my $line=<DFH>) {
549: &log($fh,&encode_entities($line,'<>&"'));
550: }
551: close (DFH);
552: } else { &log($fh,"No perm log\n") }
553:
554: my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
555: &rotate_logfile($fname,$fh,'lonnet log');
556:
557: &log($fh,"</pre>");
558: &errout($fh);
559: }
560:
561: sub rotate_other_logs {
562: my ($fh) = @_;
563: my %logs = (
564: autoenroll => 'Auto Enroll log',
565: autocreate => 'Create Course log',
566: searchcat => 'Search Cataloguing log',
567: autoupdate => 'Auto Update log',
568: refreshcourseids_db => 'Refresh CourseIDs db log',
569: );
570: foreach my $item (keys(%logs)) {
571: my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
572: &rotate_logfile($fname,$fh,$logs{$item});
573: }
574: }
575:
576: # ----------------------------------------------------------------- Connections
577: sub test_connections {
578: my ($fh)=@_;
579: &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
580: print "Testing connections.\n";
581: &log($fh,"<table border='2'>");
582: my ($good,$bad)=(0,0);
583: my %hostname = &Apache::lonnet::all_hostnames();
584: foreach my $tryserver (sort(keys(%hostname))) {
585: print(".");
586: my $result;
587: my $answer=&Apache::lonnet::reply("ping",$tryserver);
588: if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
589: $result="<b>ok</b>";
590: $good++;
591: } else {
592: $result=$answer;
593: $warnings++;
594: if ($answer eq 'con_lost') {
595: $bad++;
596: $warnings++;
597: } else {
598: $good++; #self connection
599: }
600: }
601: if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
602: &log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
603: }
604: &log($fh,"</table>");
605: print "\n$good good, $bad bad connections\n";
606: &errout($fh);
607: }
608:
609:
610: # ------------------------------------------------------------ Delayed messages
611: sub check_delayed_msg {
612: my ($fh)=@_;
613: &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
614: print "Checking buffers.\n";
615:
616: &log($fh,'<h3>Scanning Permanent Log</h3>');
617:
618: my $unsend=0;
619:
620: my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
621: while (my $line=<$dfh>) {
622: my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
623: if ($sdf eq 'F') {
624: my $local=localtime($time);
625: &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br />");
626: $warnings++;
627: }
628: if ($sdf eq 'S') { $unsend--; }
629: if ($sdf eq 'D') { $unsend++; }
630: }
631:
632: &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
633: if ($unsend > 0) {
634: $warnings=$warnings+5*$unsend;
635: }
636:
637: if ($unsend) { $simplestatus{'unsend'}=$unsend; }
638: &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
639: # list directory with delayed messages and remember offline servers
640: my %servers=();
641: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
642: while (my $line=<DFH>) {
643: my ($server)=($line=~/\.(\w+)$/);
644: if ($server) { $servers{$server}=1; }
645: &log($fh,&encode_entities($line,'<>&"'));
646: }
647: &log($fh,"</pre>\n");
648: close (DFH);
649: my %hostname = &Apache::lonnet::all_hostnames();
650: my $numhosts = scalar(keys(%hostname));
651: # pong to all servers that have delayed messages
652: # this will trigger a reverse connection, which should flush the buffers
653: foreach my $tryserver (sort(keys(%servers))) {
654: if ($hostname{$tryserver} || !$numhosts) {
655: my $answer;
656: eval {
657: local $SIG{ ALRM } = sub { die "TIMEOUT" };
658: alarm(20);
659: $answer = &Apache::lonnet::reply("pong",$tryserver);
660: alarm(0);
661: };
662: if ($@ && $@ =~ m/TIMEOUT/) {
663: &log($fh,"Attempted pong to $tryserver timed out<br />");
664: print "Time out while contacting: $tryserver for pong.\n";
665: } else {
666: &log($fh,"Pong to $tryserver: $answer<br />");
667: }
668: } else {
669: &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
670: }
671: }
672: }
673:
674: sub finish_logging {
675: my ($fh)=@_;
676: &log($fh,"<a name='errcount' />\n");
677: $totalcount=$notices+4*$warnings+100*$errors;
678: &errout($fh);
679: &log($fh,"<h1>Total Error Count: $totalcount</h1>");
680: my $now=time;
681: my $date=localtime($now);
682: &log($fh,"<hr />$date ($now)</body></html>\n");
683: print "lon-status webpage updated.\n";
684: $fh->close();
685:
686: if ($errors) { $simplestatus{'errors'}=$errors; }
687: if ($warnings) { $simplestatus{'warnings'}=$warnings; }
688: if ($notices) { $simplestatus{'notices'}=$notices; }
689: $simplestatus{'time'}=time;
690: }
691:
692: sub log_simplestatus {
693: rename("$statusdir/newstatus.html","$statusdir/index.html");
694:
695: my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
696: foreach (keys %simplestatus) {
697: print $sfh $_.'='.$simplestatus{$_}.'&';
698: }
699: print $sfh "\n";
700: $sfh->close();
701: }
702:
703: sub write_loncaparevs {
704: print "Retrieving LON-CAPA version information.\n";
705: my %hostname = &Apache::lonnet::all_hostnames();
706: my $output;
707: foreach my $id (sort(keys(%hostname))) {
708: if ($id ne '') {
709: my $loncaparev;
710: eval {
711: local $SIG{ ALRM } = sub { die "TIMEOUT" };
712: alarm(10);
713: $loncaparev =
714: &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
715: alarm(0);
716: };
717: if ($@ && $@ =~ m/TIMEOUT/) {
718: print "Time out while contacting lonHost: $id for version.\n";
719: }
720: if ($loncaparev =~ /^[\w.\-]+$/) {
721: $output .= $id.':'.$loncaparev."\n";
722: }
723: }
724: }
725: if ($output) {
726: if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
727: print $fh $output;
728: close($fh);
729: &Apache::lonnet::load_loncaparevs();
730: }
731: }
732: return;
733: }
734:
735: sub write_serverhomeIDs {
736: print "Retrieving LON-CAPA lonHostID information.\n";
737: my %name_to_host = &Apache::lonnet::all_names();
738: my $output;
739: foreach my $name (sort(keys(%name_to_host))) {
740: if ($name ne '') {
741: if (ref($name_to_host{$name}) eq 'ARRAY') {
742: my $serverhomeID;
743: eval {
744: local $SIG{ ALRM } = sub { die "TIMEOUT" };
745: alarm(10);
746: $serverhomeID =
747: &Apache::lonnet::get_server_homeID($name,1,'loncron');
748: alarm(0);
749: };
750: if ($@ && $@ =~ m/TIMEOUT/) {
751: print "Time out while contacting server: $name\n";
752: }
753: if ($serverhomeID ne '') {
754: $output .= $name.':'.$serverhomeID."\n";
755: } else {
756: $output .= $name.':'.$name_to_host{$name}->[0]."\n";
757: }
758: }
759: }
760: }
761: if ($output) {
762: if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
763: print $fh $output;
764: close($fh);
765: &Apache::lonnet::load_serverhomeIDs();
766: }
767: }
768: return;
769: }
770:
771: sub write_checksums {
772: my $distro = &LONCAPA::distro();
773: if ($distro) {
774: print "Retrieving file version and checksumming.\n";
775: my $numchksums = 0;
776: my ($chksumsref,$versionsref) =
777: &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
778: $perlvar{'lonLib'},
779: $perlvar{'lonIncludes'},
780: $perlvar{'lonTabDir'});
781: if (ref($chksumsref) eq 'HASH') {
782: $numchksums = scalar(keys(%{$chksumsref}));
783: }
784: print "File version retrieved and checksumming completed for $numchksums files.\n";
785: } else {
786: print "File version retrieval and checksumming skipped - could not determine Linux distro.\n";
787: }
788: return;
789: }
790:
791: sub send_mail {
792: my $defdom = $perlvar{'lonDefDomain'};
793: my $origmail = $perlvar{'lonAdmEMail'};
794: my $emailto = &Apache::loncommon::build_recipient_list(undef,
795: 'lonstatusmail',$defdom,$origmail);
796: if ($totalcount>2500) {
797: $emailto.=",$perlvar{'lonSysEMail'}";
798: }
799: my $from;
800: my $hostname=`/bin/hostname`;
801: chop($hostname);
802: $hostname=~s/[^\w\.]//g;
803: if ($hostname) {
804: $from = 'www@'.$hostname;
805: }
806: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
807: my $loncronmail = "To: $emailto\n".
808: "From: $from\n".
809: "Subject: ".$subj."\n".
810: "Content-type: text/html\; charset=UTF-8\n".
811: "MIME-Version: 1.0\n\n";
812: if (open(my $fh,"<$statusdir/index.html")) {
813: while (<$fh>) {
814: $loncronmail .= $_;
815: }
816: close($fh);
817: } else {
818: $loncronmail .= "Failed to read from http://$hostname/lon-status/index.html\n";
819: }
820: $loncronmail .= "\n\n";
821: if (open(my $mailh, "|/usr/lib/sendmail -oi -t -odb")) {
822: print $mailh $loncronmail;
823: close($mailh);
824: print "Sending mail.\n";
825: } else {
826: print "Sending mail failed.\n";
827: }
828: }
829:
830: sub usage {
831: print(<<USAGE);
832: loncron - housekeeping program that checks up on various parts of LON-CAPA
833:
834: Options:
835: --help Display
836: --noemail Do not send the status email
837: --justcheckconnections Only check the current status of the lonc/d
838: connections, do not send emails do not
839: check if the daemons are running, do not
840: generate lon-status
841: --justcheckdaemons Only check that all of the Lon-CAPA daemons are
842: running, do not send emails do not
843: check the lonc/d connections, do not
844: generate lon-status
845: --justreload Only tell the daemons to reload the config files,
846: do not send emails do not
847: check if the daemons are running, do not
848: generate lon-status
849:
850: USAGE
851: }
852:
853: # ================================================================ Main Program
854: sub main () {
855: my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
856: $justreload);
857: &GetOptions("help" => \$help,
858: "justcheckdaemons" => \$justcheckdaemons,
859: "noemail" => \$noemail,
860: "justcheckconnections" => \$justcheckconnections,
861: "justreload" => \$justreload
862: );
863: if ($help) { &usage(); return; }
864: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
865: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
866: %perlvar=%{$perlvarref};
867: undef $perlvarref;
868: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
869: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
870: chdir($perlvar{'lonDaemons'});
871: # --------------------------------------- Make sure that LON-CAPA is configured
872: # I only test for one thing here (lonHostID). This is just a safeguard.
873: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
874: print("Unconfigured machine.\n");
875: my $emailto=$perlvar{'lonSysEMail'};
876: my $hostname=`/bin/hostname`;
877: chop $hostname;
878: $hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
879: my $subj="LON: Unconfigured machine $hostname";
880: system("echo 'Unconfigured machine $hostname.' |\
881: mailto $emailto -s '$subj' > /dev/null");
882: exit 1;
883: }
884:
885: # ----------------------------- Make sure this process is running from user=www
886: my $wwwid=getpwnam('www');
887: if ($wwwid!=$<) {
888: print("User ID mismatch. This program must be run as user 'www'.\n");
889: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
890: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
891: system("echo 'User ID mismatch. loncron must be run as user www.' |\
892: mailto $emailto -s '$subj' > /dev/null");
893: exit 1;
894: }
895:
896: # -------------------------------------------- Force reload of host information
897: &Apache::lonnet::load_hosts_tab(1);
898: &Apache::lonnet::load_domain_tab(1);
899: &Apache::lonnet::get_iphost(1);
900:
901: # ----------------------------------------- Force firewall update for lond port
902:
903: if ((!$justcheckdaemons) && (!$justreload)) {
904: my $now = time;
905: my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
906: $now.$$.int(rand(10000));
907: if (open(my $fh,">$tmpfile")) {
908: my %iphosts = &Apache::lonnet::get_iphost();
909: foreach my $key (keys(%iphosts)) {
910: print $fh "$key\n";
911: }
912: close($fh);
913: if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
914: my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
915: system("$execpath $tmpfile");
916: unlink('/tmp/lock_lciptables'); # Remove the lock file.
917: }
918: unlink($tmpfile);
919: }
920: }
921:
922: # ---------------------------------------------------------------- Start report
923:
924: $errors=0;
925: $warnings=0;
926: $notices=0;
927:
928:
929: my $fh;
930: if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
931: $fh=&start_logging();
932:
933: &log_machine_info($fh);
934: &clean_tmp($fh);
935: &clean_lonIDs($fh);
936: &check_httpd_logs($fh);
937: &rotate_lonnet_logs($fh);
938: &rotate_other_logs($fh);
939: }
940: if (!$justcheckconnections && !$justreload) {
941: &checkon_daemon($fh,'lonmemcached',40000);
942: &checkon_daemon($fh,'lonsql',200000);
943: if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
944: &checkon_daemon($fh,'lond',40000,'USR2');
945: }
946: &checkon_daemon($fh,'lonc',40000,'USR1');
947: &checkon_daemon($fh,'lonmaxima',40000);
948: &checkon_daemon($fh,'lonr',40000);
949: }
950: if ($justreload) {
951: &checkon_daemon($fh,'lond',40000,'USR2');
952: &checkon_daemon($fh,'lonc',40000,'USR2');
953: }
954: if ($justcheckconnections) {
955: &test_connections($fh);
956: }
957: if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
958: &check_delayed_msg($fh);
959: &finish_logging($fh);
960: &log_simplestatus();
961: &write_loncaparevs();
962: &write_serverhomeIDs();
963: &write_checksums();
964: if ($totalcount>200 && !$noemail) { &send_mail(); }
965: }
966: }
967:
968: &main();
969: 1;
970:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>