In the short overview say "obs/loc" instead of just "obs" for local or obsolete packages
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-packages
1 #!/usr/bin/perl
2
3 # dsa-check-packages
4
5 # checks for obsolete/local and upgradeable packages.
6 #
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.
11 #
12 # Takes one optional argument, the location of the ignore file.
13
14
15 # Copyright (C) 2008, 2009 Peter Palfrader <peter@palfrader.org>
16 #
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:
24 #
25 # The above copyright notice and this permission notice shall be
26 # included in all copies or substantial portions of the Software.
27 #
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.
35
36 use strict;
37 use warnings;
38 use English;
39
40 my $IGNORE = "/etc/nagios/obsolete-packages-ignore";
41 my $IGNORED = "/etc/nagios/obsolete-packages-ignore.d";
42
43 my %CODE = (
44         'OK'            => 0,
45         'WARNING'       => 1,
46         'CRITICAL'      => 2,
47         'UNKNOWN'       => 3
48 );
49 my $EXITCODE = 'OK';
50 sub record($) {
51         my ($newexit) = @_;
52         die "code $newexit not defined\n" unless defined $CODE{$newexit};
53
54         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
55                 $EXITCODE = $newexit;
56         };
57 }
58
59
60
61 sub get_packages {
62         $ENV{'COLUMNS'} = 1000;
63         $ENV{'LC_ALL'} = 'C';
64         open(F, "dpkg -l|") or die ("Cannot run dpkg: $!\n");
65         my @lines = <F>;
66         close(F);
67         chomp(@lines);
68
69         shift @lines while ($lines[0] !~ /\+\+\+/);
70         shift @lines;
71
72         my %pkgs;
73         for my $line (@lines) {
74                 my ($state, $pkg, $version, undef) = split(/  */, $line);
75                 $pkgs{$state}{$pkg} = { 'installed' => $version }
76         }
77
78         my $installed = $pkgs{'ii'};
79         delete $pkgs{'ii'};
80
81         open my $olderr, ">&STDERR"   or die "Can't dup STDERR: $!";
82         open     STDERR, ">/dev/null" or die "Can't dup STDOUT: $!";
83
84         open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
85         @lines = <F>;
86         close(F);
87         chomp(@lines);
88         open STDERR, ">&", $olderr  or die "Can't dup OLDERR: $!";
89
90         my $line;
91         my $pkgname = undef;
92         while (defined($line = shift @lines)) {
93                 if ($line =~ /^([^ ]*):$/) {
94                         $pkgname = $1;
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 =~ /^ +\*\*\*/) {
101                         my @l;
102                         @l = split(/ +/, $line);
103                         $line = shift @lines;
104                         @l = split(/ +/, $line);
105                         $installed->{$pkgname}{'origin'} = $l[2];
106                 }
107         }
108
109         my (%current, %obsolete, %outofdate);
110         for my $pkgname (keys %$installed) {
111                 my $pkg = $installed->{$pkgname};
112
113                 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
114                         $obsolete{$pkgname} = $pkg;
115                         next;
116                }
117                         
118                 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
119                         $outofdate{$pkgname} = $pkg;
120                         next;
121                 };
122                 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
123                         $obsolete{$pkgname} = $pkg;
124                         next;
125                 }
126                 $current{$pkgname} = $pkg;
127         }
128
129         $pkgs{'current'} = \%current;
130         $pkgs{'outofdate'} = \%outofdate;
131         $pkgs{'obsolete'} = \%obsolete;
132         return \%pkgs;
133 }
134
135 sub load_ignores {
136         my ($ignorefiles, $require_file) = @_;
137
138         my @ignores;
139
140         for my $ignoreitem (@$ignorefiles) {
141                 next if (!$require_file and ! -e $ignoreitem);
142
143                 my @filestoopen;
144                 if (-d $ignoreitem) {
145                         opendir(DIR, $ignoreitem) or die ("Cannot open dir $ignoreitem: $!\n");
146                         @filestoopen = readdir(DIR);
147                         closedir(DIR);
148
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;
153                 } else {
154                         push @filestoopen, $ignoreitem;
155                 }
156
157                 for my $f (@filestoopen) {
158                         open (F, "< $f") or die ("Cannot open $f: $!\n");
159                         push @ignores, <F>;
160                         close F;
161                 }
162         }
163         chomp(@ignores);
164         return \@ignores;
165 }
166
167 sub check_ignore {
168         my ($pkg, $ignores) = @_;
169
170         my $ignore_this = 0;
171         for my $ignore (@$ignores) {
172                 my $ig = $ignore;
173                 return 1 if ($ig eq $pkg);
174                 if (substr($ig,0,1) eq '/') {
175                         substr($ig, 0, 1, '');
176                         $ig =~ s,/$,,;
177                         return 1 if ($pkg =~ /$ig/);
178                 }
179         }
180         return 0
181 }
182
183 sub filter_ignored {
184         my ($packages, $ignores) = @_;
185
186         my $obs = $packages->{'obsolete'};
187
188         my (%ignored, %bad);
189         for my $pkg (keys %$obs) {
190                 if (check_ignore($pkg, $ignores)) {
191                         $ignored{$pkg} = $obs->{$pkg};
192                 } else {
193                         $bad{$pkg} = $obs->{$pkg};
194                 };
195         }
196         delete $packages->{'obsolete'};
197         $packages->{'obsolete'} = \%bad;
198         $packages->{'obsolete-ignored'} = \%ignored;
199 };
200
201 sub usage {
202         my ($fd, $exit) = @_;
203         print $fd "Usage: $PROGRAM_NAME [<ignorefile|dir> [<ignorefile|dir> ...]]\n";
204         exit $exit;
205 }
206
207 my $ignorefiles = [$IGNORE, $IGNORED];
208 my $ignorefile_userset = 0;
209 if (@ARGV >= 1) {
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;
214 };
215
216 my $ignores = load_ignores($ignorefiles, $ignorefile_userset);
217 my $packages = get_packages();
218
219 filter_ignored($packages, $ignores);
220
221
222
223 my @reportform = (
224         { 'key' => 'obsolete',
225           'listpackages' => 1,
226           'long' => "%d local or obsolete packages: %s",
227           'short' => "%d obs/loc",
228           'status' => 'WARNING' },
229         { 'key' => 'outofdate',
230           'listpackages' => 1,
231           'long' => "%d out of date packages: %s",
232           'short' => "%d updates",
233           'status' => 'WARNING' },
234         { 'key' => 'current',
235           'listpackages' => 0,
236           'long' => "%d packages current.",
237           'short' => "%d ok",
238           'status' => 'OK' },
239         { 'key' => 'obsolete-ignored',
240           'listpackages' => 1,
241           'long' => "%d whitelisted local or obsolete packages: %s",
242           'short' => "%d obs/loc(ignored)",
243           'status' => 'OK' },
244         { 'key' => 'rc',
245           'listpackages' => 1,
246           'long' => "%d packages removed but not purged: %s",
247           'short' => "%d rc",
248           'status' => 'OK' },
249         { 'key' => 'hi',
250           'listpackages' => 1,
251           'long' => "%d packages on hold: %s",
252           'short' => "%d hi",
253           'status' => 'OK' },
254         { 'key' => 'pc',
255           'listpackages' => 1,
256           'long' => "%d packages requested to be purged but conffiles still installed: %s",
257           'short' => "%d pc",
258           'status' => 'WARNING' },
259         );
260
261 my @longout;
262 my @shortout;
263 for my $form (@reportform) {
264         my $pkgs = $packages->{$form->{'key'}};
265         delete $packages->{$form->{'key'}};
266         my $num = scalar keys %$pkgs;
267         next unless ($num > 0);
268         if ($form->{'listpackages'}) {
269                 my $list = join(", ", keys %$pkgs);
270                 push @longout, sprintf($form->{'long'}, $num, $list);
271         } else {
272                 push @longout, sprintf($form->{'long'}, $num);
273         };
274         push @shortout, sprintf($form->{'short'}, $num);
275         record($form->{'status'});
276 };
277 if (scalar keys %$packages) {
278         record('WARNING');
279         unshift @shortout, "unk: ".join(", ", keys %$packages);
280         for my $status (sort {$b cmp $a} keys %$packages) {
281                 my $pkgs = $packages->{$status};
282                 my $list = join(", ", keys %$pkgs);
283                 unshift @longout, "Unknown package status $status: $list";
284         };
285 }
286
287 my $shortout = $EXITCODE.": ".join(", ", @shortout);
288 my $longout = join("\n", @longout);
289
290 print $shortout,"\n";
291 print $longout,"\n";
292
293 exit $CODE{$EXITCODE};