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