Annotation of loncom/build/CHECKRPMS, revision 1.4

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: 
                     42: fedora - yum
                     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);
                     83: } elsif ($distro =~ /^(suse|sles)9\.\d$/) {
                     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);
                     91: } elsif ($distro =~ /^rhes4$/) {
                     92:     $cmd ='up2date -u --nox';
                     93:     &prepare_msg($tmpfile,$cmd);
                     94:     ($send,$addsubj) = &check_with_up2date($tmpfile);
                     95: } else {
                     96:     $cmd = '/usr/local/bin/check-rpms --update';
                     97:     ($send,$addsubj) = &check_with_checkrpms($tmpfile);
                     98: }
                     99: if ($send) {
                    100:     $subj .= $addsubj;
                    101:     system(qq{mail -s '$subj' $emailto < $tmpfile});
                    102: }
                    103: 
                    104: sub prepare_msg {
                    105:     my ($tmpfile,$cmd) = @_;
                    106:     #
                    107:     # Put some nice text in $tmpfile
                    108:     open(TMPFILE,">>$tmpfile");
                    109:     print TMPFILE <<ENDHEADER;
                    110: Your system needs to be updated.  Please execute (as root)
                    111: 
                    112: $cmd
                    113: 
                    114: to bring it up to date.
                    115: 
                    116: This is very important for the security of your server.  The table below lists the packages which need to be updated.
                    117: 
                    118: ENDHEADER
                    119:     close(TMPFILE);
                    120:     return;
                    121: }
                    122: 
                    123: sub check_with_you {
                    124:     my ($tmpfile) =@_;
                    125:     my $you = '/usr/bin/online_update';
                    126:     my $sendflag = 0;
                    127:     my $append_to_subj;
                    128: 
                    129:     if (open (PIPE, "$you -d -k -l en 2>&1 |")) {
                    130:         my $output=<PIPE>;
                    131:         close(PIPE);
                    132:         chomp $output;
                    133:         unless ($output eq 'No updates available.') {
                    134:             my $command = $you.' -s  -k -l en |grep ^[^I]  >>'.$tmpfile;
                    135:             system($command);
                    136:             $sendflag = 1;
                    137:             $append_to_subj = ' RPMS to upgrade';
                    138:         }
                    139:     } else {
                    140:         $sendflag = 1;
                    141:         $append_to_subj = ' Error running RPM update script';
                    142:     }
                    143:     return ($sendflag,$append_to_subj);
                    144: }
                    145: 
                    146: sub check_with_yum {
                    147:     my ($tmpfile) = @_;
                    148:     my $yum = '/usr/bin/yum';
                    149:     my $sendflag = 0;
                    150:     my $append_to_subj;
                    151: 
                    152:     #
                    153:     # Execute yum command
                    154:     my $command = $yum.' check-update '.'>>'.$tmpfile;
                    155:     system($command);
                    156: 
                    157:     my $returnvalue = $?>>8;
                    158: 
                    159:     #
                    160:     # Determine status of yum run
                    161:     if (100 == $returnvalue) {
                    162:         $sendflag = 1;
                    163:         $append_to_subj = ' RPMS to upgrade';
                    164:     } elsif (0 != $returnvalue) {
                    165:         $sendflag = 1;
                    166:         $append_to_subj = ' Error running RPM update script';
                    167:     } else {
                    168:         # yum returned 0, so everything is up to date.
                    169:     }
                    170:     return ($sendflag,$append_to_subj);
                    171: }
                    172: 
                    173: sub check_with_up2date {
                    174:     my ($tmpfile) = @_;
                    175:     my $up2date = '/usr/bin/up2date-nox';
                    176:     my $sendflag = 0;
                    177:     my $append_to_subj;
                    178:     #
                    179:     # Execute online_update command to check for updates
                    180:     my $up2date_error = 1;
                    181:     if (open (PIPE, "$up2date -l 2>&1 |")) {
                    182:         my @result=<PIPE>;
                    183:         close(PIPE);
1.4     ! raeburn   184:         my $output; 
        !           185:         foreach my $line (@result) {
        !           186:             if ($line =~ /^The following Packages were marked to be skipped by your configuration:/) {
        !           187:                 last;
        !           188:             } else {
        !           189:                 $output .= $line;
        !           190:             }
        !           191:         } 
1.1       raeburn   192:         if (@result > 0) {
                    193:             if ($output =~ /Fetching Obsoletes list/) {
                    194:                 $up2date_error = 0;
                    195:                 if ($output =~ /Name\s+Version\s+Rel\s+[\n\r\f]+\-+[\n\r\f]+(.+)/s) {
                    196:                     my $packagelist = $1;
1.4     ! raeburn   197:                     if ($packagelist ne '' && $packagelist !~ /^[\s\n\r\f]+$/) {
1.1       raeburn   198:                         open(TMPFILE,">>$tmpfile");
                    199:                         print TMPFILE $packagelist;
                    200:                         close(TMPFILE);
                    201:                         $append_to_subj = ' RPMS to upgrade';
                    202:                         $sendflag = 1;
                    203:                     }
                    204:                 }
                    205:             }
                    206:         }
                    207:     }
                    208:     if ($up2date_error) {
                    209:         $append_to_subj = ' Error running RPM update script';
                    210:         $sendflag = 1;
                    211:     }
                    212:     return ($sendflag,$append_to_subj);
                    213: }
                    214: 
                    215: sub check_with_rug {
                    216:     my ($tmpfile) = @_;
                    217:     my $rug = '/usr/bin/rug';
                    218:     my $sendflag = 0;
                    219:     my $append_to_subj;
                    220:     #
                    221:     # Execute rug command to check for updates
                    222:     if (open (PIPE, "$rug up -N 2>&1 |")) {
                    223:         my @output=<PIPE>;
                    224:         close(PIPE);
                    225:         chomp(@output);
                    226:         my @clean_output;
                    227:         foreach my $line (@output) {
1.3       raeburn   228:             if ($line =~ /^Waking\sup\sZMD\.\.\./) {
1.1       raeburn   229:                 next;
1.2       raeburn   230:             } elsif ($line eq 'Done') {
                    231:                 next;
                    232:             } elsif ($line eq '') {
                    233:                 next;
                    234:             } elsif ($line eq 'The following packages will be installed:') {
                    235:                 next;
                    236:             } elsif ($line eq 'Resolving Dependencies...') {
                    237:                 next;
                    238:             } elsif ($line eq 'Transaction...') {
                    239:                 last;
                    240:             } elsif ($line eq 'No updates are available.') {
1.1       raeburn   241:                 last;
                    242:             } else {
                    243:                 push(@clean_output,$line);
                    244:             }
                    245:         }
                    246:         if (@clean_output > 0) {
                    247:             open(TMPFILE,">>$tmpfile");
                    248:             print TMPFILE join("\n",@clean_output);
                    249:             close(TMPFILE);
                    250:             $append_to_subj= ' RPMS to upgrade';
                    251:             $sendflag = 1;
                    252:          }
                    253:     } else {
                    254:         $append_to_subj = ' Error running RPM update check';
                    255:         $sendflag = 1;
                    256:     }
                    257:     return ($sendflag,$append_to_subj);
                    258: }
                    259: 
                    260: sub check_with_checkrpms {
                    261:     my ($tmpfile,$perlvar) = @_;
                    262:     my $checkrpms = '/usr/local/bin/check-rpms';
                    263:     my $sendflag = 0;
                    264:     my $append_to_subj;
                    265: 
                    266:     # Run Martin Seigert's checkrpms script.  See
                    267:     # See http://www.sfu.ca/acs/security/linux/check-rpms.html 
                    268:     # for more information.
                    269: 
                    270:     #
                    271:     # Check that checkrpms is installed and is the proper version...
                    272:     if (! -e $checkrpms) {
                    273:        open(TMPFILE,">>$tmpfile");
                    274:        print TMPFILE <<END;
                    275: 
                    276: Unable to locate check-rpms on your system.  Please go to
                    277: http://www.sfu.ca/acs/security/linux/check-rpms.html, download and
                    278: install check-rpms on this system.
                    279: 
                    280: END
                    281:         $append_to_subj = ' Error running RPM update check';
                    282:         $sendflag = 1; 
                    283:     } else {
                    284:         #
                    285:         # Run check-rpms and capture its output
                    286:         if (open (PIPE, "$checkrpms 2>&1 |")) {
                    287:             my $output=<PIPE>;
                    288:             close(PIPE);
                    289:             if ($output ne '') {
                    290:                 $output = <<"END";
                    291: 
                    292: checkrpms checked the status of the packages on your system and
                    293: produced the following output:
                    294: -------------------------------------------------------
                    295: $output
                    296: -------------------------------------------------------
                    297: If there are rpms which need to be installed, please log into
                    298: $perlvar->{'lonHostID'} and run the following command
                    299: 
                    300: $checkrpms --update
                    301: 
                    302: If there are kernel packages to be installed, use
                    303: 
                    304: $checkrpms --update --install-kernel
                    305: 
                    306: Keeping your system up to date is very important.
                    307: Ensuring you are using up to date software is a prerequisite for a
                    308: secure system.
                    309: 
                    310: END
                    311:                 open(TMPFILE,">>$tmpfile");
                    312:                 print TMPFILE $output;
                    313:                 close(TMPFILE);
                    314:                 $append_to_subj = ' RPMS to upgrade';
                    315:                 $sendflag = 1; 
                    316:             }
                    317:         }
                    318:     }
                    319:     return ($sendflag,$append_to_subj);
                    320: }

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