]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/nagios/files/dsa-check-libs
Ship dsa-check-libs via puppet for now
[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         '--config=s'    => \$params->{'config'},
57         )) {
58         dief ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--quiet] [--config=<CONFIGFILE>]\n");
59 };
60 if ($params->{'help'}) {
61         print "$PROGRAM_NAME: Usage: $PROGRAM_NAME [--help|--version] [--verbose] [--quiet] [--config=<CONFIGFILE>]\n";
62         print "Reports processes that are linked against libraries that no longer exist.\n";
63         print "The optional config file can specify ignore rules - see the sample config file.\n";
64         exit (0);
65 };
66 if ($params->{'version'}) {
67         print "nagios-check-libs $VERSION\n";
68         print "nagios check for availability of debian (security) updates\n";
69         print "Copyright (c) 2005, 2006, 2007, 2008, 2012 Peter Palfrader <peter\@palfrader.org>\n";
70         exit (0);
71 };
72
73 if (! defined $params->{'config'}) {
74         $params->{'config'} = '/etc/nagios/check-libs.conf';
75 } elsif (! -e $params->{'config'}) {
76         dief("Config file $params->{'config'} does not exist.\n");
77 }
78
79 if (-e $params->{'config'}) {
80         eval "use YAML::Syck; 1" or dief "you need YAML::Syck (libyaml-syck-perl) to load a config file";
81         open(my $fh, '<', $params->{'config'}) or dief "Cannot open config file $params->{'config'}: $!";
82         $config = LoadFile($fh);
83         close($fh);
84         if (!(ref($config) eq "HASH")) {
85                 dief("Loaded config is not a hash!\n");
86         }
87 } else {
88         $config = {
89                 'ignorelist' => [
90                         '$path =~ m#^/proc/#',
91                         '$path =~ m#^/var/tmp/#',
92                         '$path =~ m#^/SYS#',
93                         '$path =~ m#^/drm$# # xserver stuff',
94                         '$path =~ m#^/dev/zero#',
95                         '$path =~ m#^/dev/shm/#',
96                 ]
97         };
98 }
99
100 if (! exists $config->{'ignorelist'}) {
101         $config->{'ignorelist'} = [];
102 } elsif (! (ref($config->{'ignorelist'}) eq 'ARRAY')) {
103         dief("Config->ignorelist is not an array!\n");
104 }
105
106
107 my %processes;
108
109 sub getPIDs($$) {
110         my ($user, $process) = @_;
111         return join(', ', sort keys %{ $processes{$user}->{$process} });
112 };
113 sub getProcs($) {
114         my ($user) = @_;
115
116         return join(', ', map { $_.' ('.getPIDs($user, $_).')' } (sort {$a cmp $b} keys %{ $processes{$user} }));
117 };
118 sub getUsers() {
119         return join('; ', (map { $_.': '.getProcs($_) } (sort {$a cmp $b} keys %processes)));
120 };
121 sub inVserver() {
122         my ($f, $key);
123         if (-e "/proc/self/vinfo" ) {
124                 $f = "/proc/self/vinfo";
125                 $key = "XID";
126         } else {
127                 $f = "/proc/self/status";
128                 $key = "s_context";
129         };
130         open(F, "< $f") or return 0;
131         while (<F>) {
132                 my ($k, $v) = split(/: */, $_, 2);
133                 if ($k eq $key) {
134                         close F;
135                         return ($v > 0);
136                 };
137         };
138         close F;
139         return 0;
140 }
141
142 my $INVSERVER = inVserver();
143
144 print STDERR "Running $LSOF -n\n" if $params->{'verbose'};
145 open (LSOF, "$LSOF -n|") or dief ("Cannot run $LSOF -n: $!\n");
146 my @lsof=<LSOF>;
147 close LSOF;
148 if ($CHILD_ERROR) { # program failed
149         dief("$LSOF -n returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n");
150 };
151
152 my ($process, $pid, $user);
153 LINE: for my $line (@lsof)  {
154         if ( $line =~ /^p/ ) {
155                 my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_  and length $_ >1} split /\0/, $line;
156                 $process = $fields{c};
157                 $pid     = $fields{p};
158                 $user    = $fields{L};
159                 next;
160         }
161
162         unless ( $line =~ /^f/ ) {
163                 dief("UNKNOWN strange line read from lsof\n");
164                 # don't print it because it contains NULL characters...
165         }
166
167         my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_  and length $_ >1} split /\0/, $line;
168
169         my $fd    = $fields{f};
170         my $inode = $fields{i};
171         my $path  = $fields{n};
172         if ($path =~ m/\.dpkg-/ || $path =~ m/\(deleted\)/ || $path =~ /path inode=/ || $path =~ m#/\.nfs# || $fd eq 'DEL') {
173                 my $deleted_in_path = ($path =~ m/\(deleted\)/);
174                 next if ($deleted_in_path && $fd =~ /^[0-9]*$/); # Ignore deleted files that are open via normal file handles.
175
176                 $path =~ s/^\(deleted\)//; # in some cases "(deleted)" is at the beginning of the string
177                 for my $i (@{$config->{'ignorelist'}}) {
178                         my $ignore = eval($i);
179                         next LINE if $ignore;
180                 }
181                 next if ($INVSERVER && ($process eq 'init') && ($pid == 1) && ($user eq 'root'));
182                 if ( $params->{'verbose'} ) {
183                         print STDERR "adding $process($pid) because of [$path]:\n";
184                         print STDERR $line;
185                 }
186                 $processes{$user}->{$process}->{$pid} = 1;
187         };
188 };
189
190
191
192 my $message='';
193 my $exit = $OK;
194 if (keys %processes) {
195         $exit = $WARNING;
196         $message = 'The following processes have libs linked that were upgraded: '. getUsers()."\n";
197 } else {
198         $message = "No upgraded libs linked in running processes\n" unless $params->{'quiet'};
199 };
200
201 print $message;
202 exit $exit;