Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239998
  • 博文数量: 49
  • 博客积分: 246
  • 博客等级: 二等列兵
  • 技术积分: 1034
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-02 13:18
文章分类

全部博文(49)

分类: 系统运维

2015-07-13 15:32:50


点击(此处)折叠或打开

  1. # -*- coding: utf-8 -*-

  2. from __future__ import division
  3. import commands
  4. import socket

  5. def server_status(hostname):
  6.     a,disk = commands.getstatusoutput("fdisk -l|grep GB|awk '{print $2,$3,$4}' |tr '\n' ' '")
  7.     cpu,processor = commands.getstatusoutput("grep -c 'processor' /proc/cpuinfo")

  8.     Status = { "Hostname":hostname ,
  9.          "CPU_process":processor,
  10.          "Disk":disk
  11.               }
  12.     print Status

  13. def mem_stat():
  14.         mem = {}
  15.         f = open('/proc/meminfo')
  16.         lines = f.readlines()
  17.         f.close()
  18.         for line in lines:
  19.                 name = line.split(':')[0]
  20.                 var = line.split(':')[1].split()[0]
  21.                 mem[name] = long(var)
  22.         Mem = ((mem['MemFree'] + mem['Buffers'] + mem['Cached']) / mem['MemTotal']) * 100
  23.     a,MemTotal = commands.getstatusoutput("awk 'NR==1{print $2}' /proc/meminfo")
  24.         free = ('%0.2f%%'%Mem)
  25.     mem = { "Mem_free":free,"Mem_Total":MemTotal}
  26.     print mem

  27. def cpu_load():
  28.     loadavg = {}
  29.     f = open("/proc/loadavg","r")
  30.         con = f.read().split()
  31.         f.close()
  32.         loadavg['lavg_1']=con[0]
  33.         loadavg['lavg_5']=con[1]
  34.         loadavg['lavg_15']=con[2]
  35.         loadavg['nr']=con[3]
  36.         loadavg['last_pid']=con[4]
  37.         print loadavg

  38. def io():
  39.     a,iowait = commands.getstatusoutput("iostat|awk 'NR==4{print $4}'")
  40.     iostat = { "iowait":iowait }

  41.     print iostat
  42.     
  43. if __name__ == '__main__':
  44.     server_status(socket.gethostname())
  45.     mem_stat()
  46.     cpu_load()
  47.     io()

阅读(2672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~