[project @ peter@palfrader.org-20090203193249-w30idzor9gljzbif]
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / 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
42 my %CODE = (
43         'OK'            => 0,
44         'WARNING'       => 1,
45         'CRITICAL'      => 2,
46         'UNKNOWN'       => 3
47 );
48 my $EXITCODE = 'OK';
49 sub record($) {
50         my ($newexit) = @_;
51         die "code $newexit not defined\n" unless defined $CODE{$newexit};
52
53         if ($CODE{$newexit} > $CODE{$EXITCODE}) {
54                 $EXITCODE = $newexit;
55         };
56 }
57
58
59
60 sub get_packages {
61         $ENV{'COLUMNS'} = 1000;
62         $ENV{'LC_ALL'} = 'C';
63         open(F, "dpkg -l|") or die ("Cannot run dpkg: $!\n");
64         my @lines = <F>;
65         close(F);
66         chomp(@lines);
67
68         shift @lines while ($lines[0] !~ /\+\+\+/);
69         shift @lines;
70
71         my %pkgs;
72         for my $line (@lines) {
73                 my ($state, $pkg, $version, undef) = split(/  */, $line);
74                 $pkgs{$state}{$pkg} = { 'installed' => $version }
75         }
76
77         my $installed = $pkgs{'ii'};
78         delete $pkgs{'ii'};
79
80         open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
81         @lines = <F>;
82         close(F);
83         chomp(@lines);
84
85         my $line;
86         my $pkgname = undef;
87         while (defined($line = shift @lines)) {
88                 if ($line =~ /^([^ ]*):$/) {
89                         $pkgname = $1;
90                 } elsif ($line =~ /^ +Installed: (.*)$/) {
91                         # etch dpkg -l does not print epochs, so use this info, it's better
92                         $installed->{$pkgname}{'installed'} = $1;
93                 } elsif ($line =~ /^ +Candidate: (.*)$/) {
94                         $installed->{$pkgname}{'candidate'} = $1;
95                 } elsif ($line =~ /^ +\*\*\*/) {
96                         my @l;
97                         @l = split(/ +/, $line);
98                         $line = shift @lines;
99                         @l = split(/ +/, $line);
100                         $installed->{$pkgname}{'origin'} = $l[2];
101                 }
102         }
103
104         my (%current, %obsolete, %outofdate);
105         for my $pkgname (keys %$installed) {
106                 my $pkg = $installed->{$pkgname};
107
108                 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
109                         $outofdate{$pkgname} = $pkg;
110                         next;
111                 };
112                 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
113                         $obsolete{$pkgname} = $pkg;
114                         next;
115                 }
116                 $current{$pkgname} = $pkg;
117         }
118
119         $pkgs{'current'} = \%current;
120         $pkgs{'outofdate'} = \%outofdate;
121         $pkgs{'obsolete'} = \%obsolete;
122         return \%pkgs;
123 }
124
125 sub load_ignores {
126         my ($ignorefile, $require_file) = @_;
127
128         my @ignores;
129         if (!$require_file and ! -e $ignorefile) {
130                 return \@ignores;
131         }
132
133         open (F, "< $ignorefile") or die ("Cannot open $ignorefile: $!\n");
134         @ignores = <F>;
135         close F;
136         chomp(@ignores);
137         return \@ignores;
138 }
139
140 sub check_ignore {
141         my ($pkg, $ignores) = @_;
142
143         my $ignore_this = 0;
144         for my $ig (@$ignores) {
145                 return 1 if ($ig eq $pkg);
146                 if (substr($ig,0,1) eq '/') {
147                         substr($ig, 0, 1, '');
148                         $ig =~ s,/$,,;
149                         return 1 if ($pkg =~ /$ig/);
150                 }
151         }
152         return 0
153 }
154
155 sub filter_ignored {
156         my ($packages, $ignores) = @_;
157
158         my $obs = $packages->{'obsolete'};
159
160         my (%ignored, %bad);
161         for my $pkg (keys %$obs) {
162                 if (check_ignore($pkg, $ignores)) {
163                         $ignored{$pkg} = $obs->{$pkg};
164                 } else {
165                         $bad{$pkg} = $obs->{$pkg};
166                 };
167         }
168         delete $packages->{'obsolete'};
169         $packages->{'obsolete'} = \%bad;
170         $packages->{'obsolete-ignored'} = \%ignored;
171 };
172
173 sub usage {
174         my ($fd, $exit) = @_;
175         print $fd "Usage: $PROGRAM_NAME [<ignorefile>]\n";
176         exit $exit;
177 }
178
179 my $ignorefile = $IGNORE;
180 my $ignorefile_userset = 0;
181 usage(\*STDERR, 1) if (@ARGV > 1);
182 if (@ARGV == 1) {
183         usage(\*STDOUT, 0) if ($ARGV[0] eq "-h");
184         usage(\*STDOUT, 0) if ($ARGV[0] eq "--help");
185         $ignorefile = $ARGV[0];
186         $ignorefile_userset = 1;
187 };
188
189 my $ignores = load_ignores($ignorefile, $ignorefile_userset);
190 my $packages = get_packages();
191
192 filter_ignored($packages, $ignores);
193
194
195
196 my @reportform = (
197         { 'key' => 'obsolete',
198           'listpackages' => 1,
199           'long' => "%d local or obsolete packages: %s",
200           'short' => "%d obs",
201           'status' => 'WARNING' },
202         { 'key' => 'outofdate',
203           'listpackages' => 1,
204           'long' => "%d out of date packages: %s",
205           'short' => "%d updates",
206           'status' => 'WARNING' },
207         { 'key' => 'obsolete-ignored',
208           'listpackages' => 1,
209           'long' => "%d local or obsolete packages (ignored): %s",
210           'short' => "%d obs(ign)",
211           'status' => 'OK' },
212         { 'key' => 'current',
213           'listpackages' => 0,
214           'long' => "%d packages current.",
215           'short' => "%d ok",
216           'status' => 'OK' },
217         { 'key' => 'rc',
218           'listpackages' => 1,
219           'long' => "%d packages removed but not purged: %s",
220           'short' => "%d rc",
221           'status' => 'OK' },
222         { 'key' => 'hi',
223           'listpackages' => 1,
224           'long' => "%d packages on hold: %s",
225           'short' => "%d hi",
226           'status' => 'OK' },
227         { 'key' => 'pc',
228           'listpackages' => 1,
229           'long' => "%d packages requested to be purged but conffiles still installed: %s",
230           'short' => "%d pc",
231           'status' => 'WARNING' },
232         );
233
234 my @longout;
235 my @shortout;
236 for my $form (@reportform) {
237         my $pkgs = $packages->{$form->{'key'}};
238         delete $packages->{$form->{'key'}};
239         my $num = scalar keys %$pkgs;
240         next unless ($num > 0);
241         if ($form->{'listpackages'}) {
242                 my $list = join(", ", keys %$pkgs);
243                 push @longout, sprintf($form->{'long'}, $num, $list);
244         } else {
245                 push @longout, sprintf($form->{'long'}, $num);
246         };
247         push @shortout, sprintf($form->{'short'}, $num);
248         record($form->{'status'});
249 };
250 if (scalar keys %$packages) {
251         record('WARNING');
252         unshift @shortout, "unk: ".join(", ", keys %$packages);
253         for my $status (sort {$b cmp $a} keys %$packages) {
254                 my $pkgs = $packages->{$status};
255                 my $list = join(", ", keys %$pkgs);
256                 unshift @longout, "Unknown package status $status: $list";
257         };
258 }
259
260 my $shortout = $EXITCODE.": ".join(", ", @shortout);
261 my $longout = join("\n", @longout);
262
263 print $shortout,"\n";
264 print $longout,"\n";
265
266 exit $CODE{$EXITCODE};