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 my $olderr, ">&STDERR" or die "Can't dup STDERR: $!";
81 open STDERR, ">/dev/null" or die "Can't dup STDOUT: $!";
83 open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
87 open STDERR, ">&", $olderr or die "Can't dup OLDERR: $!";
91 while (defined($line = shift @lines)) {
92 if ($line =~ /^([^ ]*):$/) {
94 } elsif ($line =~ /^ +Installed: (.*)$/) {
95 # etch dpkg -l does not print epochs, so use this info, it's better
96 $installed->{$pkgname}{'installed'} = $1;
97 } elsif ($line =~ /^ +Candidate: (.*)$/) {
98 $installed->{$pkgname}{'candidate'} = $1;
99 } elsif ($line =~ /^ +\*\*\*/) {
101 @l = split(/ +/, $line);
102 $line = shift @lines;
103 @l = split(/ +/, $line);
104 $installed->{$pkgname}{'origin'} = $l[2];
108 my (%current, %obsolete, %outofdate);
109 for my $pkgname (keys %$installed) {
110 my $pkg = $installed->{$pkgname};
112 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
113 $obsolete{$pkgname} = $pkg;
117 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
118 $outofdate{$pkgname} = $pkg;
121 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
122 $obsolete{$pkgname} = $pkg;
125 $current{$pkgname} = $pkg;
128 $pkgs{'current'} = \%current;
129 $pkgs{'outofdate'} = \%outofdate;
130 $pkgs{'obsolete'} = \%obsolete;
135 my ($ignorefile, $require_file) = @_;
138 if (!$require_file and ! -e $ignorefile) {
142 open (F, "< $ignorefile") or die ("Cannot open $ignorefile: $!\n");
150 my ($pkg, $ignores) = @_;
153 for my $ignore (@$ignores) {
155 return 1 if ($ig eq $pkg);
156 if (substr($ig,0,1) eq '/') {
157 substr($ig, 0, 1, '');
159 return 1 if ($pkg =~ /$ig/);
166 my ($packages, $ignores) = @_;
168 my $obs = $packages->{'obsolete'};
171 for my $pkg (keys %$obs) {
172 if (check_ignore($pkg, $ignores)) {
173 $ignored{$pkg} = $obs->{$pkg};
175 $bad{$pkg} = $obs->{$pkg};
178 delete $packages->{'obsolete'};
179 $packages->{'obsolete'} = \%bad;
180 $packages->{'obsolete-ignored'} = \%ignored;
184 my ($fd, $exit) = @_;
185 print $fd "Usage: $PROGRAM_NAME [<ignorefile>]\n";
189 my $ignorefile = $IGNORE;
190 my $ignorefile_userset = 0;
191 usage(\*STDERR, 1) if (@ARGV > 1);
193 usage(\*STDOUT, 0) if ($ARGV[0] eq "-h");
194 usage(\*STDOUT, 0) if ($ARGV[0] eq "--help");
195 $ignorefile = $ARGV[0];
196 $ignorefile_userset = 1;
199 my $ignores = load_ignores($ignorefile, $ignorefile_userset);
200 my $packages = get_packages();
202 filter_ignored($packages, $ignores);
207 { 'key' => 'obsolete',
209 'long' => "%d local or obsolete packages: %s",
211 'status' => 'WARNING' },
212 { 'key' => 'outofdate',
214 'long' => "%d out of date packages: %s",
215 'short' => "%d updates",
216 'status' => 'WARNING' },
217 { 'key' => 'current',
219 'long' => "%d packages current.",
222 { 'key' => 'obsolete-ignored',
224 'long' => "%d whitelisted local or obsolete packages: %s",
225 'short' => "%d obs(ignored)",
229 'long' => "%d packages removed but not purged: %s",
234 'long' => "%d packages on hold: %s",
239 'long' => "%d packages requested to be purged but conffiles still installed: %s",
241 'status' => 'WARNING' },
246 for my $form (@reportform) {
247 my $pkgs = $packages->{$form->{'key'}};
248 delete $packages->{$form->{'key'}};
249 my $num = scalar keys %$pkgs;
250 next unless ($num > 0);
251 if ($form->{'listpackages'}) {
252 my $list = join(", ", keys %$pkgs);
253 push @longout, sprintf($form->{'long'}, $num, $list);
255 push @longout, sprintf($form->{'long'}, $num);
257 push @shortout, sprintf($form->{'short'}, $num);
258 record($form->{'status'});
260 if (scalar keys %$packages) {
262 unshift @shortout, "unk: ".join(", ", keys %$packages);
263 for my $status (sort {$b cmp $a} keys %$packages) {
264 my $pkgs = $packages->{$status};
265 my $list = join(", ", keys %$pkgs);
266 unshift @longout, "Unknown package status $status: $list";
270 my $shortout = $EXITCODE.": ".join(", ", @shortout);
271 my $longout = join("\n", @longout);
273 print $shortout,"\n";
276 exit $CODE{$EXITCODE};