From c184ebc092de5d462ec286203f2cac9f238a29e2 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 21 Apr 2012 08:11:16 +0000 Subject: Uli Martens: nagios-check-libs: Use machine readable lsof output format git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@520 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- nagios-check-libs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/nagios-check-libs b/nagios-check-libs index ab89460..977605d 100755 --- a/nagios-check-libs +++ b/nagios-check-libs @@ -1,6 +1,7 @@ #!/usr/bin/perl -w # Copyright (C) 2005, 2006, 2007, 2008, 2012 Peter Palfrader +# 2012 Uli Martens # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -28,8 +29,8 @@ use Getopt::Long; $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; -my $LSOF = '/usr/bin/lsof'; -my $VERSION = '0.1.0'; +my $LSOF = '/usr/bin/lsof -F0'; +my $VERSION = '0.2012042101'; # nagios exit codes my $OK = 0; @@ -148,15 +149,36 @@ if ($CHILD_ERROR) { # program failed dief("$LSOF -n returned with non-zero exit code: ".($CHILD_ERROR / 256)."\n"); }; +my ($process, $pid, $user); LINE: for my $line (@lsof) { - my ($process, $pid, $user, $fd, undef, undef, undef, $path, $rest) = split /\s+/, $line; - if ($line =~ m/\.dpkg-/ || $line =~ m/path inode=/ || $fd eq 'DEL') { + if ( $line =~ /^p/ ) { + my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_ and length $_ >1} split /\0/, $line; + $process = $fields{c}; + $pid = $fields{p}; + $user = $fields{L}; + next; + } + + unless ( $line =~ /^f/ ) { + dief("UNKNOWN strange line read from lsof\n"); + # don't print it because it contains NULL characters... + } + + my %fields = map { m/^(.)(.*)$/ ; $1 => $2 } grep { defined $_ and length $_ >1} split /\0/, $line; + + my $fd = $fields{f}; + my $inode = $fields{i}; + my $path = $fields{n}; + if ($path =~ m/\.dpkg-/ || $path =~ m/\(deleted\)/ || $path =~ /path inode=/ || $fd eq 'DEL') { for my $i (@{$config->{'ignorelist'}}) { my $ignore = eval($i); next LINE if $ignore; } next if ($INVSERVER && ($process eq 'init') && ($pid == 1) && ($user eq 'root')); - #$processes{$user}->{$process} = [] unless defined $processes{$user}->{$process}; + if ( $params->{'verbose'} ) { + print STDERR "adding $process($pid) because of [$path]:\n"; + print STDERR $line; + } $processes{$user}->{$process}->{$pid} = 1; }; }; -- cgit v1.2.3