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

全部博文(49)

分类: 系统运维

2015-11-19 14:13:15


点击(此处)折叠或打开

  1. # coding: utf-8
  2. #by_wangyi
  3. #by: QQ917611
  4. import psutil
  5. import time
  6. import os
  7. import sys
  8. import re

  9. stats = [0,1]
  10. stoptimes = 2

  11. if len(sys.argv) > 1:
  12.     interface = sys.argv[1]
  13. else:
  14.     interface = 'eth0'


  15. class Monitor:

  16.     def __init__(self,user_uid,stoptime):
  17.         self.user_uid = user_uid
  18.         self.stoptime = stoptime

  19.         if user_uid == 0:
  20.             print "请使用root用户运行此脚本"
  21.             exit()

  22.     def meminfo(self,used_vm,free_vm,buffers,cached):

  23.         total = psutil.virtual_memory().total / 1024 / 1024
  24.         used = used_vm - (buffers + cached )
  25.         free = total - used

  26.         print "total mem: %sM" %total
  27.         print "used mem: %sM" %used
  28.         print "free mem: %sM" %free

  29.     def diskinfo(self):
  30.         list = []
  31.         for i in psutil.disk_partitions():
  32.             list.append(i[1])

  33.         for k in range(len(list)):
  34.             total= "%-15s 分区\t total: %s" %(list[k] , psutil.disk_usage(list[k]).total / 1024 / 1024 / 1024)
  35.             used = "used: %s" %(psutil.disk_usage(list[k]).used / 1024 / 1024 / 1024)
  36.             free = "free: %s" %(psutil.disk_usage(list[k]).free / 1024 / 1024 / 1024)
  37.             print "%sG\t%sG\t%sG" %(total,used,free)

  38.     def cpuinfo(self):
  39.         with open('/proc/loadavg') as f:
  40.             loadavg = f.read()
  41.             print "5分钟 cpu load: %s" %(loadavg.split( )[0])
  42.             print "10分钟cpu load: %s" %(loadavg.split( )[1])
  43.             print "15分钟cpu load: %s" %(loadavg.split( )[2])
  44.             print "当前运行 proc: %s" %(loadavg.split()[3].split('/')[0])
  45.             print "最后运行 pid: %s" %(loadavg.split( )[4])
  46.             print '当前cpu %s' %(psutil.cpu_percent())

  47.     def flowinfo(self):
  48.         f = open('/proc/net/dev','r').readlines()
  49.         for i in f:
  50.             if re.search(interface,i):
  51.                 rx = i.split(':')[1].split()[0]
  52.                 tx = i.split()[8]
  53.                 stats[0] = rx
  54.                 stats[1] = tx

  55. used_vm = psutil.virtual_memory().used / 1024 / 1024
  56. free_vm = psutil.virtual_memory().free / 1024 / 1024
  57. buffers = psutil.virtual_memory().buffers / 1024 / 1024
  58. cached = psutil.virtual_memory().cached / 1024 / 1024

  59. if __name__ == '__main__':
  60.     user_uid = os.geteuid()
  61.     stoptime = 2

  62.     task = Monitor(user_uid,stoptime)
  63.     print "server meminfo:\n"
  64.     task.meminfo(used_vm,free_vm,buffers,cached)
  65.     print "=================================="
  66.     print "server diskinfo:\n"
  67.     task.diskinfo()
  68.     print "==================================="
  69.     print "server cpuinfo:\n"
  70.     task.cpuinfo()
  71.     print "==================================="
  72.     print "server flowinfo:\n"

  73.     task.flowinfo()
  74.     RX_one = float(stats[0])
  75.     TX_one = float(stats[1])
  76.     time.sleep(stoptimes)
  77.     task.flowinfo()
  78.     RX_two= float(stats[0])
  79.     TX_two= float(stats[1])
  80.     RX_rate = round(float(RX_two - RX_one)/1024,2)
  81.     TX_rate = round(float(TX_two - TX_one)/1024,2)

  82.     print time.strftime("%Y-%m-%d %H:%M:%S"),'RXbytes=',RX_rate,'KB'
  83.     print time.strftime("%Y-%m-%d %H:%M:%S"),'TXbytes=',TX_rate,'KB'

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