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";
41 my $IGNORED = "/etc/nagios/obsolete-packages-ignore.d";
52 die "code $newexit not defined\n" unless defined $CODE{$newexit};
54 if ($CODE{$newexit} > $CODE{$EXITCODE}) {
62 $ENV{'COLUMNS'} = 1000;
64 open(F, "dpkg -l|") or die ("Cannot run dpkg: $!\n");
71 while (defined($line = shift @lines) && ($line !~ /\+\+\+/)) {
72 if ($line =~ /Architecture/) { $has_arch = 1; }
77 my ($state, $pkg, $version, $arch, undef) = split(/ */, $line);
78 $arch = '' unless $has_arch;
79 $pkgs{$state}{$pkg} = { 'installed' => $version, arch => $arch }
82 my $installed = $pkgs{'ii'};
85 my @installed_packages = keys(%$installed);
86 my @cmd = ("apt-cache", "policy", @installed_packages);
88 open my $olderr, ">&STDERR" or die "Can't dup STDERR: $!";
89 open STDERR, ">/dev/null" or die "Can't dup STDOUT: $!";
90 open (F, "-|", @cmd) or die ("Cannot run apt-cache policy: $!\n");
93 open STDERR, ">&", $olderr or die "Can't dup OLDERR: $!";
97 while (defined($line = shift @lines)) {
98 if ($line =~ /^([^ ]*):$/) {
99 # when we have multi-arch capable fu, we require that
100 # apt-cache policy output is in the same order as its
103 # We needs thi, because the output block in apt-cache
104 # policy does not show the arch:
106 # | weasel@stanley:~$ apt-cache policy libedit2:amd64
108 # | Installed: 2.11-20080614-5
109 # | Candidate: 2.11-20080614-5
111 # We replace the package name in the output with the
112 # one we asked for ($pkg:$arch) - but to match this up
113 # sanely we need the order to be correct.
115 # For squeeze systems (no m-a), apt-cache policy output
119 my $from_list = shift @installed_packages;
120 next if ($pkgname eq $from_list); # no :$arch in pkgname we asked for
122 my $ma_fix_pkgname = $pkgname.':'.$installed->{$from_list}->{'arch'};
123 ($ma_fix_pkgname eq $from_list) or die "Unexpected order mismatch in apt-cache policy output\n";
124 # print $pkgname, " - ", $from_list, "\n";
125 $pkgname = $from_list;
127 } elsif ($line =~ /^ +Installed: (.*)$/) {
128 # etch dpkg -l does not print epochs, so use this info, it's better
129 $installed->{$pkgname}{'installed'} = $1;
130 } elsif ($line =~ /^ +Candidate: (.*)$/) {
131 $installed->{$pkgname}{'candidate'} = $1;
132 } elsif ($line =~ /^ +\*\*\*/) {
133 $line = shift @lines;
134 my @l = split(/ +/, $line);
135 $installed->{$pkgname}{'origin'} = $l[2];
139 my (%current, %obsolete, %outofdate);
140 for my $pkgname (keys %$installed) {
141 my $pkg = $installed->{$pkgname};
143 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
144 $obsolete{$pkgname} = $pkg;
148 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
149 $outofdate{$pkgname} = $pkg;
152 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
153 $obsolete{$pkgname} = $pkg;
156 $current{$pkgname} = $pkg;
159 $pkgs{'current'} = \%current;
160 $pkgs{'outofdate'} = \%outofdate;
161 $pkgs{'obsolete'} = \%obsolete;
166 my ($ignorefiles, $require_file) = @_;
170 for my $ignoreitem (@$ignorefiles) {
171 next if (!$require_file and ! -e $ignoreitem);
174 if (-d $ignoreitem) {
175 opendir(DIR, $ignoreitem) or die ("Cannot open dir $ignoreitem: $!\n");
176 @filestoopen = readdir(DIR);
179 @filestoopen = grep { -f ($ignoreitem.'/'.$_) } @filestoopen;
180 @filestoopen = grep { /^([a-z0-9_.-]+)+[a-z0-9]+$/i } @filestoopen;
181 @filestoopen = grep { !/dpkg-(old|dist|new|tmp)$/ } @filestoopen;
182 @filestoopen = map { ($ignoreitem.'/'.$_) } @filestoopen;
184 push @filestoopen, $ignoreitem;
187 for my $f (@filestoopen) {
188 open (F, "< $f") or die ("Cannot open $f: $!\n");
198 my ($pkg, $ignores) = @_;
201 for my $ignore (@$ignores) {
203 return 1 if ($ig eq $pkg);
204 if (substr($ig,0,1) eq '/') {
205 substr($ig, 0, 1, '');
207 return 1 if ($pkg =~ /$ig/);
214 my ($packages, $ignores) = @_;
216 my $obs = $packages->{'obsolete'};
219 for my $pkg (keys %$obs) {
220 if (check_ignore($pkg, $ignores)) {
221 $ignored{$pkg} = $obs->{$pkg};
223 $bad{$pkg} = $obs->{$pkg};
226 delete $packages->{'obsolete'};
227 $packages->{'obsolete'} = \%bad;
228 $packages->{'obsolete-ignored'} = \%ignored;
232 my ($fd, $exit) = @_;
233 print $fd "Usage: $PROGRAM_NAME [<ignorefile|dir> [<ignorefile|dir> ...]]\n";
237 my $ignorefiles = [$IGNORE, $IGNORED];
238 my $ignorefile_userset = 0;
240 usage(\*STDOUT, 0) if ($ARGV[0] eq "-h");
241 usage(\*STDOUT, 0) if ($ARGV[0] eq "--help");
242 $ignorefile_userset = 1;
243 $ignorefiles = \@ARGV;
246 my $ignores = load_ignores($ignorefiles, $ignorefile_userset);
247 my $packages = get_packages();
249 filter_ignored($packages, $ignores);
254 { 'key' => 'obsolete',
256 'long' => "%d local or obsolete packages: %s",
257 'short' => "%d obs/loc",
258 'perf' => "obs_loc=%d;1;5;0",
259 'status' => 'WARNING' },
260 { 'key' => 'outofdate',
262 'long' => "%d out of date packages: %s",
263 'short' => "%d updates",
264 'perf' => "outdated=%d;1;5;0",
265 'status' => 'WARNING' },
266 { 'key' => 'current',
268 'long' => "%d packages current.",
270 'perf' => "current=%d;;;0",
272 { 'key' => 'obsolete-ignored',
274 'long' => "%d whitelisted local or obsolete packages: %s",
275 'short' => "%d obs/loc(ignored)",
276 'perf' => "obs_ign=%d;;;0",
280 'long' => "%d packages removed but not purged: %s",
282 'perf' => "rm_unprg=%d;;;0",
286 'long' => "%d packages on hold: %s",
288 'perf' => "hold=%d;;;0",
292 'long' => "%d packages requested to be purged but conffiles still installed: %s",
294 'perf' => "prg_conf=%d;1;;0",
295 'status' => 'WARNING' },
301 for my $form (@reportform) {
302 my $pkgs = $packages->{$form->{'key'}};
303 delete $packages->{$form->{'key'}};
304 my $num = scalar keys %$pkgs;
305 push @perfout, sprintf($form->{'perf'}, $num);
306 next unless ($num > 0);
307 if ($form->{'listpackages'}) {
308 my $list = join(", ", keys %$pkgs);
309 push @longout, sprintf($form->{'long'}, $num, $list);
311 push @longout, sprintf($form->{'long'}, $num);
313 push @shortout, sprintf($form->{'short'}, $num);
314 record($form->{'status'});
316 if (scalar keys %$packages) {
318 unshift @shortout, "unk: ".join(", ", keys %$packages);
319 for my $status (sort {$b cmp $a} keys %$packages) {
320 my $pkgs = $packages->{$status};
321 my $list = join(", ", keys %$pkgs);
322 unshift @longout, "Unknown package status $status: $list";
326 my $shortout = $EXITCODE.": ".join(", ", @shortout);
327 my $longout = join("\n", @longout);
328 my $perfout = "|".join(" ", @perfout);
330 print $shortout,"\n";
334 exit $CODE{$EXITCODE};