python實(shí)現(xiàn)監(jiān)控linux性能及進(jìn)程消耗性能的方法
更新時(shí)間:2014年07月25日 09:22:08 投稿:shichen2014
這篇文章主要介紹了python實(shí)現(xiàn)監(jiān)控linux性能及進(jìn)程消耗性能的方法,需要的朋友可以參考下
本文以實(shí)例形式實(shí)現(xiàn)了python監(jiān)控linux性能以及進(jìn)程消耗性能的方法,具體實(shí)現(xiàn)代碼如下:
# -*- 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 的進(jìn)程號(hào)
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()
相關(guān)文章
對python GUI實(shí)現(xiàn)完美進(jìn)度條的示例詳解
今天小編就為大家分享一篇對python GUI實(shí)現(xiàn)完美進(jìn)度條的示例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
pytorch創(chuàng)建tensor函數(shù)詳情
這篇文章主要介紹了pytorch創(chuàng)建tensor函數(shù)詳情,文章圍繞tensor函數(shù)的相關(guān)自來哦展開詳細(xì)內(nèi)容的介紹,需要的小伙伴可以參考一下,希望對你有所幫助2022-03-03
python中的內(nèi)置函數(shù)max()和min()及mas()函數(shù)的高級(jí)用法
這篇文章主要介紹了python中的內(nèi)置函數(shù)max()和min()的相關(guān)知識(shí)及python中內(nèi)置函數(shù)max()的高級(jí)用法,需要的朋友可以參考下2018-03-03
Python?requests用法和django后臺(tái)處理詳解
這篇文章主要給大家介紹了關(guān)于Python中requests用法和django后臺(tái)處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
python中re模塊知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python中re模塊知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01
python?pandas數(shù)據(jù)處理教程之合并與拼接
在實(shí)際處理數(shù)據(jù)業(yè)務(wù)需求中,我們經(jīng)常會(huì)遇到這樣的需求,將多個(gè)表連接起來再進(jìn)行數(shù)據(jù)的處理和分析,類似SQL中的連接查詢功能,下面這篇文章主要給大家介紹了關(guān)于python?pandas數(shù)據(jù)處理教程之合并與拼接的相關(guān)資料,需要的朋友可以參考下2022-02-02

