This article mainly introduces how to obtain CPU and memory information from python. if you need it, you can refer to it and we all know that everything in linux is a file, there is a/proc directory under the linux/unix root directory. This/proc is a kernel and kernel module used to process) the mechanism for sending information (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) change Settings (by changing 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:
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.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
... ...
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 obtain the required information through python programming.
1. obtain 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 the following command:
The 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
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 result is as follows:
The code is as follows:
Liujl @ liujl-ThinkPad-Edge-E431 :~ /Mypython $ python meminfo. py
Total memory: 3593316 kB
Free memory: 2113712 kB