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