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