I'm looking to grab process information about processes that are using over a certain CPU limit.
Ex:
#!/usr/bin/env python
def get_processes_over_usage(cpu_usage):
#Find the processes.
return processes
Specifically, I'm looking for the executable and the username of the process.
Thanks!
Here is the answer, I just can't post it yet: This doesn't exactly conform to the requirements, but it's close enough.
#!/usr/bin/env python
import commands
def get_processes():
output = commands.getoutput('/usr/bin/top -n 1')
lines = output.split('\n')
header = ['PID', 'USER', 'PR', 'NI', 'VIRT', 'RES', 'SHR', 'S', '%CPU', '%MEM', 'TIME+', 'COMMAND']
header_found = False
processes = []
for line in lines:
data = line.split()
if(data):
data.pop()
if(data):
data.pop(0)
if(header_found):
processes.append(dict(zip(header, data)))
if(data == header):
header_found = True
return processes