Annotation of loncom/build/CHECKRPMS, revision 1.3
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);
184: if (@result > 0) {
185: my $output = join('',@result);
186: if ($output =~ /Fetching Obsoletes list/) {
187: $up2date_error = 0;
188: if ($output =~ /Name\s+Version\s+Rel\s+[\n\r\f]+\-+[\n\r\f]+(.+)/s) {
189: my $packagelist = $1;
190: unless (($packagelist =~ /^The following Packages were marked to be skipped by your configuration:/) || ($packagelist eq '')) {
191: open(TMPFILE,">>$tmpfile");
192: print TMPFILE $packagelist;
193: close(TMPFILE);
194: $append_to_subj = ' RPMS to upgrade';
195: $sendflag = 1;
196: }
197: }
198: }
199: }
200: }
201: if ($up2date_error) {
202: $append_to_subj = ' Error running RPM update script';
203: $sendflag = 1;
204: }
205: return ($sendflag,$append_to_subj);
206: }
207:
208: sub check_with_rug {
209: my ($tmpfile) = @_;
210: my $rug = '/usr/bin/rug';
211: my $sendflag = 0;
212: my $append_to_subj;
213: #
214: # Execute rug command to check for updates
215: if (open (PIPE, "$rug up -N 2>&1 |")) {
216: my @output=<PIPE>;
217: close(PIPE);
218: chomp(@output);
219: my @clean_output;
220: foreach my $line (@output) {
1.3 ! raeburn 221: if ($line =~ /^Waking\sup\sZMD\.\.\./) {
1.1 raeburn 222: next;
1.2 raeburn 223: } elsif ($line eq 'Done') {
224: next;
225: } elsif ($line eq '') {
226: next;
227: } elsif ($line eq 'The following packages will be installed:') {
228: next;
229: } elsif ($line eq 'Resolving Dependencies...') {
230: next;
231: } elsif ($line eq 'Transaction...') {
232: last;
233: } elsif ($line eq 'No updates are available.') {
1.1 raeburn 234: last;
235: } else {
236: push(@clean_output,$line);
237: }
238: }
239: if (@clean_output > 0) {
240: open(TMPFILE,">>$tmpfile");
241: print TMPFILE join("\n",@clean_output);
242: close(TMPFILE);
243: $append_to_subj= ' RPMS to upgrade';
244: $sendflag = 1;
245: }
246: } else {
247: $append_to_subj = ' Error running RPM update check';
248: $sendflag = 1;
249: }
250: return ($sendflag,$append_to_subj);
251: }
252:
253: sub check_with_checkrpms {
254: my ($tmpfile,$perlvar) = @_;
255: my $checkrpms = '/usr/local/bin/check-rpms';
256: my $sendflag = 0;
257: my $append_to_subj;
258:
259: # Run Martin Seigert's checkrpms script. See
260: # See http://www.sfu.ca/acs/security/linux/check-rpms.html
261: # for more information.
262:
263: #
264: # Check that checkrpms is installed and is the proper version...
265: if (! -e $checkrpms) {
266: open(TMPFILE,">>$tmpfile");
267: print TMPFILE <<END;
268:
269: Unable to locate check-rpms on your system. Please go to
270: http://www.sfu.ca/acs/security/linux/check-rpms.html, download and
271: install check-rpms on this system.
272:
273: END
274: $append_to_subj = ' Error running RPM update check';
275: $sendflag = 1;
276: } else {
277: #
278: # Run check-rpms and capture its output
279: if (open (PIPE, "$checkrpms 2>&1 |")) {
280: my $output=<PIPE>;
281: close(PIPE);
282: if ($output ne '') {
283: $output = <<"END";
284:
285: checkrpms checked the status of the packages on your system and
286: produced the following output:
287: -------------------------------------------------------
288: $output
289: -------------------------------------------------------
290: If there are rpms which need to be installed, please log into
291: $perlvar->{'lonHostID'} and run the following command
292:
293: $checkrpms --update
294:
295: If there are kernel packages to be installed, use
296:
297: $checkrpms --update --install-kernel
298:
299: Keeping your system up to date is very important.
300: Ensuring you are using up to date software is a prerequisite for a
301: secure system.
302:
303: END
304: open(TMPFILE,">>$tmpfile");
305: print TMPFILE $output;
306: close(TMPFILE);
307: $append_to_subj = ' RPMS to upgrade';
308: $sendflag = 1;
309: }
310: }
311: }
312: return ($sendflag,$append_to_subj);
313: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>