-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_check.py
61 lines (50 loc) · 1.35 KB
/
score_check.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
#!/usr/bin/python
"""
Python script for obtaining processes and their oom score
"""
from __future__ import print_function
# Standard Library
import os
import sys
from optparse import OptionParser
def getscores():
"""
Function finds and returns a list of tuples containing:
(oom_score process_name)
"""
return [
(
int(open(os.path.join(proc.path, "oom_score")).read().strip()),
open(os.path.join(proc.path, "comm")).read().strip(),
)
for proc in os.scandir("/proc")
if proc.is_dir() and proc.name.isdigit()
]
def printscores(oom_scores, show):
"""
Print oom score and process name
"""
for process in sorted(oom_scores, key=lambda x: x[0], reverse=True)[:show]:
print(f"{process[0]} {process[1]}")
def main():
"""
Usage and help overview
Option parsing
"""
parser = OptionParser(usage="usage: %prog [option]")
parser.add_option(
"-s",
"--show",
action="store",
dest="show",
metavar="show",
help="Number of processes to show",
)
(options, _) = parser.parse_args()
show = int(options.show) if options.show else 10
try:
printscores(getscores(), show)
except ValueError:
print("Please enter a value number", file=sys.stderr)
if __name__ == "__main__":
main()