5 # checks for obsolete/local and upgradeable packages.
7 # packages for the obsolete/local check can be ignored, by
8 # listing their full name in /etc/nagios/obsolete-packages-ignore
9 # or by having a regex (starting a line with "/") that matches
10 # the packagename in said file.
12 # Takes one optional argument, the location of the ignore file.
15 # Copyright (C) 2008, 2009 Peter Palfrader <peter@palfrader.org>
17 # Permission is hereby granted, free of charge, to any person obtaining
18 # a copy of this software and associated documentation files (the
19 # "Software"), to deal in the Software without restriction, including
20 # without limitation the rights to use, copy, modify, merge, publish,
21 # distribute, sublicense, and/or sell copies of the Software, and to
22 # permit persons to whom the Software is furnished to do so, subject to
23 # the following conditions:
25 # The above copyright notice and this permission notice shall be
26 # included in all copies or substantial portions of the Software.
28 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 my $IGNORE = "/etc/nagios/obsolete-packages-ignore";
51 die "code $newexit not defined\n" unless defined $CODE{$newexit};
53 if ($CODE{$newexit} > $CODE{$EXITCODE}) {
61 $ENV{'COLUMNS'} = 1000;
63 open(F, "dpkg -l|") or die ("Cannot run dpkg: $!\n");
68 shift @lines while ($lines[0] !~ /\+\+\+/);
72 for my $line (@lines) {
73 my ($state, $pkg, $version, undef) = split(/ */, $line);
74 $pkgs{$state}{$pkg} = { 'installed' => $version }
77 my $installed = $pkgs{'ii'};
80 open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
87 while (defined($line = shift @lines)) {
88 if ($line =~ /^([^ ]*):$/) {
90 } elsif ($line =~ /^ +Installed: (.*)$/) {
91 # etch dpkg -l does not print epochs, so use this info, it's better
92 $installed->{$pkgname}{'installed'} = $1;
93 } elsif ($line =~ /^ +Candidate: (.*)$/) {
94 $installed->{$pkgname}{'candidate'} = $1;
95 } elsif ($line =~ /^ +\*\*\*/) {
97 @l = split(/ +/, $line);
99 @l = split(/ +/, $line);
100 $installed->{$pkgname}{'origin'} = $l[2];
104 my (%current, %obsolete, %outofdate);
105 for my $pkgname (keys %$installed) {
106 my $pkg = $installed->{$pkgname};
108 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
109 $outofdate{$pkgname} = $pkg;
112 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
113 $obsolete{$pkgname} = $pkg;
116 $current{$pkgname} = $pkg;
119 $pkgs{'current'} = \%current;
120 $pkgs{'outofdate'} = \%outofdate;
121 $pkgs{'obsolete'} = \%obsolete;
126 my ($ignorefile, $require_file) = @_;
129 if (!$require_file and ! -e $ignorefile) {
133 open (F, "< $ignorefile") or die ("Cannot open $ignorefile: $!\n");
141 my ($pkg, $ignores) = @_;
144 for my $ig (@$ignores) {
145 return 1 if ($ig eq $pkg);
146 if (substr($ig,0,1) eq '/') {
147 substr($ig, 0, 1, '');
149 return 1 if ($pkg =~ /$ig/);
156 my ($packages, $ignores) = @_;
158 my $obs = $packages->{'obsolete'};
161 for my $pkg (keys %$obs) {
162 if (check_ignore($pkg, $ignores)) {
163 $ignored{$pkg} = $obs->{$pkg};
165 $bad{$pkg} = $obs->{$pkg};
168 delete $packages->{'obsolete'};
169 $packages->{'obsolete'} = \%bad;
170 $packages->{'obsolete-ignored'} = \%ignored;
174 my ($fd, $exit) = @_;
175 print $fd "Usage: $PROGRAM_NAME [<ignorefile>]\n";
179 my $ignorefile = $IGNORE;
180 my $ignorefile_userset = 0;
181 usage(\*STDERR, 1) if (@ARGV > 1);
183 usage(\*STDOUT, 0) if ($ARGV[0] eq "-h");
184 usage(\*STDOUT, 0) if ($ARGV[0] eq "--help");
185 $ignorefile = $ARGV[0];
186 $ignorefile_userset = 1;
189 my $ignores = load_ignores($ignorefile, $ignorefile_userset);
190 my $packages = get_packages();
192 filter_ignored($packages, $ignores);
197 { 'key' => 'obsolete',
199 'long' => "%d local or obsolete packages: %s",
201 'status' => 'WARNING' },
202 { 'key' => 'outofdate',
204 'long' => "%d out of date packages: %s",
205 'short' => "%d updates",
206 'status' => 'WARNING' },
207 { 'key' => 'obsolete-ignored',
209 'long' => "%d local or obsolete packages (ignored): %s",
210 'short' => "%d obs(ign)",
212 { 'key' => 'current',
214 'long' => "%d packages current.",
219 'long' => "%d packages removed but not purged: %s",
224 'long' => "%d packages on hold: %s",
229 'long' => "%d packages requested to be purged but conffiles still installed: %s",
231 'status' => 'WARNING' },
236 for my $form (@reportform) {
237 my $pkgs = $packages->{$form->{'key'}};
238 delete $packages->{$form->{'key'}};
239 my $num = scalar keys %$pkgs;
240 next unless ($num > 0);
241 if ($form->{'listpackages'}) {
242 my $list = join(", ", keys %$pkgs);
243 push @longout, sprintf($form->{'long'}, $num, $list);
245 push @longout, sprintf($form->{'long'}, $num);
247 push @shortout, sprintf($form->{'short'}, $num);
248 record($form->{'status'});
250 if (scalar keys %$packages) {
252 unshift @shortout, "unk: ".join(", ", keys %$packages);
253 for my $status (sort {$b cmp $a} keys %$packages) {
254 my $pkgs = $packages->{$status};
255 my $list = join(", ", keys %$pkgs);
256 unshift @longout, "Unknown package status $status: $list";
260 my $shortout = $EXITCODE.": ".join(", ", @shortout);
261 my $longout = join("\n", @longout);
263 print $shortout,"\n";
266 exit $CODE{$EXITCODE};