How to Use python to obtain CPU and memory information (linux)

Source: Internet
Author: User

Everyone knows that everything in linux is a file. There is a/proc directory under the root directory of linux/unix, this/proc 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 interaction 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 instead of the hard disk. The proc file system provides the following information:

• Process Information: any process in the system has a process ID with the same name in the proc sub-directory, which can be found in cmdline, mem, root, stat, statm, and status. Some information is only visible to super users, such as the process root directory. Each process that contains existing process information has some available dedicated links. Any process in the system has a separate self-link pointing to process information, the command line information is obtained from the process.
• System Information: If you need to know the entire system information, you can also obtain it from/proc/stat, including CPU usage, disk space, memory swap, and interruption.
• CPU information: the current accurate information of the central processor can be obtained using the/proc/CPUinfo file.
• Load information: The/proc/loadavg file contains the system load information.
• System memory information: The/proc/meminfo file contains detailed information about the system memory, including the number of physical memory, the number of available swap space, and the number of idle memory.

In this way, you can use the cat command to view the relevant information:

Copy codeThe Code is as follows:
Liujl @ liujl-ThinkPad-Edge-E431 :~ /Mybash $ cat/proc/cpuinfo
Processor: 0
Vendor_id: GenuineIntel
Cpu family: 6
Model: 58
Model name: Intel (R) Core (TM) i5-3230M CPU @ 2.60 GHz
Stepping: 9
Microcode: 0x15
Cpu MHz: 1200.000
Cache size: 3072 KB
Physical id: 0
Siblings: 4
Core id: 0
Cpu cores: 2
Apicid: 0

... ...
Copy codeThe Code is as follows:
Liujl @ liujl-ThinkPad-Edge-E431 :~ /Mybash $ cat/proc/meminfo
MemTotal: 3593316 kB
MemFree: 2145916 kB
Buffers': 93372 kB
Cached: 684864 kB
SwapCached: 0 kB
Active: 706564 kB
Inactive: 554052 kB
Active (anon): 483996 kB
Inactive (anon): 178388 kB
Active (file): 222568 kB
Inactive (file): 375664 kB

.. . ...

The following describes how to obtain the required information through python programming.

1. Obtain cpu Information

Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Filename: CPU1.py

From _ future _ import print_function
From collections import OrderedDict
Import pprint

Def CPUinfo ():
'''Return the info in/proc/cpuinfo
As a dirctionary in the follow format:
CPU_info ['proc0'] = {...}
CPU_info ['proc1 '] = {...}
'''

CPUinfo = OrderedDict ()
Procinfo = OrderedDict ()

Nprocs = 0
With open ('/proc/cpuinfo') as f:
For line in f:
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

If _ name _ = '_ main __':
CPUinfo = CPUinfo ()
For processor in CPUinfo. keys ():
Print ('cpuinfo [{0}] = {1} '. format (processor, CPUinfo [processor] ['model name'])

Run the following command:
Copy codeThe Code is as follows:
Liujl @ liujl-ThinkPad-Edge-E431 :~ /Mypython $ python CPU1.py
CPUinfo [proc0] = Intel (R) Core (TM) i5-3230M CPU @ 2.60 GHz
CPUinfo [proc1] = Intel (R) Core (TM) i5-3230M CPU @ 2.60 GHz
CPUinfo [proc2] = Intel (R) Core (TM) i5-3230M CPU @ 2.60 GHz
CPUinfo [proc3] = Intel (R) Core (TM) i5-3230M CPU @ 2.60 GHz

2. Obtain memory information
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Filename: meminfo. py

From _ future _ import print_function
From collections import OrderedDict

Def meminfo ():
'''Return the info of/proc/meminfo
As a dictionary
'''
Meminfo = OrderedDict ()

With open ('/proc/meminfo') as f:
For line in f:
Meminfo [line. split (':') [0] = line. split (':') [1]. strip ()
Return meminfo


If _ name _ = '_ main __':
Meminfo = meminfo ()

Print ("Total memory: {0}". format (meminfo ['memtotal'])
Print ("Free memory: {0}". format (meminfo ['memfree'])

The result is as follows:

Copy codeThe Code is as follows:
Liujl @ liujl-ThinkPad-Edge-E431 :~ /Mypython $ python meminfo. py
Total memory: 3593316 kB
Free memory: 2113712 kB

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.