Skip to content

Instantly share code, notes, and snippets.

@itxx00
Forked from laiwei/60_stats_per_cpu_core.py
Last active August 29, 2015 14:23
Show Gist options
  • Save itxx00/ab11b5379bd78c3b4654 to your computer and use it in GitHub Desktop.
Save itxx00/ab11b5379bd78c3b4654 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time, os, socket
import json
metric = ['usr', 'nice', 'sys', 'idle', 'iowait', 'irq', 'soft', 'steal', 'guest']
host = socket.gethostname()
def get_cpu_core_stat(num):
data = []
for x in range(num):
try:
handler = os.popen("cat /proc/stat | grep cpu%d " % x)
except:
continue
output = handler.read().strip().split()[1:]
if len(output) != 9:
continue
index=0
for m in output:
t = {}
t['metric'] = 'cpu.core.%s' % metric[index]
t['endpoint'] = host
t['timestamp'] = int(time.time())
t['step'] = 60
t['counterType'] = 'COUNTER'
t['tags'] = 'core=%s' % str(x)
t['value'] = m
index += 1
data.append(t)
return data
if __name__ == "__main__":
core_total = int(os.popen("cat /proc/cpuinfo | grep processor | tail -1 | cut -d' ' -f2").read().strip()) + 1
print json.dumps(get_cpu_core_stat(core_total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment