Python3 Getting Linux System Information

Source: Internet
Author: User

System Information
Import Platformplatform.uname () Platform.architecture ()

CPU Information

The/proc/cpuinfo file contains information about the system processor unit.

#!/usr/bin/env/python "" "/proc/cpuinfo as a python dict" "from __future__ Import print_functionfrom collections Import Or  Dereddictimport pprintdef 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 I n F:if not Line.strip (): # End of one processor cpuinfo[' proc%s '% nprocs] = Pro                Cinfo 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 cpuinfoif __name_ _== ' __main__ ': Cpuinfo = Cpuinfo () print (Cpuinfo) for processor in CpuinFo.keys (): Print (cpuinfo[processor][' model name ') 

Memory Information

File/proc/meminfo system memory information

#!/usr/bin/env pythonfrom __future__ Import print_functionfrom Collections import Ordereddictdef 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 Meminfoif __name__== ' __main__ ':    print (Meminfo ())    meminfo = Meminfo ()    print (' Total memory: {0} '. Format (meminfo[' Memtotal '))    print (' free memory: {0} '. Format (meminfo[' Memfree '))

Network Statistics Information

/proc/net/dev file

#!/usr/bin/env pythonfrom __future__ Import print_functionfrom Collections import Namedtupledef Netdevs (): "" "     RX and TX bytes for each of the network devices "" "with    open ('/proc/net/dev ') as f:        net_dump = F.readlin ES ()    device_data={}    data = namedtuple (' Data ', [' Rx ', ' TX ']) for line in    net_dump[2:]: line        = Line.split (': ')        If Line[0].strip ()! = ' lo ':            device_data[line[0].strip ()] = data (float (line[1].split () [0])/(1024.0*1024.0),                                                Float (line[1].split () [8])/(1024.0*1024.0))       return device_dataif __name__== ' __main__ ':    print (Netdevs ())    Netdevs = Netdevs () for    Dev in Netdevs.keys ():        

Process Information

The/proc directory contains all the running process directories. The names of these directories are the same as the process identifiers. So, if you traverse the directories in the/proc directory that use numbers as their names, you get a list of all the processes that are now running.

#!/usr/bin/env python "" "List of all process IDs currently active" "" from __future__ import print_functionimport osdef proce Ss_list ():    PIDs = [] for    subdir in Os.listdir ('/proc '):        if Subdir.isdigit ():            pids.append (SubDir)    return pidsif __name__== ' __main__ ':    print (Process_list ())    PIDs = process_list ()    print (' Total Number of running processes:: {0} '. Format (len (PIDs)))

Block Devices

The block devices in the system can be found from the/sys/block directory. Therefore, there may be/SYS/BLOCK/SDA,/sys/block/sdb and so on such a directory.

#!/usr/bin/env python "" "Read block device data from Sysfs" "" from __future__ import print_functionimport globimport reimpo RT os# ADD Any other device pattern to read Fromdev_pattern = [' sd.* ', ' mmcblk* ']def size (device):    nr_sectors = open (de Vice+ '/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):                print (' device:: {0}, Size:: {1} GiB '. Format (device , size (device))) if __name__== ' __main__ ':    Detect_devs ()

Reference: Http://www.oschina.net/translate/linux-system-mining-with-python

Python3 Getting Linux System Information

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.