1: #!/usr/bin/perl
2:
3: # The LearningOnline Network
4: # Housekeeping program, started by cron
5: #
6: # (TCP networking package
7: # 6/1/99,6/2,6/10,6/11,6/12,6/14,6/26,6/28,6/29,6/30,
8: # 7/1,7/2,7/9,7/10,7/12 Gerd Kortemeyer)
9: #
10: # 7/14,7/15,7/19,7/21,7/22,11/18,
11: # 2/8 Gerd Kortemeyer
12: # 12/23 Gerd Kortemeyer
13: # YEAR=2001
14: # 09/04,09/06,11/26 Gerd Kortemeyer
15:
16: $|=1;
17:
18: use lib '/home/httpd/lib/perl/';
19: use LONCAPA::Configuration;
20:
21: use IO::File;
22: use IO::Socket;
23: use strict;
24: #globals
25: use vars qw (%perlvar %simplestatus $errors $warnings $notices $totalcount);
26:
27: my $statusdir="/home/httpd/html/lon-status";
28:
29:
30: # -------------------------------------------------- Non-critical communication
31: sub reply {
32: my ($cmd,$server)=@_;
33: my $peerfile="$perlvar{'lonSockDir'}/$server";
34: my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
35: Type => SOCK_STREAM,
36: Timeout => 10)
37: or return "con_lost";
38: print $client "$cmd\n";
39: my $answer=<$client>;
40: chomp($answer);
41: if (!$answer) { $answer="con_lost"; }
42: return $answer;
43: }
44:
45: # --------------------------------------------------------- Output error status
46:
47: sub log {
48: my $fh=shift;
49: if ($fh) { print $fh @_ }
50: }
51:
52: sub errout {
53: my $fh=shift;
54: &log($fh,(<<ENDERROUT));
55: <p><table border=2 bgcolor="#CCCCCC">
56: <tr><td>Notices</td><td>$notices</td></tr>
57: <tr><td>Warnings</td><td>$warnings</td></tr>
58: <tr><td>Errors</td><td>$errors</td></tr>
59: </table><p><a href="#top">Top</a><p>
60: ENDERROUT
61: }
62:
63: sub start_daemon {
64: my ($fh,$daemon,$pidfile) = @_;
65: my $progname=$daemon;
66: if ($daemon eq 'lonc' && $ARGV[0] eq 'new') {
67: $progname='loncnew';
68: print "new ";
69: }
70: system("$perlvar{'lonDaemons'}/$progname 2>>$perlvar{'lonDaemons'}/logs/${daemon}_errors");
71: sleep 2;
72: if (-e $pidfile) {
73: &log($fh,"Seems like it started ...<p>");
74: my $lfh=IO::File->new("$pidfile");
75: my $daemonpid=<$lfh>;
76: chomp($daemonpid);
77: sleep 2;
78: if (kill 0 => $daemonpid) {
79: return 1;
80: } else {
81: return 0;
82: }
83: }
84: &log($fh,"Seems like that did not work!<p>");
85: $errors++;
86: return 0;
87: }
88:
89: sub checkon_daemon {
90: my ($fh,$daemon,$maxsize,$sendusr1)=@_;
91:
92: &log($fh,'<hr><a name="'.$daemon.'"><h2>'.$daemon.'</h2><h3>Log</h3><pre>');
93: printf("%-10s ",$daemon);
94: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
95: open (DFH,"tail -n25 $perlvar{'lonDaemons'}/logs/$daemon.log|");
96: while (my $line=<DFH>) {
97: &log($fh,"$line");
98: if ($line=~/INFO/) { $notices++; }
99: if ($line=~/WARNING/) { $notices++; }
100: if ($line=~/CRITICAL/) { $warnings++; }
101: };
102: close (DFH);
103: }
104: &log($fh,"</pre>");
105:
106: my $pidfile="$perlvar{'lonDaemons'}/logs/$daemon.pid";
107:
108: my $restartflag=1;
109: my $daemonpid;
110: if (-e $pidfile) {
111: my $lfh=IO::File->new("$pidfile");
112: $daemonpid=<$lfh>;
113: chomp($daemonpid);
114: if (kill 0 => $daemonpid) {
115: &log($fh,"<h3>$daemon at pid $daemonpid responding");
116: if ($sendusr1) { &log($fh,", sending USR1"); }
117: &log($fh,"</h3>");
118: if ($sendusr1) { kill USR1 => $daemonpid; }
119: $restartflag=0;
120: print "running\n";
121: } else {
122: $errors++;
123: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
124: $restartflag=1;
125: &log($fh,"<h3>Decided to clean up stale .pid file and restart $daemon</h3>");
126: }
127: }
128: if ($restartflag==1) {
129: $simplestatus{$daemon}='off';
130: $errors++;
131: &log($fh,'<br><font color="red">Killall '.$daemon.': '.
132: `killall $daemon 2>&1`.' - ');
133: sleep 2;
134: &log($fh,unlink($pidfile).' - '.
135: `killall -9 $daemon 2>&1`.
136: '</font><br>');
137: &log($fh,"<h3>$daemon not running, trying to start</h3>");
138:
139: if (&start_daemon($fh,$daemon,$pidfile)) {
140: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
141: $simplestatus{$daemon}='restarted';
142: print "started\n";
143: } else {
144: $errors++;
145: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
146: &log($fh,"Give it one more try ...<p>");
147: print " ";
148: if (&start_daemon($fh,$daemon,$pidfile)) {
149: &log($fh,"<h3>$daemon at pid $daemonpid responding</h3>");
150: $simplestatus{$daemon}='restarted';
151: print "started\n";
152: } else {
153: print " failed\n";
154: $simplestatus{$daemon}='failed';
155: $errors++; $errors++;
156: &log($fh,"<h3>$daemon at pid $daemonpid not responding</h3>");
157: &log($fh,"Unable to start $daemon<p>");
158: }
159: }
160:
161: if (-e "$perlvar{'lonDaemons'}/logs/$daemon.log"){
162: &log($fh,"<p><pre>");
163: open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/$daemon.log|");
164: while (my $line=<DFH>) {
165: &log($fh,"$line");
166: if ($line=~/WARNING/) { $notices++; }
167: if ($line=~/CRITICAL/) { $notices++; }
168: };
169: close (DFH);
170: &log($fh,"</pre>");
171: }
172: }
173:
174: my $fname="$perlvar{'lonDaemons'}/logs/$daemon.log";
175:
176: my ($dev,$ino,$mode,$nlink,
177: $uid,$gid,$rdev,$size,
178: $atime,$mtime,$ctime,
179: $blksize,$blocks)=stat($fname);
180:
181: if ($size>$maxsize) {
182: &log($fh,"Rotating logs ...<p>");
183: rename("$fname.2","$fname.3");
184: rename("$fname.1","$fname.2");
185: rename("$fname","$fname.1");
186: }
187:
188: &errout($fh);
189: }
190:
191: # --------------------------------------------------------------------- Machine
192: sub log_machine_info {
193: my ($fh)=@_;
194: &log($fh,'<hr><a name="machine"><h2>Machine Information</h2>');
195: &log($fh,"<h3>loadavg</h3>");
196:
197: open (LOADAVGH,"/proc/loadavg");
198: my $loadavg=<LOADAVGH>;
199: close (LOADAVGH);
200:
201: &log($fh,"<tt>$loadavg</tt>");
202:
203: my @parts=split(/\s+/,$loadavg);
204: if ($parts[1]>4.0) {
205: $errors++;
206: } elsif ($parts[1]>2.0) {
207: $warnings++;
208: } elsif ($parts[1]>1.0) {
209: $notices++;
210: }
211:
212: &log($fh,"<h3>df</h3>");
213: &log($fh,"<pre>");
214:
215: open (DFH,"df|");
216: while (my $line=<DFH>) {
217: &log($fh,"$line");
218: @parts=split(/\s+/,$line);
219: my $usage=$parts[4];
220: $usage=~s/\W//g;
221: if ($usage>90) {
222: $warnings++;
223: $notices++;
224: } elsif ($usage>80) {
225: $warnings++;
226: } elsif ($usage>60) {
227: $notices++;
228: }
229: if ($usage>95) { $warnings++; $warnings++; $simplestatus{'diskfull'}++; }
230: }
231: close (DFH);
232: &log($fh,"</pre>");
233:
234:
235: &log($fh,"<h3>ps</h3>");
236: &log($fh,"<pre>");
237: my $psproc=0;
238:
239: open (PSH,"ps -aux --cols 140 |");
240: while (my $line=<PSH>) {
241: &log($fh,"$line");
242: $psproc++;
243: }
244: close (PSH);
245: &log($fh,"</pre>");
246:
247: if ($psproc>200) { $notices++; }
248: if ($psproc>250) { $notices++; }
249:
250: &errout($fh);
251: }
252:
253: sub start_logging {
254: my ($hostdom,$hostrole,$hostname,$spareid)=@_;
255: my $fh=IO::File->new(">$statusdir/newstatus.html");
256: my %simplestatus=();
257: my $now=time;
258: my $date=localtime($now);
259:
260:
261: &log($fh,(<<ENDHEADERS));
262: <html>
263: <head>
264: <title>LON Status Report $perlvar{'lonHostID'}</title>
265: </head>
266: <body bgcolor="#AAAAAA">
267: <a name="top">
268: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
269: <h2>$date ($now)</h2>
270: <ol>
271: <li><a href="#configuration">Configuration</a>
272: <li><a href="#machine">Machine Information</a>
273: <li><a href="#tmp">Temporary Files</a>
274: <li><a href="#tokens">Session Tokens</a>
275: <li><a href="#httpd">httpd</a>
276: <li><a href="#lonsql">lonsql</a>
277: <li><a href="#lond">lond</a>
278: <li><a href="#lonc">lonc</a>
279: <li><a href="#lonhttpd">lonhttpd</a>
280: <li><a href="#lonnet">lonnet</a>
281: <li><a href="#connections">Connections</a>
282: <li><a href="#delayed">Delayed Messages</a>
283: <li><a href="#errcount">Error Coindex.html.unt</a>
284: </ol>
285: <hr>
286: <a name="configuration">
287: <h2>Configuration</h2>
288: <h3>PerlVars</h3>
289: <table border=2>
290: ENDHEADERS
291:
292: foreach my $varname (sort(keys(%perlvar))) {
293: &log($fh,"<tr><td>$varname</td><td>$perlvar{$varname}</td></tr>\n");
294: }
295: &log($fh,"</table><h3>Hosts</h3><table border=2>");
296: foreach my $id (sort(keys(%{$hostname}))) {
297: &log($fh,
298: "<tr><td>$id</td><td>".$hostdom->{$id}.
299: "</td><td>".$hostrole->{$id}.
300: "</td><td>".$hostname->{$id}."</td></tr>\n");
301: }
302: &log($fh,"</table><h3>Spare Hosts</h3><ol>");
303: foreach my $id (sort(keys(%{$spareid}))) {
304: &log($fh,"<li>$id\n");
305: }
306: &log($fh,"</ol>\n");
307: return $fh;
308: }
309:
310: # --------------------------------------------------------------- clean out tmp
311: sub clean_tmp {
312: my ($fh)=@_;
313: &log($fh,'<hr><a name="tmp"><h2>Temporary Files</h2>');
314: my $cleaned=0;
315: my $old=0;
316: while (my $fname=<$perlvar{'lonDaemons'}/tmp/*>) {
317: my ($dev,$ino,$mode,$nlink,
318: $uid,$gid,$rdev,$size,
319: $atime,$mtime,$ctime,
320: $blksize,$blocks)=stat($fname);
321: my $now=time;
322: my $since=$now-$mtime;
323: if ($since>$perlvar{'lonExpire'}) {
324: my $line='';
325: if (open(PROBE,$fname)) {
326: $line=<PROBE>;
327: close(PROBE);
328: }
329: unless ($line=~/^CHECKOUTTOKEN\&/) {
330: $cleaned++;
331: unlink("$fname");
332: } else {
333: if ($since>365*$perlvar{'lonExpire'}) {
334: $cleaned++;
335: unlink("$fname");
336: } else { $old++; }
337: }
338: }
339: }
340: &log($fh,"Cleaned up ".$cleaned." files (".$old." old checkout tokens).");
341: }
342:
343: # ------------------------------------------------------------ clean out lonIDs
344: sub clean_lonIDs {
345: my ($fh)=@_;
346: &log($fh,'<hr><a name="tokens"><h2>Session Tokens</h2>');
347: my $cleaned=0;
348: my $active=0;
349: while (my $fname=<$perlvar{'lonIDsDir'}/*>) {
350: my ($dev,$ino,$mode,$nlink,
351: $uid,$gid,$rdev,$size,
352: $atime,$mtime,$ctime,
353: $blksize,$blocks)=stat($fname);
354: my $now=time;
355: my $since=$now-$mtime;
356: if ($since>$perlvar{'lonExpire'}) {
357: $cleaned++;
358: &log($fh,"Unlinking $fname<br>");
359: unlink("$fname");
360: } else {
361: $active++;
362: }
363: }
364: &log($fh,"<p>Cleaned up ".$cleaned." stale session token(s).");
365: &log($fh,"<h3>$active open session(s)</h3>");
366: }
367:
368:
369: # ----------------------------------------------------------------------- httpd
370: sub check_httpd_logs {
371: my ($fh)=@_;
372: &log($fh,'<hr><a name="httpd"><h2>httpd</h2><h3>Access Log</h3><pre>');
373:
374: open (DFH,"tail -n25 /etc/httpd/logs/access_log|");
375: while (my $line=<DFH>) { &log($fh,"$line") };
376: close (DFH);
377:
378: &log($fh,"</pre><h3>Error Log</h3><pre>");
379:
380: open (DFH,"tail -n25 /etc/httpd/logs/error_log|");
381: while (my $line=<DFH>) {
382: &log($fh,"$line");
383: if ($line=~/\[error\]/) { $notices++; }
384: }
385: close (DFH);
386: &log($fh,"</pre>");
387: &errout($fh);
388: }
389:
390: # ---------------------------------------------------------------------- lonnet
391:
392: sub rotate_logs {
393: my ($fh)=@_;
394: &log($fh,'<hr><a name="lonnet"><h2>lonnet</h2><h3>Temp Log</h3><pre>');
395: print "checking logs\n";
396: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
397: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
398: while (my $line=<DFH>) {
399: &log($fh,"$line");
400: }
401: close (DFH);
402: }
403: &log($fh,"</pre><h3>Perm Log</h3><pre>");
404:
405: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
406: open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
407: while (my $line=<DFH>) {
408: &log($fh,"$line");
409: }
410: close (DFH);
411: } else { &log($fh,"No perm log\n") }
412:
413: my $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
414:
415: my ($dev,$ino,$mode,$nlink,
416: $uid,$gid,$rdev,$size,
417: $atime,$mtime,$ctime,
418: $blksize,$blocks)=stat($fname);
419:
420: if ($size>40000) {
421: &log($fh,"Rotating logs ...<p>");
422: rename("$fname.2","$fname.3");
423: rename("$fname.1","$fname.2");
424: rename("$fname","$fname.1");
425: }
426:
427: &log($fh,"</pre>");
428: &errout($fh);
429: }
430:
431: # ----------------------------------------------------------------- Connections
432: sub test_connections {
433: my ($fh,$hostname)=@_;
434: &log($fh,'<hr><a name="connections"><h2>Connections</h2>');
435: print "testing connections\n";
436: &log($fh,"<table border=2>");
437: foreach my $tryserver (sort(keys(%{$hostname}))) {
438: print(".");
439: my $result;
440: my $answer=reply("pong",$tryserver);
441: if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
442: $result="<b>ok</b>";
443: } else {
444: $result=$answer;
445: $warnings++;
446: if ($answer eq 'con_lost') { $warnings++; }
447: }
448: if ($answer =~ /con_lost/) { print(" $tryserver down\n"); }
449: &log($fh,"<tr><td>$tryserver</td><td>$result</td></tr>\n");
450: }
451: &log($fh,"</table>");
452:
453: &errout($fh);
454: }
455:
456:
457: # ------------------------------------------------------------ Delayed messages
458: sub check_delayed_msg {
459: my ($fh)=@_;
460: &log($fh,'<hr><a name="delayed"><h2>Delayed Messages</h2>');
461: print "checking buffers\n";
462:
463: &log($fh,'<h3>Scanning Permanent Log</h3>');
464:
465: my $unsend=0;
466:
467: my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
468: while (my $line=<$dfh>) {
469: my ($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
470: if ($sdf eq 'F') {
471: my $local=localtime($time);
472: &log($fh,"<b>Failed: $time, $dserv, $dcmd</b><br>");
473: $warnings++;
474: }
475: if ($sdf eq 'S') { $unsend--; }
476: if ($sdf eq 'D') { $unsend++; }
477: }
478:
479: &log($fh,"Total unsend messages: <b>$unsend</b><p>\n");
480: $warnings=$warnings+5*$unsend;
481:
482: if ($unsend) { $simplestatus{'unsend'}=$unsend; }
483: &log($fh,"<h3>Outgoing Buffer</h3>");
484:
485: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
486: while (my $line=<DFH>) {
487: &log($fh,"$line<br>");
488: }
489: close (DFH);
490: }
491:
492: sub finish_logging {
493: my ($fh)=@_;
494: &log($fh,"<a name=errcount>\n");
495: $totalcount=$notices+4*$warnings+100*$errors;
496: &errout($fh);
497: &log($fh,"<h1>Total Error Count: $totalcount</h1>");
498: my $now=time;
499: my $date=localtime($now);
500: &log($fh,"<hr>$date ($now)</body></html>\n");
501: print "lon-status webpage updated\n";
502: $fh->close();
503:
504: if ($errors) { $simplestatus{'errors'}=$errors; }
505: if ($warnings) { $simplestatus{'warnings'}=$warnings; }
506: if ($notices) { $simplestatus{'notices'}=$notices; }
507: $simplestatus{'time'}=time;
508: }
509:
510: sub log_simplestatus {
511: rename ("$statusdir/newstatus.html","$statusdir/index.html");
512:
513: my $sfh=IO::File->new(">$statusdir/loncron_simple.txt");
514: foreach (keys %simplestatus) {
515: print $sfh $_.'='.$simplestatus{$_}.'&';
516: }
517: print $sfh "\n";
518: $sfh->close();
519: }
520:
521: sub send_mail {
522: print "sending mail\n";
523: my $emailto="$perlvar{'lonAdmEMail'}";
524: if ($totalcount>1000) {
525: $emailto.=",$perlvar{'lonSysEMail'}";
526: }
527: my $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices";
528: system("metasend -b -t $emailto -s '$subj' -f $statusdir/index.html -m text/html");
529: }
530:
531: # ================================================================ Main Program
532: sub main () {
533: # --------------------------------- Read loncapa_apache.conf and loncapa.conf
534: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
535: %perlvar=%{$perlvarref};
536: undef $perlvarref;
537: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
538: delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
539:
540: # --------------------------------------- Make sure that LON-CAPA is configured
541: # I only test for one thing here (lonHostID). This is just a safeguard.
542: if ('{[[[[lonHostID]]]]}' eq $perlvar{'lonHostID'}) {
543: print("Unconfigured machine.\n");
544: my $emailto=$perlvar{'lonSysEMail'};
545: my $hostname=`/bin/hostname`;
546: chop $hostname;
547: $hostname=~s/[^\w\.]//g; # make sure is safe to pass through shell
548: my $subj="LON: Unconfigured machine $hostname";
549: system("echo 'Unconfigured machine $hostname.' |\
550: mailto $emailto -s '$subj' > /dev/null");
551: exit 1;
552: }
553:
554: # ----------------------------- Make sure this process is running from user=www
555: my $wwwid=getpwnam('www');
556: if ($wwwid!=$<) {
557: print("User ID mismatch. This program must be run as user 'www'\n");
558: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
559: my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
560: system("echo 'User ID mismatch. loncron must be run as user www.' |\
561: mailto $emailto -s '$subj' > /dev/null");
562: exit 1;
563: }
564:
565: # ------------------------------------------------------------- Read hosts file
566: my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");
567:
568: my (%hostname,%hostdom,%hostrole,%spareid);
569: while (my $configline=<$config>) {
570: my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
571: if ($id && $domain && $role && $name && $ip) {
572: $hostname{$id}=$name;
573: $hostdom{$id}=$domain;
574: $hostrole{$id}=$role;
575: } else {
576: if ($configline) {
577: # &logthis("Skipping hosts.tab line -$configline-");
578: }
579: }
580: }
581: undef $config;
582:
583: # ------------------------------------------------------ Read spare server file
584: $config=IO::File->new("$perlvar{'lonTabDir'}/spare.tab");
585:
586: while (my $configline=<$config>) {
587: chomp($configline);
588: if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
589: $spareid{$configline}=1;
590: }
591: }
592: undef $config;
593:
594: # ---------------------------------------------------------------- Start report
595:
596: $errors=0;
597: $warnings=0;
598: $notices=0;
599:
600: my $fh=&start_logging(\%hostdom,\%hostrole,\%hostname,\%spareid);
601:
602: &log_machine_info($fh);
603: &clean_tmp($fh);
604: &clean_lonIDs($fh);
605: &check_httpd_logs($fh);
606: &checkon_daemon($fh,'lonsql',200000);
607: &checkon_daemon($fh,'lond',40000,1);
608: &checkon_daemon($fh,'lonc',40000,1);
609: &checkon_daemon($fh,'lonhttpd',40000);
610:
611: &test_connections($fh,\%hostname);
612: &check_delayed_msg($fh);
613:
614: &finish_logging($fh);
615: &log_simplestatus();
616:
617: if ($totalcount>200) { &send_mail(); }
618: }
619:
620: &main();
621: 1;
622:
623:
624:
625:
626:
627:
628:
629:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>