Annotation of loncom/build/CHECKRPMS, revision 1.7

1.1       raeburn     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: 
1.7     ! raeburn    42: fedora, rhel 5/5+ - yum
1.1       raeburn    43: suse 9.X and sles9 - you
                     44: suse 10.X and sles10 - rug
                     45: rhel 4 - up2date
                     46: others - check-rpms
                     47: 
                     48: Created by amalgamating previous distribution-specific CHECKRPMS.dist files (where dist was one of: fedora, rhel, suse, sles10, default).
                     49: 
                     50: Must be run as root or www.
                     51: 
                     52: =cut
                     53: 
                     54: use strict;
                     55: use lib '/home/httpd/lib/perl/';
                     56: use LONCAPA::Configuration;
                     57: 
                     58: my $tmpfile = '/tmp/CHECKRPMS.'.$$;
                     59: my $perlvar= LONCAPA::Configuration::read_conf('loncapa.conf');
                     60: 
                     61: # Determine who we email
                     62: my $emailto = "$perlvar->{'lonAdmEMail'}";
                     63: my $subj = $perlvar->{'lonHostID'};
                     64: 
                     65: # Get Linux distro
                     66: open(PIPE, "$perlvar->{'lonDaemons'}/distprobe |");
                     67: my $distro = <PIPE>;
                     68: close(PIPE);
                     69: 
                     70: undef($perlvar);
                     71: 
                     72: my $hostname = `hostname`;
                     73: chomp($hostname);
                     74: open(TMPFILE,">$tmpfile");
                     75: print TMPFILE localtime(time).'    '.$hostname."\n";
                     76: close(TMPFILE);
                     77: 
                     78: my ($cmd,$send,$addsubj);
                     79: if ($distro =~ /^fedora\d+$/) {
                     80:     $cmd = 'yum update';
                     81:     &prepare_msg($tmpfile,$cmd);
                     82:     ($send,$addsubj) = &check_with_yum($tmpfile);
1.6       albertel   83: } elsif ($distro =~ /^(suse|sles)9\.?\d?$/) {
1.1       raeburn    84:     $cmd = 'you';
                     85:     &prepare_msg($tmpfile,$cmd);
                     86:     ($send,$addsubj) = &check_with_you($tmpfile);
                     87: } elsif ($distro =~ /^(suse|sles)10\.?\d?$/) {
                     88:     $cmd = 'rug up';
                     89:     &prepare_msg($tmpfile,$cmd);
                     90:     ($send,$addsubj) = &check_with_rug($tmpfile);
1.7     ! raeburn    91: } elsif ($distro =~ /^rhes(\d+)$/) {
        !            92:     my $version = $1;
        !            93:     if ($version == 4) {
        !            94:         $cmd ='up2date -u --nox';
        !            95:         &prepare_msg($tmpfile,$cmd);
        !            96:         ($send,$addsubj) = &check_with_up2date($tmpfile);
        !            97:     } elsif ($version > 4) {
        !            98:         $cmd = 'yum update';
        !            99:         &prepare_msg($tmpfile,$cmd);
        !           100:         ($send,$addsubj) = &check_with_yum($tmpfile);
        !           101:     }
1.1       raeburn   102: } else {
                    103:     $cmd = '/usr/local/bin/check-rpms --update';
                    104:     ($send,$addsubj) = &check_with_checkrpms($tmpfile);
                    105: }
                    106: if ($send) {
                    107:     $subj .= $addsubj;
                    108:     system(qq{mail -s '$subj' $emailto < $tmpfile});
                    109: }
                    110: 
                    111: sub prepare_msg {
                    112:     my ($tmpfile,$cmd) = @_;
                    113:     #
                    114:     # Put some nice text in $tmpfile
                    115:     open(TMPFILE,">>$tmpfile");
                    116:     print TMPFILE <<ENDHEADER;
                    117: Your system needs to be updated.  Please execute (as root)
                    118: 
                    119: $cmd
                    120: 
                    121: to bring it up to date.
                    122: 
1.5       raeburn   123: This is very important for the security of your server.  The packages which need to be updated are listed below.
1.1       raeburn   124: 
                    125: ENDHEADER
                    126:     close(TMPFILE);
                    127:     return;
                    128: }
                    129: 
                    130: sub check_with_you {
                    131:     my ($tmpfile) =@_;
                    132:     my $you = '/usr/bin/online_update';
                    133:     my $sendflag = 0;
                    134:     my $append_to_subj;
                    135: 
1.5       raeburn   136:     if (open (PIPE, "$you -k -len 2>&1 |")) {
1.1       raeburn   137:         my $output=<PIPE>;
                    138:         close(PIPE);
                    139:         chomp $output;
                    140:         unless ($output eq 'No updates available.') {
1.5       raeburn   141:             if (open (PIPE, "$you -s -d -len |grep ^INSTALL |")) {
                    142:                 my @updates = <PIPE>;
                    143:                 close(PIPE);
                    144:                 my $allpackages;
                    145:                 foreach my $line (@updates) {
                    146:                     my $package = substr($line,rindex($line,'/')+1);
                    147:                     if ($package ne '') {
                    148:                         $allpackages .= $package;
                    149:                     }
                    150:                 }
                    151:                 if ($allpackages ne '') {
                    152:                     open(TMPFILE,">>$tmpfile");
                    153:                     print TMPFILE $allpackages;
                    154:                     close(TMPFILE);
                    155:                     $sendflag = 1;
                    156:                     $append_to_subj = ' RPMS to upgrade';
                    157:                 }
                    158:             } else {
                    159:                 $sendflag = 1;
                    160:                 $append_to_subj = ' Error running RPM update script';
                    161:             }
1.1       raeburn   162:         }
                    163:     } else {
                    164:         $sendflag = 1;
                    165:         $append_to_subj = ' Error running RPM update script';
                    166:     }
                    167:     return ($sendflag,$append_to_subj);
                    168: }
                    169: 
                    170: sub check_with_yum {
                    171:     my ($tmpfile) = @_;
                    172:     my $yum = '/usr/bin/yum';
                    173:     my $sendflag = 0;
                    174:     my $append_to_subj;
                    175: 
                    176:     #
                    177:     # Execute yum command
                    178:     my $command = $yum.' check-update '.'>>'.$tmpfile;
                    179:     system($command);
                    180: 
                    181:     my $returnvalue = $?>>8;
                    182: 
                    183:     #
                    184:     # Determine status of yum run
                    185:     if (100 == $returnvalue) {
                    186:         $sendflag = 1;
                    187:         $append_to_subj = ' RPMS to upgrade';
                    188:     } elsif (0 != $returnvalue) {
                    189:         $sendflag = 1;
                    190:         $append_to_subj = ' Error running RPM update script';
                    191:     } else {
                    192:         # yum returned 0, so everything is up to date.
                    193:     }
                    194:     return ($sendflag,$append_to_subj);
                    195: }
                    196: 
                    197: sub check_with_up2date {
                    198:     my ($tmpfile) = @_;
                    199:     my $up2date = '/usr/bin/up2date-nox';
                    200:     my $sendflag = 0;
                    201:     my $append_to_subj;
                    202:     #
                    203:     # Execute online_update command to check for updates
                    204:     my $up2date_error = 1;
                    205:     if (open (PIPE, "$up2date -l 2>&1 |")) {
                    206:         my @result=<PIPE>;
                    207:         close(PIPE);
1.4       raeburn   208:         my $output; 
                    209:         foreach my $line (@result) {
                    210:             if ($line =~ /^The following Packages were marked to be skipped by your configuration:/) {
                    211:                 last;
                    212:             } else {
                    213:                 $output .= $line;
                    214:             }
                    215:         } 
1.1       raeburn   216:         if (@result > 0) {
                    217:             if ($output =~ /Fetching Obsoletes list/) {
                    218:                 $up2date_error = 0;
                    219:                 if ($output =~ /Name\s+Version\s+Rel\s+[\n\r\f]+\-+[\n\r\f]+(.+)/s) {
                    220:                     my $packagelist = $1;
1.4       raeburn   221:                     if ($packagelist ne '' && $packagelist !~ /^[\s\n\r\f]+$/) {
1.1       raeburn   222:                         open(TMPFILE,">>$tmpfile");
                    223:                         print TMPFILE $packagelist;
                    224:                         close(TMPFILE);
                    225:                         $append_to_subj = ' RPMS to upgrade';
                    226:                         $sendflag = 1;
                    227:                     }
                    228:                 }
                    229:             }
                    230:         }
                    231:     }
                    232:     if ($up2date_error) {
                    233:         $append_to_subj = ' Error running RPM update script';
                    234:         $sendflag = 1;
                    235:     }
                    236:     return ($sendflag,$append_to_subj);
                    237: }
                    238: 
                    239: sub check_with_rug {
                    240:     my ($tmpfile) = @_;
                    241:     my $rug = '/usr/bin/rug';
                    242:     my $sendflag = 0;
                    243:     my $append_to_subj;
                    244:     #
                    245:     # Execute rug command to check for updates
                    246:     if (open (PIPE, "$rug up -N 2>&1 |")) {
                    247:         my @output=<PIPE>;
                    248:         close(PIPE);
                    249:         chomp(@output);
                    250:         my @clean_output;
                    251:         foreach my $line (@output) {
1.3       raeburn   252:             if ($line =~ /^Waking\sup\sZMD\.\.\./) {
1.1       raeburn   253:                 next;
1.2       raeburn   254:             } elsif ($line eq 'Done') {
                    255:                 next;
                    256:             } elsif ($line eq '') {
                    257:                 next;
                    258:             } elsif ($line eq 'The following packages will be installed:') {
                    259:                 next;
                    260:             } elsif ($line eq 'Resolving Dependencies...') {
                    261:                 next;
                    262:             } elsif ($line eq 'Transaction...') {
                    263:                 last;
                    264:             } elsif ($line eq 'No updates are available.') {
1.1       raeburn   265:                 last;
1.5       raeburn   266:             } elsif ($line eq 'Downloading Packages...') {
                    267:                 last;
1.1       raeburn   268:             } else {
                    269:                 push(@clean_output,$line);
                    270:             }
                    271:         }
                    272:         if (@clean_output > 0) {
                    273:             open(TMPFILE,">>$tmpfile");
                    274:             print TMPFILE join("\n",@clean_output);
                    275:             close(TMPFILE);
                    276:             $append_to_subj= ' RPMS to upgrade';
                    277:             $sendflag = 1;
                    278:          }
                    279:     } else {
                    280:         $append_to_subj = ' Error running RPM update check';
                    281:         $sendflag = 1;
                    282:     }
                    283:     return ($sendflag,$append_to_subj);
                    284: }
                    285: 
                    286: sub check_with_checkrpms {
                    287:     my ($tmpfile,$perlvar) = @_;
                    288:     my $checkrpms = '/usr/local/bin/check-rpms';
                    289:     my $sendflag = 0;
                    290:     my $append_to_subj;
                    291: 
                    292:     # Run Martin Seigert's checkrpms script.  See
                    293:     # See http://www.sfu.ca/acs/security/linux/check-rpms.html 
                    294:     # for more information.
                    295: 
                    296:     #
                    297:     # Check that checkrpms is installed and is the proper version...
                    298:     if (! -e $checkrpms) {
                    299:        open(TMPFILE,">>$tmpfile");
                    300:        print TMPFILE <<END;
                    301: 
                    302: Unable to locate check-rpms on your system.  Please go to
                    303: http://www.sfu.ca/acs/security/linux/check-rpms.html, download and
                    304: install check-rpms on this system.
                    305: 
                    306: END
                    307:         $append_to_subj = ' Error running RPM update check';
                    308:         $sendflag = 1; 
                    309:     } else {
                    310:         #
                    311:         # Run check-rpms and capture its output
                    312:         if (open (PIPE, "$checkrpms 2>&1 |")) {
                    313:             my $output=<PIPE>;
                    314:             close(PIPE);
                    315:             if ($output ne '') {
                    316:                 $output = <<"END";
                    317: 
                    318: checkrpms checked the status of the packages on your system and
                    319: produced the following output:
                    320: -------------------------------------------------------
                    321: $output
                    322: -------------------------------------------------------
                    323: If there are rpms which need to be installed, please log into
                    324: $perlvar->{'lonHostID'} and run the following command
                    325: 
                    326: $checkrpms --update
                    327: 
                    328: If there are kernel packages to be installed, use
                    329: 
                    330: $checkrpms --update --install-kernel
                    331: 
                    332: Keeping your system up to date is very important.
                    333: Ensuring you are using up to date software is a prerequisite for a
                    334: secure system.
                    335: 
                    336: END
                    337:                 open(TMPFILE,">>$tmpfile");
                    338:                 print TMPFILE $output;
                    339:                 close(TMPFILE);
                    340:                 $append_to_subj = ' RPMS to upgrade';
                    341:                 $sendflag = 1; 
                    342:             }
                    343:         }
                    344:     }
                    345:     return ($sendflag,$append_to_subj);
                    346: }

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