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 that has a process ID with 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.
Load information: The/proc/loadavg file contains system payload information.
? System memory Information: The/proc/meminfo file contains details about the system memory, which shows the amount of physical memory, the number of free swap space, and the amount of free memory.
This allows you to view information through the cat command:
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
——————————————————————
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 information about the requirements in Python programming:#!/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: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.60GHzGet memory Information:#! /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:
Total memory:3593316 KB
Free memory:2113712 KB
The idea and implementation of using Python to get CPU and memory information (Linux system)