-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_open_files.pl
111 lines (91 loc) · 3.33 KB
/
check_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl
use Getopt::Long;
use vars qw($opt_h $PROGNAME $opt_w $opt_c $opt_t $opt_vi $msg $state);
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS &print_revision &support &usage );
sub print_help ();
sub print_usage ();
sub process_arguments();
Getopt::Long::Configure('bundling');
$status=process_arguments();
if ($status){
print "ERROR: Processing Arguments\n";
exit $ERRORS{'WARNING'};
}
$SIG{'ALRM'} = sub {
print ("ERROR: timed out waiting for $PROGNAME \n");
exit $ERRORS{"WARNING"};
};
alarm($opt_t);
$procfile="/proc/sys/fs/file-nr";
open(FILE, $procfile) or die "$procfile not exits!";
my $line=<FILE>;
close FILE;
my ($nb_openfiles, $nb_freehandlers, $file_max)= split(/\s/, $line);
### See /usr/src/linux/Documentation/sysctl/fs.txt : the real number of open files is
# the first field minus the second one
my $realfreehandlers = $nb_openfiles - $nb_freehandlers;
$warning_threshold=int($file_max * $opt_w /100);
$critical_threshold=int($file_max * $opt_c /100);
if ($realfreehandlers < $warning_threshold ){
$msg = "OK: open files ($realfreehandlers) is below threshold ($warning_threshold/$critical_threshold)";
$state=$ERRORS{'OK'};
}
if ($realfreehandlers >= $warning_threshold && $realfreehandlers < $critical_threshold){
$msg = "WARNING: open files ($realfreehandlers) exceeds (threshold=$warning_threshold/$critical_threshold)";
$state=$ERRORS{'WARNING'};
}
if ($realfreehandlers >= $critical_threshold ){
$msg = "CRITICAL: open files ($realfreehandlers) exceeds (threshold=$critical_threshold/$critical_threshold)";
$state=$ERRORS{'CRITICAL'};
}
print "$msg|open_files=$realfreehandlers;$warning_threshold;$critical_threshold\n";
exit $state;
########## SUBS ##############################################
sub process_arguments(){
GetOptions (
"w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number
"c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number
"t=i" => \$opt_t, "timeout=i" => \$opt_t,
"h" => \$opt_h, "help" => \$opt_h,
"v" => \$opt_v, "version" => \$opt_v
);
if ($opt_v){
print_revision ($PROGNAME ,'$Revision: 1.1 $');
exit $ERRORS{'OK'};
}
if ($opt_h){
print_help();
exit $ERRORS{'OK'};
}
unless (defined $opt_t){
$opt_t = $utils::TIMEOUT;
}
unless (defined $opt_w && defined $opt_c){
print_usage();
exit $ERRORS{'UNKNOWN'};
}
if ( $opt_w >= $opt_c) {
print "Warning (-w) cannot be greater than Critical (-c)!\n";
exit $ERRORS{'UNKNOWN'};
}
return $ERRORS{'OK'};
}
sub print_usage () {
print "Usage: $PROGNAME -w <warn> -c <crit> [-t <timeout>] [-v version] [-h help]\n";
}
sub print_help () {
print_revision($PROGNAME,'$Revision: 1.1 $');
print "Copyright (c) 2007 Christophe Thiesset\n";
print "\n";
print_usage();
print "\n";
print " Checks the open files number against the max autorized\n";
print "-w (--warning) = Percentage of opened files to generate warning alert\n";
print "-c (--critical) = Percentage of opened files to generate critical( w < c )\n";
print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
print "-h (--help)\n";
print "-v (--version)\n";
print "\n\n";
support();
}