This script requests following informations remotely from a Windows machine:
- Uptime
- CPU Utilization
- Available Memory
- Memory Used
- Ping
Attention: This script needs the WMI module from Tim Golden.
import re
import wmi
from subprocess import Popen, PIPE
# this script comes from http://coreygoldberg.blogspot.com/2008/12/python-monitor-windows-remotely-with.html
def get_uptime(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
secs_up = int([uptime.SystemUpTime for uptime in c.Win32_PerfFormattedData_PerfOS_System()][0])
hours_up = secs_up / 3600
return hours_up
def get_cpu(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
utilizations = [cpu.LoadPercentage for cpu in c.Win32_Processor()]
utilization = int(sum(utilizations) / len(utilizations)) # avg all cores/processors
return utilization
def get_mem_mbytes(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
available_mbytes = int([mem.AvailableMBytes for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return available_mbytes
def get_mem_pct(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
pct_in_use = int([mem.PercentCommittedBytesInUse for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return pct_in_use
def ping(host_name):
p = Popen('ping -n 1 ' + host_name, stdout=PIPE)
m = re.search('Average = (.*)ms', p.stdout.read())
if m:
return True
else:
raise Exception
You’ve an error.
The line m = re.search(‘Average = (.*)ms’, p.stdout.read())
should have a b:
m = re.search(b’Average = (.*)ms’, p.stdout.read())