当前位置: 首页 > 编程笔记 >

python实现监控linux性能及进程消耗性能的方法

郦良才
2023-03-14
本文向大家介绍python实现监控linux性能及进程消耗性能的方法,包括了python实现监控linux性能及进程消耗性能的方法的使用技巧和注意事项,需要的朋友参考一下

本文以实例形式实现了python监控linux性能以及进程消耗性能的方法,具体实现代码如下:

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 10 10:20:13 2014

@author: lifeix
"""

from collections import OrderedDict
import time
import os

def cpuinfo():
  lines = open('/proc/stat').readlines()
  for line in lines:
    ln = line.split()
    if ln[0].startswith('cpu'):
      return ln;
  return []
W = cpuinfo()
one_cpuTotal=long(W[1])+long(W[2])+long(W[3])+long(W[4])+long(W[5])+long(W[6])+long(W[7])
one_cpuused=long(W[1])+long(W[2])+long(W[3])

def CPUinfo():
  ''' Return the information in /proc/CPUinfo
  as a dictionary in the following format:
  CPU_info['proc0']={...}
  CPU_info['proc1']={...}
  '''
  CPUinfo=OrderedDict()
  procinfo=OrderedDict()

  nprocs = 0
  f = open('/proc/cpuinfo')
  for line in f.readlines():
    if not line.strip():
      # end of one processor
      CPUinfo['proc%s' % nprocs] = procinfo
      nprocs=nprocs+1
      # Reset
      procinfo=OrderedDict()
    else:
      if len(line.split(':')) == 2:
        procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
      else:
        procinfo[line.split(':')[0].strip()] = ''
  return CPUinfo

def meminfo():
  ''' Return the information in /proc/meminfo
  as a dictionary '''
  meminfo=OrderedDict()

  f = open('/proc/meminfo')
  for line in f.readlines():
    meminfo[line.split(':')[0]] = line.split(':')[1].strip()
  return meminfo

f = open("sysinfo.log",'a')
def logSysInfo(cpu,mem,line):
  f.write('\ncpu:%s -------mem: %s------mongocpu:%s'%(cpu,mem,line))
  f.flush();

def process_info():
  #获取drm_processes 的进程号
  textlist = os.popen('top -bcn 1 -p 12023').readlines()
  line = ''
  for t in textlist:
    if t.find('12023'):
      line = t
  line = line.split(' ')
  #此处的值按照自己的需求去取
  return line[15]
if __name__=='__main__':
  CPUinfo = CPUinfo()
  for processor in CPUinfo.keys():
    print(CPUinfo[processor]['model name'])
    f.write("cpu:%s"%CPUinfo[processor]['model name'])
  #meminfo = meminfo()
  #print('Total memory: {0}'.format(meminfo['MemTotal'])) 

  try:
    while True:
      line = process_info()
      time.sleep(2)
      mi = meminfo()
      print('Free memory: {0}'.format(mi['MemFree']))
      W = cpuinfo()
      two_cpuTotal=long(W[1])+long(W[2])+long(W[3])+long(W[4])+long(W[5])+long(W[6])+long(W[7])
      two_cpuused=long(W[1])+long(W[2])+long(W[3])
      cpuused=float(two_cpuused-one_cpuused)/(two_cpuTotal-one_cpuTotal)
      print ('%.2f%%'%(cpuused*100))
      print line
      cpu = '%.2f%%'%(cpuused*100)
      logSysInfo(cpu,format(mi['MemFree']),line)
  except KeyboardInterrupt, e:
    print ("\ncpumonit exited")
    f.close()
f.close()
 类似资料:
  • "性能监控"整合了Threejs的监控组件stats,如果你对这个组件感兴趣,你可以从它的 github 仓库中了解更多细节。如果你想要在gio场景中显示这个组件,你可以使用 enableStats() API,这个性能监控面板默认会显示在左上角。

  • 本文向大家介绍MongoDB性能优化及监控,包括了MongoDB性能优化及监控的使用技巧和注意事项,需要的朋友参考一下 MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。 一、索引 MongoDB 提供了多样性

  • 目录 性能监控 监控CPU 查询内存 查询页面交换 查询硬盘使用 综合应用 总结 附录 在使用操作系统的过程中,我们经常需要查看当前的性能如何,需要了解CPU、内存和硬盘的使用情况;本节介绍的这几个工具能满足日常工作要求; 6.1. 监控CPU 查看CPU使用率 - sar -u   eg: - sar -u 1 2 [/home/weber#]sar -u 1 2 Linux 2.6.35-22

  • Ganglia监控Apache Haoop集群部署文档&参数表一览(最全面)

  • 本文向大家介绍python实现可视化动态CPU性能监控,包括了python实现可视化动态CPU性能监控的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python可视化动态CPU性能监控的具体代码,供大家参考,具体内容如下 打算开发web性能监控,以后会去学js,现在用matp来补救下,在官网有此类模板,花了一点时间修改了下,有兴趣的可以去官网看看。 基于matplotoilb和ps

  • 本文向大家介绍Linux性能监控工具nmon安装及使用教程解析,包括了Linux性能监控工具nmon安装及使用教程解析的使用技巧和注意事项,需要的朋友参考一下 一、nmon简介 Nmon (Nigel's Monitor)是由IBM 提供、免费监控 AIX 系统与 Linux 系统资源的工具。该工具可将服务器系统资源耗用情况收集起来并输出一个特定的文件,并可利用 excel 分析工具(nmon a