-
Notifications
You must be signed in to change notification settings - Fork 2
/
check-unix-open-files.pl
executable file
·64 lines (51 loc) · 1.8 KB
/
check-unix-open-files.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl -w
use strict;
use Getopt::Long ;
Getopt::Long::Configure ("no_ignore_case") ;
use constant USAGEMSG => <<USAGE;
Usage:
check_unix_open_files -p <proc_name> -w <warn_threshold> -c <critical_threshold>
This plugin checks the number of file descriptors opened by a process
Example:
check_unix_open_files.pl -a nscd -w 20 -c 25
It returns CRITICAL if number of file descriptors opened by nscd is higher than 25.
if not it returns WARNING if number of file descriptors opened by nscd is higher than 20.
In other cases it returns OK if check has been performed succesfully.
USAGE
my ( $process, $warning, $critical ) ;
GetOptions("process|p=s" => \$process,
"warning|w=s" => \$warning,
"critical|c=s" => \$critical)
or Getopt::Long::HelpMessage(2);
$process and $warning and $critical or die USAGEMSG ;
my $PsCommand;
my $PsResult;
my @PsResultLines;
my $ProcPid;
my $LsofCommand;
my $LsofResult;
my $ProcCount = 0;
$PsCommand = "ps -eaf | grep $process";
$PsResult = `$PsCommand`;
@PsResultLines = split(/\n/, $PsResult);
if ( $#PsResultLines > 1 ) {
foreach my $Proc (split(/\n/, $PsResult)) {
if ($Proc !~ /check_unix_open_fds/ && $Proc !~ / grep /) {
$ProcCount += 1;
$ProcPid = (split(/\s+/, $Proc))[1];
$LsofCommand = "sudo /usr/sbin/lsof -p $ProcPid | wc -l";
$LsofResult = `$LsofCommand`;
$LsofResult = ($LsofResult > 0 ) ? ($LsofResult - 1) : 0;
}
}
}
if ( $LsofResult >= $critical) {
print "CRITICAL: $process has $LsofResult files open\n";
exit 2;
} elsif ( $LsofResult >= $warning) {
print "WARNING: $process has $LsofResult files open\n";
exit 1;
} else {
print "OK: $process has $LsofResult files open\n";
exit 0;
}