473,657 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to monitor CPU and RAM usage using Python

3 New Member
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 18404
ghostdog74
511 Recognized Expert Contributor
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(St ructure):
_fields_ = [
('dwLength', DWORD),
('dwMemoryLoad' , DWORD),
('dwTotalPhys', DWORD),
('dwAvailPhys', DWORD),
('dwTotalPageFi le', DWORD),
('dwAvailPageFi le', DWORD),
('dwTotalVirtua l', DWORD),
('dwAvailVirtua l', DWORD),
]
def winmem():
m = MEMORYSTATUS()
windll.kernel32 .GlobalMemorySt atus(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
soulofstar
3 New Member
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 Recognized Expert Expert
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 New Member
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 New Member
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
3782
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... Initially I was justing looking at using Python for some event scripting. So basically an event would trigger an object to run the appropriate Python script, which would be run in it's entirety and return control to the C++ code. After looking...
8
2102
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 one. I had an idea of writing the IDE in C/C++, as it is a big project, bcoz of the following 1. if python is used, then the memory required for running the IDE will be high.
8
4582
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 from embedded devices). Currently, we are using a heavyweight, proprietary log tool developed by another part of the company. This tool contains all "standard" logging functionality, but we also need to insert "debug" log points in the software of...
1
3044
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 to monitor the software does not suit our requirements. However I am unsure how to convert dmidecode information like so : Handle 0x0025
27
2042
by: hacker1017 | last post by:
im just asking out of curiosity.
1
2330
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 advance and waiting for your reply.
0
1930
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 following peace of code, which uses Pexpect for the same. The mentioned Poc may certainly help others, incase they need some requirement like this. import pexpect, sys if len(sys.argv) != 3: print "Usage: "+sys.argv+" user new_password." ...
0
1649
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 username and password. Often gets "Access denied" while running the code. I have tried to add the user to the \root\MicrosoftExchangeV2, and the delegate control, but still cannot. Is there anything I missing to add?
11
1317
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 Python for a variety of tasks. Given that the students come from a variety of backgrounds and are not necessarily 'hackers', I'm trying to look for examples with a bit of wow-factor. Is there any list with people/groups/companies using Python for...
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.