Skip to content

Commit 6b5c364

Browse files
authored
Merge pull request #6 from gjbex/development
Simplify script
2 parents 4c940e1 + 8a4a23a commit 6b5c364

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

source-code/processes/monitor.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def find_ancestor(pid=None, username=None):
5050
for parent in reversed(parents):
5151
if parent.username() == username:
5252
return parent
53+
return process
5354

5455

5556
def get_cmdline(process):
@@ -128,21 +129,6 @@ def process_status(process, metrics):
128129
return ','.join(status)
129130

130131

131-
def get_filename_suffix(file_id):
132-
filename = ''
133-
if 'PBS_JOBID' in os.environ:
134-
filename += f'_{os.environ["PBS_JOBID"]}'
135-
if 'PBS_ARRAYID' in os.environ:
136-
filename += f'_{os.environ["PBS_ARRAYJOBID"]}'
137-
if file_id:
138-
filename += f'_{file_id}'
139-
return f'{filename}.txt'
140-
141-
142-
def get_filename(file_id):
143-
return platform.node() + get_filename_suffix(file_id)
144-
145-
146132
def main():
147133
arg_parser = ArgumentParser(description='monitor processes')
148134
arg_parser.add_argument('--pid', type=int, help='parent process ID ot monitor')
@@ -152,33 +138,32 @@ def main():
152138
arg_parser.add_argument('--affinity', action='store_true',
153139
help='monitor process affinity')
154140
arg_parser.add_argument('--files', action='store_true', help='monitor poen files')
155-
output_group = arg_parser.add_mutually_exclusive_group()
156-
output_group.add_argument('--output-file', help='name of file to store informatoin')
157-
output_group.add_argument('--stdout', action='store_true',
158-
help='output to standard output')
159-
output_group.add_argument('--file_id', help='identification string for output filename')
141+
arg_parser.add_argument('--ancestor', action='store_true',
142+
help='search for ancestor owned by use and report on all its decendants')
143+
arg_parser.add_argument('--output-file', help='name of file to store informatoin')
160144
options = arg_parser.parse_args()
161-
ancestor = find_ancestor(options.pid, options.user)
145+
if options.ancestor:
146+
process = find_ancestor(options.pid, options.user)
147+
else:
148+
process = psutil.Process(options.pid)
162149
inactive = []
163150
if not options.affinity:
164151
inactive.append('affinity')
165152
if not options.files:
166153
inactive.append('read_files')
167154
inactive.append('write_files')
168155
metrics = define_actions(inactive)
169-
if options.stdout:
170-
file = sys.stdout
171-
elif options.output_file:
156+
if options.output_file:
172157
file = open(options.output_file, 'w')
173158
else:
174-
file = open(get_filename(options.file_id), 'w')
159+
file = sys.stdout
175160
try:
176161
with file:
177162
print(status_header(metrics), file=file)
178163
while True:
179-
process_info = [process_status(ancestor, metrics)]
180-
for process in ancestor.children(recursive=True):
181-
process_info.append(process_status(process, metrics))
164+
process_info = [process_status(process, metrics)]
165+
for child_process in process.children(recursive=True):
166+
process_info.append(process_status(child_process, metrics))
182167
print('\n'.join(process_info), file=file)
183168
time.sleep(options.delta)
184169
except KeyboardInterrupt:

0 commit comments

Comments
 (0)