SystemHealth.py
上传用户:liufeng210
上传日期:2016-03-28
资源大小:164k
文件大小:6k
源码类别:

PlugIns编程

开发平台:

Python

  1. import win32com.client
  2. import win32process
  3. import win32pdh
  4. import win32pdhquery
  5. import win32pdhutil
  6. import pywintypes
  7. import ctypes
  8. import time
  9. import os
  10. import math
  11. __version__ = '1.0.0'
  12. # NT4 does not have WMI by default
  13. try:
  14.     import wmi
  15.     wmi_found = True
  16. except pywintypes.com_error:
  17.     wmi_found = False
  18. class CPU:
  19.     def __init__(self):
  20.         path = win32pdh.MakeCounterPath((None, "Processor", "_Total", None, -1, "% Processor Time"))
  21.         self.base = win32pdh.OpenQuery()
  22.         self.counter = win32pdh.AddCounter(self.base, path)
  23.         self.reset()
  24.     def reset(self):
  25.         win32pdh.CollectQueryData(self.base)
  26.     def get_usage(self):
  27.         win32pdh.CollectQueryData(self.base)
  28.         try:
  29.             value = win32pdh.GetFormattedCounterValue(self.counter, win32pdh.PDH_FMT_LONG)[1]
  30.         except pywintypes.error:
  31.             value = 0
  32.         return value
  33. def _disk_info():
  34.     drive = unicode(os.getenv("SystemDrive"))
  35.     freeuser = ctypes.c_int64()
  36.     total = ctypes.c_int64()
  37.     free = ctypes.c_int64()
  38.     ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive, ctypes.byref(freeuser), ctypes.byref(total), ctypes.byref(free))
  39.     return freeuser.value 
  40. def disk_free():
  41.     return int(_disk_info() / 1024)
  42. def _mem_info():
  43.         kernel32 = ctypes.windll.kernel32
  44.         c_ulong = ctypes.c_ulong
  45.         class MEMORYSTATUS(ctypes.Structure):
  46.             _fields_ = [
  47.                 ('dwLength', c_ulong),
  48.                 ('dwMemoryLoad', c_ulong),
  49.                 ('dwTotalPhys', c_ulong),
  50.                 ('dwAvailPhys', c_ulong),
  51.                 ('dwTotalPageFile', c_ulong),
  52.                 ('dwAvailPageFile', c_ulong),
  53.                 ('dwTotalVirtual', c_ulong),
  54.                 ('dwAvailVirtual', c_ulong)
  55.             ]
  56.             
  57.         memoryStatus = MEMORYSTATUS()
  58.         memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
  59.         kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))
  60.         return (memoryStatus.dwTotalPhys, memoryStatus.dwAvailPhys)
  61. def mem_used():
  62.     counter=r'MemoryCommitted Bytes'
  63.     machine, object, instance, parentInstance, index, counter = win32pdh.ParseCounterPath(counter)
  64.     instance = None
  65.     inum=-1
  66.     format = win32pdh.PDH_FMT_DOUBLE
  67.     machine=None
  68.     
  69.     path = win32pdh.MakeCounterPath( (machine,object, instance, None, inum,counter) )
  70.     hq = win32pdh.OpenQuery()
  71.     try:
  72.         hc = win32pdh.AddCounter(hq, path)
  73.         try:
  74.             win32pdh.CollectQueryData(hq)
  75.             type, val = win32pdh.GetFormattedCounterValue(hc, format)
  76.             return int(val / 1024)
  77.         except pywintypes.error:
  78.             return 0
  79.         finally:
  80.             win32pdh.RemoveCounter(hc)
  81.     finally:
  82.         win32pdh.CloseQuery(hq)
  83. def mem_free():
  84.     total,free = _mem_info()
  85.     return int(free / 1024)
  86. def mem_total():
  87.     total,free = _mem_info()
  88.     return int(total / 1024)
  89. def mem_percent():
  90.     total,free = _mem_info()
  91.     return ( float(total - free) / float(total) ) * 100.0
  92.     
  93. def _task_list():
  94.     psapi = ctypes.windll.psapi
  95.     kernel = ctypes.windll.kernel32
  96.     hModule = ctypes.c_ulong()
  97.     count = ctypes.c_ulong()
  98.     modname = ctypes.c_buffer(30)
  99.     PROCESS_QUERY_INFORMATION = 0x0400
  100.     PROCESS_VM_READ = 0x0010
  101.     pid_list = win32process.EnumProcesses()
  102.     info_list = []
  103.     
  104.     for pid in pid_list:
  105.         
  106.         hProcess = kernel.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid)
  107.         if hProcess:
  108.             psapi.EnumProcessModules(hProcess, ctypes.byref(hModule), ctypes.sizeof(hModule), ctypes.byref(count))
  109.             psapi.GetModuleBaseNameA(hProcess, hModule.value, modname, ctypes.sizeof(modname))
  110.             pname = ctypes.string_at(modname)
  111.             procmeminfo = win32process.GetProcessMemoryInfo(hProcess)
  112.             procmemusage = (procmeminfo["WorkingSetSize"]/1024)
  113.             info_list.append((pid, pname, procmemusage))
  114.                         
  115.             kernel.CloseHandle(hProcess)
  116.     return info_list
  117. def task_mem(image_name):
  118.     image_name = str.lower(image_name)
  119.     if image_name[-4:] != '.exe': image_name = image_name + '.exe'
  120.     return sum([mem for pid,name,mem in _task_list() if str.lower(name) == image_name])
  121. def task_exists(image_name):
  122.     image_name = str.lower(image_name)
  123.     if image_name[-4:] != '.exe': image_name = image_name + '.exe'
  124.     return len([mem for pid,name,mem in _task_list() if str.lower(name) == image_name]) > 0
  125. def task_cpu(image_name):
  126.     if not wmi_found: return 0.0
  127.     image_name = str.lower(image_name)
  128.     if image_name[-4:] == '.exe': image_name = image_name[:-4]
  129.     c = wmi.WMI()
  130.     process_info = {}
  131.     pct_cpu_time = 0.0
  132.     for i in range(2):
  133.     
  134.         for p in c.Win32_PerfRawData_PerfProc_Process(name=image_name):
  135.             id = long(p.IDProcess)
  136.             n1, d1 = long(p.PercentProcessorTime), long(p.Timestamp_Sys100NS)
  137.             n0, d0, so_far = process_info.get(id, (0, 0, []))
  138.             try:
  139.                 pct_cpu_time += (float (n1 - n0) / float (d1 - d0)) * 100.0
  140.             except ZeroDivisionError:
  141.                 pct_cpu_time += 0.0
  142.             
  143.             so_far.append(pct_cpu_time)
  144.             process_info[id] = (n1, d1, so_far)
  145.         if i == 0:
  146.             time.sleep(0.1)
  147.             pct_cpu_time = 0.0
  148.     num_cpu = int(os.environ['NUMBER_OF_PROCESSORS'])
  149.     return min(pct_cpu_time / num_cpu, 100.0)
  150. def sine_wave():
  151.     min = float(time.localtime()[4])
  152.     sec = float(time.localtime()[5])
  153.     T = (min + (sec/60.0)) % 10.0
  154.     return math.sin(2.0 * math.pi * T/10.0) * 100.0
  155. def saw_wave():
  156.     min = float(time.localtime()[4])
  157.     sec = float(time.localtime()[5])
  158.     T = (min + (sec/60.0)) % 10.0
  159.     return (T/10.0) * 100.0
  160.