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");
69 shift @lines while ($lines[0] !~ /\+\+\+/);
73 for my $line (@lines) {
74 my ($state, $pkg, $version, undef) = split(/ */, $line);
75 $pkgs{$state}{$pkg} = { 'installed' => $version }
78 my $installed = $pkgs{'ii'};
81 open my $olderr, ">&STDERR" or die "Can't dup STDERR: $!";
82 open STDERR, ">/dev/null" or die "Can't dup STDOUT: $!";
84 open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
88 open STDERR, ">&", $olderr or die "Can't dup OLDERR: $!";
92 while (defined($line = shift @lines)) {
93 if ($line =~ /^([^ ]*):$/) {
95 } elsif ($line =~ /^ +Installed: (.*)$/) {
96 # etch dpkg -l does not print epochs, so use this info, it's better
97 $installed->{$pkgname}{'installed'} = $1;
98 } elsif ($line =~ /^ +Candidate: (.*)$/) {
99 $installed->{$pkgname}{'candidate'} = $1;
100 } elsif ($line =~ /^ +\*\*\*/) {
102 @l = split(/ +/, $line);
103 $line = shift @lines;
104 @l = split(/ +/, $line);
105 $installed->{$pkgname}{'origin'} = $l[2];
109 my (%current, %obsolete, %outofdate);
110 for my $pkgname (keys %$installed) {
111 my $pkg = $installed->{$pkgname};
113 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
114 $obsolete{$pkgname} = $pkg;
118 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
119 $outofdate{$pkgname} = $pkg;
122 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
123 $obsolete{$pkgname} = $pkg;
126 $current{$pkgname} = $pkg;
129 $pkgs{'current'} = \%current;
130 $pkgs{'outofdate'} = \%outofdate;
131 $pkgs{'obsolete'} = \%obsolete;
136 my ($ignorefiles, $require_file) = @_;
140 for my $ignoreitem (@$ignorefiles) {
141 next if (!$require_file and ! -e $ignoreitem);
144 if (-d $ignoreitem) {
145 opendir(DIR, $ignoreitem) or die ("Cannot open dir $ignoreitem: $!\n");
146 @filestoopen = readdir(DIR);
149 @filestoopen = grep { -f ($ignoreitem.'/'.$_) } @filestoopen;
150 @filestoopen = grep { /^([a-z0-9_.-]+)+[a-z0-9]+$/i } @filestoopen;
151 @filestoopen = grep { !/dpkg-(old|dist|new|tmp)$/ } @filestoopen;
152 @filestoopen = map { ($ignoreitem.'/'.$_) } @filestoopen;
154 push @filestoopen, $ignoreitem;
157 for my $f (@filestoopen) {
158 open (F, "< $f") or die ("Cannot open $f: $!\n");
168 my ($pkg, $ignores) = @_;
171 for my $ignore (@$ignores) {
173 return 1 if ($ig eq $pkg);
174 if (substr($ig,0,1) eq '/') {
175 substr($ig, 0, 1, '');
177 return 1 if ($pkg =~ /$ig/);
184 my ($packages, $ignores) = @_;
186 my $obs = $packages->{'obsolete'};
189 for my $pkg (keys %$obs) {
190 if (check_ignore($pkg, $ignores)) {
191 $ignored{$pkg} = $obs->{$pkg};
193 $bad{$pkg} = $obs->{$pkg};
196 delete $packages->{'obsolete'};
197 $packages->{'obsolete'} = \%bad;
198 $packages->{'obsolete-ignored'} = \%ignored;
202 my ($fd, $exit) = @_;
203 print $fd "Usage: $PROGRAM_NAME [<ignorefile|dir> [<ignorefile|dir> ...]]\n";
207 my $ignorefiles = [$IGNORE, $IGNORED];
208 my $ignorefile_userset = 0;
210 usage(\*STDOUT, 0) if ($ARGV[0] eq "-h");
211 usage(\*STDOUT, 0) if ($ARGV[0] eq "--help");
212 $ignorefile_userset = 1;
213 $ignorefiles = \@ARGV;
216 my $ignores = load_ignores($ignorefiles, $ignorefile_userset);
217 my $packages = get_packages();
219 filter_ignored($packages, $ignores);
224 { 'key' => 'obsolete',
226 'long' => "%d local or obsolete packages: %s",
227 'short' => "%d obs/loc",
228 'perf' => "obs_loc=%d;1;5;0",
229 'status' => 'WARNING' },
230 { 'key' => 'outofdate',
232 'long' => "%d out of date packages: %s",
233 'short' => "%d updates",
234 'perf' => "outdated=%d;1;5;0",
235 'status' => 'WARNING' },
236 { 'key' => 'current',
238 'long' => "%d packages current.",
240 'perf' => "current=%d;;;0",
242 { 'key' => 'obsolete-ignored',
244 'long' => "%d whitelisted local or obsolete packages: %s",
245 'short' => "%d obs/loc(ignored)",
246 'perf' => "obs_ign=%d;;;0",
250 'long' => "%d packages removed but not purged: %s",
252 'perf' => "rm_unprg=%d;;;0",
256 'long' => "%d packages on hold: %s",
258 'perf' => "hold=%d;;;0",
262 'long' => "%d packages requested to be purged but conffiles still installed: %s",
264 'perf' => "prg_conf=%d;1;;0",
265 'status' => 'WARNING' },
271 for my $form (@reportform) {
272 my $pkgs = $packages->{$form->{'key'}};
273 delete $packages->{$form->{'key'}};
274 my $num = scalar keys %$pkgs;
275 push @perfout, sprintf($form->{'perf'}, $num);
276 next unless ($num > 0);
277 if ($form->{'listpackages'}) {
278 my $list = join(", ", keys %$pkgs);
279 push @longout, sprintf($form->{'long'}, $num, $list);
281 push @longout, sprintf($form->{'long'}, $num);
283 push @shortout, sprintf($form->{'short'}, $num);
284 record($form->{'status'});
286 if (scalar keys %$packages) {
288 unshift @shortout, "unk: ".join(", ", keys %$packages);
289 for my $status (sort {$b cmp $a} keys %$packages) {
290 my $pkgs = $packages->{$status};
291 my $list = join(", ", keys %$pkgs);
292 unshift @longout, "Unknown package status $status: $list";
296 my $shortout = $EXITCODE.": ".join(", ", @shortout);
297 my $longout = join("\n", @longout);
298 my $perfout = "|".join(" ", @perfout);
304 exit $CODE{$EXITCODE};