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 --print-architecture|") or die ("Cannot run dpkg --print-architecture: $!\n");
70 open(F, "dpkg -l|") or die ("Cannot run dpkg -l: $!\n");
75 shift @lines while ($lines[0] !~ /\+\+\+/);
79 for my $line (@lines) {
80 my ($state, $pkg, $version, undef) = split(/ */, $line);
81 $pkg =~ s/\Q:$arch\E$//;
82 $pkgs{$state}{$pkg} = { 'installed' => $version }
85 my $installed = $pkgs{'ii'};
88 open my $olderr, ">&STDERR" or die "Can't dup STDERR: $!";
89 open STDERR, ">/dev/null" or die "Can't dup STDOUT: $!";
91 open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
95 open STDERR, ">&", $olderr or die "Can't dup OLDERR: $!";
99 while (defined($line = shift @lines)) {
100 if ($line =~ /^([^ ]*):$/) {
102 } elsif ($line =~ /^ +Installed: (.*)$/) {
103 # etch dpkg -l does not print epochs, so use this info, it's better
104 $installed->{$pkgname}{'installed'} = $1;
105 } elsif ($line =~ /^ +Candidate: (.*)$/) {
106 $installed->{$pkgname}{'candidate'} = $1;
107 } elsif ($line =~ /^ +\*\*\*/) {
109 @l = split(/ +/, $line);
110 $line = shift @lines;
111 @l = split(/ +/, $line);
112 $installed->{$pkgname}{'origin'} = $l[2];
116 my (%current, %obsolete, %outofdate);
117 for my $pkgname (keys %$installed) {
118 my $pkg = $installed->{$pkgname};
120 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
121 $obsolete{$pkgname} = $pkg;
125 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
126 $outofdate{$pkgname} = $pkg;
129 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
130 $obsolete{$pkgname} = $pkg;
133 $current{$pkgname} = $pkg;
136 $pkgs{'current'} = \%current;
137 $pkgs{'outofdate'} = \%outofdate;
138 $pkgs{'obsolete'} = \%obsolete;
143 my ($ignorefiles, $require_file) = @_;
147 for my $ignoreitem (@$ignorefiles) {
148 next if (!$require_file and ! -e $ignoreitem);
151 if (-d $ignoreitem) {
152 opendir(DIR, $ignoreitem) or die ("Cannot open dir $ignoreitem: $!\n");
153 @filestoopen = readdir(DIR);
156 @filestoopen = grep { -f ($ignoreitem.'/'.$_) } @filestoopen;
157 @filestoopen = grep { /^([a-z0-9_.-]+)+[a-z0-9]+$/i } @filestoopen;
158 @filestoopen = grep { !/dpkg-(old|dist|new|tmp)$/ } @filestoopen;
159 @filestoopen = map { ($ignoreitem.'/'.$_) } @filestoopen;
161 push @filestoopen, $ignoreitem;
164 for my $f (@filestoopen) {
165 open (F, "< $f") or die ("Cannot open $f: $!\n");
175 my ($pkg, $ignores) = @_;
178 for my $ignore (@$ignores) {
180 return 1 if ($ig eq $pkg);
181 if (substr($ig,0,1) eq '/') {
182 substr($ig, 0, 1, '');
184 return 1 if ($pkg =~ /$ig/);
191 my ($packages, $ignores) = @_;
193 my $obs = $packages->{'obsolete'};
196 for my $pkg (keys %$obs) {
197 if (check_ignore($pkg, $ignores)) {
198 $ignored{$pkg} = $obs->{$pkg};
200 $bad{$pkg} = $obs->{$pkg};
203 delete $packages->{'obsolete'};
204 $packages->{'obsolete'} = \%bad;
205 $packages->{'obsolete-ignored'} = \%ignored;
209 my ($fd, $exit) = @_;
210 print $fd "Usage: $PROGRAM_NAME [<ignorefile|dir> [<ignorefile|dir> ...]]\n";
214 my $ignorefiles = [$IGNORE, $IGNORED];
215 my $ignorefile_userset = 0;
217 usage(\*STDOUT, 0) if ($ARGV[0] eq "-h");
218 usage(\*STDOUT, 0) if ($ARGV[0] eq "--help");
219 $ignorefile_userset = 1;
220 $ignorefiles = \@ARGV;
223 my $ignores = load_ignores($ignorefiles, $ignorefile_userset);
224 my $packages = get_packages();
226 filter_ignored($packages, $ignores);
231 { 'key' => 'obsolete',
233 'long' => "%d local or obsolete packages: %s",
234 'short' => "%d obs/loc",
235 'perf' => "obs_loc=%d;1;5;0",
236 'status' => 'WARNING' },
237 { 'key' => 'outofdate',
239 'long' => "%d out of date packages: %s",
240 'short' => "%d updates",
241 'perf' => "outdated=%d;1;5;0",
242 'status' => 'WARNING' },
243 { 'key' => 'current',
245 'long' => "%d packages current.",
247 'perf' => "current=%d;;;0",
249 { 'key' => 'obsolete-ignored',
251 'long' => "%d whitelisted local or obsolete packages: %s",
252 'short' => "%d obs/loc(ignored)",
253 'perf' => "obs_ign=%d;;;0",
257 'long' => "%d packages removed but not purged: %s",
259 'perf' => "rm_unprg=%d;;;0",
263 'long' => "%d packages on hold: %s",
265 'perf' => "hold=%d;;;0",
269 'long' => "%d packages requested to be purged but conffiles still installed: %s",
271 'perf' => "prg_conf=%d;1;;0",
272 'status' => 'WARNING' },
278 for my $form (@reportform) {
279 my $pkgs = $packages->{$form->{'key'}};
280 delete $packages->{$form->{'key'}};
281 my $num = scalar keys %$pkgs;
282 push @perfout, sprintf($form->{'perf'}, $num);
283 next unless ($num > 0);
284 if ($form->{'listpackages'}) {
285 my $list = join(", ", keys %$pkgs);
286 push @longout, sprintf($form->{'long'}, $num, $list);
288 push @longout, sprintf($form->{'long'}, $num);
290 push @shortout, sprintf($form->{'short'}, $num);
291 record($form->{'status'});
293 if (scalar keys %$packages) {
295 unshift @shortout, "unk: ".join(", ", keys %$packages);
296 for my $status (sort {$b cmp $a} keys %$packages) {
297 my $pkgs = $packages->{$status};
298 my $list = join(", ", keys %$pkgs);
299 unshift @longout, "Unknown package status $status: $list";
303 my $shortout = $EXITCODE.": ".join(", ", @shortout);
304 my $longout = join("\n", @longout);
305 my $perfout = "|".join(" ", @perfout);
311 exit $CODE{$EXITCODE};