Monitor Windows CPU usage with Python

This little Python script lets you monitor the average CPU-load on a Windows machine. Needs WMI to be present.

import os

def get_cpu_load():
    """ Returns a list CPU Loads"""
    result = []
    cmd = "WMIC CPU GET LoadPercentage "
    response = os.popen(cmd + ' 2>&1','r').read().strip().split("\r\n")
    for load in response[1:]:
       result.append(int(load))
    return result

if __name__ == '__main__':
    print get_cpu_load()
Advertisements
Published in: on June 8, 2009 at 10:25 am  Comments (5)  

The URI to TrackBack this entry is: https://codepoetry.wordpress.com/2009/06/08/get-windows-cpu-usage-with-python/trackback/

RSS feed for comments on this post.

5 CommentsLeave a comment

  1. you forgot to “import os” 🙂

    • Thank you, Denis.
      I added this the missing line.

  2. Yeah, this is fucking great, but I need a routine that returns one single integer with a value from 0 to 100 (%), i.e., CPU load, not this mess.

  3. […] This blog entry shows some code: […]

  4. […] This blog entry shows some code: […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: