python实现简易内存监控

本例主要功能:每隔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

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

(0)

相关推荐

  • Python实现数据可视化看如何监控你的爬虫状态【推荐】

    今天主要是来说一下怎么可视化来监控你的爬虫的状态. 相信大家在跑爬虫的过程中,也会好奇自己养的爬虫一分钟可以爬多少页面,多大的数据量,当然查询的方式多种多样.今天我来讲一种可视化的方法. 关于爬虫数据在mongodb里的版本我写了一个可以热更新配置的版本,即添加了新的爬虫配置以后,不用重启程序,即可获取刚刚添加的爬虫的状态数据. 1.成品图 这个是监控服务器网速的最后成果,显示的是下载与上传的网速,单位为M.爬虫的原理都是一样的,只不过将数据存到InfluxDB的方式不一样而已, 如下图. 可以

  • Python实现一个服务器监听多个客户端请求

    学习Python网络通信的时候发现书上只有一个服务端对应一个客户端的情形,于是自己想自己动手实现一个服务端响应多个客户端. 首先建立服务器的socket来监听客户端的请求: tcpSerSock=socket(AF_INET,SOCK_STREAM) tcpSerSock.bind(ADDR) tcpSerSock.listen(5) 这样服务器的监听socket就建好了. 接下来的思路是,如果要监听多个客户端,则 tcpSerSock.accept() #(accept()是阻塞式的) 必须放

  • 利用Prometheus与Grafana对Mysql服务器的性能监控详解

    概述 Prometheus是一个开源的服务监控系统,它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上.它提供了一个简单的网页界面.一个功能强大的查询语言以及HTTP接口等等.Prometheus通过安装在远程机器上的exporter来收集监控数据,这里用到了以下两个exporter: node_exporter – 用于机器系统数据 mysqld_exporter – 用于Mysql服务器数据 Grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化.它内置了

  • Python文件监听工具pyinotify与watchdog实例

    pyinotify库 支持的监控事件 @cvar IN_ACCESS: File was accessed. @type IN_ACCESS: int @cvar IN_MODIFY: File was modified. @type IN_MODIFY: int @cvar IN_ATTRIB: Metadata changed. @type IN_ATTRIB: int @cvar IN_CLOSE_WRITE: Writtable file was closed. @type IN_CLO

  • 使用Python编写Prometheus监控的方法

    要使用python编写Prometheus监控,需要你先开启Prometheus集群.可以参考//www.jb51.net/article/148895.htm 安装.在python中实现服务器端.在Prometheus中配置请求网址,Prometheus会定期向该网址发起申请获取你想要返回的数据. 使用Python和Flask编写Prometheus监控 Installation pip install flask pip install prometheus_client Metrics P

  • python实现内存监控系统

    本文实例为大家分享了python实现内存监控系统的具体代码,供大家参考,具体内容如下 思路:通过系统命令或操作系统文件获取到内存信息(linux 内存信息存在/proc/meminfo文件中,mac os 通过命令vm_stat命令可以查看) 并将获取到信息保存到数据库中,通过web将数据实时的展示出来.(获取数据-展示数据) 1.后台数据采集(获取数据) import subprocess import re import MySQLdb as mysql import time import

  • Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法

    Python编写从ZabbixAPI获取信息 此脚本用Python3.6执行是OK的. # -*- coding: utf-8 -*- import json import urllib.request, urllib.error, urllib.parse class ZabbixAPI: def __init__(self): self.__url = 'http://192.168.56.102/zabbix/api_jsonrpc.php' self.__user = 'admin' s

  • Python利用pyHook实现监听用户鼠标与键盘事件

    本文以一段简单的监听鼠标.键盘事件的程序,实现获取用户的输入(比如登录某些网站的账号.密码)的功能.经测试,对于一台"裸奔"的电脑,完全能获取到用户输入的任何信息:但是如果安装了杀毒软件,就够呛了.具体实现方法如下: 一.代码部分:获取用户输入信息,并与截图一起保存到XX目录下 # -*- coding: utf-8 -*- # import pythoncom import pyHook import time import socket from PIL import ImageG

  • 使用python编写监听端

    本文实例为大家分享了python编写监听端的具体代码,供大家参考,具体内容如下 import socket import time import sys import string import struct import errno import binascii #Definition ser_ip = 'localhost' ser_port = 15001 HEADER_LISTENER = "IIII" split_time = 4 class TcpClient: def

  • python脚本监控Tomcat服务器的方法

    文章出处:https://blog.csdn.net/sdksdk0/article/details/80933444 作者:朱培      ID:sdksdk0     -------------------------------------------------------------------------------------------- 对于最近的开发环境,偶尔会有挂掉的现象发生,然而并没有及时发现,下载需要添加一个监控功能,当服务挂掉的时候需要有邮件提醒,同时我们的系统每天晚

  • 使用Python监控文件内容变化代码实例

    利用seek监控文件内容,并打印出变化内容: #/usr/bin/env python #-*- coding=utf-8 -*- pos = 0 while True: con = open("a.txt") if pos != 0: con.seek(pos,0) while True: line = con.readline() if line.strip(): print line.strip() pos = pos + len(line) if not line.strip(

  • Python实现监控键盘鼠标操作示例【基于pyHook与pythoncom模块】

    本文实例讲述了Python实现监控键盘鼠标操作.分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- import pythoncom import pyHook import time def onMouseEvent(event): "处理鼠标事件" fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n') fobj.writelines("Current Time:%s\

随机推荐