-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_num_fds.py
82 lines (59 loc) · 2.25 KB
/
check_num_fds.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
#! /usr/bin/python
# Nagios check to monitor the number of files opened by a process.
# Forked from https://bitbucket.org/fabio79ch/check_num_fds/src/master/check_num_fds.py .
# http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_num_fds/details
# Requires psutil 5.6.1 (or better?).
# Usage:
# check_num_fds.py -w 1000 -c 2000
# OK: bad pids: dict_values([])
try:
import psutil
except ImportError:
fail("You need the psutil python module")
import optparse
usage = """
%prog -w 1024 -c 2048
"""
parser = optparse.OptionParser(usage=usage)
parser.add_option("-v", "--verbose" , action="store_true" , dest="verbose" , help="verbose mode.")
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:
fail( parser.print_help() )
import os
import sys
import pprint
def check_pid(pid):
# Getting the number of files opened by pid
num_fds = 0
try:
num_fds = psutil.Process( pid ).num_fds()
# don't need to worry about a PID that's gone, just ignore it
except (psutil.NoSuchProcess):
# debugging: print pids not found to make sure script doesn't fail
# print('pid ' + str(pid) + ' not found', file=sys.stderr)
pass
return num_fds
####
assert options.warn_value > 0
assert options.crit_value > options.warn_value
bad_pids = dict()
status = 0
for pid in (psutil.pids()):
try:
num_fds=check_pid(pid)
# don't need to worry about a PID that's gone, just ignore it
except (psutil.NoSuchProcess):
pass
if num_fds > options.crit_value:
bad_pids[pid] = {'pid':pid,'num_fds':num_fds}
status = 2
elif num_fds > options.warn_value:
bad_pids[pid] = {'pid':pid,'num_fds':num_fds}
if status == 0: status = 1
# Nagios possible states
status_dict= {0:"OK",1:"WARNING",2:"CRITICAL",3:"UNKNOWN"}
print ("{}: bad pids: ".format(status_dict[status]) + str(bad_pids.values() ) )
# 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)