|  | #!/usr/bin/env python | 
        
          |  | # coding: utf-8 | 
        
          |  |  | 
        
          |  | import sys | 
        
          |  | reload(sys) | 
        
          |  | sys.setdefaultencoding('utf-8') | 
        
          |  |  | 
        
          |  | import os | 
        
          |  | import time | 
        
          |  |  | 
        
          |  | import psutil | 
        
          |  | import statsd | 
        
          |  |  | 
        
          |  |  | 
        
          |  | def send(stats): | 
        
          |  | t1 = time.time() | 
        
          |  | print('CPU:\n====') | 
        
          |  | cpu_percent = psutil.cpu_percent(interval=1) | 
        
          |  | stats.gauge('cpu.percent.all', cpu_percent) | 
        
          |  | print('cpu.percent.all: {}'.format(cpu_percent)) | 
        
          |  | m1, m5, m15 = os.getloadavg() | 
        
          |  | stats.gauge('cpu.load.1', m1) | 
        
          |  | stats.gauge('cpu.load.5', m5) | 
        
          |  | stats.gauge('cpu.load.15', m15) | 
        
          |  | print('cpu.load.1: {}'.format(m1)) | 
        
          |  | print('cpu.load.5: {}'.format(m5)) | 
        
          |  | print('cpu.load.15: {}'.format(m15)) | 
        
          |  |  | 
        
          |  | print(u'\n内存:\n=====') | 
        
          |  | vmem = psutil.virtual_memory() | 
        
          |  | for k in ['total', 'used', 'buffers', 'cached']: | 
        
          |  | key = 'memory.virtual.{}'.format(k) | 
        
          |  | value = getattr(vmem, k) | 
        
          |  | stats.gauge(key, value) | 
        
          |  | print('{}: {}'.format(key, value)) | 
        
          |  | smem = psutil.swap_memory() | 
        
          |  | stats.gauge('memory.swap.used', smem.used) | 
        
          |  | print('memory.swap.used: {}'.format(smem.used)) | 
        
          |  |  | 
        
          |  | print(u'\n磁盘:\n=====') | 
        
          |  | disk_parts = psutil.disk_partitions() | 
        
          |  | for disk_part in disk_parts: | 
        
          |  | mountpoint = disk_part.mountpoint.replace('/', '_') | 
        
          |  | try: | 
        
          |  | disk_usage = psutil.disk_usage(disk_part.mountpoint) | 
        
          |  | stats.gauge('disk.usage.percent.{}'.format(mountpoint), disk_usage.percent) | 
        
          |  | stats.gauge('disk.usage.total.{}'.format(mountpoint), disk_usage.total) | 
        
          |  | print('disk.usage.percent.{}: {}%'.format(mountpoint, disk_usage.percent)) | 
        
          |  | print('disk.usage.total.{}: {}'.format(mountpoint, disk_usage.total)) | 
        
          |  | except OSError: | 
        
          |  | pass | 
        
          |  | disk_io = psutil.disk_io_counters(perdisk=False) | 
        
          |  | for k in ['read_count', 'write_count', 'read_bytes', 'write_bytes']: | 
        
          |  | key = 'disk.io.{}.all'.format(k) | 
        
          |  | value = getattr(disk_io, k) | 
        
          |  | stats.gauge(key, value) | 
        
          |  | print('{}: {}'.format(key, value)) | 
        
          |  |  | 
        
          |  | print(u'\n网络:\n====') | 
        
          |  | net_io = psutil.net_io_counters(pernic=False) | 
        
          |  | stats.gauge('net.io.bytes_sent.all', net_io.bytes_sent) | 
        
          |  | stats.gauge('net.io.bytes_recv.all', net_io.bytes_recv) | 
        
          |  | print('net.io.bytes_sent.all: {}'.format(net_io.bytes_sent)) | 
        
          |  | print('net.io.bytes_recv.all: {}'.format(net_io.bytes_recv)) | 
        
          |  |  | 
        
          |  | print(u'\n其它:\n=====') | 
        
          |  | uptime = int(time.time() - psutil.boot_time()) | 
        
          |  | stats.gauge('uptime', uptime) | 
        
          |  | h = uptime / 3600 | 
        
          |  | m = (uptime % 3600) / 60 | 
        
          |  | s = uptime % 60 | 
        
          |  | print('uptime: {} hour, {} minutes, {} seconds'.format(h, m, s)) | 
        
          |  | print('[Cost]: {}s'.format(time.time() - t1)) | 
        
          |  |  | 
        
          |  |  | 
        
          |  | def main(): | 
        
          |  | stats = statsd.client.StatsClient() | 
        
          |  | while True: | 
        
          |  | send(stats) | 
        
          |  | time.sleep(15) | 
        
          |  |  | 
        
          |  |  | 
        
          |  | if __name__ == '__main__': | 
        
          |  | main() |