Skip to content

Instantly share code, notes, and snippets.

@laiwei
Created June 30, 2015 09:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save laiwei/77bdb7661a269150f284 to your computer and use it in GitHub Desktop.
Save laiwei/77bdb7661a269150f284 to your computer and use it in GitHub Desktop.
open-falcon plugin stats per cpu core
#! /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))
@heroapn
Copy link

heroapn commented Apr 28, 2020

output = handler.read().strip().split()[1:] it should --> output = handler.read().strip().split()[2:]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment