Annotation of loncom/loncron, revision 1.2

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

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