add new check
[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         my $line;
70         my $has_arch = 0;
71         while (defined($line = shift @lines) && ($line !~ /\+\+\+/)) {
72                 if ($line =~ /Architecture/) { $has_arch = 1; }
73         }
74
75         my %pkgs;
76         for $line (@lines) {
77                 my ($state, $pkg, $version, $arch, undef) = split(/  */, $line);
78                 $arch = '' unless $has_arch;
79                 $pkgs{$state}{$pkg} = { 'installed' => $version, arch => $arch }
80         }
81
82         my $installed = $pkgs{'ii'};
83         delete $pkgs{'ii'};
84
85         my @installed_packages = keys(%$installed);
86         my @cmd = ("apt-cache", "policy", @installed_packages);
87
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");
91         @lines = <F>;
92         close(F);
93         open STDERR, ">&", $olderr  or die "Can't dup OLDERR: $!";
94         chomp(@lines);
95
96         my $pkgname = undef;
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
101                         # arguments.
102                         #
103                         # We needs thi, because the output block in apt-cache
104                         # policy does not show the arch:
105                         #
106                         # | weasel@stanley:~$ apt-cache policy libedit2:amd64
107                         # | libedit2:
108                         # |   Installed: 2.11-20080614-5
109                         # |   Candidate: 2.11-20080614-5
110                         #
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.
114                         #
115                         # For squeeze systems (no m-a), apt-cache policy output
116                         # is all different.
117                         $pkgname = $1;
118                         if ($has_arch) {
119                                 my $from_list = shift @installed_packages;
120                                 next if ($pkgname eq $from_list); # no :$arch in pkgname we asked for
121
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;
126                         }
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];
136                 }
137         }
138
139         my (%current, %obsolete, %outofdate);
140         for my $pkgname (keys %$installed) {
141                 my $pkg = $installed->{$pkgname};
142
143                 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
144                         $obsolete{$pkgname} = $pkg;
145                         next;
146                 }
147
148                 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
149                         $outofdate{$pkgname} = $pkg;
150                         next;
151                 };
152                 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
153                         $obsolete{$pkgname} = $pkg;
154                         next;
155                 }
156                 $current{$pkgname} = $pkg;
157         }
158
159         $pkgs{'current'} = \%current;
160         $pkgs{'outofdate'} = \%outofdate;
161         $pkgs{'obsolete'} = \%obsolete;
162         return \%pkgs;
163 }
164
165 sub load_ignores {
166         my ($ignorefiles, $require_file) = @_;
167
168         my @ignores;
169
170         for my $ignoreitem (@$ignorefiles) {
171                 next if (!$require_file and ! -e $ignoreitem);
172
173                 my @filestoopen;
174                 if (-d $ignoreitem) {
175                         opendir(DIR, $ignoreitem) or die ("Cannot open dir $ignoreitem: $!\n");
176                         @filestoopen = readdir(DIR);
177                         closedir(DIR);
178
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;
183                 } else {
184                         push @filestoopen, $ignoreitem;
185                 }
186
187                 for my $f (@filestoopen) {
188                         open (F, "< $f") or die ("Cannot open $f: $!\n");
189                         push @ignores, <F>;
190                         close F;
191                 }
192         }
193         chomp(@ignores);
194         return \@ignores;
195 }
196
197 sub check_ignore {
198         my ($pkg, $ignores) = @_;
199
200         my $ignore_this = 0;
201         for my $ignore (@$ignores) {
202                 my $ig = $ignore;
203                 return 1 if ($ig eq $pkg);
204                 if (substr($ig,0,1) eq '/') {
205                         substr($ig, 0, 1, '');
206                         $ig =~ s,/$,,;
207                         return 1 if ($pkg =~ /$ig/);
208                 }
209         }
210         return 0
211 }
212
213 sub filter_ignored {
214         my ($packages, $ignores) = @_;
215
216         my $obs = $packages->{'obsolete'};
217
218         my (%ignored, %bad);
219         for my $pkg (keys %$obs) {
220                 if (check_ignore($pkg, $ignores)) {
221                         $ignored{$pkg} = $obs->{$pkg};
222                 } else {
223                         $bad{$pkg} = $obs->{$pkg};
224                 };
225         }
226         delete $packages->{'obsolete'};
227         $packages->{'obsolete'} = \%bad;
228         $packages->{'obsolete-ignored'} = \%ignored;
229 };
230
231 sub usage {
232         my ($fd, $exit) = @_;
233         print $fd "Usage: $PROGRAM_NAME [<ignorefile|dir> [<ignorefile|dir> ...]]\n";
234         exit $exit;
235 }
236
237 my $ignorefiles = [$IGNORE, $IGNORED];
238 my $ignorefile_userset = 0;
239 if (@ARGV >= 1) {
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;
244 };
245
246 my $ignores = load_ignores($ignorefiles, $ignorefile_userset);
247 my $packages = get_packages();
248
249 filter_ignored($packages, $ignores);
250
251
252
253 my @reportform = (
254         { 'key' => 'obsolete',
255           'listpackages' => 1,
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',
261           'listpackages' => 1,
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',
267           'listpackages' => 0,
268           'long' => "%d packages current.",
269           'short' => "%d ok",
270           'perf' => "current=%d;;;0",
271           'status' => 'OK' },
272         { 'key' => 'obsolete-ignored',
273           'listpackages' => 1,
274           'long' => "%d whitelisted local or obsolete packages: %s",
275           'short' => "%d obs/loc(ignored)",
276           'perf' => "obs_ign=%d;;;0",
277           'status' => 'OK' },
278         { 'key' => 'rc',
279           'listpackages' => 1,
280           'long' => "%d packages removed but not purged: %s",
281           'short' => "%d rc",
282           'perf' => "rm_unprg=%d;;;0",
283           'status' => 'OK' },
284         { 'key' => 'hi',
285           'listpackages' => 1,
286           'long' => "%d packages on hold: %s",
287           'short' => "%d hi",
288           'perf' => "hold=%d;;;0",
289           'status' => 'OK' },
290         { 'key' => 'pc',
291           'listpackages' => 1,
292           'long' => "%d packages requested to be purged but conffiles still installed: %s",
293           'short' => "%d pc",
294           'perf' => "prg_conf=%d;1;;0",
295           'status' => 'WARNING' },
296         );
297
298 my @longout;
299 my @perfout;
300 my @shortout;
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);
310         } else {
311                 push @longout, sprintf($form->{'long'}, $num);
312         };
313         push @shortout, sprintf($form->{'short'}, $num);
314         record($form->{'status'});
315 };
316 if (scalar keys %$packages) {
317         record('WARNING');
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";
323         };
324 }
325
326 my $shortout = $EXITCODE.": ".join(", ", @shortout);
327 my $longout = join("\n", @longout);
328 my $perfout = "|".join(" ", @perfout);
329
330 print $shortout,"\n";
331 print $longout,"\n";
332 print $perfout,"\n";
333
334 exit $CODE{$EXITCODE};