Annotation of loncom/loncron, revision 1.71
1.1 albertel 1: #!/usr/bin/perl
2:
1.47 albertel 3: # Housekeeping program, started by cron, loncontrol and loncron.pl
4: #
1.71 ! albertel 5: # $Id: loncron,v 1.70 2007/02/02 12:59:13 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;
35:
1.1 albertel 36: use IO::File;
37: use IO::Socket;
1.48 albertel 38: use HTML::Entities;
1.49 albertel 39: use Getopt::Long;
1.46 albertel 40: #globals
41: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
42:
43: my $statusdir="/home/httpd/html/lon-status";
44:
1.1 albertel 45:
46: # -------------------------------------------------- Non-critical communication
47: sub reply {
1.64 albertel 48: my ($cmd,$server,$hostname)=@_;
49: my $peerfile="$perlvar{'lonSockDir'}/".$hostname->{$server};
1.1 albertel 50: my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
51: Type => SOCK_STREAM,
52: Timeout => 10)
53: or return "con_lost";
1.64 albertel 54: print $client "sethost:$server:$cmd\n";
1.1 albertel 55: my $answer=<$client>;
56: chomp($answer);
57: if (!$answer) { $answer="con_lost"; }
58: return $answer;
59: }
60:
61: # --------------------------------------------------------- Output error status
62:
1.46 albertel 63: sub log {
64: my $fh=shift;
65: if ($fh) { print $fh @_ }
66: }
67:
1.1 albertel 68: sub errout {
69: my $fh=shift;
1.46 albertel 70: &log($fh,(<<ENDERROUT));
1.48 albertel 71: <table border="2" bgcolor="#CCCCCC">
1.1 albertel 72: <tr><td>Notices</td><td>$notices</td></tr>
73: <tr><td>Warnings</td><td>$warnings</td></tr>
74: <tr><td>Errors</td><td>$errors</td></tr>
1.48 albertel 75: </table><p><a href="#top">Top</a></p>
1.1 albertel 76: ENDERROUT
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";
86: my $size=(stat($error_fname))[7];
87: if ($size>40000) {
88: &log($fh,"<p>Rotating error logs ...</p>");
89: rename("$error_fname.2","$error_fname.3");
90: rename("$error_fname.1","$error_fname.2");
91: rename("$error_fname","$error_fname.1");
92: }
93: system("$perlvar{'lonDaemons'}/$progname 2>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
1.60 albertel 94: sleep 1;
1.42 albertel 95: if (-e $pidfile) {
1.48 albertel 96: &log($fh,"<p>Seems like it started ...</p>");
1.42 albertel 97: my $lfh=IO::File->new("$pidfile");
98: my $daemonpid=<$lfh>;
99: chomp($daemonpid);
1.62 albertel 100: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.42 albertel 101: return 1;
102: } else {
103: return 0;
104: }
105: }
1.48 albertel 106: &log($fh,"<p>Seems like that did not work!</p>");
1.42 albertel 107: $errors++;
108: return 0;
109: }
110:
111: sub checkon_daemon {
1.59 albertel 112: my ($fh,$daemon,$maxsize,$send,$args)=@_;
1.42 albertel 113:
1.63 albertel 114: my $result;
1.48 albertel 115: &log($fh,'<hr /><a name="'.$daemon.'" /><h2>'.$daemon.'</h2><h3>Log</h3><p style="white-space: pre;"><tt>');
1.57 albertel 116: printf("%-15s ",$daemon);
1.42 albertel 117: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
118: open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46 albertel 119: while (my $line=<DFH>) {
120: &log($fh,"$line");
1.42 albertel 121: if ($line=~/INFO/) { $notices++; }
122: if ($line=~/WARNING/) { $notices++; }
123: if ($line=~/CRITICAL/) { $warnings++; }
124: };
125: close (DFH);
126: }
1.48 albertel 127: &log($fh,"</tt></p>");
1.42 albertel 128:
129: my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
130:
131: my $restartflag=1;
1.46 albertel 132: my $daemonpid;
1.42 albertel 133: if (-e $pidfile) {
134: my $lfh=IO::File->new("$pidfile");
1.46 albertel 135: $daemonpid=<$lfh>;
1.42 albertel 136: chomp($daemonpid);
1.62 albertel 137: if ($daemonpid =~ /^\d+$/ && kill 0 => $daemonpid) {
1.46 albertel 138: &log($fh,"<h3>$daemon at pid $daemonpid responding");
1.59 albertel 139: if ($send) { &log($fh,", sending $send"); }
1.46 albertel 140: &log($fh,"</h3>");
1.59 albertel 141: if ($send eq 'USR1') { kill USR1 => $daemonpid; }
142: if ($send eq 'USR2') { kill USR2 => $daemonpid; }
1.42 albertel 143: $restartflag=0;
1.59 albertel 144: if ($send eq 'USR2') {
1.63 albertel 145: $result = 'reloaded';
1.59 albertel 146: print "reloaded\n";
147: } else {
1.63 albertel 148: $result = 'running';
1.59 albertel 149: print "running\n";
150: }
1.42 albertel 151: } else {
152: $errors++;
1.46 albertel 153: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.42 albertel 154: $restartflag=1;
1.46 albertel 155: &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
1.42 albertel 156: }
157: }
158: if ($restartflag==1) {
159: $simplestatus{$daemon}='off';
160: $errors++;
1.57 albertel 161: my $kadaemon=$daemon;
162: if ($kadaemon eq 'lonmemcached') { $kadaemon='memcached'; }
1.46 albertel 163: &log($fh,'<br><font color="red">Killall '.$daemon.': '.
1.57 albertel 164: `killall $kadaemon 2>&1`.' - ');
1.60 albertel 165: sleep 1;
1.46 albertel 166: &log($fh,unlink($pidfile).' - '.
1.57 albertel 167: `killall -9 $kadaemon 2>&1`.
1.46 albertel 168: '</font><br>');
169: &log($fh,"<h3>$daemon not running, trying to start</h3>");
1.42 albertel 170:
1.50 albertel 171: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46 albertel 172: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42 albertel 173: $simplestatus{$daemon}='restarted';
1.63 albertel 174: $result = 'started';
1.42 albertel 175: print "started\n";
176: } else {
177: $errors++;
1.46 albertel 178: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48 albertel 179: &log($fh,"<p>Give it one more try ...</p>");
1.42 albertel 180: print " ";
1.50 albertel 181: if (&start_daemon($fh,$daemon,$pidfile,$args)) {
1.46 albertel 182: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
1.42 albertel 183: $simplestatus{$daemon}='restarted';
1.63 albertel 184: $result = 'started';
1.42 albertel 185: print "started\n";
186: } else {
1.63 albertel 187: $result = 'failed';
1.42 albertel 188: print " failed\n";
189: $simplestatus{$daemon}='failed';
190: $errors++; $errors++;
1.46 albertel 191: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
1.48 albertel 192: &log($fh,"<p>Unable to start $daemon</p>");
1.42 albertel 193: }
194: }
195:
196: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
1.46 albertel 197: &log($fh,"<p><pre>");
1.42 albertel 198: open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
1.46 albertel 199: while (my $line=<DFH>) {
200: &log($fh,"$line");
1.42 albertel 201: if ($line=~/WARNING/) { $notices++; }
202: if ($line=~/CRITICAL/) { $notices++; }
203: };
204: close (DFH);
1.48 albertel 205: &log($fh,"</pre></p>");
1.42 albertel 206: }
207: }
208:
1.46 albertel 209: my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
1.42 albertel 210:
211: my ($dev,$ino,$mode,$nlink,
212: $uid,$gid,$rdev,$size,
213: $atime,$mtime,$ctime,
214: $blksize,$blocks)=stat($fname);
215:
216: if ($size>$maxsize) {
1.48 albertel 217: &log($fh,"<p>Rotating logs ...</p>");
1.42 albertel 218: rename("$fname.2","$fname.3");
219: rename("$fname.1","$fname.2");
220: rename("$fname","$fname.1");
221: }
222:
223: &errout($fh);
1.63 albertel 224: return $result;
1.42 albertel 225: }
1.1 albertel 226:
1.46 albertel 227: # --------------------------------------------------------------------- Machine
228: sub log_machine_info {
229: my ($fh)=@_;
1.48 albertel 230: &log($fh,'<hr /><a name="machine" /><h2>Machine Information</h2>');
1.46 albertel 231: &log($fh,"<h3>loadavg</h3>");
232:
233: open (LOADAVGH,"/proc/loadavg");
234: my $loadavg=<LOADAVGH>;
235: close (LOADAVGH);
236:
237: &log($fh,"<tt>$loadavg</tt>");
238:
239: my @parts=split(/\s+/,$loadavg);
240: if ($parts[1]>4.0) {
241: $errors++;
242: } elsif ($parts[1]>2.0) {
243: $warnings++;
244: } elsif ($parts[1]>1.0) {
245: $notices++;
246: }
1.13 harris41 247:
1.46 albertel 248: &log($fh,"<h3>df</h3>");
249: &log($fh,"<pre>");
1.14 harris41 250:
1.46 albertel 251: open (DFH,"df|");
252: while (my $line=<DFH>) {
1.48 albertel 253: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 254: @parts=split(/\s+/,$line);
255: my $usage=$parts[4];
256: $usage=~s/\W//g;
257: if ($usage>90) {
258: $warnings++;
259: $notices++;
260: } elsif ($usage>80) {
261: $warnings++;
262: } elsif ($usage>60) {
263: $notices++;
1.31 albertel 264: }
1.46 albertel 265: if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
1.1 albertel 266: }
1.46 albertel 267: close (DFH);
268: &log($fh,"</pre>");
1.1 albertel 269:
270:
1.46 albertel 271: &log($fh,"<h3>ps</h3>");
272: &log($fh,"<pre>");
273: my $psproc=0;
1.1 albertel 274:
1.53 albertel 275: open (PSH,"ps aux --cols 140 |");
1.46 albertel 276: while (my $line=<PSH>) {
1.48 albertel 277: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 278: $psproc++;
279: }
280: close (PSH);
281: &log($fh,"</pre>");
1.1 albertel 282:
1.46 albertel 283: if ($psproc>200) { $notices++; }
284: if ($psproc>250) { $notices++; }
1.1 albertel 285:
1.61 albertel 286: &log($fh,"<h3>distprobe</h3>");
287: &log($fh,"<pre>");
288: open(DSH,"$perlvar{'lonDaemons'}/distprobe |");
289: while (my $line=<DSH>) {
290: &log($fh,&encode_entities($line,'<>&"'));
291: $psproc++;
292: }
293: close(DSH);
294: &log($fh,"</pre>");
295:
1.46 albertel 296: &errout($fh);
297: }
1.1 albertel 298:
1.46 albertel 299: sub start_logging {
300: my ($hostdom,$hostrole,$hostname,$spareid)=@_;
1.43 albertel 301: my $fh=IO::File->new(">$statusdir/newstatus.html");
302: my %simplestatus=();
1.46 albertel 303: my $now=time;
304: my $date=localtime($now);
1.43 albertel 305:
1.46 albertel 306:
307: &log($fh,(<<ENDHEADERS));
1.1 albertel 308: <html>
309: <head>
310: <title>LON Status Report $perlvar{'lonHostID'}</title>
311: </head>
1.3 www 312: <body bgcolor="#AAAAAA">
1.48 albertel 313: <a name="top" />
1.1 albertel 314: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
315: <h2>$date ($now)</h2>
316: <ol>
1.48 albertel 317: <li><a href="#configuration">Configuration</a></li>
318: <li><a href="#machine">Machine Information</a></li>
319: <li><a href="#tmp">Temporary Files</a></li>
320: <li><a href="#tokens">Session Tokens</a></li>
321: <li><a href="#httpd">httpd</a></li>
322: <li><a href="#lonsql">lonsql</a></li>
323: <li><a href="#lond">lond</a></li>
324: <li><a href="#lonc">lonc</a></li>
325: <li><a href="#lonhttpd">lonhttpd</a></li>
326: <li><a href="#lonnet">lonnet</a></li>
327: <li><a href="#connections">Connections</a></li>
328: <li><a href="#delayed">Delayed Messages</a></li>
329: <li><a href="#errcount">Error Count</a></li>
1.1 albertel 330: </ol>
1.48 albertel 331: <hr />
332: <a name="configuration" />
1.1 albertel 333: <h2>Configuration</h2>
334: <h3>PerlVars</h3>
1.48 albertel 335: <table border="2">
1.1 albertel 336: ENDHEADERS
337:
1.46 albertel 338: foreach my $varname (sort(keys(%perlvar))) {
1.48 albertel 339: &log($fh,"<tr><td>$varname</td><td>".
340: &encode_entities($perlvar{$varname},'<>&"')."</td></tr>\n");
1.43 albertel 341: }
1.48 albertel 342: &log($fh,"</table><h3>Hosts</h3><table border='2'>");
1.46 albertel 343: foreach my $id (sort(keys(%{$hostname}))) {
344: &log($fh,
345: "<tr><td>$id</td><td>".$hostdom->{$id}.
346: "</td><td>".$hostrole->{$id}.
347: "</td><td>".$hostname->{$id}."</td></tr>\n");
348: }
349: &log($fh,"</table><h3>Spare Hosts</h3><ol>");
350: foreach my $id (sort(keys(%{$spareid}))) {
1.48 albertel 351: &log($fh,"<li>$id\n</li>");
1.43 albertel 352: }
1.46 albertel 353: &log($fh,"</ol>\n");
354: return $fh;
355: }
1.11 www 356:
357: # --------------------------------------------------------------- clean out tmp
1.46 albertel 358: sub clean_tmp {
359: my ($fh)=@_;
1.48 albertel 360: &log($fh,'<hr /><a name="tmp" /><h2>Temporary Files</h2>');
1.46 albertel 361: my $cleaned=0;
362: my $old=0;
363: while (my $fname=<$perlvar{'lonDaemons'}/tmp/*>) {
1.43 albertel 364: my ($dev,$ino,$mode,$nlink,
365: $uid,$gid,$rdev,$size,
366: $atime,$mtime,$ctime,
367: $blksize,$blocks)=stat($fname);
1.46 albertel 368: my $now=time;
369: my $since=$now-$mtime;
1.43 albertel 370: if ($since>$perlvar{'lonExpire'}) {
1.46 albertel 371: my $line='';
1.43 albertel 372: if (open(PROBE,$fname)) {
373: $line=<PROBE>;
374: close(PROBE);
375: }
376: unless ($line=~/^CHECKOUTTOKEN\&/) {
377: $cleaned++;
378: unlink("$fname");
379: } else {
380: if ($since>365*$perlvar{'lonExpire'}) {
381: $cleaned++;
382: unlink("$fname");
383: } else { $old++; }
384: }
385: }
386: }
1.46 albertel 387: &log($fh,"Cleaned up ".$cleaned." files (".$old." old checkout tokens).");
388: }
1.11 www 389:
390: # ------------------------------------------------------------ clean out lonIDs
1.46 albertel 391: sub clean_lonIDs {
392: my ($fh)=@_;
1.48 albertel 393: &log($fh,'<hr /><a name="tokens" /><h2>Session Tokens</h2>');
1.46 albertel 394: my $cleaned=0;
395: my $active=0;
396: while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
1.43 albertel 397: my ($dev,$ino,$mode,$nlink,
398: $uid,$gid,$rdev,$size,
399: $atime,$mtime,$ctime,
400: $blksize,$blocks)=stat($fname);
1.46 albertel 401: my $now=time;
402: my $since=$now-$mtime;
1.43 albertel 403: if ($since>$perlvar{'lonExpire'}) {
404: $cleaned++;
1.46 albertel 405: &log($fh,"Unlinking $fname<br>");
1.43 albertel 406: unlink("$fname");
407: } else {
408: $active++;
409: }
1.46 albertel 410: }
1.48 albertel 411: &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).</p>");
1.46 albertel 412: &log($fh,"<h3>$active open session(s)</h3>");
413: }
1.43 albertel 414:
1.11 www 415:
1.1 albertel 416: # ----------------------------------------------------------------------- httpd
1.46 albertel 417: sub check_httpd_logs {
418: my ($fh)=@_;
1.48 albertel 419: &log($fh,'<hr /><a name="httpd" /><h2>httpd</h2><h3>Access Log</h3><pre>');
1.43 albertel 420:
421: open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
1.48 albertel 422: while (my $line=<DFH>) { &log($fh,&encode_entities($line,'<>&"')) };
1.43 albertel 423: close (DFH);
1.46 albertel 424:
425: &log($fh,"</pre><h3>Error Log</h3><pre>");
426:
1.43 albertel 427: open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
1.46 albertel 428: while (my $line=<DFH>) {
429: &log($fh,"$line");
1.43 albertel 430: if ($line=~/\[error\]/) { $notices++; }
1.46 albertel 431: }
1.43 albertel 432: close (DFH);
1.46 albertel 433: &log($fh,"</pre>");
1.43 albertel 434: &errout($fh);
1.46 albertel 435: }
1.1 albertel 436:
437: # ---------------------------------------------------------------------- lonnet
438:
1.48 albertel 439: sub rotate_lonnet_logs {
1.46 albertel 440: my ($fh)=@_;
1.48 albertel 441: &log($fh,'<hr /><a name="lonnet" /><h2>lonnet</h2><h3>Temp Log</h3><pre>');
1.43 albertel 442: print "checking logs\n";
443: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
444: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
1.46 albertel 445: while (my $line=<DFH>) {
1.48 albertel 446: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 447: }
1.43 albertel 448: close (DFH);
449: }
1.46 albertel 450: &log($fh,"</pre><h3>Perm Log</h3><pre>");
1.43 albertel 451:
452: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
453: open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
1.46 albertel 454: while (my $line=<DFH>) {
1.48 albertel 455: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 456: }
1.43 albertel 457: close (DFH);
1.46 albertel 458: } else { &log($fh,"No perm log\n") }
1.43 albertel 459:
1.46 albertel 460: my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
1.43 albertel 461:
462: my ($dev,$ino,$mode,$nlink,
463: $uid,$gid,$rdev,$size,
464: $atime,$mtime,$ctime,
465: $blksize,$blocks)=stat($fname);
466:
467: if ($size>40000) {
1.48 albertel 468: &log($fh,"<p>Rotating logs ...</p>");
1.43 albertel 469: rename("$fname.2","$fname.3");
470: rename("$fname.1","$fname.2");
471: rename("$fname","$fname.1");
472: }
1.1 albertel 473:
1.46 albertel 474: &log($fh,"</pre>");
1.43 albertel 475: &errout($fh);
1.46 albertel 476: }
477:
1.43 albertel 478: # ----------------------------------------------------------------- Connections
1.46 albertel 479: sub test_connections {
480: my ($fh,$hostname)=@_;
1.48 albertel 481: &log($fh,'<hr /><a name="connections" /><h2>Connections</h2>');
1.43 albertel 482: print "testing connections\n";
1.48 albertel 483: &log($fh,"<table border='2'>");
1.49 albertel 484: my ($good,$bad)=(0,0);
1.46 albertel 485: foreach my $tryserver (sort(keys(%{$hostname}))) {
1.43 albertel 486: print(".");
1.46 albertel 487: my $result;
1.68 www 488: my $answer=&reply("ping",$tryserver,$hostname);
1.43 albertel 489: if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
490: $result="<b>ok</b>";
1.49 albertel 491: $good++;
1.43 albertel 492: } else {
493: $result=$answer;
494: $warnings++;
1.49 albertel 495: if ($answer eq 'con_lost') {
496: $bad++;
497: $warnings++;
1.50 albertel 498: } else {
499: $good++; #self connection
1.49 albertel 500: }
1.43 albertel 501: }
502: if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
1.46 albertel 503: &log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
1.1 albertel 504: }
1.46 albertel 505: &log($fh,"</table>");
1.49 albertel 506: print "\n$good good, $bad bad connections\n";
1.43 albertel 507: &errout($fh);
1.46 albertel 508: }
509:
510:
1.1 albertel 511: # ------------------------------------------------------------ Delayed messages
1.46 albertel 512: sub check_delayed_msg {
1.68 www 513: my ($fh,$hostname)=@_;
1.48 albertel 514: &log($fh,'<hr /><a name="delayed" /><h2>Delayed Messages</h2>');
1.43 albertel 515: print "checking buffers\n";
1.46 albertel 516:
517: &log($fh,'<h3>Scanning Permanent Log</h3>');
1.1 albertel 518:
1.46 albertel 519: my $unsend=0;
1.1 albertel 520:
1.46 albertel 521: my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
522: while (my $line=<$dfh>) {
523: my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
524: if ($sdf eq 'F') {
525: my $local=localtime($time);
526: &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
527: $warnings++;
1.43 albertel 528: }
1.46 albertel 529: if ($sdf eq 'S') { $unsend--; }
530: if ($sdf eq 'D') { $unsend++; }
1.1 albertel 531: }
1.46 albertel 532:
1.48 albertel 533: &log($fh,"<p>Total unsend messages: <b>$unsend</b></p>\n");
1.43 albertel 534: $warnings=$warnings+5*$unsend;
1.1 albertel 535:
1.43 albertel 536: if ($unsend) { $simplestatus{'unsend'}=$unsend; }
1.48 albertel 537: &log($fh,"<h3>Outgoing Buffer</h3>\n<pre>");
1.68 www 538: # list directory with delayed messages and remember offline servers
539: my %servers=();
1.43 albertel 540: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
1.68 www 541: while (my $line=<DFH>) {
542: my ($server)=($line=~/\.(\w+)$/);
543: if ($server) { $servers{$server}=1; }
1.48 albertel 544: &log($fh,&encode_entities($line,'<>&"'));
1.46 albertel 545: }
1.48 albertel 546: &log($fh,"</pre>\n");
1.43 albertel 547: close (DFH);
1.68 www 548: # pong to all servers that have delayed messages
549: # this will trigger a reverse connection, which should flush the buffers
550: foreach my $tryserver (keys %servers) {
551: my $answer=&reply("pong",$tryserver,$hostname);
1.69 www 552: &log($fh,"Pong to $tryserver: $answer<br />");
1.68 www 553: }
1.46 albertel 554: }
1.1 albertel 555:
1.46 albertel 556: sub finish_logging {
557: my ($fh)=@_;
1.48 albertel 558: &log($fh,"<a name='errcount' />\n");
1.43 albertel 559: $totalcount=$notices+4*$warnings+100*$errors;
560: &errout($fh);
1.46 albertel 561: &log($fh,"<h1>Total Error Count: $totalcount</h1>");
562: my $now=time;
563: my $date=localtime($now);
1.48 albertel 564: &log($fh,"<hr />$date ($now)</body></html>\n");
1.43 albertel 565: print "lon-status webpage updated\n";
566: $fh->close();
1.46 albertel 567:
568: if ($errors) { $simplestatus{'errors'}=$errors; }
569: if ($warnings) { $simplestatus{'warnings'}=$warnings; }
570: if ($notices) { $simplestatus{'notices'}=$notices; }
571: $simplestatus{'time'}=time;
1.1 albertel 572: }
573:
1.46 albertel 574: sub log_simplestatus {
575: rename ("$statusdir/newstatus.html","$statusdir/index.html");
576:
1.43 albertel 577: my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
578: foreach (keys %simplestatus) {
579: print $sfh $_.'='.$simplestatus{$_}.'&';
580: }
581: print $sfh "\n";
582: $sfh->close();
1.41 www 583: }
1.46 albertel 584:
585: sub send_mail {
1.43 albertel 586: print "sending mail\n";
1.46 albertel 587: my $emailto="$perlvar{'lonAdmEMail'}";
1.54 www 588: if ($totalcount>2500) {
1.43 albertel 589: $emailto.=",$perlvar{'lonSysEMail'}";
590: }
1.46 albertel 591: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
1.52 albertel 592:
1.58 albertel 593: my $result=system("metasend -b -S 4000000 -t $emailto -s '$subj' -f $statusdir/index.html -m text/html >& /dev/null");
1.52 albertel 594: if ($result != 0) {
595: $result=system("mail -s '$subj' $emailto < $statusdir/index.html");
596: }
1.1 albertel 597: }
1.46 albertel 598:
1.49 albertel 599: sub usage {
600: print(<<USAGE);
601: loncron - housekeeping program that checks up on various parts of Lon-CAPA
602:
603: Options:
1.71 ! albertel 604: --help Display
1.49 albertel 605: --noemail Do not send the status email
606: --justcheckconnections Only check the current status of the lonc/d
607: connections, do not send emails do not
608: check if the daemons are running, do not
609: generate lon-status
610: --justcheckdaemons Only check that all of the Lon-CAPA daemons are
611: running, do not send emails do not
612: check the lonc/d connections, do not
613: generate lon-status
1.59 albertel 614: --justreload Only tell the daemons to reload the config files,
615: do not send emails do not
616: check if the daemons are running, do not
617: generate lon-status
1.49 albertel 618:
619: USAGE
620: }
621:
1.46 albertel 622: # ================================================================ Main Program
623: sub main () {
1.71 ! albertel 624: my ($help,$justcheckdaemons,$noemail,$justcheckconnections,
1.59 albertel 625: $justreload);
1.49 albertel 626: &GetOptions("help" => \$help,
627: "justcheckdaemons" => \$justcheckdaemons,
628: "noemail" => \$noemail,
1.59 albertel 629: "justcheckconnections" => \$justcheckconnections,
630: "justreload" => \$justreload
1.49 albertel 631: );
632: if ($help) { &usage(); return; }
1.46 albertel 633: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
634: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
635: %perlvar=%{$perlvarref};
636: undef $perlvarref;
637: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
638: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
639:
640: # --------------------------------------- Make sure that LON-CAPA is configured
641: # I only test for one thing here (lonHostID). This is just a safeguard.
642: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
643: print("Unconfigured machine.\n");
644: my $emailto=$perlvar{'lonSysEMail'};
645: my $hostname=`/bin/hostname`;
646: chop $hostname;
647: $hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
648: my $subj="LON: Unconfigured machine $hostname";
649: system("echo 'Unconfigured machine $hostname.' |\
650: mailto $emailto -s '$subj' > /dev/null");
651: exit 1;
652: }
653:
654: # ----------------------------- Make sure this process is running from user=www
655: my $wwwid=getpwnam('www');
656: if ($wwwid!=$<) {
657: print("User ID mismatch. This program must be run as user 'www'\n");
658: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
659: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
660: system("echo 'User ID mismatch. loncron must be run as user www.' |\
661: mailto $emailto -s '$subj' > /dev/null");
662: exit 1;
663: }
664:
665: # ------------------------------------------------------------- Read hosts file
666: my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");
667:
668: my (%hostname,%hostdom,%hostrole,%spareid);
669: while (my $configline=<$config>) {
1.49 albertel 670: next if ($configline =~ /^(\#|\s*\$)/);
1.56 albertel 671: my ($id,$domain,$role,$name)=split(/:/,$configline);
672: if ($id && $domain && $role && $name) {
673: $name=~s/\s//g;
1.46 albertel 674: $hostname{$id}=$name;
675: $hostdom{$id}=$domain;
676: $hostrole{$id}=$role;
677: }
678: }
679: undef $config;
680:
681: # ------------------------------------------------------ Read spare server file
682: $config=IO::File->new("$perlvar{'lonTabDir'}/spare.tab");
683:
684: while (my $configline=<$config>) {
685: chomp($configline);
686: if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
687: $spareid{$configline}=1;
688: }
689: }
690: undef $config;
691:
692: # ---------------------------------------------------------------- Start report
693:
694: $errors=0;
695: $warnings=0;
696: $notices=0;
697:
698:
1.49 albertel 699: my $fh;
1.59 albertel 700: if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.49 albertel 701: $fh=&start_logging(\%hostdom,\%hostrole,\%hostname,\%spareid);
702:
703: &log_machine_info($fh);
704: &clean_tmp($fh);
705: &clean_lonIDs($fh);
706: &check_httpd_logs($fh);
707: &rotate_lonnet_logs($fh);
708: }
1.59 albertel 709: if (!$justcheckconnections && !$justreload) {
1.49 albertel 710: &checkon_daemon($fh,'lonsql',200000);
1.63 albertel 711: if ( &checkon_daemon($fh,'lond',40000,'USR1') eq 'running') {
712: &checkon_daemon($fh,'lond',40000,'USR2');
713: }
1.71 ! albertel 714: &checkon_daemon($fh,'lonc',40000,'USR1');
1.49 albertel 715: &checkon_daemon($fh,'lonhttpd',40000);
1.57 albertel 716: &checkon_daemon($fh,'lonmemcached',40000);
1.70 raeburn 717: &checkon_daemon($fh,'lonmaxima',40000);
1.49 albertel 718: }
1.59 albertel 719: if ($justreload) {
720: &checkon_daemon($fh,'lond',40000,'USR2');
1.71 ! albertel 721: &checkon_daemon($fh,'lonc',40000,'USR2');
1.59 albertel 722: }
1.63 albertel 723: if ($justcheckconnections) {
1.49 albertel 724: &test_connections($fh,\%hostname);
725: }
1.59 albertel 726: if (!$justcheckdaemons && !$justcheckconnections && !$justreload) {
1.68 www 727: &check_delayed_msg($fh,\%hostname);
1.49 albertel 728: &finish_logging($fh);
729: &log_simplestatus();
730:
731: if ($totalcount>200 && !$noemail) { &send_mail(); }
732: }
1.46 albertel 733: }
734:
735: &main();
1.1 albertel 736: 1;
737:
738:
739:
740:
741:
742:
743:
744:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>