-
Notifications
You must be signed in to change notification settings - Fork 16
/
openfiles.py
executable file
·110 lines (85 loc) · 3.29 KB
/
openfiles.py
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/env python
import os
import sys
import psutil
import optparse
"""
Nagios check to monitor the number of files opened by a process.
Usage:
-p -f /var/run/mydaemon.pid
OK: Process 13403 has 4037 file descriptors opened|num_fds=4037;6000;6000;;
http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_num_fds/details
"""
usage = """
Typically %prog is invoked by the nrpe service that runs as the user nagios:
sudo %prog -p -f /var/run/mydaemon.pid
"""
parser = optparse.OptionParser(usage=usage)
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
help="verbose mode.")
parser.add_option("-p", "--proc", action="store_true", dest="proc",
help="to use the soft/hard limits from /proc/pid as the warning/critical thresholds.")
parser.add_option("-f", "--file", dest="file", type="string",
help=".pid file containing the process pid.")
parser.add_option("-w", "--warn", dest="warn_value", default="-1", type="int",
help="warning threshold.")
parser.add_option("-c", "--crit", dest="crit_value", default="-1", type="int",
help="critical threshold.")
(options, args) = parser.parse_args()
if len(args) != 0:
sys.exit(parser.print_help())
if options.verbose:
print "Checking if the provided string: " + options.file + " is really a file."
assert os.path.isfile(options.file)
try:
if options.verbose :
print "Opening file: " + options.file
pidfile = open(options.file, 'r')
pid = int(pidfile.readline())
if options.verbose:
print "Found pid: " + str(pid)
if options.verbose:
print "Checking if the pid=" + str(pid) + " is a live process."
assert psutil.pid_exists( pid )
except IOError:
print "Can't open the file %s", options.file
sys.exit(1)
if options.proc:
try:
if options.verbose:
print "Opening the file: /proc/" + str(pid) + "/limits"
procfile = open('/proc/' + str(pid) + '/limits', 'r')
for line in procfile:
if options.verbose:
print "Searching for the 'Max open files' settings in: " + line
if "Max open files" in line:
mylist = [int(s) for s in line.split() if s.isdigit()]
options.warn_value = mylist[0]
options.crit_value = mylist[1]
if options.verbose:
print "Found soft limit: " + str(options.warn_value)
if options.verbose:
print "Found hard limit: " + str(options.crit_value)
break
except IOError:
print "Can't open the file %s", options.file
sys.exit(1)
# Getting the number of files opened by pid
num_fds = psutil.Process(pid).get_num_fds()
assert options.warn_value > 0
assert options.crit_value > 0
# Nagios possible states
status_dict = {
0: "OK",
1: "WARNING",
2: "CRITICAL",
3: "UNKNOWN"
}
if num_fds > options.crit_value:
status = 2
elif num_fds > options.warn_value:
status = 1
else:
status = 0
print "{0}: Process {1} has {2} file descriptors opened|num_fds={2};{3};{4};;".format(status_dict[status], str(pid), str(num_fds), str(options.warn_value), str(options.crit_value))
exit(status)