forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
executable file
·59 lines (51 loc) · 1.72 KB
/
process.py
File metadata and controls
executable file
·59 lines (51 loc) · 1.72 KB
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
#!/usr/bin/env python
import psutil
import sys
"""
update required_processes with a dictionary of process name and how many should be running
"""
required_processes = {"bash": 5,
"launchd": 4}
# wrap getting the process name with exception handling
def get_proc_name(proc):
try:
return proc.name()
except psutil.AccessDenied:
# IGNORE: we don't have permission to access this process
pass
except psutil.NoSuchProcess:
# IGNORE: process has died between listing and getting info
pass
except Exception, e:
print "error accessing process info: %s" % e
return None
# get all of the process counts
running_processes = {}
for p in psutil.process_iter():
process_name = get_proc_name(p)
if get_proc_name(p) in running_processes:
running_processes[process_name] += 1
else:
running_processes[process_name] = 1
# print the counts and exit correctly
output = ""
exit_status = 0
for process, number in required_processes.iteritems():
if process in running_processes and required_processes[process] == running_processes[process]:
# the process name and count are correct
output += str(process) + '.count=' + str(running_processes[process]) + ';;;; '
else:
# the process is running but the count is wrong
if process in running_processes:
output += str(process) + '.count=' + str(running_processes[process]) + ';;;; '
exit_status = 2
# the process isn't running at all
else:
output += str(process) + '.count=0;;;; '
exit_status = 2
if exit_status == 0:
print "OK | " + output
sys.exit(0)
else:
print "FAIL | " + output
sys.exit(2)