注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

zjcjack的博客

 
 
 

日志

 
 

python获取linux的系统信息  

2012-06-13 21:55:10|  分类: python |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
  • 内存信息 / meminfo
    返回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信息 / cpuinfo
    返回list,每核心一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
  • 负载信息 / loadavg
    返回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
  • 运转时间 / Uptime
    返回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 uptime
  • 获取网卡流量信息 /proc/net/dev
    返回dict,单位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['ReceivePackets'] = int(con[2])         intf['ReceiveErrs'] = int(con[3])         intf['ReceiveDrop'] = int(con[4])         intf['ReceiveFifo'] = int(con[5])         intf['ReceiveFrames'] = int(con[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','ReceiveBytes','ReceivePackets',                   'ReceiveErrs','ReceiveDrop','ReceiveFifo',                   'ReceiveFrames','ReceiveCompressed','ReceiveMulticast',                   'TransmitBytes','TransmitPackets','TransmitErrs',                   'TransmitDrop', 'TransmitFifo','TransmitFrames',                   'TransmitCompressed','TransmitMulticast' ),                 ( 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
  • 磁盘空间使用
    使用内置python内置函数,返回dict,单位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
  评论这张
 
阅读(626)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017