File:  [LON-CAPA] / loncom / build / Attic / CHECKRPMS.default
Revision 1.17: download - view: text, annotated - select for diffs
Wed Jul 30 16:49:27 2003 UTC (20 years, 10 months ago) by www
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
Simple one-line status for cluster-wide automated reporting

    1: #!/usr/bin/perl
    2: 
    3: =pod
    4: 
    5: =head1 NAME
    6: 
    7: B<CHECKRPMS> - automated status report about RPMs on a system
    8: 
    9: =head1 SYNOPSIS
   10: 
   11: ./B<CHECKRPMS> [I<modeflag>]
   12: 
   13: or
   14: 
   15: B<perl> B<CHECKRPMS> [I<modeflag>]
   16: 
   17: If I<modeflag> is left blank, the mode is "interactive".  Otherwise,
   18: other modes can be specified as shown in the listing below:
   19: 
   20: =over 4
   21: 
   22: =item DEFAULT
   23: 
   24: When left blank, the script runs in interactive mode.  First, a proposed
   25: list of RPMs is presented to the user.  Then, the user is asked if he or
   26: she wants to download the RPMs to /tmp/loncapa_rpm_updates/.
   27: 
   28: =item view
   29: 
   30: A proposed list of RPMs to update is presented to the user.
   31: 
   32: =item download
   33: 
   34: A proposed set of RPMs to update are downloaded into /tmp/loncapa_rpm_updates/.
   35: Note that prior information inside /tmp/loncapa_rpm_updates/ is removed.
   36: 
   37: =item redownload
   38: 
   39: A proposed set of RPMs to update are downloaded into /tmp/loncapa_rpm_updates/.
   40: Note that prior information inside /tmp/loncapa_rpm_updates/ is not removed.
   41: (This helps support continual attempts from dialup connections.)
   42: 
   43: =item html
   44: 
   45: Similar to view mode. XHTML-formatted output is delivered; presumably
   46: to a web client.  html mode is automatically chosen if $ENV{'QUERY_STRING'} is
   47: defined.
   48: 
   49: =back
   50: 
   51: =head1 DESCRIPTION
   52: 
   53: This file automates the usage of Martin Siegert's "check-rpms"
   54: script.  It runs through a list of possible mirror sites
   55: until it finds one with a reasonably good FTP connection.
   56: 
   57: For instructions on usage, see L<SYNOPSIS>.
   58: 
   59: =head1 AUTHOR
   60: 
   61: Scott Harrison, sharrison@users.sourceforge.net, 2002
   62: 
   63: =cut
   64: 
   65: # ================================================== READ IN COMMAND ARGUMENTS.
   66: 
   67: # ---------------------------------------------------- Process download option.
   68: my $argument = shift(@ARGV);
   69: my $document;
   70: my $mode;
   71: if ($argument eq '--download' or $argument eq '--redownload')
   72:   {
   73:     if ($< != 0) # Download mode requires 'root'.
   74:       {
   75:         print(
   76: 	      '**** ERROR **** Download mode needs to be run as root'."\n");
   77: 	exit(0); # Exit.
   78:       }
   79:     `rm -Rf /tmp/loncapa_rpm_updates` if $argument eq '--download';
   80:     $download='-v -dl -d /tmp/loncapa_rpm_updates'; # Part of check-rpms args.
   81:     $mode = 'download';
   82:   }
   83: elsif ($argument eq '--view')
   84:   {
   85:     $mode = 'view';
   86:   }
   87: elsif ($argument eq '--cronmail')
   88:   {
   89:     $mode = 'cronmail';
   90:   }
   91: elsif ($ENV{'QUERY_STRING'} or $argument eq '--html')
   92:   {
   93:     $mode = 'html';
   94:   }
   95: else
   96:   {
   97:     $mode = 'interactive';
   98:   }
   99: 
  100: # ================================================== GENERAL INITIAL VARIABLES.
  101: my $command_name=$0;
  102: 
  103: # ---------------- The FTP servers (and their directory paths) to check against
  104: my @serverpaths_to_try = 
  105:   (
  106:     'ftpmirror:loncapa@install.lon-capa.org/pub/redhat/linux/updates/',
  107:     'mirror.pa.msu.edu/linux/redhat/linux/updates/',
  108:     'distro.ibiblio.org/pub/linux/distributions/redhat/updates/',
  109:     'limestone.uoregon.edu/redhat/updates/',
  110:     'rufus.w3.org/linux/redhat/linux/updates/',
  111:   );
  112: 
  113: # -------------------------------------------- Use check-rpms command this way.
  114: my $checkcommand = 'check-rpms -nk '.$download.' --rpmuser www -ftp';
  115: 
  116: my $FTPSERVER; # ------------------------- the server portion of the serverpath
  117: my $FTPUPDATES; # ----------------------------- the actual update root location
  118: my @rpms; # ---------------------------------- this will store the list of RPMs
  119: my $goodoutput; # ------------------------------------ good stuff was returned!
  120: my $reallygoodoutput; # ------------------------------- you are 100% up-to-date
  121: 
  122: my $simplestatus='time='.time.'&';
  123: 
  124: # ===================================================== Control flow of output.
  125: my $out = \*STDOUT; # Default: go to standard output (directly to terminal).
  126: 
  127: if ($mode eq 'cronmail') # If cronmail mode, then save to file.
  128:   {
  129:     open(FOUT,'>/tmp/CHECKRPMS.'.$$);
  130:     $out = \*FOUT;
  131:   }
  132: 
  133: $| = 1; # Flush to output whenever possible.
  134: 
  135: # ========================================== Variables that must be defineable.
  136: 
  137: # --------------------------------------------------- Determine RedHat version.
  138: my $RHversion = (split /\s/, `cat /etc/redhat-release`)[4]; # - 6.2 or 7.3 or ?
  139: 
  140: unless ($RHversion)
  141:   {
  142:     terminate($mode,$out,
  143: 	      '**** ERROR **** /etc/redhat-release not found'."\n".
  144: 	      'This script does not appear to be running on RedHat.'."\n");
  145:   }
  146: 
  147: # ----------------------------------------- Find the check-rpms script location
  148: if (-e './check-rpms')
  149:   {
  150:     $commandpre='perl ./'; # Use the check-rpms in the current directory.
  151:   }
  152: elsif (-e 'loncom/build/check-rpms')
  153:   {
  154:     $commandpre='perl loncom/build/'; # Use check-rpms in the loncom/build dir.
  155:   }
  156: elsif (-e '/usr/local/loncapa/bin/check-rpms')
  157:   {
  158:     $commandpre='perl /usr/local/loncapa/bin/'; # Use /usr/local dir.
  159:   }
  160: else # Cannot find check-rpms, so abort.
  161:   {
  162:     terminate($mode,$out,
  163: 	      '**** ERROR **** CANNOT FIND THE check-rpms SCRIPT'."\n");
  164:   }
  165: 
  166: # Define check-rpms invocation based on the path to the check-rpms command.
  167: $checkcommand = $commandpre.$checkcommand;
  168: 
  169: # ============================================================= Initial output.
  170: 
  171: print($out <<END) if $mode eq 'html';
  172: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  173:  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  174: <html>
  175: <head>
  176: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  177: <title>CHECKRPMS STATUS REPORT</title>
  178: </head>
  179: <body bgcolor="white">
  180: <h1>CHECKRPMS STATUS REPORT</h1>
  181: <hr />
  182: <pre>
  183: END
  184: 
  185: # Notify user of current action.
  186: print($out <<END);
  187: THIS SCRIPT IS NOW PROBING SEVERAL FTP SERVERS....
  188: PLEASE BE PATIENT, THIS MAY TAKE A FEW MINUTES.
  189: END
  190: 
  191: # ============== Go through all the servers until a decent connection is found.
  192: SERVERLOOP: foreach my $serverpath (@serverpaths_to_try)
  193:   {
  194:     $serverpath=~/^(.*?)\//; # Pattern match the ip name.
  195:     $FTPSERVER=$1; # Set to the ip name.
  196:     $FTPSERVER_noauth=$FTPSERVER;
  197:     $FTPSERVER_noauth=~s/^.*?\@//;
  198:     print($out
  199: 	  "Trying $FTPSERVER_noauth...\n"); # Notify of attempts with ip name.
  200:     `ping -c 1 -w 10 $FTPSERVER_noauth 2>/dev/null`;#Ping ftp server (u there?)
  201:     if ($?==0) # If the ftp server can be pinged.
  202:       {
  203: 	print($out "$FTPSERVER found...\n"); # Tell user ftp server is found.
  204: 	`ncftpls ftp://$FTPSERVER`; # Try to access server with ftp protocol.
  205: 	if ($?==0) # If the ftp server can be accessed with the ftp protocol.
  206:           {
  207: 	    $FTPUPDATES="$serverpath$RHversion/en/os"; # The full update path.
  208: 	    # Print the check-rpms command that will be executed.
  209: 	    print($out $checkcommand.' '.$FTPUPDATES."\n");
  210: 	    if ($mode eq 'download') # Was CHECKRPMS run in download mode?
  211:               {
  212: 		$|=1; # Try to send things immediately to stdout; err umm....
  213: 		# Tell the user about the /tmp/loncapa_rpm_updates directory.
  214: 		print($out '**** NOTE **** '.
  215: 		      'To check the status of the download, you can '.
  216: 		      'periodically inspect the contents of the '.
  217: 		      '/tmp/loncapa_rpm_updates directory.  '.
  218: 		      'Please be patient; this download may take a while.'.
  219: 		      "\n");
  220: 		# Do the download.
  221: 		print($out `$checkcommand $FTPUPDATES 2>\&1`);
  222: 		# Tell the user about what action they need to take with the
  223: 		# downloaded RPMs.
  224: 		print($out
  225: 		      'You may now wish to visit the /tmp/loncapa_rpm_updates'.
  226: 		      ' directory and upgrade the RPMs.  '."\n".
  227: 		      'If this is a critical server (it is currently being'.
  228: 		      ' used for classes) and you do not know how to upgrade'.
  229: 		      ' RPMs, you should consult someone who has experience '.
  230: 		      'with the "rpm" command.'."\n");
  231: 		clean_exit($mode,$out,0); # Assume everything is okay and exit.
  232: 	      }
  233: 	    @rpms=`$checkcommand $FTPUPDATES 2>\&1`; # Read in list of RPMs.
  234: 	    # Create a text string that can be pattern matched.
  235: 	    my $rpmtext=join('',@rpms);
  236: 	    if ($rpmtext=~/You do not seem to have a/) # No www?
  237:               {
  238: 		print($out "You do not have a 'www' user on your system.\n".
  239: 		      "Please add this user and try this command again.\n");
  240: 		clean_exit($mode,$out,0);
  241: 	      }
  242: 	    if ($rpmtext=~/This account is currently not/) # ------------ uh-oh
  243: 	      {
  244: 		print($out "...strange error, moving on ($FTPSERVER)\n");
  245: 	      }
  246: 	    else # --------------------------------------- the output is "good"
  247: 	      {
  248: 		$goodoutput=$rpmtext;
  249: 		unless (@rpms) # If there are no RPMs to update.
  250: 		  {
  251: 		    $reallygoodoutput = <<END;
  252: **** NOTE **** All RPMS on your system appear to be up to date.
  253: END
  254:                     $goodoutput = ' ';
  255: 		  }
  256: 		last SERVERLOOP;
  257: 	      }
  258: 	  }
  259: 	print($out '...cannot establish an ftp session with '.$FTPSERVER."\n");
  260:       }
  261:     else
  262:       {
  263: 	print($out "...cannot find $FTPSERVER on the network\n");
  264:       }
  265:   }
  266: if (!$goodoutput) # If never received any useable output, assume "no server".
  267:   {
  268:     print($out '**** ERROR **** Cannot find a working ftp server.'."\n");
  269:     $simplestatus.='status=fail';
  270:     clean_exit($mode,$out,0);
  271:   }
  272: elsif ($reallygoodoutput) # Everything is peachy keen and up-to-date already.
  273:   {
  274:     print($out $reallygoodoutput);
  275:     $simplestatus.='status=okay';
  276:   }
  277: else # There are RPMs that need to be updated; show list to user.
  278:   {
  279:     my $rpmcount=scalar(@rpms); # Count up size of RPM list.
  280:     print($out <<END); # Print out an advisory warning to user.
  281: **** WARNING **** You need to update at least $rpmcount RPMS shown in
  282: the list below.  THIS IS IMPORTANT FOR SECURITY.
  283: 
  284: END
  285:     print($out $goodoutput); # Output the RPM list.
  286:     $simplestatus.='status=okay&rpmcount='.$rpmcount;
  287:     if ($mode eq 'interactive')
  288:       {
  289: 	print($out <<END);
  290: Do you want to download the RPMs listed above (y/n)?
  291: END
  292:         my $in=<>;
  293: 	if ($in=~/^y/)
  294: 	  {
  295:             print($out 'Please be patient... downloading into '.
  296: 		  '/tmp/loncapa_rpm_updates'."\n");
  297:             print($out `perl $command_name --download`);
  298:             clean_exit($mode,$out,0);
  299: 	  }
  300:       }
  301:     print($out <<END); # Output instructions to user about taking action.
  302: 
  303: Please visit ftp://$FTPUPDATES
  304: and download the RPMS you need.
  305: For instructions on working with (and upgrading) RPMS, please
  306: visit http://www.rpm.org/max-rpm/.
  307: To automatically download these RPMs to /tmp/loncapa_rpm_updates/,
  308: run the CHECKRPMS command as "./CHECKRPMS --download"
  309: END
  310:     if ($mode eq 'cronmail')
  311:       {
  312: 	print($out <<END); # Output more instructions to user.
  313: CHECKRPMS should be located in /usr/local/loncapa/bin/.
  314: END
  315:       }
  316:   }
  317: 
  318: clean_exit($mode,$out,0);
  319: 
  320: # ================================================================ Subroutines.
  321: 
  322: sub clean_exit
  323:   {
  324:     my ($mode,$out,$code)=@_;
  325: #
  326: # Store simple status
  327: #
  328:     open(SMP,'>/home/httpd/html/lon-status/checkrpms.txt');
  329:     print SMP $simplestatus."\n";
  330:     close(SMP);
  331: #
  332: # Mail
  333: #
  334:     if ($mode eq 'cronmail') # If cronmail mode, then mail LON-CAPA sys admin.
  335:       {
  336:         close(FOUT);
  337: 
  338: 	# Read in configuration to get e-mail addresses.
  339: 	my $perlvarref = read_conf('loncapa.conf');
  340: 	my %perlvar = %{$perlvarref};
  341: 	undef $perlvarref;
  342: 	delete $perlvar{'lonReceipt'}; # remove since sensitive
  343: 	delete $perlvar{'lonSqlAccess'}; # remove since sensitive
  344: 
  345: 	# Set metadata for the e-mail.
  346:         my $emailto = "$perlvar{'lonAdmEMail'}";
  347: 
  348: 	my $subj=$perlvar{'lonHostID'}.', RPMS to upgrade';
  349: 
  350: 	# Make the e-mail's subject header to describe whether up-to-date.
  351: 	if ($reallygoodoutput)
  352: 	  {
  353: 	    $subj = 'happy_lon: '.$subj; # Machine is up-to-date.
  354: 	  }
  355: 	else
  356: 	  {
  357: 	    $subj = 'ALERT_lon: '.$subj; # There are out-of-date RPMs.
  358: 	  }
  359: 
  360: 	# Send the e-mail.
  361: 	my $date = `date +"\%e"`; # Always send a day after the "Ides" or after
  362: 	                          # the beginning of a month.  (In other words,
  363: 	                          # send an e-mail every two weeks regardless.)
  364:         if (!$reallygoodoutput or
  365: 	    $date == 2 or
  366: 	    $date == 16)
  367: 	  {
  368: 	    system(
  369: 		   'metasend -b -t '.$emailto.' -s '.
  370: 		   "'$subj' -f /tmp/CHECKRPMS.$$ -m text/plain");
  371: 	  }
  372:       }
  373: 
  374:     print($out <<END) if $mode eq 'html'; # If html mode, print ending tags.
  375: </pre>
  376: </body>
  377: </html>
  378: END
  379:     exit($code);
  380:   }
  381: 
  382: sub terminate
  383:   {
  384:     my ($mode,$out,$output);
  385:     if ($mode eq 'html')
  386:       {
  387:         print($out <<END);
  388: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  389:  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  390: <html>
  391: <head>
  392: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  393: <title>CHECKRPMS ERROR</title>
  394: </head>
  395: <body bgcolor="white">
  396: <h1>CHECKRPMS ERROR</h1>
  397: <hr />
  398: <p><font color="red"><font size="+1">
  399: END
  400:       }
  401:     print($out $output);
  402:     if ($mode eq 'html')
  403:       {
  404:         print($out <<END);
  405: </font></font></p></body></html>
  406: END
  407:       }
  408:   }
  409: 
  410: 
  411: # - read_conf: read LON-CAPA server configuration, especially PerlSetVar values
  412: sub read_conf
  413:   {
  414:     my (@conf_files)=@_;
  415:     my %perlvar;
  416:     my $confdir='/etc/httpd/conf/';
  417:     foreach my $filename (@conf_files,'loncapa_apache.conf')
  418:       {
  419: 	open(CONFIG,'<'.$confdir.$filename) or
  420: 	    die("Can't read $confdir$filename");
  421: 	while (my $configline=<CONFIG>)
  422: 	  {
  423: 	    if ($configline =~ /^[^\#]*PerlSetVar/)
  424: 	      {
  425: 		my ($unused,$varname,$varvalue)=split(/\s+/,$configline);
  426: 		chomp($varvalue);
  427: 		$perlvar{$varname}=$varvalue;
  428: 	      }
  429: 	  }
  430: 	close(CONFIG);
  431:       }
  432:     my $perlvarref=\%perlvar;
  433:     return ($perlvarref);
  434:   }
  435: 

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