3 # Copyright (C) 2005, 2006, 2007, 2008, 2012, 2015 Peter Palfrader <peter@palfrader.org>
4 # 2012 Uli Martens <uli@youam.net>
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:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
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.
29 $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin';
30 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
32 my $LSOF = '/usr/bin/lsof -F0';
33 my $VERSION = '0.2015012901';
44 Getopt::Long::config('bundling');
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'},
59 dief ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--quiet] [--config=<CONFIGFILE>]\n");
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";
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";
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");
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);
85 if (!(ref($config) eq "HASH")) {
86 dief("Loaded config is not a hash!\n");
91 '$path =~ m#^/proc/#',
92 '$path =~ m#^/var/tmp/#',
94 '$path =~ m#^/drm$# # xserver stuff',
95 '$path =~ m#^/dev/zero#',
96 '$path =~ m#^/dev/shm/#',
101 if (! exists $config->{'ignorelist'}) {
102 $config->{'ignorelist'} = [];
103 } elsif (! (ref($config->{'ignorelist'}) eq 'ARRAY')) {
104 dief("Config->ignorelist is not an array!\n");
111 my ($user, $process) = @_;
112 return join(', ', sort keys %{ $processes{$user}->{$process} });
117 return join(', ', map { $_.' ('.getPIDs($user, $_).')' } (sort {$a cmp $b} keys %{ $processes{$user} }));
120 return join('; ', (map { $_.': '.getProcs($_) } (sort {$a cmp $b} keys %processes)));
124 if (-e "/proc/self/vinfo" ) {
125 $f = "/proc/self/vinfo";
128 $f = "/proc/self/status";
131 open(F, "< $f") or return 0;
133 my ($k, $v) = split(/: */, $_, 2);
143 my $INVSERVER = inVserver();
145 print STDERR "Running $LSOF -n\n" if $params->{'verbose'};
146 open (LSOF, "$LSOF -n|") or dief ("Cannot run $LSOF -n: $!\n");
149 if ($CHILD_ERROR) { # program failed
150 dief("$LSOF -n returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
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};
163 unless ( $line =~ /^f/ ) {
164 dief("UNKNOWN strange line read from lsof\n");
165 # don't print it because it contains NULL characters...
168 my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_ and length $_ >1} split /\0/, $line;
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\)/ || $path =~ m/\.nfs/);
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.
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;
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";
188 $processes{$user}->{$process}->{$pid} = 1;
196 if (keys %processes) {
198 $message = 'The following processes have libs linked that were upgraded: '. getUsers()."\n";
200 $message = "No upgraded libs linked in running processes\n" unless $params->{'quiet'};