Annotation of loncom/build/CHECKRPMS_custom, revision 1.2

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

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