Sarath M
Sarath M

Reputation: 46

Remote PC CPU usage using python

I am using the following code to get remote PC CPU percentage of usage witch is slow and loading the remote PC because of SSHing.

per=(subprocess.check_output('ssh [email protected] nohup python psutilexe.py',stdin=None,stderr=subprocess.STDOUT,shell=True)).split(' ')
print 'CPU %=',float(per[0])
print 'MEM %=',float(per[1])

where psutilexe.py is as follows:

import psutil
print psutil.cpu_percent(), psutil.virtual_memory()[2]

Would you please let me know if there is any alternate way to measure remote PC CPU % of usage using Python?

Upvotes: 3

Views: 10122

Answers (3)

julio
julio

Reputation: 121

I had been looking for it for a while and I think WMI does what you need.

WMI_Python_Documentation

import wmi
pc = wmi.WMI('PC_Name')
cpu = pc.Win32_Processor()
for i in cpu:
    print (i.LoadPercentage)

Hopefully this is what you need.

Upvotes: 0

Arseni Mourzenko
Arseni Mourzenko

Reputation: 52321

You don't need a custom Python script, since you can have CPU usage directly with top, (or with sysstat, if installed).

Have you profiled your app? Is it the custom script which is making it slow, or the SSHing itself? If it's the SSHing, then:

  • Consider logging once if you're getting multiple values, or:

  • Consider using message queue instead of SSHing: monitored machines will constantly send their CPU usage to a message queue service, which will be listened by the machine which is in charge of gathering the results.

Upvotes: 0

Maxo
Maxo

Reputation: 538

I would suggest taking look at Glances. It's written in python and can also be used for remote server monitoring:

https://github.com/nicolargo/glances

Using glances on remote server:

http://mylinuxbook.com/glances-an-all-in-one-system-monitoring-tool/

Upvotes: 1

Related Questions