Python obtains linux system information

Source: Internet
Author: User

A script written in python that captures the main information of a linux system, mainly memory, hard disk, CPU, and other information.

Memory information/meminfo returns dict #! /Usr/bin/env python def memory_stat (): mem = {} f = open ("/proc/meminfo") lines = f. readlines () f. close () for line in lines: if len (line) <2: continue name = line. split (':') [0] var = line. split (':') [1]. split () [0] mem [name] = long (var) * 1024.0 mem ['memused'] = mem ['memtotal']-mem ['memfree']-mem ['buffers']-mem ['cached'] return mem CPU Information /cpuinfo returns list, each core is a dict #! /Usr/bin/env python def cpu_stat (): cpu = [] cpuinfo = {} f = open ("/proc/cpuinfo") lines = f. readlines () f. close () for line in lines: if line = 'N': cpu. append (cpuinfo) cpuinfo ={} if len (line) <2: continue name = line. split (':') [0]. rstrip () var = line. split (':') [1] cpuinfo [name] = var return cpu load information/loadavg return dict #! /Usr/bin/env python def load_stat (): loadavg = {} f = open ("/proc/loadavg") con = f. read (). split () f. close () loadavg ['lavg _ 1'] = con [0] loadavg ['lavg _ 5'] = con [1] loadavg ['lavg _ 15'] = con [2] loadavg ['nr '] = con [3] loadavg ['last _ pid'] = con [4] return loadavg running time/Uptime return dict #! /Usr/bin/env python def uptime_stat (): uptime = {} f = open ("/proc/uptime") con = f. read (). split () f. close () all_sec = float (con [0]) MINUTE, HOUR, DAY = 60, 3600, 86400 uptime ['day'] = int (all_sec/day) uptime ['hour'] = int (all_sec % DAY)/hour) uptime ['minute '] = int (all_sec % HOUR)/minute) uptime ['second'] = int (all_sec % MINUTE) uptime ['free rate'] = float (con [1])/float (con [0]) return uptim E. Obtain network card Traffic Information/proc/net/dev and return dict. Unit: byte #! /Usr/bin/env python def net_stat (): net = [] f = open ("/proc/net/dev") lines = f. readlines () f. close () for line in lines [2:]: con = line. split () "" intf = {} intf ['interface'] = con [0]. lstrip (":") intf ['receivebytes '] = int (con [1]) intf ['audio epackets'] = int (con [2]) intf ['receiveerrs '] = int (con [3]) intf ['incluedro'] = int (con [4]) intf ['receivefifo '] = int (con [5]) intf ['receiveframes'] = int (c On [6]) intf ['receivecompressed '] = int (con [7]) intf ['receivemulticast'] = int (con [8]) intf ['transmitbytes '] = int (con [9]) intf ['transmitpackets'] = int (con [10]) intf ['transmiterrs'] = int (con [11]) intf ['transmitdrop'] = int (con [12]) intf ['transmitfifo '] = int (con [13]) intf ['transmitframes'] = int (con [14]) intf ['transmitcompressed '] = int (con [15]) intf ['transmitmulticast'] = int (con [16]) "intf = Dict (zip ('interface', 'recevebytes ', 'incluepackets', 'receiveerrs ', 'inclueddrop', 'receivefifo', 'receiveframes ', 'receivecompressed ', 'receivemulticast ', 'transmitbytes', 'transmitpackets ', 'transmiterrs', 'transmitdrop', 'transmitfifo ', 'transmitframes', 'transmitcompressed ', 'authorization '), (con [0]. rstrip (":"), int (con [1]), int (con [2]), int (con [3]), int (con [4]), int (con [5]), int (con [6]), int (Con [7]), int (con [8]), int (con [9]), int (con [10]), int (con [11]), int (con [12]), int (con [13]), int (con [14]), int (con [15]), int (con [16]),) net. append (intf) return net disk space use built-in python built-in functions, return dict, unit: byte #! /Usr/bin/env python def disk_stat (): import OS hd ={} disk = OS. statvfs ("/") hd ['available'] = disk. f_bsize * disk. f_bavail hd ['capacity '] = disk. f_bsize * disk. f_blocks hd ['used'] = disk. f_bsize * disk. f_bfree return hd

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.