No longer install nrpe_dsa.cfg
[mirror/dsa-nagios.git] / dsa-nagios-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
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 my $olderr, ">&STDERR"   or die "Can't dup STDERR: $!";
81         open     STDERR, ">/dev/null" or die "Can't dup STDOUT: $!";
82
83         open (F, "apt-cache policy ".(join(" ", keys(%$installed)))." |") or die ("Cannot run apt-cache policy: $!\n");
84         @lines = <F>;
85         close(F);
86         chomp(@lines);
87         open STDERR, ">&", $olderr  or die "Can't dup OLDERR: $!";
88
89         my $line;
90         my $pkgname = undef;
91         while (defined($line = shift @lines)) {
92                 if ($line =~ /^([^ ]*):$/) {
93                         $pkgname = $1;
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 =~ /^ +\*\*\*/) {
100                         my @l;
101                         @l = split(/ +/, $line);
102                         $line = shift @lines;
103                         @l = split(/ +/, $line);
104                         $installed->{$pkgname}{'origin'} = $l[2];
105                 }
106         }
107
108         my (%current, %obsolete, %outofdate);
109         for my $pkgname (keys %$installed) {
110                 my $pkg = $installed->{$pkgname};
111
112                 unless (defined($pkg->{'candidate'}) && defined($pkg->{'origin'})) {
113                         $obsolete{$pkgname} = $pkg;
114                         next;
115                }
116                         
117                 if ($pkg->{'candidate'} ne $pkg->{'installed'}) {
118                         $outofdate{$pkgname} = $pkg;
119                         next;
120                 };
121                 if ($pkg->{'origin'} eq '/var/lib/dpkg/status') {
122                         $obsolete{$pkgname} = $pkg;
123                         next;
124                 }
125                 $current{$pkgname} = $pkg;
126         }
127
128         $pkgs{'current'} = \%current;
129         $pkgs{'outofdate'} = \%outofdate;
130         $pkgs{'obsolete'} = \%obsolete;
131         return \%pkgs;
132 }
133
134 sub load_ignores {
135         my ($ignorefile, $require_file) = @_;
136
137         my @ignores;
138         if (!$require_file and ! -e $ignorefile) {
139                 return \@ignores;
140         }
141
142         open (F, "< $ignorefile") or die ("Cannot open $ignorefile: $!\n");
143         @ignores = <F>;
144         close F;
145         chomp(@ignores);
146         return \@ignores;
147 }
148
149 sub check_ignore {
150         my ($pkg, $ignores) = @_;
151
152         my $ignore_this = 0;
153         for my $ignore (@$ignores) {
154                 my $ig = $ignore;
155                 return 1 if ($ig eq $pkg);
156                 if (substr($ig,0,1) eq '/') {
157                         substr($ig, 0, 1, '');
158                         $ig =~ s,/$,,;
159                         return 1 if ($pkg =~ /$ig/);
160                 }
161         }
162         return 0
163 }
164
165 sub filter_ignored {
166         my ($packages, $ignores) = @_;
167
168         my $obs = $packages->{'obsolete'};
169
170         my (%ignored, %bad);
171         for my $pkg (keys %$obs) {
172                 if (check_ignore($pkg, $ignores)) {
173                         $ignored{$pkg} = $obs->{$pkg};
174                 } else {
175                         $bad{$pkg} = $obs->{$pkg};
176                 };
177         }
178         delete $packages->{'obsolete'};
179         $packages->{'obsolete'} = \%bad;
180         $packages->{'obsolete-ignored'} = \%ignored;
181 };
182
183 sub usage {
184         my ($fd, $exit) = @_;
185         print $fd "Usage: $PROGRAM_NAME [<ignorefile>]\n";
186         exit $exit;
187 }
188
189 my $ignorefile = $IGNORE;
190 my $ignorefile_userset = 0;
191 usage(\*STDERR, 1) if (@ARGV > 1);
192 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;
197 };
198
199 my $ignores = load_ignores($ignorefile, $ignorefile_userset);
200 my $packages = get_packages();
201
202 filter_ignored($packages, $ignores);
203
204
205
206 my @reportform = (
207         { 'key' => 'obsolete',
208           'listpackages' => 1,
209           'long' => "%d local or obsolete packages: %s",
210           'short' => "%d obs",
211           'status' => 'WARNING' },
212         { 'key' => 'outofdate',
213           'listpackages' => 1,
214           'long' => "%d out of date packages: %s",
215           'short' => "%d updates",
216           'status' => 'WARNING' },
217         { 'key' => 'current',
218           'listpackages' => 0,
219           'long' => "%d packages current.",
220           'short' => "%d ok",
221           'status' => 'OK' },
222         { 'key' => 'obsolete-ignored',
223           'listpackages' => 1,
224           'long' => "%d whitelisted local or obsolete packages: %s",
225           'short' => "%d obs(ignored)",
226           'status' => 'OK' },
227         { 'key' => 'rc',
228           'listpackages' => 1,
229           'long' => "%d packages removed but not purged: %s",
230           'short' => "%d rc",
231           'status' => 'OK' },
232         { 'key' => 'hi',
233           'listpackages' => 1,
234           'long' => "%d packages on hold: %s",
235           'short' => "%d hi",
236           'status' => 'OK' },
237         { 'key' => 'pc',
238           'listpackages' => 1,
239           'long' => "%d packages requested to be purged but conffiles still installed: %s",
240           'short' => "%d pc",
241           'status' => 'WARNING' },
242         );
243
244 my @longout;
245 my @shortout;
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);
254         } else {
255                 push @longout, sprintf($form->{'long'}, $num);
256         };
257         push @shortout, sprintf($form->{'short'}, $num);
258         record($form->{'status'});
259 };
260 if (scalar keys %$packages) {
261         record('WARNING');
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";
267         };
268 }
269
270 my $shortout = $EXITCODE.": ".join(", ", @shortout);
271 my $longout = join("\n", @longout);
272
273 print $shortout,"\n";
274 print $longout,"\n";
275
276 exit $CODE{$EXITCODE};