python--psutil系統資訊模組
阿新 • • 發佈:2019-02-16
class MachineStatue:
def _getHostName(self,):
self._hostname = socket.getfqdn(socket.gethostname())
return self._hostname
def _getIP(self,):
return socket.gethostbyname(self._hostname)
def _getCPUState(self, interval=1):
return psutil.cpu_percent(interval)
def _getProcessInfo(self,):
proc_pids = psutil.pids()
proc_name = []
for pid in proc_pids:
proc = psutil.Process(pid)
proc_name.append(proc.name())
return zip(proc_pids,proc_name)
def getInfo(self,):
hostname = self._getHostName()
ip = self._getIP()
CPUState = self._getCPUState()
processInfo = self._getProcessInfo()
info = {'hostname' :hostname,'ip':ip,'CPUState':CPUState,'processInfo':processInfo}
return info