Annotation of loncom/loncron, revision 1.100
1.1 albertel 1: #!/usr/bin/perl
2:
1.47 albertel 3: # Housekeeping program, started by cron, loncontrol and loncron.pl
4: #
1.100 ! bisitz 5: # $Id: loncron,v 1.99 2013/05/29 18:10:54 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.46 albertel 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.46 albertel 165: '</font><br>');
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.1 albertel 288: <html>
289: <head>
290: <title>LON Status Report $perlvar{'lonHostID'}</title>
291: </head>
1.3 www 292: <body bgcolor="#AAAAAA">
1.48 albertel 293: <a name="top" />
1.1 albertel 294: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
295: <h2>$date ($now)</h2>
296: <ol>
1.48 albertel 297: <li><a href="#configuration">Configuration</a></li>
298: <li><a href="#machine">Machine Information</a></li>
299: <li><a href="#tmp">Temporary Files</a></li>
300: <li><a href="#tokens">Session Tokens</a></li>
301: <li><a href="#httpd">httpd</a></li>
302: <li><a href="#lonsql">lonsql</a></li>
303: <li><a href="#lond">lond</a></li>
304: <li><a href="#lonc">lonc</a></li>
305: <li><a href="#lonnet">lonnet</a></li>
306: <li><a href="#connections">Connections</a></li>
307: <li><a href="#delayed">Delayed Messages</a></li>
308: <li><a href="#errcount">Error Count</a></li>
1.1 albertel 309: </ol>
1.48 albertel 310: <hr />
311: <a name="configuration" />
1.1 albertel 312: <h2>Configuration</h2>
313: <h3>PerlVars</h3>
1.48 albertel 314: <table border="2">
1.1 albertel 315: ENDHEADERS
316:
1.46 albertel 317: foreach my $varname (sort(keys(%perlvar))) {
1.48 albertel 318: &log($fh,"<tr><td>$varname</td><td>".
319: &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43 albertel 320: }
1.48 albertel 321: &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.72 albertel 322: my %hostname = &Apache::lonnet::all_hostnames();
323: foreach my $id (sort(keys(%hostname))) {
324: my $role = (&Apache::lonnet::is_library($id) ? 'library'
325: : 'access');
1.46 albertel 326: &log($fh,
1.72 albertel 327: "<tr><td>$id</td><td>".&Apache::lonnet::host_domain($id).
328: "</td><td>".$role.
329: "</td><td>".&Apache::lonnet::hostname($id)."</td></tr>\n");
330: }
331: &log($fh,"</table><h3>Spare Hosts</h3><ul>");
332: foreach my $type (sort(keys(%Apache::lonnet::spareid))) {
333: &log($fh,"<li>$type\n<ol>");
334: foreach my $id (@{ $Apache::lonnet::spareid{$type} }) {
335: &log($fh,"<li>$id</li>\n");
336: }
337: &log($fh,"</ol>\n</li>\n");
1.43 albertel 338: }
1.72 albertel 339: &log($fh,"</ul>\n");
1.46 albertel 340: return $fh;
341: }
1.11 www 342:
343: # --------------------------------------------------------------- clean out tmp
1.46 albertel 344: sub clean_tmp {
345: my ($fh)=@_;
1.48 albertel 346: &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.82 raeburn 347: my ($cleaned,$old,$removed) = (0,0,0);
348: my %errors = (
349: dir => [],
350: file => [],
351: failopen => [],
352: );
353: my %error_titles = (
354: dir => 'failed to remove empty directory:',
355: file => 'failed to unlike stale file',
356: failopen => 'failed to open file or directory'
357: );
358: ($cleaned,$old,$removed) = &recursive_clean_tmp('',$cleaned,$old,$removed,\%errors);
359: &log($fh,"Cleaned up: ".$cleaned." files; removed: $removed empty directories; (found: $old old checkout tokens)");
360: foreach my $key (sort(keys(%errors))) {
361: if (ref($errors{$key}) eq 'ARRAY') {
362: if (@{$errors{$key}} > 0) {
363: &log($fh,"Error during cleanup ($error_titles{$key}):<ul><li>".
364: join('</li><li><tt>',@{$errors{$key}}).'</tt></li></ul><br />');
365: }
366: }
367: }
368: }
369:
370: sub recursive_clean_tmp {
371: my ($subdir,$cleaned,$old,$removed,$errors) = @_;
372: my $base = "$perlvar{'lonDaemons'}/tmp";
373: my $path = $base;
374: next if ($subdir =~ m{\.\./});
375: next unless (ref($errors) eq 'HASH');
376: unless ($subdir eq '') {
377: $path .= '/'.$subdir;
378: }
379: if (opendir(my $dh,"$path")) {
380: while (my $file = readdir($dh)) {
381: next if ($file =~ /^\.\.?$/);
382: my $fname = "$path/$file";
383: if (-d $fname) {
384: my $innerdir;
385: if ($subdir eq '') {
386: $innerdir = $file;
387: } else {
388: $innerdir = $subdir.'/'.$file;
389: }
390: ($cleaned,$old,$removed) =
391: &recursive_clean_tmp($innerdir,$cleaned,$old,$removed,$errors);
392: my @doms = &Apache::lonnet::current_machine_domains();
393:
394: if (open(my $dirhandle,$fname)) {
395: unless (($innerdir eq 'helprequests') ||
396: (($innerdir =~ /^addcourse/) && ($innerdir !~ m{/\d+$}))) {
397: my @contents = grep {!/^\.\.?$/} readdir($dirhandle);
398: join('&&',@contents)."\n";
399: if (scalar(grep {!/^\.\.?$/} readdir($dirhandle)) == 0) {
400: closedir($dirhandle);
401: if ($fname =~ m{^\Q$perlvar{'lonDaemons'}\E/tmp/}) {
402: if (rmdir($fname)) {
403: $removed ++;
404: } elsif (ref($errors->{dir}) eq 'ARRAY') {
405: push(@{$errors->{dir}},$fname);
406: }
407: }
408: }
409: } else {
410: closedir($dirhandle);
411: }
412: }
413: } else {
414: my ($dev,$ino,$mode,$nlink,
415: $uid,$gid,$rdev,$size,
416: $atime,$mtime,$ctime,
417: $blksize,$blocks)=stat($fname);
418: my $now=time;
419: my $since=$now-$mtime;
420: if ($since>$perlvar{'lonExpire'}) {
421: if ($subdir eq '') {
422: my $line='';
423: if ($fname =~ /\.db$/) {
424: if (unlink($fname)) {
425: $cleaned++;
426: } elsif (ref($errors->{file}) eq 'ARRAY') {
427: push(@{$errors->{file}},$fname);
428: }
429: } elsif (open(PROBE,$fname)) {
430: my $line='';
431: $line=<PROBE>;
432: close(PROBE);
433: if ($line=~/^CHECKOUTTOKEN\&/) {
434: if ($since>365*$perlvar{'lonExpire'}) {
435: if (unlink($fname)) {
436: $cleaned++;
437: } elsif (ref($errors->{file}) eq 'ARRAY') {
438: push(@{$errors->{file}},$fname);
439: }
440: } else {
441: $old++;
442: }
443: } else {
444: if (unlink($fname)) {
445: $cleaned++;
446: } elsif (ref($errors->{file}) eq 'ARRAY') {
447: push(@{$errors->{file}},$fname);
448: }
449: }
450: } elsif (ref($errors->{failopen}) eq 'ARRAY') {
451: push(@{$errors->{failopen}},$fname);
452: }
453: } else {
454: if (unlink($fname)) {
455: $cleaned++;
456: } elsif (ref($errors->{file}) eq 'ARRAY') {
457: push(@{$errors->{file}},$fname);
458: }
459: }
460: }
461: }
462: }
463: closedir($dh);
464: } elsif (ref($errors->{failopen}) eq 'ARRAY') {
465: push(@{$errors->{failopen}},$path);
1.43 albertel 466: }
1.82 raeburn 467: return ($cleaned,$old,$removed);
1.46 albertel 468: }
1.11 www 469:
470: # ------------------------------------------------------------ clean out lonIDs
1.46 albertel 471: sub clean_lonIDs {
472: my ($fh)=@_;
1.48 albertel 473: &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46 albertel 474: my $cleaned=0;
475: my $active=0;
476: while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.43 albertel 477: my ($dev,$ino,$mode,$nlink,
478: $uid,$gid,$rdev,$size,
479: $atime,$mtime,$ctime,
480: $blksize,$blocks)=stat($fname);
1.46 albertel 481: my $now=time;
482: my $since=$now-$mtime;
1.43 albertel 483: if ($since>$perlvar{'lonExpire'}) {
484: $cleaned++;
1.46 albertel 485: &log($fh,"Unlinking $fname<br>");
1.43 albertel 486: unlink("$fname");
487: } else {
488: $active++;
489: }
1.46 albertel 490: }
1.48 albertel 491: &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46 albertel 492: &log($fh,"<h3>$active open session(s)</h3>");
493: }
1.43 albertel 494:
1.74 albertel 495: # ----------------------------------------------------------- clean out sockets
496: sub clean_sockets {
497: my ($fh)=@_;
498: my $cleaned=0;
499: opendir(SOCKETS,$perlvar{'lonSockDir'});
500: while (my $fname=readdir(SOCKETS)) {
501: next if (-d $fname
1.80 www 502: || $fname=~/(mysqlsock|maximasock|rsock|\Q$perlvar{'lonSockDir'}\E)/);
1.74 albertel 503: $cleaned++;
504: &log($fh,"Unlinking $fname<br />");
505: unlink("/home/httpd/sockets/$fname");
506: }
507: &log($fh,"<p>Cleaned up ".$cleaned." stale sockets.</p>");
508: }
509:
1.11 www 510:
1.1 albertel 511: # ----------------------------------------------------------------------- httpd
1.46 albertel 512: sub check_httpd_logs {
513: my ($fh)=@_;
1.94 raeburn 514: if (open(PIPE,"./lchttpdlogs|")) {
1.93 raeburn 515: while (my $line=<PIPE>) {
516: &log($fh,$line);
517: if ($line=~/\[error\]/) { $notices++; }
518: }
519: close(PIPE);
1.46 albertel 520: }
1.43 albertel 521: &errout($fh);
1.46 albertel 522: }
1.1 albertel 523:
524: # ---------------------------------------------------------------------- lonnet
525:
1.48 albertel 526: sub rotate_lonnet_logs {
1.46 albertel 527: my ($fh)=@_;
1.48 albertel 528: &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.100 ! bisitz 529: print "Checking logs.\n";
1.43 albertel 530: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
531: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.46 albertel 532: while (my $line=<DFH>) {
1.48 albertel 533: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 534: }
1.43 albertel 535: close (DFH);
536: }
1.46 albertel 537: &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.43 albertel 538:
539: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
540: open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.46 albertel 541: while (my $line=<DFH>) {
1.48 albertel 542: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 543: }
1.43 albertel 544: close (DFH);
1.46 albertel 545: } else { &log($fh,"No perm log\n") }
1.43 albertel 546:
1.46 albertel 547: my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.73 albertel 548: &rotate_logfile($fname,$fh,'lonnet log');
1.1 albertel 549:
1.46 albertel 550: &log($fh,"</pre>");
1.43 albertel 551: &errout($fh);
1.46 albertel 552: }
553:
1.73 albertel 554: sub rotate_other_logs {
555: my ($fh) = @_;
1.83 raeburn 556: my %logs = (
557: autoenroll => 'Auto Enroll log',
558: autocreate => 'Create Course log',
559: searchcat => 'Search Cataloguing log',
560: autoupdate => 'Auto Update log',
561: refreshcourseids_db => 'Refresh CourseIDs db log',
562: );
563: foreach my $item (keys(%logs)) {
564: my $fname=$perlvar{'lonDaemons'}.'/logs/'.$item.'.log';
565: &rotate_logfile($fname,$fh,$logs{$item});
566: }
1.73 albertel 567: }
568:
1.43 albertel 569: # ----------------------------------------------------------------- Connections
1.46 albertel 570: sub test_connections {
1.72 albertel 571: my ($fh)=@_;
1.48 albertel 572: &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.100 ! bisitz 573: print "Testing connections.\n";
1.48 albertel 574: &log($fh,"<table border='2'>");
1.49 albertel 575: my ($good,$bad)=(0,0);
1.72 albertel 576: my %hostname = &Apache::lonnet::all_hostnames();
577: foreach my $tryserver (sort(keys(%hostname))) {
1.43 albertel 578: print(".");
1.46 albertel 579: my $result;
1.72 albertel 580: my $answer=&Apache::lonnet::reply("ping",$tryserver);
1.43 albertel 581: if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
582: $result="<b>ok</b>";
1.49 albertel 583: $good++;
1.43 albertel 584: } else {
585: $result=$answer;
586: $warnings++;
1.49 albertel 587: if ($answer eq 'con_lost') {
588: $bad++;
589: $warnings++;
1.50 albertel 590: } else {
591: $good++; #self connection
1.49 albertel 592: }
1.43 albertel 593: }
594: if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46 albertel 595: &log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1 albertel 596: }
1.46 albertel 597: &log($fh,"</table>");
1.49 albertel 598: print "\n$good good, $bad bad connections\n";
1.43 albertel 599: &errout($fh);
1.46 albertel 600: }
601:
602:
1.1 albertel 603: # ------------------------------------------------------------ Delayed messages
1.46 albertel 604: sub check_delayed_msg {
1.72 albertel 605: my ($fh)=@_;
1.48 albertel 606: &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.100 ! bisitz 607: print "Checking buffers.\n";
1.46 albertel 608:
609: &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1 albertel 610:
1.46 albertel 611: my $unsend=0;
1.1 albertel 612:
1.46 albertel 613: my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
614: while (my $line=<$dfh>) {
615: my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
616: if ($sdf eq 'F') {
617: my $local=localtime($time);
618: &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
619: $warnings++;
1.43 albertel 620: }
1.46 albertel 621: if ($sdf eq 'S') { $unsend--; }
622: if ($sdf eq 'D') { $unsend++; }
1.1 albertel 623: }
1.46 albertel 624:
1.48 albertel 625: &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.95 raeburn 626: if ($unsend > 0) {
627: $warnings=$warnings+5*$unsend;
628: }
1.1 albertel 629:
1.43 albertel 630: if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48 albertel 631: &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68 www 632: # list directory with delayed messages and remember offline servers
633: my %servers=();
1.43 albertel 634: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68 www 635: while (my $line=<DFH>) {
636: my ($server)=($line=~/\.(\w+)$/);
637: if ($server) { $servers{$server}=1; }
1.48 albertel 638: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 639: }
1.48 albertel 640: &log($fh,"</pre>\n");
1.43 albertel 641: close (DFH);
1.95 raeburn 642: my %hostname = &Apache::lonnet::all_hostnames();
643: my $numhosts = scalar(keys(%hostname));
1.68 www 644: # pong to all servers that have delayed messages
645: # this will trigger a reverse connection, which should flush the buffers
1.95 raeburn 646: foreach my $tryserver (sort(keys(%servers))) {
647: if ($hostname{$tryserver} || !$numhosts) {
648: my $answer;
649: eval {
650: local $SIG{ ALRM } = sub { die "TIMEOUT" };
651: alarm(20);
652: $answer = &Apache::lonnet::reply("pong",$tryserver);
653: alarm(0);
654: };
655: if ($@ && $@ =~ m/TIMEOUT/) {
656: &log($fh,"Attempted pong to $tryserver timed out<br />");
1.100 ! bisitz 657: print "Time out while contacting: $tryserver for pong.\n";
1.95 raeburn 658: } else {
659: &log($fh,"Pong to $tryserver: $answer<br />");
660: }
1.91 raeburn 661: } else {
1.95 raeburn 662: &log($fh,"$tryserver has delayed messages, but is not part of the cluster -- skipping 'Pong'.<br />");
1.91 raeburn 663: }
1.68 www 664: }
1.46 albertel 665: }
1.1 albertel 666:
1.46 albertel 667: sub finish_logging {
668: my ($fh)=@_;
1.48 albertel 669: &log($fh,"<a name='errcount' />\n");
1.43 albertel 670: $totalcount=$notices+4*$warnings+100*$errors;
671: &errout($fh);
1.46 albertel 672: &log($fh,"<h1>Total Error Count: $totalcount</h1>");
673: my $now=time;
674: my $date=localtime($now);
1.48 albertel 675: &log($fh,"<hr />$date ($now)</body></html>\n");
1.100 ! bisitz 676: print "lon-status webpage updated.\n";
1.43 albertel 677: $fh->close();
1.46 albertel 678:
679: if ($errors) { $simplestatus{'errors'}=$errors; }
680: if ($warnings) { $simplestatus{'warnings'}=$warnings; }
681: if ($notices) { $simplestatus{'notices'}=$notices; }
682: $simplestatus{'time'}=time;
1.1 albertel 683: }
684:
1.46 albertel 685: sub log_simplestatus {
1.73 albertel 686: rename("$statusdir/newstatus.html","$statusdir/index.html");
1.46 albertel 687:
1.43 albertel 688: my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
689: foreach (keys %simplestatus) {
690: print $sfh $_.'='.$simplestatus{$_}.'&';
691: }
692: print $sfh "\n";
693: $sfh->close();
1.41 www 694: }
1.46 albertel 695:
1.84 raeburn 696: sub write_loncaparevs {
1.100 ! bisitz 697: print "Retrieving LON-CAPA version information.\n";
1.99 raeburn 698: my %hostname = &Apache::lonnet::all_hostnames();
699: my $output;
700: foreach my $id (sort(keys(%hostname))) {
701: if ($id ne '') {
702: my $loncaparev;
703: eval {
704: local $SIG{ ALRM } = sub { die "TIMEOUT" };
705: alarm(10);
706: $loncaparev =
707: &Apache::lonnet::get_server_loncaparev('',$id,1,'loncron');
708: alarm(0);
709: };
710: if ($@ && $@ =~ m/TIMEOUT/) {
1.100 ! bisitz 711: print "Time out while contacting lonHost: $id for version.\n";
1.99 raeburn 712: }
713: if ($loncaparev =~ /^[\w.\-]+$/) {
714: $output .= $id.':'.$loncaparev."\n";
715: }
716: }
717: }
718: if ($output) {
719: if (open(my $fh,">$perlvar{'lonTabDir'}/loncaparevs.tab")) {
720: print $fh $output;
721: close($fh);
722: &Apache::lonnet::load_loncaparevs();
723: }
724: }
725: return;
726: }
727:
728: sub write_serverhomeIDs {
1.100 ! bisitz 729: print "Retrieving LON-CAPA lonHostID information.\n";
1.99 raeburn 730: my %name_to_host = &Apache::lonnet::all_names();
731: my $output;
732: foreach my $name (sort(keys(%name_to_host))) {
733: if ($name ne '') {
734: if (ref($name_to_host{$name}) eq 'ARRAY') {
735: my $serverhomeID;
1.90 raeburn 736: eval {
737: local $SIG{ ALRM } = sub { die "TIMEOUT" };
738: alarm(10);
1.99 raeburn 739: $serverhomeID =
740: &Apache::lonnet::get_server_homeID($name,1,'loncron');
1.90 raeburn 741: alarm(0);
742: };
743: if ($@ && $@ =~ m/TIMEOUT/) {
1.99 raeburn 744: print "Time out while contacting server: $name\n";
1.90 raeburn 745: }
1.99 raeburn 746: if ($serverhomeID ne '') {
747: $output .= $name.':'.$serverhomeID."\n";
748: } else {
749: $output .= $name.':'.$name_to_host{$name}->[0]."\n";
1.84 raeburn 750: }
751: }
752: }
753: }
1.99 raeburn 754: if ($output) {
755: if (open(my $fh,">$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
756: print $fh $output;
757: close($fh);
758: &Apache::lonnet::load_serverhomeIDs();
1.85 raeburn 759: }
760: }
761: return;
762: }
763:
1.96 raeburn 764: sub write_checksums {
1.98 raeburn 765: my $distro = &LONCAPA::distro();
1.96 raeburn 766: if ($distro) {
767: print "Retrieving file version and checksumming.\n";
1.97 raeburn 768: my $numchksums = 0;
1.96 raeburn 769: my ($chksumsref,$versionsref) =
1.97 raeburn 770: &LONCAPA::Checksumming::get_checksums($distro,$perlvar{'lonDaemons'},
771: $perlvar{'lonLib'},
772: $perlvar{'lonIncludes'},
773: $perlvar{'lonTabDir'});
1.96 raeburn 774: if (ref($chksumsref) eq 'HASH') {
775: $numchksums = scalar(keys(%{$chksumsref}));
776: }
777: print "File version retrieved and checksumming completed for $numchksums files.\n";
778: } else {
779: print "File version retrieval and checksumming skipped - could not determine Linux distro.\n";
780: }
1.97 raeburn 781: return;
1.96 raeburn 782: }
783:
1.46 albertel 784: sub send_mail {
1.100 ! bisitz 785: print "Sending mail.\n";
1.79 raeburn 786: my $defdom = $perlvar{'lonDefDomain'};
787: my $origmail = $perlvar{'lonAdmEMail'};
1.78 raeburn 788: my $emailto = &Apache::loncommon::build_recipient_list(undef,
789: 'lonstatusmail',$defdom,$origmail);
1.54 www 790: if ($totalcount>2500) {
1.43 albertel 791: $emailto.=",$perlvar{'lonSysEMail'}";
792: }
1.46 albertel 793: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
1.52 albertel 794:
1.58 albertel 795: my $result=system("metasend -b -S 4000000 -t $emailto -s '$subj' -f $statusdir/index.html -m text/html >& /dev/null");
1.52 albertel 796: if ($result != 0) {
797: $result=system("mail -s '$subj' $emailto < $statusdir/index.html");
798: }
1.1 albertel 799: }
1.46 albertel 800:
1.49 albertel 801: sub usage {
802: print(<<USAGE);
1.100 ! bisitz 803: loncron - housekeeping program that checks up on various parts of LON-CAPA
1.49 albertel 804:
805: Options:
1.71 albertel 806: --help Display
1.49 albertel 807: --noemail Do not send the status email
808: --justcheckconnections Only check the current status of the lonc/d
809: connections, do not send emails do not
810: check if the daemons are running, do not
811: generate lon-status
812: --justcheckdaemons Only check that all of the Lon-CAPA daemons are
813: running, do not send emails do not
814: check the lonc/d connections, do not
815: generate lon-status
1.59 albertel 816: --justreload Only tell the daemons to reload the config files,
817: do not send emails do not
818: check if the daemons are running, do not
819: generate lon-status
1.49 albertel 820:
821: USAGE
822: }
823:
1.46 albertel 824: # ================================================================ Main Program
825: sub main () {
1.71 albertel 826: my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.59 albertel 827: $justreload);
1.49 albertel 828: &GetOptions("help" => \$help,
829: "justcheckdaemons" => \$justcheckdaemons,
830: "noemail" => \$noemail,
1.59 albertel 831: "justcheckconnections" => \$justcheckconnections,
832: "justreload" => \$justreload
1.49 albertel 833: );
834: if ($help) { &usage(); return; }
1.46 albertel 835: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
836: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
837: %perlvar=%{$perlvarref};
838: undef $perlvarref;
839: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
840: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
1.75 albertel 841: chdir($perlvar{'lonDaemons'});
1.46 albertel 842: # --------------------------------------- Make sure that LON-CAPA is configured
843: # I only test for one thing here (lonHostID). This is just a safeguard.
844: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
845: print("Unconfigured machine.\n");
846: my $emailto=$perlvar{'lonSysEMail'};
847: my $hostname=`/bin/hostname`;
848: chop $hostname;
849: $hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
850: my $subj="LON: Unconfigured machine $hostname";
851: system("echo 'Unconfigured machine $hostname.' |\
852: mailto $emailto -s '$subj' > /dev/null");
853: exit 1;
854: }
855:
856: # ----------------------------- Make sure this process is running from user=www
857: my $wwwid=getpwnam('www');
858: if ($wwwid!=$<) {
1.100 ! bisitz 859: print("User ID mismatch. This program must be run as user 'www'.\n");
1.46 albertel 860: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
861: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
1.100 ! bisitz 862: system("echo 'User ID mismatch. loncron must be run as user www.' |\
1.46 albertel 863: mailto $emailto -s '$subj' > /dev/null");
864: exit 1;
865: }
866:
1.72 albertel 867: # -------------------------------------------- Force reload of host information
868: &Apache::lonnet::load_hosts_tab(1);
869: &Apache::lonnet::load_domain_tab(1);
870: &Apache::lonnet::get_iphost(1);
1.46 albertel 871:
1.81 raeburn 872: # ----------------------------------------- Force firewall update for lond port
873:
874: if ((!$justcheckdaemons) && (!$justreload)) {
875: my $now = time;
876: my $tmpfile = $perlvar{'lonDaemons'}.'/tmp/lciptables_iphost_'.
877: $now.$$.int(rand(10000));
878: if (open(my $fh,">$tmpfile")) {
879: my %iphosts = &Apache::lonnet::get_iphost();
880: foreach my $key (keys(%iphosts)) {
881: print $fh "$key\n";
882: }
883: close($fh);
1.89 raeburn 884: if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
885: my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
886: system("$execpath $tmpfile");
887: unlink('/tmp/lock_lciptables'); # Remove the lock file.
888: }
1.88 raeburn 889: unlink($tmpfile);
1.81 raeburn 890: }
891: }
892:
1.46 albertel 893: # ---------------------------------------------------------------- Start report
894:
895: $errors=0;
896: $warnings=0;
897: $notices=0;
898:
899:
1.49 albertel 900: my $fh;
1.59 albertel 901: if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72 albertel 902: $fh=&start_logging();
1.49 albertel 903:
904: &log_machine_info($fh);
905: &clean_tmp($fh);
906: &clean_lonIDs($fh);
907: &check_httpd_logs($fh);
908: &rotate_lonnet_logs($fh);
1.73 albertel 909: &rotate_other_logs($fh);
1.49 albertel 910: }
1.59 albertel 911: if (!$justcheckconnections && !$justreload) {
1.76 albertel 912: &checkon_daemon($fh,'lonmemcached',40000);
1.49 albertel 913: &checkon_daemon($fh,'lonsql',200000);
1.63 albertel 914: if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
915: &checkon_daemon($fh,'lond',40000,'USR2');
916: }
1.71 albertel 917: &checkon_daemon($fh,'lonc',40000,'USR1');
1.70 raeburn 918: &checkon_daemon($fh,'lonmaxima',40000);
1.80 www 919: &checkon_daemon($fh,'lonr',40000);
1.49 albertel 920: }
1.59 albertel 921: if ($justreload) {
922: &checkon_daemon($fh,'lond',40000,'USR2');
1.71 albertel 923: &checkon_daemon($fh,'lonc',40000,'USR2');
1.59 albertel 924: }
1.63 albertel 925: if ($justcheckconnections) {
1.72 albertel 926: &test_connections($fh);
1.49 albertel 927: }
1.59 albertel 928: if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.72 albertel 929: &check_delayed_msg($fh);
1.49 albertel 930: &finish_logging($fh);
931: &log_simplestatus();
1.87 raeburn 932: &write_loncaparevs();
933: &write_serverhomeIDs();
1.97 raeburn 934: &write_checksums();
1.49 albertel 935: if ($totalcount>200 && !$noemail) { &send_mail(); }
936: }
1.46 albertel 937: }
938:
939: &main();
1.1 albertel 940: 1;
941:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>