As you all know, Linux is all files, in the root directory of Linux/unix, there is a/proc directory, this/proc is a kernel and the kernel module is used to send information to processes (process) mechanism (so called "proc"), This pseudo file system allows you to interact with the internal data structure of the kernel, get useful information about the process, and change the settings (by changing the kernel parameters) in the runtime (on the fly). Unlike other file systems,/proc is present in memory rather than on the hard disk. The information provided by the proc file system is as follows:
• Process information: Any process in the system that has a process ID with the same name in the subdirectory of proc, can find CmdLine, mem, root, Stat, STATM, and status. Some information is visible only to superuser, such as the process root directory. Each process that contains information about existing processes has a few specialized links available, and any process in the system has a separate link to process information that is useful for obtaining 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 consumption, disk space, memory swap, interrupt, etc.
CPU Information: Use the/proc/cpuinfo file to obtain the current accurate information of the central processing Unit.
• Load Information:/proc/loadavg file contains system load information.
• System memory Information: The/proc/meminfo file contains detailed information about the system memory, showing the number of physical memory, the amount of available swap space, and the amount of free memory.
This way, you can view the information through the cat command:
Copy Code code 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
。。。 。。。
Copy Code code 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
。。 。 。。。
Here's how to get the information you need in Python programming.
1, get the information of the CPU
Copy Code code 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:
Copy Code code 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
Copy Code code 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:
Copy Code code as follows:
liujl@liujl-thinkpad-edge-e431:~/mypython$ python meminfo.py
Total memory:3593316 KB
Free memory:2113712 KB