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

yipeiwu_com5年前Python基础

本文以实例形式实现了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()

相关文章

python 图像平移和旋转的实例

如下所示: import cv2 import math import numpy as np def move(img): height, width, channels = i...

python代码打印100-999之间的回文数示例

python代码打印100-999之间的回文数示例

打印100-999之间的回文数(即百位和个位的数字相等),并每10个打印一行 i = 100 x = 0 # 使用计数器,每10个换行打印 while i <= 999:...

Python使用pyserial进行串口通信的实例

安装pyserial pip install pyserial 查看可用的端口 # coding:utf-8 import serial.tools.list_ports...

在Python中处理字符串之ljust()方法的使用简介

 ljust()方法返回字符串左对齐的字符串长度宽度。填充是通过使用指定的fillchar(默认为空格)。如果宽度小于len(s)返回原始字符串。 语法 以下是ljust()方...

pytorch索引查找 index_select的例子

index_select anchor_w = self.FloatTensor(self.scaled_anchors).index_select(1, self.LongTensor...