python实现简易内存监控


Posted in Python onJune 21, 2018

本例主要功能:每隔3秒获取系统内存,当内存超过设定的警报值时,获取所有进程占用内存并发出警报声。内存值和所有进程占用内存记入log,log文件按天命名。

1 获取cpu、内存、进程信息

利用WMI

简单说明下,WMI的全称是Windows Management Instrumentation,即Windows管理规范。它是Windows操作系统上管理数据和操作的基础设施。我们可以使用WMI脚本或者应用自动化管理任务等。

安装模块

WMI下载地址
win32com下载地址:

学会使用WMI

不错的教程

获取cpu、内存、磁盘

def getSysInfo(wmiService = None):
 result = {}
 if wmiService == None:
  wmiService = wmi.WMI()
 # cpu
 for cpu in wmiService.Win32_Processor():
  timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
  result['cpuPercent'] = cpu.loadPercentage
 # memory
 cs = wmiService.Win32_ComputerSystem()
 os = wmiService.Win32_OperatingSystem()
 result['memTotal'] = int(int(cs[0].TotalPhysicalMemory)/1024/1024)
 result['memFree'] = int(int(os[0].FreePhysicalMemory)/1024)
 result['memPercent']=result['memFree'] * 100 /result['memTotal']
 #disk
 result['diskTotal'] = 0
 result['diskFree'] = 0
 for disk in wmiService.Win32_LogicalDisk(DriveType=3):
  result['diskTotal'] += int(disk.Size)
  result['diskFree'] += int(disk.FreeSpace)
 result['diskTotal'] = int(result['diskTotal']/1024/1024)
 result['diskFree'] = int(result['diskFree']/1024/1024)
 return result

获取所有进程占用内存

def getAllProcessInfo(mywmi = None): 
 """取出全部进程的进程名,进程ID,进程实际内存, 虚拟内存,CPU使用率 
 """ 
 allProcessList = []

 allProcess = mywmi.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process")
 #print (allProcess.count)
 for j in allProcess:
  #print j.Properties_("PercentPrivilegedTime").__int__()
  ##print j.Properties_("name").__str__()+" "+j.Properties_("IDProcess").__str__()+" "+j.Properties_("PercentPrivilegedTime").__str__()
  #for pro in j.Properties_:
  # print (pro.name)
  #break
  name = j.Properties_("name").__str__()
  if name != "_Total" and name !="Idle":
   pid = j.Properties_("IDProcess").__str__()
   PercentPrivilegedTime = j.Properties_("PercentPrivilegedTime").__int__()
   WorkingSetPrivate = j.Properties_("WorkingSetPrivate").__int__()/1024
   WorkingSet = j.Properties_("WorkingSet").__int__()/1024
   allProcessList.append([name, pid, WorkingSetPrivate, WorkingSet, PercentPrivilegedTime])

 return allProcessList

也可以用psutil

import psutil,time 
from operator import itemgetter, attrgetter

def getProcessInfo(p): 
 """取出指定进程占用的进程名,进程ID,进程实际内存, 虚拟内存,CPU使用率 
 """ 
 try: 
  cpu = int(p.cpu_percent(interval=0)) 
  memory = p.memory_info() 
  rss = memory.rss/1024
  vms = memory.vms/1024
  name = p.name() 
  pid = p.pid 
 except psutil.Error: 
  name = "Closed_Process" 
  pid = 0 
  rss = 0 
  vms = 0 
  cpu = 0 
 #return [name.upper(), pid, rss, vms] 
 return [name, pid, vms, rss, cpu] 

def getAllProcessInfo(): 
 """取出全部进程的进程名,进程ID,进程实际内存, 虚拟内存,CPU使用率 
 """ 
 instances = [] 
 all_processes = list(psutil.process_iter()) 
 for proc in all_processes: 
  proc.cpu_percent(interval=0) 
 #此处sleep1秒是取正确取出CPU使用率的重点 
 time.sleep(1) 
 for proc in all_processes: 
  instances.append(getProcessInfo(proc)) 
 return instances 


if __name__ == '__main__': 
 processInfoList = getAllProcessInfo()
 processInfoList.sort(key=itemgetter(2), reverse=True)
 for p in processInfoList:
  print(p)

2. 保存log

配置config文件

[loggers]
keys=example01
[logger_example01]
handlers=hand04

[handlers]
keys=hand04
[handler_hand04]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=form01
args=('./logs/monitor.log', 'd', 1, 7)

[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

记录log

import logging
import logging.config

logger.info("message")
logger.warning("message")
logger.error("message")

3. 完整代码

文件夹结构:

maintain
?monitor
—-logs
—-logger.conf
—-monitor.py
?packages
—-init.py
—-processinfo.py
—-sysinfo.py

monitor

import wmi 
import time
import winsound 
import logging
import logging.config
from operator import itemgetter, attrgetter
from os import path

import packages.sysinfo #使用wmi
#import packages.ProcessInfo #使用

#def ShowProcessInfo():
# processInfoList = packages.ProcessInfo.getAllProcessInfo()
# processInfoList.sort(key=itemgetter(2), reverse=True)
# for p in processInfoList:  
#  logger.info(p)

def ShowProcessInfo(wmiService = None):
 processInfoList = packages.sysinfo.getAllProcessInfo(wmiService)
 processInfoList.sort(key=itemgetter(2), reverse=True)
 for p in processInfoList:  
  logger.info(p)

if __name__ == '__main__':
 MemPerWorningLine = 50
 MemPerErrorLine = 20
 ErrorAlertCount = 10
 ProcessInfoCount = 10
 counterProcessInfo = ProcessInfoCount

 print("Memory monitor start!")
 log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logger.conf')
 #print(log_file_path)
 logging.config.fileConfig(log_file_path)
 logger = logging.getLogger("example01")
 wmiService = wmi.WMI()
 while True:
  memPercent = int(packages.sysinfo.getSysInfo(wmiService)['memPercent'])
  strMemPercent = 'FreeMemory: ' + str(memPercent) + '%'
  if(memPercent < MemPerErrorLine):
   logger.error(strMemPercent)
   #ProcessInfoList
   counterProcessInfo+=1
   if(counterProcessInfo >= ProcessInfoCount):
    ShowProcessInfo(wmiService)
    counterProcessInfo = 0
   #ALert
   counter = 1
   while counter <= ErrorAlertCount:
    winsound.Beep(2080, 100) 
    time.sleep(0.1)
    counter += 1
  elif(memPercent < MemPerWorningLine):
   logger.warning(strMemPercent)
   #ProcessInfoList
   counterProcessInfo+=1
   if(counterProcessInfo >= ProcessInfoCount):
    ShowProcessInfo(wmiService)
    counterProcessInfo = 0
   #ALert
   winsound.Beep(2015, 2000) 
  else:
   logger.info(strMemPercent)
  time.sleep(3)

sysinfo

# -*- coding: utf-8 -*-

import wmi 
import os 
import sys 
import platform 
import time 
import win32api
import win32com
from win32com.client import GetObject
from operator import itemgetter, attrgetter

def getSysInfo(wmiService = None):
 result = {}
 if wmiService == None:
  wmiService = wmi.WMI()
 # cpu
 for cpu in wmiService.Win32_Processor():
  timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
  result['cpuPercent'] = cpu.loadPercentage
 # memory
 cs = wmiService.Win32_ComputerSystem()
 os = wmiService.Win32_OperatingSystem()
 result['memTotal'] = int(int(cs[0].TotalPhysicalMemory)/1024/1024)
 result['memFree'] = int(int(os[0].FreePhysicalMemory)/1024)
 result['memPercent']=result['memFree'] * 100 /result['memTotal']
 #disk
 result['diskTotal'] = 0
 result['diskFree'] = 0
 for disk in wmiService.Win32_LogicalDisk(DriveType=3):
  result['diskTotal'] += int(disk.Size)
  result['diskFree'] += int(disk.FreeSpace)
 result['diskTotal'] = int(result['diskTotal']/1024/1024)
 result['diskFree'] = int(result['diskFree']/1024/1024)
 return result

def sys_version(): 
 c = wmi.WMI () 
 #获取操作系统版本 
 for sys in c.Win32_OperatingSystem(): 
  print ("Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber)
  print (sys.OSArchitecture.encode("UTF8"))#系统是32位还是64位的 
  print (sys.NumberOfProcesses) #当前系统运行的进程总数

def cpu_mem(): 
 c = wmi.WMI ()  
 #CPU类型和内存 
 for processor in c.Win32_Processor(): 
  #print "Processor ID: %s" % processor.DeviceID 
  print ("Process Name: %s" % processor.Name.strip() )
 for Memory in c.Win32_PhysicalMemory(): 
  print ("Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576))

def cpu_use(): 
 #5s取一次CPU的使用率 
 c = wmi.WMI() 
 while True: 
  for cpu in c.Win32_Processor(): 
    timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) 
    print ('%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage)) 
    time.sleep(5) 

def disk(): 
 c = wmi.WMI () 
 #获取硬盘分区 
 for physical_disk in c.Win32_DiskDrive (): 
  for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"): 
   for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"): 
    print (physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption)

 #获取硬盘使用百分情况 
 for disk in c.Win32_LogicalDisk (DriveType=3): 
  print (disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size)))

def network(): 
 c = wmi.WMI ()  
 #获取MAC和IP地址 
 for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): 
  print ("MAC: %s" % interface.MACAddress )
 for ip_address in interface.IPAddress: 
  print ("ip_add: %s" % ip_address )
 print

 #获取自启动程序的位置 
 for s in c.Win32_StartupCommand (): 
  print ("[%s] %s <%s>" % (s.Location.encode("UTF8"), s.Caption.encode("UTF8"), s.Command.encode("UTF8"))) 


 #获取当前运行的进程 
 for process in c.Win32_Process (): 
  print (process.ProcessId, process.Name)

def getAllProcessInfo(mywmi = None): 
 """取出全部进程的进程名,进程ID,内存(专有工作集), 工作集
 """ 
 allProcessList = []

 allProcess = mywmi.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process")
 #print (allProcess.count)
 for j in allProcess:
  #print j.Properties_("PercentPrivilegedTime").__int__()
  ##print j.Properties_("name").__str__()+" "+j.Properties_("IDProcess").__str__()+" "+j.Properties_("PercentPrivilegedTime").__str__()
  #for pro in j.Properties_:
  # print (pro.name)
  #break
  name = j.Properties_("name").__str__()
  if name != "_Total" and name !="Idle":
   pid = j.Properties_("IDProcess").__str__()
   PercentPrivilegedTime = j.Properties_("PercentPrivilegedTime").__int__()
   WorkingSetPrivate = j.Properties_("WorkingSetPrivate").__int__()/1024
   WorkingSet = j.Properties_("WorkingSet").__int__()/1024
   allProcessList.append([name, pid, WorkingSetPrivate, WorkingSet, PercentPrivilegedTime])

# allProcess = mywmi.ExecQuery("select * from Win32_Process")
# for i in allProcess:
#  Name = str(i.Properties_("Name"))
#  ProcessID = int(i.Properties_("ProcessID"))
#  WorkingSetSize = int(i.Properties_("WorkingSetSize"))/1024
#  #VirtualSize = int(i.Properties_("VirtualSize"))/1024
#  PeakWorkingSetSize = int(i.Properties_("PeakWorkingSetSize"))/1024
#  CreationDate = str(i.Properties_("CreationDate"))
#  allProcessList.append([Name, ProcessID, WorkingSetSize, PeakWorkingSetSize, CreationDate])

 return allProcessList

#def main(): 
 #sys_version() 
 #cpu_mem() 
 #disk() 
 #network() 
 #cpu_use()

if __name__ == '__main__': 
 #mywmi = GetObject("winmgmts:")
 mywmi = wmi.WMI()
 processInfoList = getAllProcessInfo(mywmi)
 processInfoList.sort(key=itemgetter(2), reverse=True)
 for processinfo in processInfoList:
  print(processinfo)

processinfo

import psutil,time 
from operator import itemgetter, attrgetter

def getProcessInfo(p): 
 """取出指定进程占用的进程名,进程ID,进程实际内存, 虚拟内存,CPU使用率 
 """ 
 try: 
  cpu = int(p.cpu_percent(interval=0)) 
  memory = p.memory_info() 
  rss = memory.rss/1024
  vms = memory.vms/1024
  name = p.name() 
  pid = p.pid 
 except psutil.Error: 
  name = "Closed_Process" 
  pid = 0 
  rss = 0 
  vms = 0 
  cpu = 0 
 #return [name.upper(), pid, rss, vms] 
 return [name, pid, vms, rss, cpu] 

def getAllProcessInfo(): 
 """取出全部进程的进程名,进程ID,进程实际内存, 虚拟内存,CPU使用率 
 """ 
 instances = [] 
 all_processes = list(psutil.process_iter()) 
 for proc in all_processes: 
  proc.cpu_percent(interval=0) 
 #此处sleep1秒是取正确取出CPU使用率的重点 
 time.sleep(1) 
 for proc in all_processes: 
  instances.append(getProcessInfo(proc)) 
 return instances 


if __name__ == '__main__': 
 processInfoList = getAllProcessInfo()
 processInfoList.sort(key=itemgetter(2), reverse=True)
 for p in processInfoList:
  print(p)

logger

#logger.conf
###############################################
[loggers]
keys=root,example01,example02
[logger_root]
level=DEBUG
handlers=hand01,hand02
[logger_example01]
handlers=hand01,hand04
qualname=example01
propagate=0
[logger_example02]
handlers=hand01,hand03
qualname=example02
propagate=0
###############################################
[handlers]
keys=hand01,hand02,hand03,hand04
[handler_hand01]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stderr,)
[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form01
args=('myapp.log', 'a')
[handler_hand03]
class=handlers.RotatingFileHandler
level=INFO
formatter=form02
args=('myapp.log', 'a', 10*1024*1024, 5)
[handler_hand04]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=form01
args=('./logs/monitor.log', 'd', 1, 7)
###############################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_form02]
format=%(asctime)-12s: %(levelname)-8s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用python提取html文件中的特定数据的实现代码
Mar 24 Python
python实现dnspod自动更新dns解析的方法
Feb 14 Python
解读Django框架中的低层次缓存API
Jul 24 Python
Python中 Lambda表达式全面解析
Nov 28 Python
python实现外卖信息管理系统
Jan 11 Python
python实现音乐下载的统计
Jun 20 Python
Python中asyncio与aiohttp入门教程
Oct 16 Python
django富文本编辑器的实现示例
Apr 10 Python
Python+opencv+pyaudio实现带声音屏幕录制
Dec 23 Python
使用jupyter notebook直接打开.md格式的文件
Apr 10 Python
Python实现捕获异常发生的文件和具体行数
Apr 25 Python
拒绝盗图!教你怎么用python给图片加水印
Jun 04 Python
Python实现的微信好友数据分析功能示例
Jun 21 #Python
python skimage 连通性区域检测方法
Jun 21 #Python
python3实现windows下同名进程监控
Jun 21 #Python
python检测主机的连通性并记录到文件的实例
Jun 21 #Python
Python基于xlrd模块操作Excel的方法示例
Jun 21 #Python
python实现自动发送报警监控邮件
Jun 21 #Python
Python中list查询及所需时间计算操作示例
Jun 21 #Python
You might like
新手配置 PHP 调试环境(IIS+PHP+MYSQL)
2007/01/10 PHP
dhtmlxTree目录树增加右键菜单以及拖拽排序的实现方法
2013/04/26 PHP
在laravel中使用Symfony的Crawler组件分析HTML
2017/06/19 PHP
js中的window.open返回object的错误的解决方法
2009/08/15 Javascript
jQuery插件开发基础简单介绍
2013/01/07 Javascript
解析Javascript小括号“()”的多义性
2013/12/03 Javascript
Javascript浅谈之this
2013/12/17 Javascript
javascript函数重载解决方案分享
2014/02/19 Javascript
struts2+jquery组合验证注册用户是否存在
2014/04/30 Javascript
分享33个jQuery与CSS3实现的绚丽鼠标悬停效果
2014/12/15 Javascript
JavaScript通过字符串调用函数的实现方法
2015/03/18 Javascript
JavaScript必知必会(六) delete in instanceof
2016/06/08 Javascript
使用vue.js2.0 + ElementUI开发后台管理系统详细教程(二)
2017/01/21 Javascript
Vuejs 单文件组件实例详解
2018/02/09 Javascript
详解如何在Vue里建立长按指令
2018/08/20 Javascript
基于jquery实现的tab选项卡功能示例【附源码下载】
2019/06/10 jQuery
javascript执行上下文、变量对象实例分析
2020/04/25 Javascript
python监控网卡流量并使用graphite绘图的示例
2014/04/27 Python
python print 按逗号或空格分隔的方法
2018/05/02 Python
Python爬虫实现简单的爬取有道翻译功能示例
2018/07/13 Python
python3 打开外部程序及关闭的示例
2018/11/06 Python
浅析Python3中的对象垃圾收集机制
2019/06/06 Python
Python PyCharm如何进行断点调试
2019/07/05 Python
基于Python3.6中的OpenCV实现图片色彩空间的转换
2020/02/03 Python
Python socket服务常用操作代码实例
2020/06/22 Python
python3 kubernetes api的使用示例
2021/01/12 Python
Coltorti Boutique官网:来自意大利的设计师品牌买手店
2018/11/09 全球购物
有影响力的品牌之家:Our Social Collective
2019/06/08 全球购物
MaBelle玛贝尔香港官网:香港钻饰连锁店
2019/09/09 全球购物
中专毕业生自我鉴定范文
2013/11/09 职场文书
JAVA程序员自荐书
2014/01/30 职场文书
研修第一天随笔感言
2014/02/15 职场文书
商铺消防安全责任书
2014/07/29 职场文书
结婚保证书
2015/01/16 职场文书
职工宿舍管理制度
2015/08/05 职场文书
国际最新研究在陨石中发现DNA主要成分 或由陨石带来地球
2022/04/29 数码科技