-
Notifications
You must be signed in to change notification settings - Fork 31
/
open_files.py
executable file
·47 lines (34 loc) · 1.15 KB
/
open_files.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
#!/usr/bin/env python
"""
Cloudkick open files plugin
Developed by Daniel Benamy at WNYC
Based on the Varnish plugin developed by Christopher Groskopf for The Chicago
Tribune News Applications Team
Source released under the MIT license.
Description:
Determines how many files a user has open using lsof.
Error reporting:
Outputs an error if a user wasn't specified as an argument or if lsof didn't
execute properly.
Warn reporting:
Never outputs a warning.
"""
from subprocess import Popen, PIPE
import sys
import time
if len(sys.argv) == 1:
print 'status err User must be specified as a command line argument.'
sys.exit()
user = sys.argv[1]
# If I take out this line, result winds up as None when run by cloudkick-agent
# but not when run manually. What the hell???!!!
f = open('/tmp/some-junk-so-the-open-files-check-works', 'w')
proc = Popen(['lsof', '-u', user, '-F', 'f'], stdout=PIPE)
result = proc.communicate()[0]
if not result:
print 'status err lsof failed to run.'
sys.exit()
lines = result.split('\n')
fds = filter(lambda line: line.startswith('f'), lines)
print 'status ok Got open file count.'
print 'metric open_files int %d' % len(fds)