• 热门专题

python编程统计系统硬件的负载信息

作者:峰云,就她了。  发布日期:2012-11-03 09:27:58
Tag标签:python编程  系统硬件  负载信息  
  • #!/usr/bin/env python   
    import os 
    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 
    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 
        mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached'] 
        return mem 
     
    def disk_stat(): 
        import os 
        hd={} 
        disk = os.statvfs("/") 
    #    print disk 
        hd['available'] = disk.f_bsize * disk.f_bavail / 1024000 
        hd['capacity'] = disk.f_bsize * disk.f_blocks /1024000 
        hd['used'] = disk.f_bsize * disk.f_bfree /1024000 
        return hd 
    #print disk_stat() 
     
    print "mem'used" ,  memory_stat()['Buffers'] 
    print "loadavg",load_stat()['lavg_15'] 
    print "hd's used",disk_stat()['used'] 
     
     
     
    def lsof_stat(): 
        passbin= {} 
        cmd = 'cat /etc/passwd' 
        textlist = os.popen(cmd).readlines() 
     
        for line in textlist: 
    #        if len(line) < 2: continue 
    #        if len(line) < 2: continue 
            if line == '\n': 
                name = line.split(':')[0] 
               
                bin = line.split(':')[6] 
                passbin[name] = bin 
        return passbin 
    print lsof_stat() 
    
    

     

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规