国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現監控linux性能及進程消耗性能的方法

2020-02-23 05:35:28
字體:
來源:轉載
供稿:網友

本文以實例形式實現了python監控linux性能以及進程消耗性能的方法,具體實現代碼如下:

# -*- coding: utf-8 -*-"""Created on Tue Jun 10 10:20:13 2014@author: lifeix"""from collections import OrderedDictimport timeimport osdef 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 CPUinfodef 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 meminfof = 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()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 同江市| 平度市| 新河县| 静乐县| 永康市| 惠州市| 无棣县| 乌鲁木齐市| 保亭| 荣昌县| 光泽县| 会泽县| 永吉县| 临武县| 博乐市| 长岭县| 富阳市| 呼伦贝尔市| 平顶山市| 平凉市| 旬阳县| 玛多县| 基隆市| 英德市| 灌云县| 和静县| 仙居县| 锦屏县| 绍兴市| 忻城县| 辽源市| 万宁市| 柳江县| 民县| 大荔县| 高碑店市| 乌拉特中旗| 新平| 敖汉旗| 墨竹工卡县| 原阳县|