473,416 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to monitor CPU and RAM usage using Python

I want to monitor the CPU and RAM usage using Python,

Now, I know how to monitor RAM usage by Python

Expand|Select|Wrap|Line Numbers
  1. from ctypes import *
  2. from ctypes.wintypes import *
  3.  
  4. class MEMORYSTATUS(Structure):
  5.     _fields_ = [
  6.     ('dwLength', DWORD),
  7.     ('dwMemoryLoad', DWORD),
  8.     ('dwTotalPhys', DWORD),
  9.     ('dwAvailPhys', DWORD),
  10.     ('dwTotalPageFile', DWORD),
  11.     ('dwAvailPageFile', DWORD),
  12.     ('dwTotalVirtual', DWORD),
  13.     ('dwAvailVirtual', DWORD),
  14.     ]
  15. def winmem():
  16.     m = MEMORYSTATUS()
  17.     windll.kernel32.GlobalMemoryStatus(byref(m))
  18.     return m
  19. m = winmem()
  20. print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)

But I don't know how to monitor CPU realtime usage, please help me.

And how to monitor CPU and RAM used by one program?
Apr 5 '07 #1
5 18361
ghostdog74
511 Expert 256MB
I want to monitor the CPU and RAM usage using Python,

Now, I know how to monitor RAM usage by Python
from ctypes import *
from ctypes.wintypes import *

class MEMORYSTATUS(Structure):
_fields_ = [
('dwLength', DWORD),
('dwMemoryLoad', DWORD),
('dwTotalPhys', DWORD),
('dwAvailPhys', DWORD),
('dwTotalPageFile', DWORD),
('dwAvailPageFile', DWORD),
('dwTotalVirtual', DWORD),
('dwAvailVirtual', DWORD),
]
def winmem():
m = MEMORYSTATUS()
windll.kernel32.GlobalMemoryStatus(byref(m))
return m
m = winmem()
print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)

But I don't know how to monitor CPU realtime usage, please help me.

And how to monitor CPU and RAM used by one program?

while i am not much into ctypes, there's another way, using WMI
check MSDN and here for more.

one example :
Expand|Select|Wrap|Line Numbers
  1. >>> import wmi
  2. >>> wm = wmi.WMI()
  3. >>> for j in wm.Win32_PerfFormattedData_PerfOS_Memory(): print j 
  4.  
another eg:
Expand|Select|Wrap|Line Numbers
  1. >>> import wmi
  2. >>> wm = wmi.WMI ()
  3. >>> for j in wm.Win32_Processor (): print j
  4.  
Apr 5 '07 #2
Now I knew how to monitor CPU and Memory usage via win32pdh, while I don't know how to monitor memory usage of one application.

snippet of code:
Expand|Select|Wrap|Line Numbers
  1. mempath = win32pdh.MakeCounterPath((None, "Memory", None, \
  2.                                             None, -1, "Available MBytes"))
  3. query = win32pdh.OpenQuery(None, 0)
  4. counter = win32pdh.AddCounter(query, mempath, 0)
  5.  
  6. win32pdh.CollectQueryData(query)
  7. status, value = win32pdh.GetFormattedCounterValue(counter,
  8.                 win32pdh.PDH_FMT_LONG)
  9.  
These code only can monitor the whole availabe memory, I do not know how to change the CounterPath to monitor one certain application's memory usage.

anyone can help me, thanks for your reply
Apr 5 '07 #3
bartonc
6,596 Expert 4TB
while i am not much into ctypes, there's another way, using WMI
check MSDN and here for more.

one example :
Expand|Select|Wrap|Line Numbers
  1. >>> import wmi
  2. >>> wm = wmi.WMI()
  3. >>> for j in wm.Win32_PerfFormattedData_PerfOS_Memory(): print j 
  4.  
another eg:
Expand|Select|Wrap|Line Numbers
  1. >>> import wmi
  2. >>> wm = wmi.WMI ()
  3. >>> for j in wm.Win32_Processor (): print j
  4.  
Great link GD! Would you consider doing an article or code post for one of the sub forums. This is something that I had not heard of. Thanks for the info.
Apr 5 '07 #4
rahali
2
Hi,
I want to use Ctypes to monitor RAM on UNIX, what can i do ?

thanks for your help.

hicham
Dec 4 '07 #5
rahali
2
i think that ctypes is not supported under unix platforms. i cant found, how to get informations about memory, and i think for each platforme, it must use the apropriate command:
exp :
linux ==> use /proc/meminfo
unix ==> use for each platforme its command

i want to use one methode to get memory informations for all platforms, please if any one can help,

thanks
Dec 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: The_Incubator | last post by:
As the subject suggests, I am interested in using Python as a scripting language for a game that is primarily implemented in C++, and I am also interested in using generators in those scripts... ...
8
by: Sridhar R | last post by:
Hi, I am a little experienced python programmer (2 months). I am somewhat experienced in C/C++. I am planning (now in design stage) to write an IDE in python. The IDE will not be a simple...
8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions regarding the logging of our software (originating...
1
by: Johhny | last post by:
Hello, I am currently looking to write a utility in python that will monitor the statis of a RAID card within linux. The card in Question is the LSI SAS1064 as the tools provided by the vendor...
27
by: hacker1017 | last post by:
im just asking out of curiosity.
1
by: dipak5801 | last post by:
i want to know how to monitor services using SNMP protocol. what is MIB and how can i get this information. anyone provide me training mateiral or step by step process using snapshot . thanks in...
0
by: geoharish | last post by:
Dear All, I started using Python just a few days back and there was a requirement of an assigned project to change the user Password through script without user interaction. I could write the...
0
by: garks | last post by:
I am using Exchange 2003(Exchange Hosting), C#. I am trying to get Mailbox usage using WMI But I follows the coding below, still cannot get the Mailbox usage. Even though, I predefined the...
11
by: Rajarshi | last post by:
Hi, I teach an introductory programming course in Python. As part of the introduction I'd like to highlight the usage of Python in industry. The idea is to show that there are big players using...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.