Python is used to monitor linux performance and process consumption performance.

Source: Internet
Author: User
Tags snmp

Python is used to monitor linux performance and process consumption performance.

This article provides an example of how to monitor linux performance and process consumption performance using python. 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 ()

Linux Server integrity can be monitored Introduction How Can Linux servers monitor integrity?

Linux server performance monitoring is a very important task. server operation should provide the most effective system performance. When the server system performance suddenly falls below the average, the problem may be caused by the processes being executed, memory usage, disk performance, network traffic, and CPU pressure. In today's budget shortage, understanding how to optimize system performance is more important than ever.

To achieve this, you must fully understand your computer and network to find the real bottleneck. This article provides some basic tools to identify and handle some performance problems. The Linux version used is Red Hat Enterprise Linux 4. The procedure is: first check the status of the entire system, and then check the specific subsystem.

There are several methods for Linux server performance monitoring, each of which has its own advantages and disadvantages.

Use standard tools such as SNMP

Standard and non-standard tools can execute one or more collection, merge, and transfer phases, such as rstatd or SNMP tools. However, the information provided by standard rstat background programs is limited, slow speed and low efficiency.

Kernel module

Several system monitoring projects use kernel modules to access monitoring data. In general, this is a very effective way to collect system data. However, the problem with this method is that when there are other changes in the main internal core source, the Code consistency must be maintained. A kernel module may conflict with other kernel modules you want to use. In addition, you must obtain or apply for a module before using the monitoring system.

/Proc Virtual File System

/Proc Virtual File System is a fast and efficient method for executing system monitoring. The main disadvantage of using/proc is that you must keep the code analysis synchronized with the/proc file format changes. It turns out that the Linux kernel changes more frequently than the/proc file format, so the/proc Virtual File System has fewer problems than the kernel module. The method described in this article is based on the/proc Virtual File System.

I. Features of/proc file system

The Linux system provides administrators with an excellent way to change the kernel while the system is running, without the need to reboot the kernel system. This is achieved through the/proc Virtual File System. The/proc file virtual system is a mechanism used by the kernel and kernel module to send information to the process (so it is called/proc ). This pseudo file system allows you to interact with the internal data structure of the kernel to obtain useful information about the process. on the fly, you can change the settings (by changing the kernel parameters ). Unlike other file systems,/proc exists in the memory rather than on the hard disk. You can view the system information without restarting CMOS. This is one of the highlights of/proc.

Tip: the content of each Linux system varies depending on the hardware and software. The/proc virtual file system has three important directories: net, scsi, and sys. The Sys directory is writable and can be used to access or modify kernel parameters. net and scsi depend on Kernel configuration.
Reference: Shanghai Express Network

In linux, nmon has been installed. How can we use nmon to monitor specified processes? Not comprehensive information, too much content

Use nagios and cacti for monitoring. Nmon has never been used.

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.