Skip to content

Instantly share code, notes, and snippets.

@TheWaWaR
Created December 7, 2016 07:10
Show Gist options
  • Save TheWaWaR/a54a810d774472430b4f709d3bca9a4f to your computer and use it in GitHub Desktop.
Save TheWaWaR/a54a810d774472430b4f709d3bca9a4f to your computer and use it in GitHub Desktop.
Statsd host metrics (python psutil)
#!/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()

CPU

# [占用百分比]
 psutil.cpu_percent()
# [Statsd key]
cpu.percent.all

# [负载]: Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes
 os.getloadavg()
# [Statsd key]
cpu.load.1    # 1 minutes
cpu.load.5    # 5 minutes
cpu.load.15   # 15 minutes

内存

# [虚拟内存]
 psutil.virtual_memory().{ total, used, buffers, cached }
# [Statsd key]
memory.virtual.total     # ??? 是常量
memory.virtual.used
memory.virtual.buffers
memory.virtual.cached

# [Swap]
psutil.swap_memory().used
# [Statsd key]
memory.swap.used

磁盘

# [分区]
psutil.disk_partitions()

# [某个分区的使用量]  ??? total 是常量
psutil.disk_usage(path)
# [Statsd key]
disk.usage.percent._       # mountpoint = /
disk.usage.total._         # mountpoint = /
disk.usage.percent._boot   # mountpoint = /boot
disk.usage.total._boot     # mountpoint = /boot

# [使用率(总体的)]
 psutil.disk_io_counters(perdisk=False).{ read_count, write_count, read_bytes, write_bytes }
# [Statsd key]
disk.io.read_count.all
disk.io.write_count.all
disk.io.read_bytes.all
disk.io.write_bytes.all

网络

# [使用率(总体)]
 psutil.net_io_counters(pernic=False).{ bytes_sent, bytes_recv }
# [Statsd key]
net.io.bytes_sent.all
net.io.bytes_recv.all

其它

# [开机时间]
int(time.time() - psutil.boot_time())
# [Statsd key]
uptime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment