Annotation of loncom/loncron, revision 1.4

1.1       albertel    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: #
1.3       www        10: # 7/14,7/15,7/19,7/21,7/22,11/18,
                     11: # 2/8 Gerd Kortemeyer
1.1       albertel   12: 
                     13: use IO::File;
                     14: use IO::Socket;
                     15: 
                     16: # -------------------------------------------------- Non-critical communication
                     17: sub reply {
                     18:     my ($cmd,$server)=@_;
                     19:     my $peerfile="$perlvar{'lonSockDir'}/$server";
                     20:     my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                     21:                                      Type    => SOCK_STREAM,
                     22:                                      Timeout => 10)
                     23:        or return "con_lost";
                     24:     print $client "$cmd\n";
                     25:     my $answer=<$client>;
                     26:     chomp($answer);
                     27:     if (!$answer) { $answer="con_lost"; }
                     28:     return $answer;
                     29: }
                     30: 
                     31: # --------------------------------------------------------- Output error status
                     32: 
                     33: sub errout {
                     34:    my $fh=shift;
                     35:    print $fh (<<ENDERROUT);
                     36:      <p><table border=2 bgcolor="#CCCCCC">
                     37:      <tr><td>Notices</td><td>$notices</td></tr>
                     38:      <tr><td>Warnings</td><td>$warnings</td></tr>
                     39:      <tr><td>Errors</td><td>$errors</td></tr>
                     40:      </table><p><a href="#top">Top</a><p>
                     41: ENDERROUT
                     42: }
                     43: 
                     44: # ================================================================ Main Program
                     45: 
                     46: 
                     47: # ------------------------------------------------------------ Read access.conf
                     48: {
                     49:     my $config=IO::File->new("/etc/httpd/conf/access.conf");
                     50: 
                     51:     while (my $configline=<$config>) {
                     52:         if ($configline =~ /PerlSetVar/) {
                     53: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
                     54:            $perlvar{$varname}=$varvalue;
                     55:         }
                     56:     }
                     57: }
                     58: 
                     59: # ------------------------------------------------------------- Read hosts file
                     60: {
                     61:     my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");
                     62: 
                     63:     while (my $configline=<$config>) {
                     64:        my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
                     65:        $hostname{$id}=$name;
                     66:        $hostdom{$id}=$domain;
                     67:        $hostrole{$id}=$role;
                     68:        $hostip{$id}=$ip;
                     69:        if (($role eq 'library') && ($id ne $perlvar{'lonHostID'})) {
                     70: 	   $libserv{$id}=$name;
                     71:        }
                     72:     }
                     73: }
                     74: 
                     75: # ------------------------------------------------------ Read spare server file
                     76: {
                     77:     my $config=IO::File->new("$perlvar{'lonTabDir'}/spare.tab");
                     78: 
                     79:     while (my $configline=<$config>) {
                     80:        chomp($configline);
                     81:        if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
                     82:           $spareid{$configline}=1;
                     83:        }
                     84:     }
                     85: }
                     86: 
                     87: # ---------------------------------------------------------------- Start report
                     88: 
                     89: $statusdir="/home/httpd/html/lon-status";
                     90: 
                     91: $errors=0;
                     92: $warnings=0;
                     93: $notices=0;
                     94: 
                     95: $now=time;
                     96: $date=localtime($now);
                     97: 
                     98: {
                     99: my $fh=IO::File->new(">$statusdir/newstatus.html");
                    100: 
                    101: print $fh (<<ENDHEADERS);
                    102: <html>
                    103: <head>
                    104: <title>LON Status Report $perlvar{'lonHostID'}</title>
                    105: </head>
1.3       www       106: <body bgcolor="#AAAAAA">
1.1       albertel  107: <a name="top">
                    108: <h1>LON Status Report $perlvar{'lonHostID'}</h1>
                    109: <h2>$date ($now)</h2>
                    110: <ol>
                    111: <li><a href="#configuration">Configuration</a>
                    112: <li><a href="#machine">Machine Information</a>
                    113: <li><a href="#httpd">httpd</a>
                    114: <li><a href="#lond">lond</a>
                    115: <li><a href="#lonc">lonc</a>
                    116: <li><a href="#lonnet">lonnet</a>
                    117: <li><a href="#connections">Connections</a>
                    118: <li><a href="#delayed">Delayed Messages</a>
                    119: <li><a href="#errcount">Error Count</a>
                    120: </ol>
                    121: <hr>
                    122: <a name="configuration">
                    123: <h2>Configuration</h2>
                    124: <h3>PerlVars</h3>
                    125: <table border=2>
                    126: ENDHEADERS
                    127: 
                    128: foreach $varname (keys %perlvar) {
                    129:     print $fh "<tr><td>$varname</td><td>$perlvar{$varname}</td></tr>\n";
                    130: }
                    131: print $fh "</table><h3>Hosts</h3><table border=2>";
                    132: foreach $id (keys %hostname) {
                    133: print $fh 
                    134:     "<tr><td>$id</td><td>$hostdom{$id}</td><td>$hostrole{$id}</td>";
                    135: print $fh "<td>$hostname{$id}</td><td>$hostip{$id}</td></tr>\n";
                    136: }
                    137: print $fh "</table><h3>Spare Hosts</h3><ol>";
                    138: foreach $id (keys %spareid) {
                    139:     print $fh "<li>$id\n";
                    140: }
                    141: 
                    142: print $fh "</ol>\n";
                    143: 
                    144: # --------------------------------------------------------------------- Machine
                    145: 
                    146: print $fh '<hr><a name="machine"><h2>Machine Information</h2>';
                    147: print $fh "<h3>loadavg</h3>";
                    148: 
                    149: open (LOADAVGH,"/proc/loadavg");
                    150: $loadavg=<LOADAVGH>;
                    151: close (LOADAVGH);
                    152: 
                    153: print $fh "<tt>$loadavg</tt>";
                    154: 
                    155: @parts=split(/\s+/,$loadavg);
1.4     ! www       156: if ($parts[1]>4.0) {
1.1       albertel  157:     $errors++;
                    158: } elsif ($parts[1]>2.0) {
                    159:     $warnings++;
                    160: } elsif ($parts[1]>1.0) {
                    161:     $notices++;
                    162: }
                    163: 
                    164: print $fh "<h3>df</h3>";
                    165: print $fh "<pre>";
                    166: 
                    167: open (DFH,"df|");
                    168: while ($line=<DFH>) { 
                    169:    print $fh "$line"; 
                    170:    @parts=split(/\s+/,$line);
                    171:    $usage=$parts[4];
                    172:    $usage=~s/\W//g;
                    173:    if ($usage>90) { 
1.4     ! www       174:       $warnings++; 
1.1       albertel  175:    } elsif ($usage>80) {
                    176:       $warnings++;
                    177:    } elsif ($usage>60) {
                    178:       $notices++;
                    179:    }
1.4     ! www       180:    if ($usage>95) { $warnings++; $warnings++ }
1.1       albertel  181: }
                    182: close (DFH);
                    183: print $fh "</pre>";
                    184: &errout($fh);
                    185: # ----------------------------------------------------------------------- httpd
                    186: 
                    187: print $fh '<hr><a name="httpd"><h2>httpd</h2><h3>Access Log</h3><pre>';
                    188: 
                    189: open (DFH,"tail -n40 /etc/httpd/logs/access_log|");
                    190: while ($line=<DFH>) { print $fh "$line" };
                    191: close (DFH);
                    192: 
                    193: print $fh "</pre><h3>Error Log</h3><pre>";
                    194: 
                    195: open (DFH,"tail -n50 /etc/httpd/logs/error_log|");
                    196: while ($line=<DFH>) { 
                    197:    print $fh "$line";
                    198:    if ($line=~/\[error\]/) { $notices++; } 
                    199: };
                    200: close (DFH);
                    201: print $fh "</pre>";
                    202: &errout($fh);
                    203: # ------------------------------------------------------------------------ lond
                    204: 
                    205: print $fh '<hr><a name="lond"><h2>lond</h2><h3>Log</h3><pre>';
                    206: 
                    207: if (-e "$perlvar{'lonDaemons'}/logs/lond.log"){
1.3       www       208: open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/lond.log|");
1.1       albertel  209: while ($line=<DFH>) { 
                    210:    print $fh "$line";
1.3       www       211:    if ($line=~/INFO/) { $notices++; }
1.4     ! www       212:    if ($line=~/WARNING/) { $notices++; }
        !           213:    if ($line=~/CRITICAL/) { $warnings++; }
1.1       albertel  214: };
                    215: close (DFH);
                    216: }
                    217: print $fh "</pre>";
                    218: 
                    219: my $londfile="$perlvar{'lonDaemons'}/logs/lond.pid";
                    220: 
                    221: if (-e $londfile) {
                    222:    my $lfh=IO::File->new("$londfile");
                    223:    my $londpid=<$lfh>;
                    224:    chomp($londpid);
                    225:    if (kill 0 => $londpid) {
                    226:       print $fh "<h3>lond at pid $londpid responding</h3>";
                    227:    } else {
                    228:       $errors++; $errors++;
                    229:       print $fh "<h3>lond at pid $londpid not responding</h3>";
                    230:    }
                    231: } else {
                    232:    $errors++;
                    233:    print $fh "<h3>lond not running, trying to start</h3>";
                    234:    system("$perlvar{'lonDaemons'}/lond");
                    235:    sleep 120;
                    236:    if (-e $londfile) {
                    237:        print $fh "Seems like it started ...<p>";
                    238:        my $lfh=IO::File->new("$londfile");
                    239:        my $londpid=<$lfh>;
                    240:        chomp($londpid);
                    241:        sleep 30;
                    242:        if (kill 0 => $londpid) {
                    243:           print $fh "<h3>lond at pid $londpid responding</h3>";
                    244:        } else {
                    245:           $errors++; $errors++;
                    246:           print $fh "<h3>lond at pid $londpid not responding</h3>";
                    247:           print $fh "Give it one more try ...<p>";
                    248:           system("$perlvar{'lonDaemons'}/lond");
                    249:           sleep 120;
                    250:        }
                    251:    } else {
                    252:        print $fh "Seems like that did not work!<p>";
                    253:        $errors++;
                    254:    }
1.3       www       255:    if (-e "$perlvar{'lonDaemons'}/logs/lond.log"){
                    256:     print $fh "<p><pre>";
                    257:     open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/lond.log|");
                    258:     while ($line=<DFH>) { 
                    259:       print $fh "$line";
1.4     ! www       260:       if ($line=~/WARNING/) { $notices++; }
        !           261:       if ($line=~/CRITICAL/) { $notices++; }
1.3       www       262:     };
                    263:     close (DFH);
                    264:     print $fh "</pre>";
                    265:    }
1.1       albertel  266: }
                    267: 
                    268: $fname="$perlvar{'lonDaemons'}/logs/lond.log";
                    269: 
                    270:                           my ($dev,$ino,$mode,$nlink,
                    271:                               $uid,$gid,$rdev,$size,
                    272:                               $atime,$mtime,$ctime,
                    273:                               $blksize,$blocks)=stat($fname);
                    274: 
                    275: if ($size>40000) {
                    276:     print $fh "Rotating logs ...<p>";
                    277:     rename("$fname.2","$fname.3");
                    278:     rename("$fname.1","$fname.2");
                    279:     rename("$fname","$fname.1");
                    280: }
                    281: 
                    282: &errout($fh);
                    283: # ------------------------------------------------------------------------ lonc
                    284: 
                    285: print $fh '<hr><a name="lonc"><h2>lonc</h2><h3>Log</h3><pre>';
                    286: 
                    287: if (-e "$perlvar{'lonDaemons'}/logs/lonc.log"){
1.3       www       288: open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/lonc.log|");
1.1       albertel  289: while ($line=<DFH>) { 
                    290:    print $fh "$line";
1.3       www       291:    if ($line=~/INFO/) { $notices++; }
1.4     ! www       292:    if ($line=~/WARNING/) { $notices++; }
        !           293:    if ($line=~/CRITICAL/) { $warnings++; }
1.1       albertel  294: };
                    295: close (DFH);
                    296: }
                    297: print $fh "</pre>";
                    298: 
                    299: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
                    300: 
                    301: if (-e $loncfile) {
                    302:    my $lfh=IO::File->new("$loncfile");
                    303:    my $loncpid=<$lfh>;
                    304:    chomp($loncpid);
                    305:    if (kill 0 => $loncpid) {
                    306:       print $fh "<h3>lonc at pid $loncpid responding, sending USR1</h3>";
                    307:       kill USR1 => $loncpid;
                    308:    } else {
                    309:       $errors++; $errors++;
                    310:       print $fh "<h3>lonc at pid $loncpid not responding</h3>";
                    311:    }
                    312: } else {
                    313:    $errors++;
                    314:    print $fh "<h3>lonc not running, trying to start</h3>";
                    315:    system("$perlvar{'lonDaemons'}/lonc");
                    316:    sleep 120;
                    317:    if (-e $loncfile) {
                    318:        print $fh "Seems like it started ...<p>";
                    319:        my $lfh=IO::File->new("$loncfile");
                    320:        my $loncpid=<$lfh>;
                    321:        chomp($loncpid);
                    322:        sleep 30;
                    323:        if (kill 0 => $loncpid) {
                    324:           print $fh "<h3>lonc at pid $loncpid responding</h3>";
                    325:        } else {
                    326:           $errors++; $errors++;
                    327:           print $fh "<h3>lonc at pid $loncpid not responding</h3>";
                    328:           print $fh "Give it one more try ...<p>";
                    329:           system("$perlvar{'lonDaemons'}/lonc");
                    330:           sleep 120;
                    331:        }
                    332:    } else {
                    333:        print $fh "Seems like that did not work!<p>";
                    334:        $errors++;
                    335:    }
1.3       www       336:    if (-e "$perlvar{'lonDaemons'}/logs/lonc.log") {
                    337:     print $fh "<p><pre>";
                    338:     open (DFH,"tail -n100 $perlvar{'lonDaemons'}/logs/lonc.log|");
                    339:     while ($line=<DFH>) { 
                    340:       print $fh "$line";
1.4     ! www       341:       if ($line=~/WARNING/) { $notices++; }
        !           342:       if ($line=~/CRITICAL/) { $notices++; }
1.3       www       343:     };
                    344:     close (DFH);
                    345:     print $fh "</pre>";
                    346:    }
1.1       albertel  347: }
                    348: 
                    349: $fname="$perlvar{'lonDaemons'}/logs/lonc.log";
                    350: 
                    351:                           my ($dev,$ino,$mode,$nlink,
                    352:                               $uid,$gid,$rdev,$size,
                    353:                               $atime,$mtime,$ctime,
                    354:                               $blksize,$blocks)=stat($fname);
                    355: 
                    356: if ($size>40000) {
                    357:     print $fh "Rotating logs ...<p>";
                    358:     rename("$fname.2","$fname.3");
                    359:     rename("$fname.1","$fname.2");
                    360:     rename("$fname","$fname.1");
                    361: }
                    362: 
                    363:    
                    364: &errout($fh);
                    365: # ---------------------------------------------------------------------- lonnet
                    366: 
                    367: print $fh '<hr><a name="lonnet"><h2>lonnet</h2><h3>Temp Log</h3><pre>';
                    368: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.log"){
                    369: open (DFH,"tail -n50 $perlvar{'lonDaemons'}/logs/lonnet.log|");
                    370: while ($line=<DFH>) { 
                    371:     print $fh "$line";
                    372: };
                    373: close (DFH);
                    374: }
                    375: print $fh "</pre><h3>Perm Log</h3>";
                    376: 
                    377: if (-e "$perlvar{'lonDaemons'}/logs/lonnet.perm.log") {
                    378:     open(DFH,"tail -n10 $perlvar{'lonDaemons'}/logs/lonnet.perm.log|");
                    379: while ($line=<DFH>) { 
                    380:    print $fh "$line";
                    381: };
                    382: close (DFH);
                    383: } else { print $fh "No perm log\n" }
                    384: 
                    385: $fname="$perlvar{'lonDaemons'}/logs/lonnet.log";
                    386: 
                    387:                           my ($dev,$ino,$mode,$nlink,
                    388:                               $uid,$gid,$rdev,$size,
                    389:                               $atime,$mtime,$ctime,
                    390:                               $blksize,$blocks)=stat($fname);
                    391: 
                    392: if ($size>40000) {
                    393:     print $fh "Rotating logs ...<p>";
                    394:     rename("$fname.2","$fname.3");
                    395:     rename("$fname.1","$fname.2");
                    396:     rename("$fname","$fname.1");
                    397: }
                    398: 
                    399: print $fh "</pre>";
                    400: &errout($fh);
                    401: # ----------------------------------------------------------------- Connections
                    402: 
                    403: print $fh '<hr><a name="connections"><h2>Connections</h2>';
                    404: 
                    405: print $fh "<table border=2>";
                    406: foreach $tryserver (keys %hostname) {
                    407: 
                    408:     $answer=reply("pong",$tryserver);
                    409:     if ($answer eq "$tryserver:$perlvar{'lonHostID'}") {
                    410: 	$result="<b>ok</b>";
                    411:     } else {
                    412:         $result=$answer;
                    413:         $warnings++;
                    414:         if ($answer eq 'con_lost') { $warnings++; }
                    415:     }
                    416:     print $fh "<tr><td>$tryserver</td><td>$result</td></tr>\n";
                    417: 
                    418: }
                    419: print $fh "</table>";
                    420: 
                    421: &errout($fh);
                    422: # ------------------------------------------------------------ Delayed messages
                    423: 
                    424: print $fh '<hr><a name="delayed"><h2>Delayed Messages</h2>';
                    425: 
                    426: print $fh '<h3>Scanning Permanent Log</h3>';
                    427: 
                    428: $unsend=0;
                    429: {
                    430:     my $dfh=IO::File->new("$perlvar{'lonDaemons'}/logs/lonnet.perm.log");
                    431:     while ($line=<$dfh>) {
                    432: 	($time,$sdf,$dserv,$dcmd)=split(/:/,$line);
                    433:         if ($sdf eq 'F') { 
                    434: 	    $local=localtime($time);
                    435:             print "<b>Failed: $time, $dserv, $dcmd</b><br>";
                    436:             $warnings++;
                    437:         }
                    438:         if ($sdf eq 'S') { $unsend--; }
                    439:         if ($sdf eq 'D') { $unsend++; }
                    440:     }
                    441: }
                    442: print $fh "Total unsend messages: <b>$unsend</b><p>\n";
                    443: $warnings=$warnings+5*$unsend;
                    444: 
                    445: print $fh "<h3>Outgoing Buffer</h3>";
                    446: 
                    447: open (DFH,"ls -lF $perlvar{'lonSockDir'}/delayed|");
                    448: while ($line=<DFH>) { 
                    449:     print $fh "$line<br>";
                    450: };
                    451: close (DFH);
                    452: 
                    453: # ------------------------------------------------------------------------- End
                    454: print $fh "<a name=errcount>\n";
                    455: $totalcount=$notices+4*$warnings+100*$errors;
                    456: &errout($fh);
                    457: print $fh "<h1>Total Error Count: $totalcount</h1>";
                    458: $now=time;
                    459: $date=localtime($now);
                    460: print $fh "<hr>$date ($now)</body></html>\n";
                    461: 
                    462: }
                    463: 
                    464: rename ("$statusdir/newstatus.html","$statusdir/index.html");
                    465: 
                    466: if ($totalcount>200) {
                    467:    $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                    468:    $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
                    469:    system(
                    470:  "metasend -b -t $emailto -s '$subj' -f $statusdir/index.html -m text/html");
                    471: }
                    472: 1;
                    473: 
                    474: 
                    475: 
                    476: 
                    477: 
                    478: 
                    479: 
                    480: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>