]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/nagios/files/dsa-check-libs
807796f41bd4b7261e3a0f8a77758d01639c7dd1
[dsa-puppet.git] / modules / nagios / files / dsa-check-libs
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2005, 2006, 2007, 2008, 2012, 2015 Peter Palfrader <peter@palfrader.org>
4 #               2012 Uli Martens <uli@youam.net>
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 use strict;
26 use English;
27 use Getopt::Long;
28
29 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
30 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
31
32 my $LSOF = '/usr/bin/lsof -F0';
33 my $VERSION = '0.2015012901';
34
35 # nagios exit codes
36 my $OK = 0;
37 my $WARNING = 1;
38 my $CRITICAL = 2;
39 my $UNKNOWN = 3;
40
41 my $params;
42 my $config;
43
44 Getopt::Long::config('bundling');
45
46 sub dief {
47         print STDERR @_;
48         exit $UNKNOWN;
49 }
50
51 if (!GetOptions (
52         '--help'        => \$params->{'help'},
53         '--version'     => \$params->{'version'},
54         '--quiet'       => \$params->{'quiet'},
55         '--verbose'     => \$params->{'verbose'},
56         '-v'            => \$params->{'verbose'},
57         '--config=s'    => \$params->{'config'},
58         )) {
59         dief ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--quiet] [--config=<CONFIGFILE>]\n");
60 };
61 if ($params->{'help'}) {
62         print "$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--quiet] [--config=<CONFIGFILE>]\n";
63         print "Reports processes that are linked against libraries that no longer exist.\n";
64         print "The optional config file can specify ignore rules - see the sample config file.\n";
65         exit (0);
66 };
67 if ($params->{'version'}) {
68         print "nagios-check-libs $VERSION\n";
69         print "nagios check for availability of debian (security) updates\n";
70         print "Copyright (c) 2005, 2006, 2007, 2008, 2012 Peter Palfrader <peter\@palfrader.org>\n";
71         exit (0);
72 };
73
74 if (! defined $params->{'config'}) {
75         $params->{'config'} = '/etc/nagios/check-libs.conf';
76 } elsif (! -e $params->{'config'}) {
77         dief("Config file $params->{'config'} does not exist.\n");
78 }
79
80 if (-e $params->{'config'}) {
81         eval "use YAML::Syck; 1" or dief "you need YAML::Syck (libyaml-syck-perl) to load a config file";
82         open(my $fh, '<', $params->{'config'}) or dief "Cannot open config file $params->{'config'}: $!";
83         $config = LoadFile($fh);
84         close($fh);
85         if (!(ref($config) eq "HASH")) {
86                 dief("Loaded config is not a hash!\n");
87         }
88 } else {
89         $config = {
90                 'ignorelist' => [
91                         '$path =~ m#^/proc/#',
92                         '$path =~ m#^/var/tmp/#',
93                         '$path =~ m#^/SYS#',
94                         '$path =~ m#^/drm$# # xserver stuff',
95                         '$path =~ m#^/dev/zero#',
96                         '$path =~ m#^/dev/shm/#',
97                 ]
98         };
99 }
100
101 if (! exists $config->{'ignorelist'}) {
102         $config->{'ignorelist'} = [];
103 } elsif (! (ref($config->{'ignorelist'}) eq 'ARRAY')) {
104         dief("Config->ignorelist is not an array!\n");
105 }
106
107
108 my %processes;
109
110 sub getPIDs($$) {
111         my ($user, $process) = @_;
112         return join(', ', sort keys %{ $processes{$user}->{$process} });
113 };
114 sub getProcs($) {
115         my ($user) = @_;
116
117         return join(', ', map { $_.' ('.getPIDs($user, $_).')' } (sort {$a cmp $b} keys %{ $processes{$user} }));
118 };
119 sub getUsers() {
120         return join('; ', (map { $_.': '.getProcs($_) } (sort {$a cmp $b} keys %processes)));
121 };
122 sub inVserver() {
123         my ($f, $key);
124         if (-e "/proc/self/vinfo" ) {
125                 $f = "/proc/self/vinfo";
126                 $key = "XID";
127         } else {
128                 $f = "/proc/self/status";
129                 $key = "s_context";
130         };
131         open(F, "< $f") or return 0;
132         while (<F>) {
133                 my ($k, $v) = split(/: */, $_, 2);
134                 if ($k eq $key) {
135                         close F;
136                         return ($v > 0);
137                 };
138         };
139         close F;
140         return 0;
141 }
142
143 my $INVSERVER = inVserver();
144
145 print STDERR "Running $LSOF -n\n" if $params->{'verbose'};
146 open (LSOF, "$LSOF -n|") or dief ("Cannot run $LSOF -n: $!\n");
147 my @lsof=<LSOF>;
148 close LSOF;
149 if ($CHILD_ERROR) { # program failed
150         dief("$LSOF -n returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
151 };
152
153 my ($process, $pid, $user);
154 LINE: for my $line (@lsof)  {
155         if ( $line =~ /^p/ ) {
156                 my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_  and length $_ >1} split /\0/, $line;
157                 $process = $fields{c};
158                 $pid     = $fields{p};
159                 $user    = $fields{L};
160                 next;
161         }
162
163         unless ( $line =~ /^f/ ) {
164                 dief("UNKNOWN strange line read from lsof\n");
165                 # don't print it because it contains NULL characters...
166         }
167
168         my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_  and length $_ >1} split /\0/, $line;
169
170         my $fd    = $fields{f};
171         my $inode = $fields{i};
172         my $path  = $fields{n};
173         if ($path =~ m/\.dpkg-/ || $path =~ m/\(deleted\)/ || $path =~ /path inode=/ || $path =~ m#/\.nfs# || $fd eq 'DEL') {
174                 my $deleted_in_path = ($path =~ m/\(deleted\)/);
175                 next if ($deleted_in_path && $fd =~ /^[0-9]*$/); # Ignore deleted files that are open via normal file handles.
176                 next if ($deleted_in_path && $fd eq 'cwd'); # Ignore deleted directories that we happen to be in.
177
178                 $path =~ s/^\(deleted\)//; # in some cases "(deleted)" is at the beginning of the string
179                 for my $i (@{$config->{'ignorelist'}}) {
180                         my $ignore = eval($i);
181                         next LINE if $ignore;
182                 }
183                 next if ($INVSERVER && ($process eq 'init') && ($pid == 1) && ($user eq 'root'));
184                 if ( $params->{'verbose'} ) {
185                         print STDERR "adding $process($pid) because of [$path]:\n";
186                         print STDERR $line;
187                 }
188                 $processes{$user}->{$process}->{$pid} = 1;
189         };
190 };
191
192
193
194 my $message='';
195 my $exit = $OK;
196 if (keys %processes) {
197         $exit = $WARNING;
198         $message = 'The following processes have libs linked that were upgraded: '. getUsers()."\n";
199 } else {
200         $message = "No upgraded libs linked in running processes\n" unless $params->{'quiet'};
201 };
202
203 print $message;
204 exit $exit;