File:  [LON-CAPA] / loncom / build / CHECKRPMS
Revision 1.9: download - view: text, annotated - select for diffs
Thu Sep 13 21:50:37 2007 UTC (16 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_99_0, version_2_5_2, HEAD
Use zypper to check for updates for SuSE 10.2 and later.
Continue using rug for SLES10 and SuSE 10.1.

    1: #!/usr/bin/perl -w
    2: #
    3: # The LearningOnline Network with CAPA
    4: # Checks status of RPM packages on system.
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: =pod
   30: 
   31: =head1 NAME
   32: 
   33: B<CHECKRPMS> - automated status report about RPMs on a system. 
   34: 
   35: =head1 DESCRIPTION
   36: 
   37: This file automates the process of checking for available updates
   38: to LON-CAPA systems. distprobe is used to determine the Linux distribution.
   39: 
   40: The utility which is used to complete the check depends on the distro:
   41: 
   42: fedora, rhel 5/5+, centos, scientific - yum
   43: suse 9.X and sles9 - you
   44: suse 10.2,10.3 - zypper 
   45: sles10,suse10.1 - rug
   46: rhel 4 - up2date
   47: others - check-rpms
   48: 
   49: Created by amalgamating previous distribution-specific CHECKRPMS.dist files (where dist was one of: fedora, rhel, suse, sles10, default).
   50: 
   51: Must be run as root or www.
   52: 
   53: =cut
   54: 
   55: use strict;
   56: use lib '/home/httpd/lib/perl/';
   57: use LONCAPA::Configuration;
   58: 
   59: my $tmpfile = '/tmp/CHECKRPMS.'.$$;
   60: my $perlvar= LONCAPA::Configuration::read_conf('loncapa.conf');
   61: 
   62: # Determine who we email
   63: my $emailto = "$perlvar->{'lonAdmEMail'}";
   64: my $subj = $perlvar->{'lonHostID'};
   65: 
   66: # Get Linux distro
   67: open(PIPE, "$perlvar->{'lonDaemons'}/distprobe |");
   68: my $distro = <PIPE>;
   69: close(PIPE);
   70: 
   71: undef($perlvar);
   72: 
   73: my $hostname = `hostname`;
   74: chomp($hostname);
   75: open(TMPFILE,">$tmpfile");
   76: print TMPFILE localtime(time).'    '.$hostname."\n";
   77: close(TMPFILE);
   78: 
   79: my ($cmd,$send,$addsubj);
   80: if ($distro =~ /^fedora\d+$/) {
   81:     $cmd = 'yum update';
   82:     &prepare_msg($tmpfile,$cmd);
   83:     ($send,$addsubj) = &check_with_yum($tmpfile);
   84: } elsif ($distro =~ /^(suse|sles)9\.?\d?$/) {
   85:     $cmd = 'you';
   86:     &prepare_msg($tmpfile,$cmd);
   87:     ($send,$addsubj) = &check_with_you($tmpfile);
   88: } elsif ($distro =~ /^suse10\.(\d)$/) {
   89:     my $version =$1;
   90:     if ($version > 1) { 
   91:         $cmd = 'zypper up';
   92:         &prepare_msg($tmpfile,$cmd);
   93:         ($send,$addsubj) = &check_with_zypper($tmpfile);
   94:     } else {
   95:         $cmd = 'rug up';
   96:         &prepare_msg($tmpfile,$cmd);
   97:         ($send,$addsubj) = &check_with_rug($tmpfile);
   98:     }
   99: } elsif ($distro =~ /^sles10$/) {
  100:     $cmd = 'rug up';
  101:     &prepare_msg($tmpfile,$cmd);
  102:     ($send,$addsubj) = &check_with_rug($tmpfile);
  103: } elsif ($distro =~ /^rhes(\d+)$/) {
  104:     my $version = $1;
  105:     if ($version == 4) {
  106:         $cmd ='up2date -u --nox';
  107:         &prepare_msg($tmpfile,$cmd);
  108:         ($send,$addsubj) = &check_with_up2date($tmpfile);
  109:     } elsif ($version > 4) {
  110:         $cmd = 'yum update';
  111:         &prepare_msg($tmpfile,$cmd);
  112:         ($send,$addsubj) = &check_with_yum($tmpfile);
  113:     }
  114: } elsif ($distro =~ /^centos\d+$/) {
  115:     $cmd = 'yum update';
  116:     &prepare_msg($tmpfile,$cmd);
  117:     ($send,$addsubj) = &check_with_yum($tmpfile);
  118: } elsif ($distro =~ /^scientific\d+\.\d$/) {
  119:     $cmd = 'yum update';
  120:     &prepare_msg($tmpfile,$cmd);
  121:     ($send,$addsubj) = &check_with_yum($tmpfile);
  122: } else {
  123:     $cmd = '/usr/local/bin/check-rpms --update';
  124:     ($send,$addsubj) = &check_with_checkrpms($tmpfile);
  125: }
  126: if ($send) {
  127:     $subj .= $addsubj;
  128:     system(qq{mail -s '$subj' $emailto < $tmpfile});
  129: }
  130: 
  131: sub prepare_msg {
  132:     my ($tmpfile,$cmd) = @_;
  133:     #
  134:     # Put some nice text in $tmpfile
  135:     open(TMPFILE,">>$tmpfile");
  136:     print TMPFILE <<ENDHEADER;
  137: Your system needs to be updated.  Please execute (as root)
  138: 
  139: $cmd
  140: 
  141: to bring it up to date.
  142: 
  143: This is very important for the security of your server.  The packages which need to be updated are listed below.
  144: 
  145: ENDHEADER
  146:     close(TMPFILE);
  147:     return;
  148: }
  149: 
  150: sub check_with_you {
  151:     my ($tmpfile) =@_;
  152:     my $you = '/usr/bin/online_update';
  153:     my $sendflag = 0;
  154:     my $append_to_subj;
  155: 
  156:     if (open (PIPE, "$you -k -len 2>&1 |")) {
  157:         my $output=<PIPE>;
  158:         close(PIPE);
  159:         chomp $output;
  160:         unless ($output eq 'No updates available.') {
  161:             if (open (PIPE, "$you -s -d -len |grep ^INSTALL |")) {
  162:                 my @updates = <PIPE>;
  163:                 close(PIPE);
  164:                 my $allpackages;
  165:                 foreach my $line (@updates) {
  166:                     my $package = substr($line,rindex($line,'/')+1);
  167:                     if ($package ne '') {
  168:                         $allpackages .= $package;
  169:                     }
  170:                 }
  171:                 if ($allpackages ne '') {
  172:                     open(TMPFILE,">>$tmpfile");
  173:                     print TMPFILE $allpackages;
  174:                     close(TMPFILE);
  175:                     $sendflag = 1;
  176:                     $append_to_subj = ' RPMS to upgrade';
  177:                 }
  178:             } else {
  179:                 $sendflag = 1;
  180:                 $append_to_subj = ' Error running RPM update script';
  181:             }
  182:         }
  183:     } else {
  184:         $sendflag = 1;
  185:         $append_to_subj = ' Error running RPM update script';
  186:     }
  187:     return ($sendflag,$append_to_subj);
  188: }
  189: 
  190: sub check_with_yum {
  191:     my ($tmpfile) = @_;
  192:     my $yum = '/usr/bin/yum';
  193:     my $sendflag = 0;
  194:     my $append_to_subj;
  195: 
  196:     #
  197:     # Execute yum command
  198:     my $command = $yum.' check-update '.'>>'.$tmpfile;
  199:     system($command);
  200: 
  201:     my $returnvalue = $?>>8;
  202: 
  203:     #
  204:     # Determine status of yum run
  205:     if (100 == $returnvalue) {
  206:         $sendflag = 1;
  207:         $append_to_subj = ' RPMS to upgrade';
  208:     } elsif (0 != $returnvalue) {
  209:         $sendflag = 1;
  210:         $append_to_subj = ' Error running RPM update script';
  211:     } else {
  212:         # yum returned 0, so everything is up to date.
  213:     }
  214:     return ($sendflag,$append_to_subj);
  215: }
  216: 
  217: sub check_with_up2date {
  218:     my ($tmpfile) = @_;
  219:     my $up2date = '/usr/bin/up2date-nox';
  220:     my $sendflag = 0;
  221:     my $append_to_subj;
  222:     #
  223:     # Execute online_update command to check for updates
  224:     my $up2date_error = 1;
  225:     if (open (PIPE, "$up2date -l 2>&1 |")) {
  226:         my @result=<PIPE>;
  227:         close(PIPE);
  228:         my $output; 
  229:         foreach my $line (@result) {
  230:             if ($line =~ /^The following Packages were marked to be skipped by your configuration:/) {
  231:                 last;
  232:             } else {
  233:                 $output .= $line;
  234:             }
  235:         } 
  236:         if (@result > 0) {
  237:             if ($output =~ /Fetching Obsoletes list/) {
  238:                 $up2date_error = 0;
  239:                 if ($output =~ /Name\s+Version\s+Rel\s+[\n\r\f]+\-+[\n\r\f]+(.+)/s) {
  240:                     my $packagelist = $1;
  241:                     if ($packagelist ne '' && $packagelist !~ /^[\s\n\r\f]+$/) {
  242:                         open(TMPFILE,">>$tmpfile");
  243:                         print TMPFILE $packagelist;
  244:                         close(TMPFILE);
  245:                         $append_to_subj = ' RPMS to upgrade';
  246:                         $sendflag = 1;
  247:                     }
  248:                 }
  249:             }
  250:         }
  251:     }
  252:     if ($up2date_error) {
  253:         $append_to_subj = ' Error running RPM update script';
  254:         $sendflag = 1;
  255:     }
  256:     return ($sendflag,$append_to_subj);
  257: }
  258: 
  259: sub check_with_rug {
  260:     my ($tmpfile) = @_;
  261:     my $rug = '/usr/bin/rug';
  262:     my $sendflag = 0;
  263:     my $append_to_subj;
  264:     #
  265:     # Execute rug command to check for updates
  266:     if (open (PIPE, "$rug up -N 2>&1 |")) {
  267:         my @output=<PIPE>;
  268:         close(PIPE);
  269:         chomp(@output);
  270:         my @clean_output;
  271:         foreach my $line (@output) {
  272:             if ($line =~ /^Waking\sup\sZMD\.\.\./) {
  273:                 next;
  274:             } elsif ($line eq 'Done') {
  275:                 next;
  276:             } elsif ($line eq '') {
  277:                 next;
  278:             } elsif ($line eq 'The following packages will be installed:') {
  279:                 next;
  280:             } elsif ($line eq 'Resolving Dependencies...') {
  281:                 next;
  282:             } elsif ($line eq 'Transaction...') {
  283:                 last;
  284:             } elsif ($line eq 'No updates are available.') {
  285:                 last;
  286:             } elsif ($line eq 'Downloading Packages...') {
  287:                 last;
  288:             } else {
  289:                 push(@clean_output,$line);
  290:             }
  291:         }
  292:         if (@clean_output > 0) {
  293:             open(TMPFILE,">>$tmpfile");
  294:             print TMPFILE join("\n",@clean_output);
  295:             close(TMPFILE);
  296:             $append_to_subj= ' RPMS to upgrade';
  297:             $sendflag = 1;
  298:          }
  299:     } else {
  300:         $append_to_subj = ' Error running RPM update check';
  301:         $sendflag = 1;
  302:     }
  303:     return ($sendflag,$append_to_subj);
  304: }
  305: 
  306: sub check_with_zypper {
  307:     my ($tmpfile) = @_;
  308:     my $zypper = '/usr/bin/zypper';
  309:     my $sendflag = 0;
  310:     my $append_to_subj;
  311:     my $header;
  312:     #
  313:     # Execute zypper command to check for updates
  314:     if (open (PIPE, "$zypper lu 2>&1 |")) {
  315:         my @output=<PIPE>;
  316:         close(PIPE);
  317:         chomp(@output);
  318:         my @clean_output;
  319:         foreach my $line (@output) {
  320:             if ($line eq 'Restoring system sources...') {
  321:                 next;
  322:             } elsif ($line =~ /^Parsing\smetadata\sfor\s/) {
  323:                 next;
  324:             } elsif ($line eq 'Parsing RPM database...') {
  325:                 next;
  326:             } elsif ($line  =~ /^Catalog\s+\|\s+Name\s+\|\s+Version\s+\|\s+Category\s+\|\s+Status$/) {
  327:                 $header = $line."\n";
  328:                 next;
  329:             } elsif ($line =~ /^[-+]+$/) {
  330:                 $header .= $line."\n";
  331:                 next;
  332:             } elsif ($line eq 'WARNING: These are only the updates affecting the updater itself.') {
  333:                 next;
  334:             } elsif ($line eq 'There are others available too.') {
  335:                 next;
  336:             } else {
  337:                 push(@clean_output,$line);
  338:             }
  339:         }
  340:         if (@clean_output > 0) {
  341:             open(TMPFILE,">>$tmpfile");
  342:             my $message = join("\n",@clean_output);
  343:             print TMPFILE $header.$message;
  344:             close(TMPFILE);
  345:             $append_to_subj= ' RPMS to upgrade';
  346:             $sendflag = 1;
  347:         }
  348:     } else {
  349:         $append_to_subj = ' Error running RPM update check';
  350:         $sendflag = 1;
  351:     }
  352:     return ($sendflag,$append_to_subj);
  353: }
  354: 
  355: sub check_with_checkrpms {
  356:     my ($tmpfile,$perlvar) = @_;
  357:     my $checkrpms = '/usr/local/bin/check-rpms';
  358:     my $sendflag = 0;
  359:     my $append_to_subj;
  360: 
  361:     # Run Martin Seigert's checkrpms script.  See
  362:     # See http://www.sfu.ca/acs/security/linux/check-rpms.html 
  363:     # for more information.
  364: 
  365:     #
  366:     # Check that checkrpms is installed and is the proper version...
  367:     if (! -e $checkrpms) {
  368:        open(TMPFILE,">>$tmpfile");
  369:        print TMPFILE <<END;
  370: 
  371: Unable to locate check-rpms on your system.  Please go to
  372: http://www.sfu.ca/acs/security/linux/check-rpms.html, download and
  373: install check-rpms on this system.
  374: 
  375: END
  376:         $append_to_subj = ' Error running RPM update check';
  377:         $sendflag = 1; 
  378:     } else {
  379:         #
  380:         # Run check-rpms and capture its output
  381:         if (open (PIPE, "$checkrpms 2>&1 |")) {
  382:             my $output=<PIPE>;
  383:             close(PIPE);
  384:             if ($output ne '') {
  385:                 $output = <<"END";
  386: 
  387: checkrpms checked the status of the packages on your system and
  388: produced the following output:
  389: -------------------------------------------------------
  390: $output
  391: -------------------------------------------------------
  392: If there are rpms which need to be installed, please log into
  393: $perlvar->{'lonHostID'} and run the following command
  394: 
  395: $checkrpms --update
  396: 
  397: If there are kernel packages to be installed, use
  398: 
  399: $checkrpms --update --install-kernel
  400: 
  401: Keeping your system up to date is very important.
  402: Ensuring you are using up to date software is a prerequisite for a
  403: secure system.
  404: 
  405: END
  406:                 open(TMPFILE,">>$tmpfile");
  407:                 print TMPFILE $output;
  408:                 close(TMPFILE);
  409:                 $append_to_subj = ' RPMS to upgrade';
  410:                 $sendflag = 1; 
  411:             }
  412:         }
  413:     }
  414:     return ($sendflag,$append_to_subj);
  415: }

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