The idea and implementation of using Python to get CPU and memory information (Linux system)

Source: Internet
Author: User
As we all know, everything in Linux is a file, in the root directory of Linux/unix, there is a/proc directory, this/proc is a kernel and kernel module used to send information to the process (processes) mechanism (so called "/proc"), This pseudo file system allows interaction with the kernel's internal data structures to obtain useful information about the process, changing the settings (by changing the kernel parameters) in the run (on the fly). Unlike other file systems,/proc exists in memory and not on the hard disk. The information provided by the proc file system is as follows:

• Process information: Any process in the system, with a process ID of the same name in the proc subdirectory, can find CmdLine, mem, root, Stat, STATM, and status. Some information is only visible to the superuser, such as the process root directory. Each process that contains existing process information alone has some specialized links available, and any process in the system has a separate self-link pointing to the process information, which is useful for getting command-line information from the process.
• System Information: If you need to know the entire system information can also be obtained from the/proc/stat, including CPU usage, disk space, memory swap, interrupt and so on.
CPU Information: Use the/proc/cpuinfo file to obtain the current accurate information of the CPU.
• Payload information: The/proc/loadavg file contains system payload information.
• System memory Information: The/proc/meminfo file contains detailed information about the system memory, which shows the amount of physical memory, the number of available swap spaces, and the amount of free memory.

This allows you to view information through the cat command:

The 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.60GHz
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

。。。 。。。

The 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 get information about requirements in Python programming.

1. Get CPU Information

The 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 as follows:

The code is as follows:


liujl@liujl-thinkpad-edge-e431:~/mypython$ python cpu1.py
Cpuinfo[proc0]=intel (R) Core (TM) i5-3230m CPU @ 2.60GHz
Cpuinfo[proc1]=intel (R) Core (TM) i5-3230m CPU @ 2.60GHz
Cpuinfo[proc2]=intel (R) Core (TM) i5-3230m CPU @ 2.60GHz
Cpuinfo[proc3]=intel (R) Core (TM) i5-3230m CPU @ 2.60GHz

2. Get Memory information

The 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 results are as follows:

The code is as follows:


liujl@liujl-thinkpad-edge-e431:~/mypython$ python meminfo.py
Total memory:3593316 KB
Free memory:2113712 KB

  • Related Article

    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.