Python-Python tutorial

Source: Internet
Author: User
This article mainly introduces how to monitor linux performance and process consumption performance using python. For more information, see the following example, the specific implementation code is as follows:

#-*-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 ------ bytes cpu: % s' % (cpu, mem, line) f. flush (); def process_info (): # obtain the Process Number of drm_processes, textlist = OS. popen ('top-bcn 1-p 12023 '). readlines () line = ''for t in textlist: if t. find ('20140901'): line = t line = line. split ('') # return line [15] if _ name __= = '_ main _' based on your needs: 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 % '% (cpu used * 100) print line cpu =' %. 2f % '% (cpuused * 100) logSysInfo (cpu, format (mi ['memfree']), line) counter t KeyboardInterrupt, e: print ("\ ncpumonit exited ") f. close () f. close ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.