Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.