File:  [LON-CAPA] / loncom / build / Attic / CHECKRPMS.default
Revision 1.12: download - view: text, annotated - select for diffs
Wed Dec 11 18:28:30 2002 UTC (21 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_0_6, HEAD

- suprressing the happy_lon emails (BUG#1048)

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

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