首页 > 代码库 > python3 获取Linux系统信息
python3 获取Linux系统信息
系统信息
import platform platform.uname() platform.architecture()
CPU信息
/proc/cpuinfo文件包含了系统处理器单元的信息。
#!/usr/bin/env/ python """ /proc/cpuinfo as a Python dict """ from __future__ import print_function from collections import OrderedDict import pprint def cpuinfo(): """ Return the information in /proc/cpuinfo as a dictionary in the following 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() print(cpuinfo) for processor in cpuinfo.keys(): print(cpuinfo[processor][‘model name‘])
内存信息
文件/proc/meminfo系统内存的信息
#!/usr/bin/env python from __future__ import print_function from collections import OrderedDict def meminfo(): """ Return the information in /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__‘: print(meminfo()) meminfo = meminfo() print(‘Total memory: {0}‘.format(meminfo[‘MemTotal‘])) print(‘Free memory: {0}‘.format(meminfo[‘MemFree‘]))
网络统计信息
/proc/net/dev文件
#!/usr/bin/env python from __future__ import print_function from collections import namedtuple def netdevs(): """ RX and TX bytes for each of the network devices """ with open(‘/proc/net/dev‘) as f: net_dump = f.readlines() device_data=http://www.mamicode.com/{}>
进程信息
/proc目录包含了所有正运行的进程目录。这些目录的名字和进程的标识符是一样的。所以,如果你遍历/proc目录下那些使用数字作为它们的名字的目录,你就会获得所有现在正在运行的进程列表。
#!/usr/bin/env python """ List of all process IDs currently active """ from __future__ import print_function import os def process_list(): pids = [] for subdir in os.listdir(‘/proc‘): if subdir.isdigit(): pids.append(subdir) return pids if __name__==‘__main__‘: print(process_list()) pids = process_list() print(‘Total number of running processes:: {0}‘.format(len(pids)))
块设备
系统中的块设备可以从/sys/block目录中找到。因此可能会有/sys/block/sda、/sys/block/sdb等这样的目录。
#!/usr/bin/env python """ Read block device data from sysfs """ from __future__ import print_function import glob import re import os # Add any other device pattern to read from dev_pattern = [‘sd.*‘,‘mmcblk*‘] def size(device): nr_sectors = open(device+‘/size‘).read().rstrip(‘\n‘) sect_size = open(device+‘/queue/hw_sector_size‘).read().rstrip(‘\n‘) # The sect_size is in bytes, so we convert it to GiB and then send it back return (float(nr_sectors)*float(sect_size))/(1024.0*1024.0*1024.0) def detect_devs(): for device in glob.glob(‘/sys/block/*‘): for pattern in dev_pattern: if re.compile(pattern).match(os.path.basename(device)): print(‘Device:: {0}, Size:: {1} GiB‘.format(device, size(device))) if __name__==‘__main__‘: detect_devs()
参考:http://www.oschina.net/translate/linux-system-mining-with-python
python3 获取Linux系统信息
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。